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){
|
|
|
|
return [($abc[0] & $abc[1]), $abc[1] | $abc[2]];
|
|
|
|
}
|
2016-10-26 15:15:00 +00:00
|
|
|
|
|
|
|
if( false && 'test_creating_dataset' ){
|
|
|
|
|
|
|
|
|
|
|
|
echo "Welcome to neural-network.php\n";
|
|
|
|
echo "-----------------------------\n";
|
|
|
|
|
|
|
|
$nn = NeuralNetwork::create(50, 100);
|
|
|
|
|
|
|
|
$nn->setHiddenLayersCount(2);
|
|
|
|
$nn->setHiddenLayerNeuronsCount(3);
|
|
|
|
|
|
|
|
$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));
|
2016-10-27 12:47:08 +00:00
|
|
|
$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));
|
2016-10-26 15:15:00 +00:00
|
|
|
|
2016-10-26 22:19:28 +00:00
|
|
|
$nn->store('test/test1', true);
|
2016-10-26 15:15:00 +00:00
|
|
|
|
2016-10-25 22:52:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
if( false && 'load_neural_network' ){
|
|
|
|
|
2016-10-26 22:19:28 +00:00
|
|
|
$nn = NeuralNetwork::load('test/test1');
|
2016-10-26 15:15:00 +00:00
|
|
|
|
|
|
|
}
|
2016-10-25 22:52:28 +00:00
|
|
|
|
2016-10-26 22:19:28 +00:00
|
|
|
if( false && 'test_genomes' ){
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (1) Basic Creation */
|
|
|
|
$a = new Genome(2, 3); // 2 layers of 3 neurons each -> randomly filled
|
2016-10-25 22:52:28 +00:00
|
|
|
|
2016-10-26 16:07:52 +00:00
|
|
|
echo "A : ".$a->serialize()."\n";
|
2016-10-26 15:24:51 +00:00
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (2) Inheritance */
|
2016-10-26 15:24:51 +00:00
|
|
|
$b = new Genome($a); // Clone of @aecho "A neurons\n";
|
2016-10-26 16:07:52 +00:00
|
|
|
echo "cloning A to B\n";
|
|
|
|
echo "B : ".$b->serialize()."\n";
|
2016-10-26 15:15:00 +00:00
|
|
|
|
|
|
|
/* (3) Section Title */
|
2016-10-26 15:53:56 +00:00
|
|
|
$b->mutation(0.3); // @b has now mutated with a threshold of 30%
|
2016-10-26 16:07:52 +00:00
|
|
|
echo "mutate B\n";
|
|
|
|
echo "B : ".$b->serialize()."\n";
|
2016-10-26 15:15:00 +00:00
|
|
|
|
|
|
|
/* (4) Cross-over (father+mother) */
|
|
|
|
$c = new Genome($a, $b); // @c is a randomly-done mix of @a and @b
|
2016-10-26 16:07:52 +00:00
|
|
|
echo "crossover : A+B -> C\n";
|
|
|
|
echo "C : ".$c->serialize()."\n";
|
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-26 22:19:28 +00:00
|
|
|
if( true ){
|
|
|
|
|
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
|
|
|
|
|
|
|
?>
|