2016-10-25 22:52:28 +00:00
|
|
|
<?php define('__ROOT__', dirname(dirname(__FILE__)) );
|
|
|
|
|
|
|
|
require_once __ROOT__.'/autoloader.php';
|
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
use \neuralnetwork\core\Genome;
|
2016-10-25 22:52:28 +00:00
|
|
|
use \neuralnetwork\core\NeuralNetwork;
|
|
|
|
use \filemanager\core\FileManager;
|
|
|
|
|
2016-10-27 12:47:08 +00:00
|
|
|
function behaviour($abc){
|
2016-10-28 11:03:08 +00:00
|
|
|
return [($abc[0] + $abc[1] - $abc[2])];
|
2016-10-27 12:47:08 +00:00
|
|
|
}
|
2016-10-26 15:15:00 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
if( false && 'learning_process' ){
|
2016-10-27 16:34:28 +00:00
|
|
|
|
|
|
|
$part = 1;
|
2016-10-26 15:15:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
echo "Welcome to neural-network.php\n";
|
2016-10-27 16:34:28 +00:00
|
|
|
echo "-----------------------------\n\n";
|
|
|
|
|
|
|
|
/* [1] Trying to load neural network
|
|
|
|
=========================================================*/
|
|
|
|
try{
|
|
|
|
|
|
|
|
$nn = NeuralNetwork::load('test/test1');
|
|
|
|
echo "$part. NeuralNetwork loaded from 'test/test1'\n"; $part++;
|
|
|
|
|
|
|
|
/* [2] Else, creates it
|
|
|
|
=========================================================*/
|
|
|
|
}catch(\Exception $e){
|
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
$nn = NeuralNetwork::create(50, 100);
|
2016-10-27 16:34:28 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
$nn->setHiddenLayersCount(3);
|
|
|
|
$nn->setHiddenLayerNeuronsCount(3);
|
2016-10-27 16:34:28 +00:00
|
|
|
$nn->setInputLayerCount(3);
|
2016-10-28 11:03:08 +00:00
|
|
|
$nn->setOutputLayerCount(1);
|
|
|
|
$nn->setMutationThreshold(0.3);
|
2016-10-27 16:34:28 +00:00
|
|
|
|
|
|
|
echo "$part. NeuralNetwork configured\n"; $part++;
|
|
|
|
|
|
|
|
$d = [0, 0, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [0, 0, 1]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [0, 1, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [0, 1, 1]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [1, 0, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [1, 0, 1]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [1, 1, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [1, 1, 1]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [0, 0, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [0, 0, 2]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [0, 2, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [0, 2, 2]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [2, 0, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [2, 0, 2]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [2, 2, 0]; $nn->addSample($d, behaviour($d));
|
|
|
|
$d = [2, 2, 2]; $nn->addSample($d, behaviour($d));
|
|
|
|
echo "$part. Samples added to NeuralNetwork\n"; $part++;
|
|
|
|
|
|
|
|
$nn->store('test/test1', true);
|
|
|
|
echo "$part. NeuralNetwork stored to 'test/test1'\n"; $part++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Initializing learning routine
|
|
|
|
=========================================================*/
|
|
|
|
$fitness = 0;
|
|
|
|
$max_fit = 0;
|
2016-10-27 19:28:09 +00:00
|
|
|
$nn->loadLearningRoutine(function($input, $output){
|
2016-10-27 16:34:28 +00:00
|
|
|
global $fitness;
|
2016-10-28 11:03:08 +00:00
|
|
|
$fitness -= abs($output[0] - behaviour($input)[0]);
|
2016-10-27 16:34:28 +00:00
|
|
|
});
|
|
|
|
echo "$part. Learning routine initialized.\n"; $part++;
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Learning through generations and genomes
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) For each generation */
|
2016-10-28 11:03:08 +00:00
|
|
|
$last_gnr = -1;
|
|
|
|
while( true ){
|
|
|
|
|
|
|
|
if( $nn->gnr > $last_gnr)
|
|
|
|
$start = microtime(true);
|
|
|
|
|
|
|
|
$last_gnr = $nn->gnr;
|
2016-10-27 16:34:28 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
$max_fit = -1e9;
|
2016-10-27 19:28:09 +00:00
|
|
|
|
2016-10-27 16:34:28 +00:00
|
|
|
/* (2) For each genome */
|
2016-10-28 11:03:08 +00:00
|
|
|
while( true ){
|
2016-10-27 16:34:28 +00:00
|
|
|
$fitness = 0;
|
|
|
|
|
|
|
|
/* (2.1) Get current genome */
|
|
|
|
$g = $nn->getGenome();
|
2016-10-28 11:03:08 +00:00
|
|
|
echo "\r[x] gnm ".($nn->gnm+1)."/100 on gnr ".($nn->gnr+1)."/50 - max_fit: $max_fit ";
|
2016-10-27 16:34:28 +00:00
|
|
|
|
|
|
|
/* (2.2) Train genome with random samples */
|
|
|
|
for( $r = 0 ; $r < 100 ; $r++ )
|
|
|
|
$g->train([rand(0,10), rand(0,10), rand(0,10)]);
|
|
|
|
|
|
|
|
/* (2.3) Set fitness & go to next genome */
|
|
|
|
if( $fitness > $max_fit ) $max_fit = $fitness;
|
|
|
|
|
|
|
|
$g->setFitness($fitness);
|
2016-10-28 11:03:08 +00:00
|
|
|
|
|
|
|
if( $nn->gnm >= 100-1 )
|
|
|
|
break;
|
|
|
|
|
2016-10-27 16:34:28 +00:00
|
|
|
$nn->nextGenome();
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
$nn->nextGenome();
|
|
|
|
|
|
|
|
// If generation evolution, notify
|
|
|
|
if( $nn->gnr > $last_gnr)
|
|
|
|
echo "\n\t".((microtime(true)-$start))."s\n";
|
|
|
|
|
|
|
|
if( $nn->gnr == 50-1 )
|
|
|
|
break;
|
2016-10-27 19:28:09 +00:00
|
|
|
|
2016-10-27 16:34:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
|
2016-10-25 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 16:34:28 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
if( true && 'guessing_process' ){
|
|
|
|
$part = 1;
|
|
|
|
|
2016-10-27 16:34:28 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
echo "Welcome to neural-network.php\n";
|
|
|
|
echo "-----------------------------\n\n";
|
2016-10-27 16:34:28 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
/* [1] Trying to load neural network
|
|
|
|
=========================================================*/
|
|
|
|
try{
|
2016-10-27 16:34:28 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
$nn = NeuralNetwork::load('test/test1');
|
|
|
|
echo "$part. NeuralNetwork loaded from 'test/test1'\n"; $part++;
|
2016-10-26 15:15:00 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
/* [2] Else, creates it
|
|
|
|
=========================================================*/
|
|
|
|
}catch(\Exception $e){
|
2016-10-26 15:15:00 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
echo "You must create/train your neural network before using it.\n";
|
|
|
|
exit();
|
|
|
|
}
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
/* [2] Fetch trained genome
|
|
|
|
=========================================================*/
|
|
|
|
$genome = $nn->getTrainedGenome();
|
|
|
|
$genome->setCallback(function($in, $out){
|
|
|
|
echo "callback input: ".implode(',', $in)."\n";
|
|
|
|
echo "callback output: ".round($out[0])."\n";
|
|
|
|
echo "callback result: ".implode(',', behaviour($in))."\n";
|
|
|
|
});
|
2016-10-26 15:24:51 +00:00
|
|
|
|
2016-10-28 11:03:08 +00:00
|
|
|
$genome->train([rand(0,10), rand(0,10), rand(0,10)]);
|
2016-10-26 15:15:00 +00:00
|
|
|
|
|
|
|
|
2016-10-26 22:19:28 +00:00
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
}
|
2016-10-25 22:52:28 +00:00
|
|
|
|
2016-10-27 16:34:28 +00:00
|
|
|
if( false ){
|
2016-10-26 22:19:28 +00:00
|
|
|
|
2016-10-27 12:47:08 +00:00
|
|
|
$g = new Genome(2, 3, 3, 2);
|
|
|
|
$fitness = 0;
|
|
|
|
$g->setCallback(function($input, $output){
|
|
|
|
global $fitness;
|
|
|
|
echo "callback output: ".round($output[0]).", ".round($output[1])."\n";
|
|
|
|
$result = behaviour($input);
|
|
|
|
if( $output[0] == $result[0] )
|
|
|
|
$fitness++;
|
|
|
|
|
|
|
|
if( $output[1] == $result[1] )
|
|
|
|
$fitness++;
|
|
|
|
});
|
|
|
|
|
|
|
|
echo $g->train([0, 0, 0]);
|
|
|
|
echo $g->train([0, 0, 1]);
|
|
|
|
echo $g->train([0, 1, 0]);
|
|
|
|
echo $g->train([0, 1, 1]);
|
|
|
|
echo $g->train([1, 0, 0]);
|
|
|
|
echo $g->train([1, 0, 1]);
|
|
|
|
echo $g->train([1, 1, 0]);
|
|
|
|
echo $g->train([1, 1, 1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
echo $g->train([0, 0, 0]);
|
|
|
|
echo $g->train([0, 0, 2]);
|
|
|
|
echo $g->train([0, 2, 0]);
|
|
|
|
echo $g->train([0, 2, 2]);
|
|
|
|
echo $g->train([2, 0, 0]);
|
|
|
|
echo $g->train([2, 0, 2]);
|
|
|
|
echo $g->train([2, 2, 0]);
|
|
|
|
echo $g->train([2, 2, 2]);
|
|
|
|
|
|
|
|
echo "fitness: $fitness\n";
|
|
|
|
$g->setFitness($fitness);
|
|
|
|
echo $g->serialize();
|
2016-10-26 22:19:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
// REWRITE TEST
|
|
|
|
// for( $a = 0, $al = 50 ; $a < $al ; $a++ )
|
|
|
|
// for( $b = 0, $bl = 20 ; $b < $bl ; $b++ ){
|
|
|
|
// print "genome $b/$bl on generation $a/$al \r";
|
|
|
|
// usleep(1000*10);
|
|
|
|
// }
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
?>
|