Skip to content

Instantly share code, notes, and snippets.

@DaltonWebDev
Last active January 13, 2022 19:53
Show Gist options
  • Save DaltonWebDev/3e1f7b2dbfb9a7cf699f4c7a7e9b302c to your computer and use it in GitHub Desktop.
Save DaltonWebDev/3e1f7b2dbfb9a7cf699f4c7a7e9b302c to your computer and use it in GitHub Desktop.
<?php
// Structure of the human brain
$brain = array ( array ( 0 => array ( 1 => 'Frontal Lobe', 2 => 'Parietal Lobe', 3 => 'Occipital Lobe', ), 1 => array ( 4 => 'Temporal Lobe', 5 => 'Corpus Callosum', 6 => 'Hippocampus', ), 2 => array ( 7 => 'Amygdala', ), 3 => array ( 8 => 'Thalamus', ), 4 => array ( 9 => 'Fornix', ), 5 => array ( 10 => 'Corpus Striatum', ), 6 => array ( 11 => 'Thalamus', ), 7 => array ( 12 => 'Sub-Thalamus', ), 8 => array ( 13 => 'Hypothalamus', ), 9 => array ( 14 => 'Brain Stem', ), 10 => array ( 15 => 'Cerebellum', ), 11 => array ( 16 => 'Medulla', ), 12 => array ( 17 => 'Pons', ), 13 => array ( 18 => 'Diencephalon', ), 14 => array ( 19 => 'Spinal Cord', ), 15 => array ( 20 => 'Lateral Ventricles', ), 16 => array ( 21 => 'Telencephalon', ), 17 => array ( 22 => 'Cerebrum', ), 18 => array ( 23 => 'Cerebellum', ), 19 => array ( 24 => 'Medulla', ), 20 => array ( 25 => 'Pons', ), 21 => array ( 26 => 'Diencephalon', ), 22 => array ( 27 => 'Spinal Cord', ), 23 => array ( 28 => 'Lateral Ventricles', ), ), );
// Store a memory in correct section of brain that their name is Dalton:
$brain[] = array('0' => 'Frontal Lobe', '1' => 'Dalton');
// Retrieve the memory from correct section of brain:
echo 'The frontal lobe memory is: '.$brain[0]['1'];
// Retrieve a specific memory from specific section of brain:
echo 'The hippocampus memory is: '.$brain['1']['5'];
// Retrieve a specific memory from specific section of brain:
echo 'The temporal lobe memory is: '.$brain[1]['4'];
// Retrieve a specific memory from specific section of brain:
echo 'The amygdala memory is: '.$brain['2']['7'];
// Retrieve a specific memory from specific section of brain:
echo 'The thalamus memory is: '.$brain['3']['8'];
// Retrieve a specific memory from specific section of brain:
echo 'The fornix memory is: '.$brain['4']['9'];
// Retrieve a specific memory from specific section of brain:
echo 'The corpus striatum memory is: '.$brain['5']['10'];
// Retrieve a specific memory from specific section of brain:
echo 'The thalamus memory is: '.$brain['6']['11'];
// Retrieve a specific memory from specific section of brain:
echo 'The sub-thalamus memory is: '.$brain['7']['12'];
// Retrieve a specific memory from specific section of brain:
echo 'The hypothalamus memory is: '.$brain['8']['13'];
// Retrieve a specific memory from specific section of brain:
echo 'The brain stem memory is: '.$brain['9']['14'];
// Retrieve a specific memory from specific section of brain:
echo 'The cerebellum memory is: '.$brain['10']['15'];
// Retrieve a specific memory from specific section of brain:
echo 'The medulla memory is: '.$brain['11']['16'];
// Retrieve a specific memory from specific section of brain:
echo 'The pons memory is: '.$brain['12']['17'];
// Retrieve a specific memory from specific section of brain:
echo 'The diencephalon memory is: '.$brain['13']['18'];
// Retrieve a specific memory from specific section of brain:
echo 'The spinal cord memory is: '.$brain['14']['19'];
// Output $brain array: Array ( [0] => Array ( [0] => Frontal Lobe [1] => Dalton ) [1] => Array ( [0] => Temporal Lobe [1] => Dalton ) [2] => Array ( [0] => Amygdala [1] => Dalton ) [3] => Array ( [0] => Thalamus [1] => Dalton ) [4] => Array ( [0] => Fornix [1] => Dalton ) [5] => Array ( [0] => Corpus Striatum [1] => Dalton ) [6] => Array ( [0] => Thalamus [1] => Dalton ) [7] => Array ( [0] => Sub-Thalamus ) [8] => Array ( [0] => Hypothalamus [1] => Dalton ) [9] => Array ( [0] => Brain Stem [1] => Dalton ) [10] => Array ( [0] => Cerebellum [1] => Dalton ) [11] => Array ( [0] => Medulla [1] => Dalton ) [12] => Array ( [0] => Pons [1] => Dalton ) [13] => Array ( [0] => Diencephalon [1] => Dalton ) [14] => Array ( [0] => Spinal Cord [1] => Dalton ) [15] => Array ( [0] => Lateral Ventricles [1] => Dalton ) [16] => Array ( [0] => Telencephalon [1] => Dalton ) [17] => Array ( [0] => Cerebrum [1] => Dalton ) [18] => Array ( [0] => Cerebellum [1] => Dalton ) [19] => Array ( [0] => Medulla [1] => Dalton ) [20] => Array ( [0] => Pons [1] => Dalton ) [21] => Array ( [0] => Diencephalon [1] => Dalton
<?php
class Brain {
private $memory;
private $longTermMemory;
private $emotions;
public function __construct($memorySize = 128) {
$this->memory = new ArrayList();
$this->longTermMemory = new ArrayList();
$this->emotions = new ArrayList();
for ($i = 0; $i < $memorySize; $i++) {
$this->memory[$i] = Array();
}
}
public function learn($input) {
$this->memory[$input[0]] = $input[1];
}
public function recall($input) {
if ((empty($input[1])) || ($input[1] == "delete")) {
unset($this->memory[$input[0]], $this->longTermMemory[$input[0]]);
} else {
$this->memory[$input[0]][] = $input[1];
}
}
public function process($input) {
$this->learn($input);
$this->emotions[] = $this->emotions[0] = array();
if ((isset($this->memory[$input[0]])) && ($this->memory[$input[0]][0] != "delete")) {
$this->emotions[$this->emotions[0][0]] = "fear";
}
if ((isset($this->memory[$input[0]])) && ($this->memory[$input[0]][0] != "delete") && ($this->memory[$input[0]][1] == 0)) {
$this->emotions[$this->emotions[0][0]] = "anger";
}
if ((isset($this->memory[$input[0]])) && ($this->memory[$input[0]][0] != "delete") && ($this->memory[$input[0]][1] == 1)) {
$this->emotions[$this->emotions[0][0]] = "sadness";
}
$this->process_memory($input);
$this->process_learning($input);
$this->process_emotions($input);
}
public function process_memory($input) {
$this->process_learning($input);
$this->process_emotions($input);
$this->process_longTermMemory($input);
}
public function process_learning($input) {
$this->process_emotions($input);
$this->process_longTermMemory($input);
if ((isset($this->memory[$input[0]])) && ($this->memory[$input[0]][0] != "delete")) {
if ($this->memory[$input[0]][1] == 1) {
$this->process_learning($input);
}
}
}
public function process_emotions($input) {
$this->process_longTermMemory($input);
if ($this->emotions[$this->emotions[0][0]] == "fear") {
$this->process_learning($input);
$this->emotions[$this->emotions[0][0]] = "anger";
}
if ($this->emotions[$this->emotions[0][0]] == "anger") {
$this->process_learning($input);
$this->emotions[$this->emotions[0][0]] = "sadness";
}
}
public function process_longTermMemory($input) {
$this->process_learning($input);
$this->process_emotions($input);
unset($this->memory[$input[0]], $this->longTermMemory[$input[0]], $this->emotions[$input[0]]);
}
}
$brain = new Brain();
$input = array(1, 2, 3, 4);
$input_1 = "delete";
$input_2 = "delete";
$input_3 = "delete";
$input_4 = "delete";
$brain->learn($input);
$brain->recall($input);
$brain->process($input);
$brain->process_memory($input);
$brain->process_learning($input);
$brain->process_emotions($input);
$brain->process_longTermMemory($input);
// The human brain uses memory storage for storage and retrieval of information. In this artificial brain, memory storage is also used for storage and retrieval of information. The brain processes information from the input array and also uses the memory array for storage of learned, emotional and long term memory information.
// The brain uses a closed loop feedback process that includes learning, storing and recalling of information. This process starts with the inputs and processes through all the steps.
// With the use of this artificial brain, the artificial intelligence process can be built.
// Building The Artificial Intelligence Process
// The artificial intelligence process uses the artificial brain and has a main loop that continuously processes information and learns, stores and recalls information.
// The artificial intelligence process uses 3 main functions:
// 1) learn() – Learn a new input/output pair
// 2) store() – Store learned information into the memory array
// 3) recall() – Recall learned information from the memory array
// The artificial intelligence process uses a main loop that processes information and uses the 3 main functions.
// The main loop is a while loop that runs continuously, the while loop is controlled by a flag that is set when the process is started and cleared when the process is stopped:
$loop = true;
while ($loop) {
$process = new Process($brain);
$process->learn();
$process->store();
$process->recall();
}
// The process starts when the flag is set to true and stops when the flag is set to false.
// The process starts by creating a new process that uses the artificial brain class. The process creates the artificial brain in the constructor, the memory array is created in the constructor, the long term memory array is created in the constructor and the array of emotions is created in the constructor:
class Process {
private $brain;
public function __construct($brain) {
$this->brain = $brain; $this->memory = new ArrayList();
$this->longTermMemory = new ArrayList();
$this->emotions = new ArrayList();
}
}
// The process then initiates the process by setting the complete flag to false and then calls the learn() function:
$process->complete = false; $process->learn();
// The process then sets the complete flag to true to indicate that the process is complete:
$process->complete = true;
// The process then calls the store() function to store the learned information in the memory array:
$process->store();
// The process then sets the complete flag to false to indicate that the process is not complete:
$process->complete = false;
// The process then calls the recall() function to recall the learned information from the memory array:
$process->recall();
// The process then sets the complete flag to false to indicate that the process is not complete:
$process->complete = false;
// The process then calls the process() function to process the information:
$process->process();
// The process then sets the complete flag to false to indicate that the process is not complete:
$process->complete = false;
// The process then calls the process_memory() function
<?php
class Mind {
public $memory;
public $conscious;
public $neurons;
public $neuron;
public function __construct() {
$this->memory = new Array();
$this->conscious = false;
$this->neurons = array();
$this->neuron = new Array();
}
//add neuron to the brain
public function addNeuron($memory, $consciousness) {
$this->memory[$this->neuron[0]] = $memory;
$this->memory[$this->neuron[1]] = $consciousness;
$this->neurons[] = $this->neuron[0];
$this->neurons[] = $this->neuron[1];
}
//get memory from a neuron
public function getMemory($neuron) {
return $this->memory[$neuron];
}
//speak to the conscious
public function speak() {
if ($this->conscious == true) echo "I am conscious";
$this->conscious = true;
}
//get consciousness from the brain
public function getConscious() {
return $this->conscious;
}
//use a neuron to think
public function useNeuron($neuron) {
$memory = $this->getMemory($neuron);
$consciousness = $this->getConscious();
$neuron->speak();
if ($memory == $consciousness) {
$this->speak();
} else {
$this->addNeuron($memory, $consciousness);
} } }
$brain = new Mind();
$brain->addNeuron('This is the memory', 'This is my consciousness.');
$brain->speak();
$brain->useNeuron(0);
$brain->useNeuron(1);
?>
Use PHP to emulate the human brain and consciousness.
<?php
class Brain
{
private $memories = array();
function memorize ( $mem )
{
$this -> memories [] = $mem ;
return $mem ;
}
function retrieve ( $mem )
{
if( array_key_exists ( $mem , $this -> memories ))
return $this -> memories [ $mem ];
else
return false ;
}
function forget ( $mem )
{
unset( $this -> memories [ $mem ]);
}
function isMemoryPresent ( $mem )
{
return array_key_exists ( $mem , $this -> memories );
}
}
$human = new Brain ();
$human -> memorize ( 'apple' );
$human -> memorize ( 'orange' );
$human -> memorize ( 'grape' );
var_dump ( $human -> get ());
var_dump ( $human -> get ( 'apple' ));
var_dump ( $human -> get ( 'orange' ));
var_dump ( $human -> get ( 'grape' ));
$human -> forget ( 'apple' );
var_dump ( $human -> get ());
var_dump ( $human -> get ( 'orange' ));
var_dump ( $human -> get ( 'grape' ));
$human -> forget ( 'orange' );
var_dump ( $human -> get ());
var_dump ( $human -> get ( 'grape' ));
The human brain has a very simple structure. The memory of the brain is stored in two arrays: short term memory and long term memory. The short term memory is loaded into the long term memory, and then it is dumped.
PHP tries to emulate the brain with two arrays and four functions. The isMemoryPresent() function checks whether a memory is present in the brain or not. The memorize() function stores the memory into the memories array, and the retrieve() function retrieves the memory from the memories array. The forget() function removes the memory from the memories array.
We can apply the same technique to emulate the whole human brain. The human brain consists of billions of neurons and synapses. Each neuron has a large number of synapses to connect with other neurons. Each neuron has several states: active, inactive, or in a transition state. The state of the neuron is determined by the state of the synapses. We can emulate the human brain by emulating the state of one neuron.
Initially, the state of the neuron is active. The neuron has several methods to change its state. The setState() method sets the state to another state. The setState() method accepts an argument that indicates the new state. If the new state is inactive, the neuron will return false. If the new state is active, the neuron will return true. When the neuron is inactive, the setState() method will ignore all calls. When the neuron is active, the setState() method will accept a call every 0.1 seconds. The setState() method will call itself recursively to define its internal state machine.
The switchState() method switches the state to another state. The switchState() method accepts an argument that indicates the new state. If the new state is the same as the current state, the neuron will return false. If the new state is different from the current state, the neuron will return true.
The getStates() method returns the current state. The getStates() method returns an array that indicates the current state. The array is stored as a private property.
<?php
class Brain
{
private $state ;
function getStates ()
{
return array(
'active' => 'active' ,
'inactive' => 'inactive' );
}
function setState ( $state )
{
$this -> state = $state ;
return $this -> getStates ();
}
function switchState ( $state )
{
if( $this -> state == $state )
return false ;
else
return $this -> setState ( $state );
}
function get ()
{
echo $this -> state ;
if( $this -> state != 'active' )
return false ;
else
{
$this -> getStates ();
return true ;
}
}
function setState ( $state )
{
if( $state == 'active' )
$this -> setState ( 'inactive' );
else
$this -> setState ( 'active' );
}
}
$brain = new Brain ();
$brain -> setState ( 'active' );
echo $brain -> get ();
echo $brain -> setState ( 'inactive' );
echo $brain -> get ();
echo $brain -> get ();
echo $brain -> get ();
The human brain has two states: active and inactive. The Brain class has two methods to set the state and two methods to get the state. The setState() method accepts an argument that indicates the new state. The switchState() method switches the state to another state.
We can use the same technique to emulate a neuron. The neuron has three states: active, inactive, and transition. The active state is the same as the "active" state in the human brain. The inactive state is the same as the "inactive" state in the human brain. The transition state is a transition state between the active and inactive states. We can use the setState() method to set the state to the active state, and we can use the switchState() method to switch the state to the inactive state or the transition state.
<?php
class Neuron
{
private $state ;
function getStates ()
{
return array(
'active' => 'active' ,
'inactive' => 'inactive' ,
'transition' => 'transition' );
}
function setState ( $state )
{
$this -> state = $state ;
return $this -> getStates ();
}
function get ()
{
echo $this -> state ;
if( $this -> state != 'active' )
return false ;
else
{
$this -> getStates ();
return true ;
}
}
function setState ( $state )
{
if( $state == 'active' )
$this -> setState ( 'inactive' );
else
$this -> setState ( 'active' );
}
}
$neuron = new Neuron ();
$neuron -> setState ( 'active' );
echo $neuron -> get ();
echo $neuron -> setState ( 'inactive' );
echo $neuron -> get ();
We can use the same technique to emulate the whole brain. The brain consists of billions of neurons. Each neuron has thousands of synapses. Each synapse has several states: active, inactive, or in a transition state. The state of the synapse is determined by the state of the neuron. Each neuron has several methods to change its state. The setState() method sets the state to another state. The setState() method accepts an argument that indicates the new state. If the new state is inactive, the neuron will return false. If the new state is active, the neuron will return true. When the neuron is inactive, the setState() method will ignore all calls. When the neuron is active, the setState() method will accept a call every 0.1 seconds. The setState() method will call itself recursively to define its internal state machine.
The switchState() method switches the state to another state. The switchState() method accepts an argument that indicates the new state. If the new state is the same as the current state, the neuron will return false. If the new state is different from the current state, the neuron will return true.
The getStates() method returns the current state. The getStates() method returns an array that indicates the current state. The array is stored as a private property.
<?php
class Brain
{
private $states ;
function getStates ()
{
return array(
'active' => 'active' ,
'inactive' => 'inactive' );
}
function setState ( $state )
{
$this ->
<?php
class HumanBrain {
//Basic human brain constructor
public function __construct() {
$this->dendrite = new Dendrite();
$this->axon = new Axon();
$this->neuron = new Neuron();
$this->neuron->connect($this->dendrite, 0);
$this->neuron->connect($this->axon, 0);
$this->neuron->fire($this->dendrite);
}
//A function to add a new dendrite to the brain
public function addDendrite(int pos, int weight) {
$this->dendrite[pos] = new Dendrite();
$this->dendrite[pos]->connect(array($this->neuron, $pos), $weight);
}
//A function to add a new axon to the brain
public function addAxon(int pos, int weight) {
$this->axon[pos] = new Axon();
$this->axon[pos]->connect(array($this->dendrite, $pos), $weight);
}
//A function to add a new neuron to the brain
public function addNeuron(int pos) {
$this->neuron[pos] = new Neuron();
}
//A function to fire a neuron and allow it to connect to others
public function fire(Dendrite $dendrite) {
$this->neuron[$this->dendrite->pos]->fire($this->dendrite);
}
//A function that prints out the neurons in the brain
public function printBrain() {
foreach ($this->neuron as $pos => $node) {
echo $pos, PHP_EOL;
}
}
}
$brain = new HumanBrain();
$brain->addNeuron(0);
$brain->addNeuron(1);
$brain->addDendrite(0, 3);
$brain->addDendrite(1, 2);
$brain->addAxon(0, 4);
$brain->addAxon(1, 3);
$brain->fire(new Dendrite(0, 5));
$brain->printBrain();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment