From 20382821ca7c6af80a781ffedc8a402312d5ecfc Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 26 Oct 2016 18:07:52 +0200 Subject: [PATCH] Done Genome serialization/unserialization [Genome::serialize()] + [Genome::unserialize(String)] --- build/neuralnetwork/core/Genome.php | 64 ++++++++++++++++++++++++++++- public/main.php | 23 ++++------- 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/build/neuralnetwork/core/Genome.php b/build/neuralnetwork/core/Genome.php index d85cb27..87d3f85 100644 --- a/build/neuralnetwork/core/Genome.php +++ b/build/neuralnetwork/core/Genome.php @@ -4,7 +4,7 @@ use filemanager\core\FileManager; - class Genome/* implements \Serializable*/{ + class Genome implements \Serializable{ /************************************************ **** Constants **** @@ -20,6 +20,10 @@ public $synapses; // Synapses between neurons + /************************************************ + **** Genome Construction **** + ************************************************/ + /* CONSTRUCTOR * * -- RANDOM CREATION -- @@ -146,6 +150,10 @@ } + /************************************************ + **** Genome Actions **** + ************************************************/ + /* APPLIES A MUTATION ON THE Genome WITH A SPECIFIC @threshold * * @threshold Mutation threshold @@ -186,6 +194,60 @@ for( $i = 0, $l = count($iSynapses) ; $i < $l ; $i++ ) $this->synapses[$iSynapses[$i]] = rand(self::MIN, self::MAX) / self::MAX; } + + + /************************************************ + **** Serialization **** + ************************************************/ + + /* SERIALIZES A Genome + * + * @return serialized 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 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]); + } + } diff --git a/public/main.php b/public/main.php index 4e831f8..085a217 100644 --- a/public/main.php +++ b/public/main.php @@ -45,31 +45,22 @@ /* (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"; + echo "A : ".$a->serialize()."\n"; /* (2) Inheritance */ $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"; + echo "cloning A to B\n"; + echo "B : ".$b->serialize()."\n"; /* (3) Section Title */ $b->mutation(0.3); // @b has now mutated with a threshold of 30% - echo "mutated clone neurons\n"; - echo implode(' ; ', $b->neurons)."\n"; - echo "mutated clone synapses\n"; - echo implode(' ; ', $b->synapses)."\n\n"; + echo "mutate B\n"; + echo "B : ".$b->serialize()."\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"; + echo "crossover : A+B -> C\n"; + echo "C : ".$c->serialize()."\n"; }