Done Genome serialization/unserialization [Genome::serialize()] + [Genome::unserialize(String)]
This commit is contained in:
parent
7cacdb3b63
commit
20382821ca
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
use filemanager\core\FileManager;
|
use filemanager\core\FileManager;
|
||||||
|
|
||||||
class Genome/* implements \Serializable*/{
|
class Genome implements \Serializable{
|
||||||
|
|
||||||
/************************************************
|
/************************************************
|
||||||
**** Constants ****
|
**** Constants ****
|
||||||
|
@ -20,6 +20,10 @@
|
||||||
public $synapses; // Synapses between neurons
|
public $synapses; // Synapses between neurons
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************
|
||||||
|
**** Genome Construction ****
|
||||||
|
************************************************/
|
||||||
|
|
||||||
/* CONSTRUCTOR
|
/* CONSTRUCTOR
|
||||||
*
|
*
|
||||||
* -- RANDOM CREATION --
|
* -- RANDOM CREATION --
|
||||||
|
@ -146,6 +150,10 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************
|
||||||
|
**** Genome Actions ****
|
||||||
|
************************************************/
|
||||||
|
|
||||||
/* APPLIES A MUTATION ON THE Genome WITH A SPECIFIC @threshold
|
/* APPLIES A MUTATION ON THE Genome WITH A SPECIFIC @threshold
|
||||||
*
|
*
|
||||||
* @threshold<double> Mutation threshold
|
* @threshold<double> Mutation threshold
|
||||||
|
@ -186,6 +194,60 @@
|
||||||
for( $i = 0, $l = count($iSynapses) ; $i < $l ; $i++ )
|
for( $i = 0, $l = count($iSynapses) ; $i < $l ; $i++ )
|
||||||
$this->synapses[$iSynapses[$i]] = rand(self::MIN, self::MAX) / self::MAX;
|
$this->synapses[$iSynapses[$i]] = rand(self::MIN, self::MAX) / self::MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/************************************************
|
||||||
|
**** Serialization ****
|
||||||
|
************************************************/
|
||||||
|
|
||||||
|
/* SERIALIZES A Genome
|
||||||
|
*
|
||||||
|
* @return serialized<String> Serialized representation of the Genome
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function serialize(){
|
||||||
|
/* (1) Initialize result */
|
||||||
|
$csv = '';
|
||||||
|
|
||||||
|
/* (2) Adds global attributes */
|
||||||
|
$csv .= $this->layers .';';
|
||||||
|
|
||||||
|
/* (3) Adds neurons data */
|
||||||
|
$csv .= implode(',', $this->neurons) .';';
|
||||||
|
|
||||||
|
/* (4) Adds synapses data */
|
||||||
|
$csv .= implode(',', $this->synapses);
|
||||||
|
|
||||||
|
return $csv;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* BUILDS A Genome BASED ON HIS SERIALIZED REPRESENTATION
|
||||||
|
*
|
||||||
|
* @serialized<String> Serialized representation of a Genome
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function unserialize($serialized){
|
||||||
|
/* (1) Segmenting data */
|
||||||
|
$segments = explode(';', $serialized);
|
||||||
|
|
||||||
|
// Manage segmentation error
|
||||||
|
if( count($segments) < 3 )
|
||||||
|
throw new \Error('Format error during Genome unserialization.');
|
||||||
|
|
||||||
|
/* (2) Get global attributes */
|
||||||
|
if( !is_numeric($segments[0]) )
|
||||||
|
throw new \Error('Format error during Genome unserialization.');
|
||||||
|
|
||||||
|
$this->layers = intval($segments[0]);
|
||||||
|
|
||||||
|
/* (3) Get neurons values */
|
||||||
|
$this->neurons = explode(',', $segments[1]);
|
||||||
|
|
||||||
|
/* (4) Get synapses values */
|
||||||
|
$this->synapses = explode(',', $segments[2]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -45,31 +45,22 @@
|
||||||
/* (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 "A : ".$a->serialize()."\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 @aecho "A neurons\n";
|
$b = new Genome($a); // Clone of @aecho "A neurons\n";
|
||||||
echo "A's clone neurons\n";
|
echo "cloning A to B\n";
|
||||||
echo implode(' ; ', $b->neurons)."\n";
|
echo "B : ".$b->serialize()."\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%
|
||||||
echo "mutated clone neurons\n";
|
echo "mutate B\n";
|
||||||
echo implode(' ; ', $b->neurons)."\n";
|
echo "B : ".$b->serialize()."\n";
|
||||||
echo "mutated clone 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 "crossover : A+B -> C\n";
|
||||||
echo implode(' ; ', $c->neurons)."\n";
|
echo "C : ".$c->serialize()."\n";
|
||||||
echo "A+B synapses\n";
|
|
||||||
echo implode(' ; ', $c->synapses)."\n\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue