Selling Icecreams
From Artificial Neural Network for PHP
Training
require_once 'ANN/ANN_Network.php';
try
{
$network = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
print 'Creating a new one...';
$network = new ANN_Network(2,8,1);
$temperature = new ANN_InputValue(-15, 50); // Temperature in Celsius
$temperature->saveToFile('input_temperature.dat');
unset($temperature);
$humidity = new ANN_InputValue(0, 100); // Humidity percentage
$humidity->saveToFile('input_humidity.dat');
unset($humidity);
$icecream = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams
$icecream->saveToFile('output_quantity.dat');
unset($icecream);
}
try
{
$temperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius
$humidity = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage
$icecream = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
print 'Error loading value objects';
}
$inputs = array(
array($temperature->getInputValue(20), $humidity->getInputValue(10)),
array($temperature->getInputValue(30), $humidity->getInputValue(40)),
array($temperature->getInputValue(32), $humidity->getInputValue(30)),
array($temperature->getInputValue(33), $humidity->getInputValue(20))
);
$outputs = array(
array($icecream->getOutputValue(20)),
array($icecream->getOutputValue(90)),
array($icecream->getOutputValue(70)),
array($icecream->getOutputValue(75))
);
$network->setInputs($inputs);
$network->setOutputs($outputs);
$network->train();
$network->saveToFile('icecreams.dat');
Using trained network
require_once 'ANN/ANN_Network.php';
try
{
$network = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
print 'Network not found.';
}
try
{
$temperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius
$humidity = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage
$icecream = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
print 'Error loading value objects';
}
$inputs = array(
array($temperature->getInputValue(20), $humidity->getInputValue(10)),
array($temperature->getInputValue(30), $humidity->getInputValue(40)),
array($temperature->getInputValue(32), $humidity->getInputValue(30)),
array($temperature->getInputValue(33), $humidity->getInputValue(20))
);
$network->setInputs($inputs);
$outputs = $network->getOutputs();
foreach($outputs as $output)
print $icecream->getRealOutputValue($output). '<br />';