Correcting genome inheritance & crossover (wrong number of neurons > now its neurons*layers)

This commit is contained in:
xdrm-brackets 2016-10-26 17:24:51 +02:00
parent fd9bcefed7
commit 3c57a83e41
2 changed files with 35 additions and 14 deletions

View File

@ -15,9 +15,9 @@
/************************************************ /************************************************
**** LOCAL ATTRIBUTES **** **** LOCAL ATTRIBUTES ****
************************************************/ ************************************************/
private $layers; // Number of layers public $layers; // Number of layers
private $neurons; // Neurons of the genome public $neurons; // Neurons of the genome
private $synapses; // Synapses between neurons public $synapses; // Synapses between neurons
/* CONSTRUCTOR /* CONSTRUCTOR
@ -76,7 +76,7 @@
/* (2) Creating random neurons */ /* (2) Creating random neurons */
$this->neurons = []; $this->neurons = [];
for( $i = 0 ; $i < $neurons ; $i++ ) for( $i = 0, $l = $neurons*$layers ; $i < $l ; $i++ )
$this->neurons[$i] = rand(self::MIN, self::MAX) / self::MAX; $this->neurons[$i] = rand(self::MIN, self::MAX) / self::MAX;
/* (3) Creating random synapses */ /* (3) Creating random synapses */
@ -102,8 +102,8 @@
/* (2) Clones into this Genome */ /* (2) Clones into this Genome */
$this->layers = $parent->layers; $this->layers = $parent->layers;
$this->neurons = $parent->neurons; $this->neurons = array_slice($parent->neurons, 0);
$this->synapses = $parent->synapses; $this->synapses = array_slice($parent->synapses, 0);
// Success state // Success state
return true; return true;
@ -126,17 +126,20 @@
if( $father->layers !== $mother->layers || count($father->neurons) !== count($mother->neurons) ) if( $father->layers !== $mother->layers || count($father->neurons) !== count($mother->neurons) )
return false; return false;
/* (3) Do random crossover for neurons */ /* (3) Set layer count */
$this->layers = $father->layers;
/* (4) Do random crossover for neurons */
$this->neurons = []; $this->neurons = [];
for( $i = 0, $l = count($father->neurons) ; $i < $l ; $i++ ) for( $i = 0, $l = count($father->neurons) ; $i < $l ; $i++ )
if( rand(0,1) ) $this->neurons[$i] = $father->neurons[$i]; if( !!rand(0,1) ) $this->neurons[$i] = $father->neurons[$i];
else $this->neurons[$i] = $mother->neurons[$i]; else $this->neurons[$i] = $mother->neurons[$i];
/* (3) Creating random synapses */ /* (5) Do random crossover for synapses */
$this->synapses = []; $this->synapses = [];
for( $i = 0, $l = pow(count($this->neurons), $this->layers) ; $i < $l ; $i++ ) for( $i = 0, $l = pow(count($this->neurons)/$this->layers, $this->layers) ; $i < $l ; $i++ )
if( rand(0,1) ) $this->synapses[$i] = $father->synapses[$i]; if( !!rand(0,1) ) $this->synapses[$i] = $father->synapses[$i];
else $this->synapses[$i] = $mother->synapses[$i]; else $this->synapses[$i] = $mother->synapses[$i];
// Success state // Success state
return true; return true;

View File

@ -45,14 +45,32 @@
/* (1) Basic Creation */ /* (1) Basic Creation */
$a = new Genome(2, 3); // 2 layers of 3 neurons each -> randomly filled $a = new Genome(2, 3); // 2 layers of 3 neurons each -> randomly filled
echo "A neurons\n";
echo implode(' ; ', $a->neurons)."\n";
echo "A synapses\n";
echo implode(' ; ', $a->synapses)."\n\n";
/* (2) Inheritance */ /* (2) Inheritance */
$b = new Genome($a); // Clone of @a $b = new Genome($a); // Clone of @aecho "A neurons\n";
echo "A's clone neurons\n";
echo implode(' ; ', $b->neurons)."\n";
echo "A's clone synapses\n";
echo implode(' ; ', $b->synapses)."\n\n";
/* (3) Section Title */ /* (3) Section Title */
// $b->mutation(0.3); // @b has now mutated with a threshold of 30% // $b->mutation(0.3); // @b has now mutated with a threshold of 30%
$b = new Genome(2, 3); // 2 layers of 3 neurons each -> randomly filled
echo "B neurons\n";
echo implode(' ; ', $b->neurons)."\n";
echo "B synapses\n";
echo implode(' ; ', $b->synapses)."\n\n";
/* (4) Cross-over (father+mother) */ /* (4) Cross-over (father+mother) */
$c = new Genome($a, $b); // @c is a randomly-done mix of @a and @b $c = new Genome($a, $b); // @c is a randomly-done mix of @a and @b
echo "A+B neurons\n";
echo implode(' ; ', $c->neurons)."\n";
echo "A+B synapses\n";
echo implode(' ; ', $c->synapses)."\n\n";
} }