Path to storage * * @return files Array containing storage files' information * @return error Returns NULL if error * */ public static function setStorage($path){ /* (1) Checks the path */ if( !preg_match("/^(?P\/(?:[a-z0-9_-]+\/)*)(?P[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 Maximum number of generations to process * @genomes Number of genomes per generation * * @return nn 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 Path to storage * * @return nn 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; } } ?>