Correcting genome inheritance & crossover (wrong number of neurons > now its neurons*layers)
This commit is contained in:
parent
fd9bcefed7
commit
3c57a83e41
|
@ -15,9 +15,9 @@
|
|||
/************************************************
|
||||
**** LOCAL ATTRIBUTES ****
|
||||
************************************************/
|
||||
private $layers; // Number of layers
|
||||
private $neurons; // Neurons of the genome
|
||||
private $synapses; // Synapses between neurons
|
||||
public $layers; // Number of layers
|
||||
public $neurons; // Neurons of the genome
|
||||
public $synapses; // Synapses between neurons
|
||||
|
||||
|
||||
/* CONSTRUCTOR
|
||||
|
@ -76,7 +76,7 @@
|
|||
|
||||
/* (2) Creating random 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;
|
||||
|
||||
/* (3) Creating random synapses */
|
||||
|
@ -102,8 +102,8 @@
|
|||
|
||||
/* (2) Clones into this Genome */
|
||||
$this->layers = $parent->layers;
|
||||
$this->neurons = $parent->neurons;
|
||||
$this->synapses = $parent->synapses;
|
||||
$this->neurons = array_slice($parent->neurons, 0);
|
||||
$this->synapses = array_slice($parent->synapses, 0);
|
||||
|
||||
// Success state
|
||||
return true;
|
||||
|
@ -126,17 +126,20 @@
|
|||
if( $father->layers !== $mother->layers || count($father->neurons) !== count($mother->neurons) )
|
||||
return false;
|
||||
|
||||
/* (3) Do random crossover for neurons */
|
||||
/* (3) Set layer count */
|
||||
$this->layers = $father->layers;
|
||||
|
||||
/* (4) Do random crossover for neurons */
|
||||
$this->neurons = [];
|
||||
for( $i = 0, $l = count($father->neurons) ; $i < $l ; $i++ )
|
||||
if( rand(0,1) ) $this->neurons[$i] = $father->neurons[$i];
|
||||
else $this->neurons[$i] = $mother->neurons[$i];
|
||||
if( !!rand(0,1) ) $this->neurons[$i] = $father->neurons[$i];
|
||||
else $this->neurons[$i] = $mother->neurons[$i];
|
||||
|
||||
/* (3) Creating random synapses */
|
||||
/* (5) Do random crossover for synapses */
|
||||
$this->synapses = [];
|
||||
for( $i = 0, $l = pow(count($this->neurons), $this->layers) ; $i < $l ; $i++ )
|
||||
if( rand(0,1) ) $this->synapses[$i] = $father->synapses[$i];
|
||||
else $this->synapses[$i] = $mother->synapses[$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];
|
||||
else $this->synapses[$i] = $mother->synapses[$i];
|
||||
|
||||
// Success state
|
||||
return true;
|
||||
|
|
|
@ -45,14 +45,32 @@
|
|||
/* (1) Basic Creation */
|
||||
$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 */
|
||||
$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 */
|
||||
// $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) */
|
||||
$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";
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue