Selling Icecreams

From Artificial Neural Network for PHP

Training

require_once 'ANN/ANN_Loader.php';

try
{
  $objNetwork = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
  print 'Creating a new one...';
	
  $objNetwork = new ANN_Network(2, 8, 1);

  $objTemperature = new ANN_InputValue(-15, 50); // Temperature in Celsius

  $objTemperature->saveToFile('input_temperature.dat');
  
  unset($objTemperature);

  $objHumidity    = new ANN_InputValue(0, 100);  // Humidity percentage

  $objHumidity->saveToFile('input_humidity.dat');

  unset($objHumidity);

  $objIcecream    = new ANN_OutputValue(0, 300); // Quantity of sold ice-creams

  $objIcecream->saveToFile('output_quantity.dat');

  unset($objIcecream);
}

try
{
  $objTemperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius

  $objHumidity    = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage

  $objIcecream    = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
  print 'Error loading value objects';
}

$arrInputs = array(
		array($objTemperature->getInputValue(20), $objHumidity->getInputValue(10)),
		array($objTemperature->getInputValue(30), $objHumidity->getInputValue(40)),
		array($objTemperature->getInputValue(32), $objHumidity->getInputValue(30)),
		array($objTemperature->getInputValue(33), $objHumidity->getInputValue(20))
		);

$arrOutputs = array(
		array($objIcecream->getOutputValue(20)),
		array($objIcecream->getOutputValue(90)),
		array($objIcecream->getOutputValue(70)),
		array($objIcecream->getOutputValue(75))
		);

$objNetwork->setInputs($arrInputs);

$objNetwork->setOutputs($arrOutputs);

$objNetwork->train();

$objNetwork->saveToFile('icecreams.dat');

Using trained network

require_once 'ANN/ANN_Loader.php';

try
{
  $objNetwork = ANN_Network::loadFromFile('icecreams.dat');
}
catch(Exception $e)
{
  print 'Network not found.';
}

try
{
  $objTemperature = ANN_InputValue::loadFromFile('input_temperature.dat'); // Temperature in Celsius

  $objHumidity = ANN_InputValue::loadFromFile('input_humidity.dat'); // Humidity percentage

  $objIcecream = ANN_OutputValue::loadFromFile('output_quantity.dat'); // Quantity of sold ice-creams
}
catch(Exception $e)
{
  print 'Error loading value objects';
}

$arrInputs = array(
	array($objTemperature->getInputValue(20), $objHumidity->getInputValue(10)),
	array($objTemperature->getInputValue(30), $objHumidity->getInputValue(40)),
	array($objTemperature->getInputValue(32), $objHumidity->getInputValue(30)),
	array($objTemperature->getInputValue(33), $objHumidity->getInputValue(20))
);

$objNetwork->setInputs($arrInputs);

$arrOutputs = $objNetwork->getOutputs();

foreach($arrOutputs as $floatOutput)
  print $objIcecream->getRealOutputValue($floatOutput). '<br />';