2016-10-25 22:52:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace neuralnetwork\core;
|
|
|
|
|
|
|
|
use neuralnetwork\core\NeuralNetworkCore;
|
2016-10-26 15:15:00 +00:00
|
|
|
use filemanager\core\FileManager;
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
/************************************************
|
|
|
|
**** Neural Network's Facade ****
|
|
|
|
************************************************/
|
|
|
|
class NeuralNetwork{
|
|
|
|
|
|
|
|
/* CREATES A NEW NEURAL NETWORK
|
|
|
|
*
|
|
|
|
* @generations<int> Maximum number of generations to process
|
|
|
|
* @genomes<int> Number of genomes per generation
|
|
|
|
*
|
|
|
|
* @return nn<NeuralNetwork> New NeuralNetwork
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function create($generations=null, $genomes=null){
|
|
|
|
/* Propagate instance creation */
|
|
|
|
return new NeuralNetworkCore($generations, $genomes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* LOADS A NEURAL NETWORK FROM STORAGE
|
|
|
|
*
|
|
|
|
* @storage<String> Path to storage
|
|
|
|
*
|
|
|
|
* @return nn<NeuralNetwork> Loaded NeuralNetwork
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function load($storage=null){
|
|
|
|
|
|
|
|
/* (1) Checks argument */
|
2016-10-26 15:15:00 +00:00
|
|
|
if( is_null($storage=NeuralNetworkCore::getStorage($storage)) )
|
2016-10-25 22:52:28 +00:00
|
|
|
throw new Error('Wrong NeuralNetwork loader\'s argument.');
|
|
|
|
|
|
|
|
/* (2) If files doesn't exist, raise error */
|
2016-10-26 15:15:00 +00:00
|
|
|
if( !$storage[0]['exists'] || !$storage[1]['exists'] || !$storage[2]['exists'] )
|
2016-10-25 22:52:28 +00:00
|
|
|
throw new Error('Loaded storage have file(s) missing.');
|
|
|
|
|
2016-10-26 15:15:00 +00:00
|
|
|
/* (3) Unserialize last NeuralNetwork */
|
|
|
|
$last_network = FileManager::read($storage[0]['filename']);
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
/* (4) Creates and fill instance */
|
2016-10-26 15:15:00 +00:00
|
|
|
$instance = new NeuralNetworkCore(0, 0);
|
|
|
|
$instance->unserialize($last_network);
|
2016-10-25 22:52:28 +00:00
|
|
|
|
|
|
|
/* (5) Returns instance */
|
|
|
|
return $instance;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|