171 lines
4.8 KiB
PHP
171 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace neuralnetwork\core;
|
|
|
|
use filemanager\core\FileManager;
|
|
|
|
class Genome/* implements \Serializable*/{
|
|
|
|
/************************************************
|
|
**** Constants ****
|
|
************************************************/
|
|
const MIN = 0;
|
|
const MAX = 1e9;
|
|
|
|
/************************************************
|
|
**** LOCAL ATTRIBUTES ****
|
|
************************************************/
|
|
private $layers; // Number of layers
|
|
private $neurons; // Neurons of the genome
|
|
private $synapses; // Synapses between neurons
|
|
|
|
|
|
/* CONSTRUCTOR
|
|
*
|
|
* -- RANDOM CREATION --
|
|
* @layers<int> Number of layers to manage
|
|
* @neurons<int> Number of neurons per layer
|
|
*
|
|
* -- CLONING --
|
|
* @base<Genom> Genome to clone to
|
|
*
|
|
* -- CROSS-OVER CREATION --
|
|
* @father<Genome> First parent
|
|
* @mother<Genome> Second parent
|
|
*
|
|
*
|
|
*/
|
|
public function __construct(){
|
|
/* (1) Get arguments */
|
|
$argv = func_get_args();
|
|
$argc = count($argv);
|
|
|
|
|
|
/* (2) If CrossoverCreation */
|
|
if( $argc > 1 && $argv[0] instanceof Genome && $argv[1] instanceof Genome )
|
|
$this->construct_crossover($argv[0], $argv[1]);
|
|
|
|
/* (3) If RandomCreation */
|
|
else if( $argc > 1 && abs(intval($argv[0])) === $argv[0] && abs(intval($argv[1])) === $argv[1] )
|
|
$this->construct_new($argv[0], $argv[1]);
|
|
|
|
/* (4) If InheritanceCreation (clone) */
|
|
else if( $argc > 0 && $argv[0] instanceof Genome )
|
|
$this->construct_inheritance($argv[0]);
|
|
|
|
/* (5) If no match */
|
|
else
|
|
throw new \Error('Invalid Genome constructor\'s arguments.');
|
|
}
|
|
|
|
/* BUILDS A Genome RANDOMLY WITH PARAMETERS
|
|
*
|
|
* @layers<int> The number of hidden layers to manage
|
|
* @neurons<int> The number neurons per layer
|
|
*
|
|
* @return created<Boolean> If Genome has been successfully created
|
|
*
|
|
*/
|
|
private function construct_new($layers=-1, $neurons=-1){
|
|
/* (1) Checks parameters */
|
|
if( abs(intval($layers)) !== $layers || abs(intval($neurons)) !== $neurons )
|
|
return false;
|
|
|
|
// set layers
|
|
$this->layers = $layers;
|
|
|
|
/* (2) Creating random neurons */
|
|
$this->neurons = [];
|
|
for( $i = 0 ; $i < $neurons ; $i++ )
|
|
$this->neurons[$i] = rand(self::MIN, self::MAX) / self::MAX;
|
|
|
|
/* (3) Creating random synapses */
|
|
$this->synapses = [];
|
|
for( $i = 0, $l = pow($neurons, $layers) ; $i < $l ; $i++ )
|
|
$this->synapses[$i] = rand(self::MIN, self::MAX) / self::MAX;
|
|
|
|
// Success status
|
|
return true;
|
|
}
|
|
|
|
/* BUILDS A Genome BASED ON A PARENT
|
|
*
|
|
* @parent<Genome> Parent genome to clone into children
|
|
*
|
|
* @return created<Boolean> If cloned Genome has been created successfully
|
|
*
|
|
*/
|
|
private function construct_inheritance($parent=null){
|
|
/* (1) Checks parent type */
|
|
if( !($parent instanceof Genome) )
|
|
return false;
|
|
|
|
/* (2) Clones into this Genome */
|
|
$this->layers = $parent->layers;
|
|
$this->neurons = $parent->neurons;
|
|
$this->synapses = $parent->synapses;
|
|
|
|
// Success state
|
|
return true;
|
|
}
|
|
|
|
/* BUILDS A Genome BASED ON TWO PARENTS
|
|
*
|
|
* @father<Genome> First parent ($father)
|
|
* @mother<Genome> Second parent ($mother)
|
|
*
|
|
* @return created<Boolean> If crossed-over Genome has been created successfully
|
|
*
|
|
*/
|
|
private function construct_crossover($father=null, $mother=null){
|
|
/* (1) Checks parent type */
|
|
if( !($father instanceof Genome) || !($mother instanceof Genome) )
|
|
return false;
|
|
|
|
/* (2) Checks number of layers+neurons (same species) */
|
|
if( $father->layers !== $mother->layers || count($father->neurons) !== count($mother->neurons) )
|
|
return false;
|
|
|
|
/* (3) 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];
|
|
|
|
/* (3) Creating random 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];
|
|
|
|
// Success state
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/************************************************
|
|
**** USE CASE ****
|
|
************************************************/
|
|
$use_case = false;
|
|
if( $use_case ){
|
|
|
|
/* (1) Basic Creation */
|
|
$a = new Genome(2, 3); // 2 layers of 3 neurons each -> randomly filled
|
|
|
|
/* (2) Inheritance */
|
|
$b = new Genome($a); // Clone of @a
|
|
|
|
/* (3) Section Title */
|
|
$b->mutation(0.3); // @b has now mutated with a threshold of 30%
|
|
|
|
/* (4) Cross-over (father+mother) */
|
|
$c = new Genome($a, $b); // @c is a randomly-done mix of @a and @b
|
|
|
|
}
|
|
|
|
?>
|