Add .gitignore

This commit is contained in:
xdrm-brackets 2016-10-27 21:28:09 +02:00
parent da50b03202
commit 10f2a3951d
8 changed files with 1869 additions and 1527 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build/neuralnetwork/storage/*

View File

@ -225,7 +225,7 @@
* @threshold<double> Mutation threshold
*
*/
public function mutation($threshold=0.5){
public function mutation($threshold=1){
/* (1) Checks @threshold argument */
if( floatval($threshold) !== $threshold || $threshold < 0 || $threshold > 1 )
throw new \Exception('Invalid threshold for Genome mutation.');
@ -295,10 +295,15 @@
// echo "result: ${neurons[$n]}\n\n";
}
if( $l == 1 )
$neurons[$n] /= $this->inputN;
else
$neurons[$n] /= $this->neurons;
}
}

View File

@ -83,6 +83,9 @@
], 'ft' => [
'filename' => $absolute_path.'.ft', // will contain genomes' fitness
'exists' => is_file($absolute_path.'.ft')
], 'ln' => [
'filename' => $absolute_path.'.ln', // will contain learnt best genomes
'exists' => is_file($absolute_path.'.ln')
]
];
@ -341,6 +344,61 @@
}
/* INITIALIZES THE LEARNING ROUTINE WITH LEARNT DATA AS THE FIRST GENOME
*
* @callback<Function> Callback function to display current state
*
*/
public function loadLearningRoutine($callback=null){
/* [1] Manages @callback argument
=========================================================*/
if( !is_callable($callback) ) $this->callback = function(){};
else $this->callback = $callback;
/* [2] Creates the First generation and serialize it
=========================================================*/
/* (1) Initializes data & storage */
$this->gnr = 0;
$this->gnm = 0;
FileManager::write($this->storage['gn']['filename'], '');
FileManager::write($this->storage['ft']['filename'], '');
/* (2) Fetch learnt best genomes */
$loadedGenomes = [
FileManager::readline($this->storage['ln']['filename'], 0),
FileManager::readline($this->storage['ln']['filename'], 1)
];
/* (3) Unserializes them */
$father = new Genome(2, 2, 2, 2);
$father->unserialize($loadedGenomes[0]);
$mother = new Genome(2, 2, 2, 2);
$mother->unserialize($loadedGenomes[1]);
/* (4) Create childrens */
for( $g = 0 ; $g < $this->maxGnm ; $g++ ){
// {6.1} Re-use father //
if( $g == 0 )
FileManager::append($this->storage['gn']['filename'], $father->serialize());
// {6.2} Re-use mother //
else if( $g == 1 )
FileManager::append($this->storage['gn']['filename'], $mother->serialize());
// {6.3} Do cross-over + mutation for the rest //
else{
$gnm = new Genome($father, $mother);
$gnm->mutation($this->mutThr);
FileManager::append($this->storage['gn']['filename'], $gnm->serialize());
}
}
}
/* RETURNS THE CURRENT GENOME
*
* @return genome<Genome> Returns the current genome
@ -441,8 +499,18 @@
/* (3) If end of process
---------------------------------------------------------*/
}else{
/* (1) Get the 2 best genomes */
$best = FileManager::readline($this->storage['ln']['filename'], 0);
$best.= FileManager::readline($this->storage['ln']['filename'], 1);
/* (2) Stores data to learnt data */
FileManager::write($this->storage['ln']['filename'], $best);
/* (3) Destroy cursors */
$this->gnr = null;
$this->gnm = null;
}
}
@ -479,6 +547,10 @@
$c++;
}
/* (3) Anti-regression, if @mother < @father only use @father*/
if( $fitnesses[$iMother] < $fitnesses[$iFather] )
$iMother = $iFather;
return [ $iFather, $iMother ];
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
{"maxGnr":50,"maxGnm":1000,"kptGnm":2,"mutThr":0.3,"fitEnd":1,"numHid":2,"numNeu":3,"inpNeu":3,"outNeu":2,"storage":{"nn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.nn","exists":false},"ex":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ex","exists":false},"gn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.gn","exists":false},"ft":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ft","exists":false}}}
{"maxGnr":50,"maxGnm":1000,"mutThr":0.5,"fitEnd":1,"numHid":4,"numNeu":4,"inpNeu":3,"outNeu":2,"storage":{"nn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.nn","exists":false},"ex":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ex","exists":false},"gn":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.gn","exists":false},"ft":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ft","exists":false},"ln":{"filename":"\/home\/xdrm-brackets\/Desktop\/git.xdrm.io\/neural-network.php\/build\/neuralnetwork\/storage\/test\/test1.ln","exists":false}}}

View File

@ -2,7 +2,7 @@
"storage_parent": "/build/neuralnetwork/storage",
"default": {
"mutation_threshold": 0.3,
"mutation_threshold": 0.5,
"fitness_end": 1,
"storage": "_buffer",
"hidden_layers": 2,

View File

@ -32,9 +32,10 @@
$nn = NeuralNetwork::create(50, 1000);
$nn->setHiddenLayersCount(4);
$nn->setHiddenLayerNeuronsCount(3);
$nn->setHiddenLayerNeuronsCount(4);
$nn->setInputLayerCount(3);
$nn->setOutputLayerCount(2);
$nn->setMutationThreshold(0.5);
echo "$part. NeuralNetwork configured\n"; $part++;
@ -66,10 +67,12 @@
=========================================================*/
$fitness = 0;
$max_fit = 0;
$nn->initLearningRoutine(function($input, $output){
$nn->loadLearningRoutine(function($input, $output){
global $fitness;
if( $output[0] == behaviour($input)[0] ) $fitness++;
if( $output[1] == behaviour($input)[1] ) $fitness++;
$diff = [ abs($output[0] - behaviour($input)[0]), abs($output[1] - behaviour($input)[1]) ];
if( $diff[0] > 0 ) $fitness += 1 / $diff[0];
if( $diff[1] > 0 ) $fitness += 1 / $diff[1];
});
echo "$part. Learning routine initialized.\n"; $part++;
@ -79,6 +82,9 @@
/* (1) For each generation */
for( $gnr = 0 ; $gnr < 50 ; $gnr++ ){
$max_fit = 0;
$start = microtime(true);
/* (2) For each genome */
for( $gnm = 0 ; $gnm < 1000 ; $gnm++ ){
$fitness = 0;
@ -98,6 +104,8 @@
$nn->nextGenome();
}
echo "\n\t".((microtime(true)-$start))."s\n";
}