neuralnet.php/public/main.php

84 lines
2.3 KiB
PHP

<?php define('__ROOT__', dirname(dirname(__FILE__)) );
require_once __ROOT__.'/autoloader.php';
use \neuralnetwork\core\Genome;
use \neuralnetwork\core\NeuralNetwork;
use \filemanager\core\FileManager;
if( false && 'test_creating_dataset' ){
function behaviour($abc){
return [($abc[0] & $abc[1]) ^ $abc[2]];
}
echo "Welcome to neural-network.php\n";
echo "-----------------------------\n";
$nn = NeuralNetwork::create(50, 100);
$nn->setMaxValues([1, 1, 1], [1]);
$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));
$nn->store('A/B/C/test', true);
}
if( false && 'load_neural_network' ){
$nn = NeuralNetwork::load('A/B/C/test');
}
if( true && 'test_genomes' ){
/* (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";
/* (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";
/* (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";
/* (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";
}
// 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);
// }
?>