91 lines
2.6 KiB
PHP
91 lines
2.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace neuralnetwork\core;
|
||
|
|
||
|
use neuralnetwork\core\NeuralNetworkCore;
|
||
|
|
||
|
/************************************************
|
||
|
**** Neural Network's Facade ****
|
||
|
************************************************/
|
||
|
class NeuralNetwork{
|
||
|
|
||
|
/* CHECKS AND STORES PATHS TO STORAGE FILES
|
||
|
*
|
||
|
* @path<String> Path to storage
|
||
|
*
|
||
|
* @return files<Array> Array containing storage files' information
|
||
|
* @return error<NULL> Returns NULL if error
|
||
|
*
|
||
|
*/
|
||
|
public static function setStorage($path){
|
||
|
/* (1) Checks the path */
|
||
|
if( !preg_match("/^(?P<path>\/(?:[a-z0-9_-]+\/)*)(?P<filename>[a-z0-9_-]+)$/i", $path, $matches) )
|
||
|
return null;
|
||
|
|
||
|
/* (2) Checks file's existence and returns it */
|
||
|
return [
|
||
|
[
|
||
|
'filename' => $matches['path'].$matches['filename'] .'.nn', // will contain neural network data
|
||
|
'exists' => is_file($matches['path'].$matches['filename'] .'.nn')
|
||
|
],
|
||
|
[
|
||
|
'filename' => $matches['path'].$matches['filename'] .'.ex', // will contain samples
|
||
|
'exists' => is_file($matches['path'].$matches['filename'] .'.ex')
|
||
|
]
|
||
|
];
|
||
|
|
||
|
}
|
||
|
|
||
|
/* 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 */
|
||
|
if( is_null($storage=self::setStorage($gnr_or_storage)) )
|
||
|
throw new Error('Wrong NeuralNetwork loader\'s argument.');
|
||
|
|
||
|
/* (2) If files doesn't exist, raise error */
|
||
|
if( !$storage[0]['exists'] || !$storage[1]['exists'] )
|
||
|
throw new Error('Loaded storage have file(s) missing.');
|
||
|
|
||
|
/* (3) Parses files */
|
||
|
$last_network = json_decode( FileManager::read($storage[0]['filename']), true);
|
||
|
|
||
|
/* (4) Creates and fill instance */
|
||
|
$instance = new NeuralNetworkCore($last_network['generations'], $last_network['genomes']);
|
||
|
$instance->setStorage($gnr_or_storage);
|
||
|
$instance->setFitnessEnd($last_network['fitness_end']);
|
||
|
$instance->setKeptGenomes($last_network['kept_genomes']);
|
||
|
$instance->setMutationThreshold($last_network['mutation_threshold']);
|
||
|
$instance->setMaxValues($last_network['max_values']);
|
||
|
|
||
|
/* (5) Returns instance */
|
||
|
return $instance;
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
?>
|