Documentation

This commit is contained in:
Dennis Eichhorn 2016-01-02 21:15:37 +01:00
parent d53cb93b79
commit c23d515173
24 changed files with 1135 additions and 35 deletions

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Account; namespace phpOMS\Account;
/**
* Account group class.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Group class Group
{ {

View File

@ -30,6 +30,12 @@ namespace phpOMS\DataStorage\Database\Query;
class Column class Column
{ {
/**
* Column name.
*
* @var string
* @since 1.0.0
*/
private $column = ''; private $column = '';
/** /**
@ -45,6 +51,14 @@ class Column
$this->column = $column; $this->column = $column;
} }
/**
* Get column string.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getColumn() : \string public function getColumn() : \string
{ {
return $this->column; return $this->column;

View File

@ -1,9 +1,50 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Regression class.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Regression class Regression
{ {
public static function linearRegression(array $x, array $y) /**
* Calculate linear regression.
*
* Example: ([1, 4, 6, 8, 9], [5, 3, 8, 6, 2])
*
* @param array $x X coordinates
* @param array $y Y coordinates
*
* @return array
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function linearRegression(array $x, array $y) : array
{ {
$count = count($x); $count = count($x);

View File

@ -1,29 +1,104 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Differential;
/**
* Chi square distribution.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class FiniteDifference class FiniteDifference
{ {
/**
* Epsilon.
*
* @var float
* @since 1.0.0
*/
const EPSILON = 0.00001; const EPSILON = 0.00001;
public static function getNewtonDifferenceQuotient(\string $formula, array $variables, \int $derivative = 1) : \float /**
* Differentiate by using the Newton difference quotient.
*
* Example: ('2*x^3-4x', ['x' => 99])
*
* @param string $formula Formula to differentiate
* @param array $variable Variable to differentiate (name value assiziation)
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getNewtonDifferenceQuotient(\string $formula, array $variable) : \float
{ {
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x']])) / self::EPSILON; return (Evaluator::evaluate($formula, ['x' => $variable['x'] + self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variable['x']])) / self::EPSILON;
} }
public static function getSymmetricDifferenceQuotient(\string $formula, array $variables, \int $derivative = 1) : \float /**
* Differentiate by using the symmetric difference quotient.
*
* Example: ('2*x^3-4x', ['x' => 99])
*
* @param string $formula Formula to differentiate
* @param array $variable Variable to differentiate (name value assiziation)
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getSymmetricDifferenceQuotient(\string $formula, array $variable) : \float
{ {
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON])) / (2 * self::EPSILON); return (Evaluator::evaluate($formula, ['x' => $variable['x'] + self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variable['x'] - self::EPSILON])) / (2 * self::EPSILON);
} }
public static function getFivePointStencil(\string $formula, array $variables, \int $derivative = 1) : \float /**
* Differentiate by using the five point stencil.
*
* Example: ('2*x^3-4x', ['x' => 99], 3)
*
* @param string $formula Formula to differentiate
* @param array $variable Variable to differentiate (name value assiziation)
* @param int $derivative Derivative (4th = highest)
*
* @return float
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getFivePointStencil(\string $formula, array $variable, \int $derivative = 1) : \float
{ {
if ($derivative === 1) { if ($derivative === 1) {
return (-Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) + 8 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - 8 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) + Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (12 * self::EPSILON); return (-Evaluator::evaluate($formula, ['x' => $variable['x'] + 2 * self::EPSILON]) + 8 * Evaluator::evaluate($formula, ['x' => $variable['x'] + self::EPSILON]) - 8 * Evaluator::evaluate($formula, ['x' => $variable['x'] - self::EPSILON]) + Evaluator::evaluate($formula, ['x' => $variable['x'] - 2 * self::EPSILON])) / (12 * self::EPSILON);
} elseif ($derivative === 2) { } elseif ($derivative === 2) {
return (-Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) + 16 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) - 30 * Evaluator::evaluate($formula, ['x' => $variables['x']]) + 16 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (12 * self::EPSILON ** 2); return (-Evaluator::evaluate($formula, ['x' => $variable['x'] + 2 * self::EPSILON]) + 16 * Evaluator::evaluate($formula, ['x' => $variable['x'] + self::EPSILON]) - 30 * Evaluator::evaluate($formula, ['x' => $variable['x']]) + 16 * Evaluator::evaluate($formula, ['x' => $variable['x'] - self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variable['x'] - 2 * self::EPSILON])) / (12 * self::EPSILON ** 2);
} elseif ($derivative === 3) { } elseif ($derivative === 3) {
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) - 2 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) + 2 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (2 * self::EPSILON ** 3); return (Evaluator::evaluate($formula, ['x' => $variable['x'] + 2 * self::EPSILON]) - 2 * Evaluator::evaluate($formula, ['x' => $variable['x'] + self::EPSILON]) + 2 * Evaluator::evaluate($formula, ['x' => $variable['x'] - self::EPSILON]) - Evaluator::evaluate($formula, ['x' => $variable['x'] - 2 * self::EPSILON])) / (2 * self::EPSILON ** 3);
} elseif ($derivative === 4) { } elseif ($derivative === 4) {
return (Evaluator::evaluate($formula, ['x' => $variables['x'] + 2 * self::EPSILON]) - 4 * Evaluator::evaluate($formula, ['x' => $variables['x'] + self::EPSILON]) + 6 * Evaluator::evaluate($formula, ['x' => $variables['x']]) - 4 * Evaluator::evaluate($formula, ['x' => $variables['x'] - self::EPSILON]) + Evaluator::evaluate($formula, ['x' => $variables['x'] - 2 * self::EPSILON])) / (self::EPSILON ** 4); return (Evaluator::evaluate($formula, ['x' => $variable['x'] + 2 * self::EPSILON]) - 4 * Evaluator::evaluate($formula, ['x' => $variable['x'] + self::EPSILON]) + 6 * Evaluator::evaluate($formula, ['x' => $variable['x']]) - 4 * Evaluator::evaluate($formula, ['x' => $variable['x'] - self::EPSILON]) + Evaluator::evaluate($formula, ['x' => $variable['x'] - 2 * self::EPSILON])) / (self::EPSILON ** 4);
} }
throw new \Exception('Derivative too high'); throw new \Exception('Derivative too high');

View File

@ -1,21 +1,118 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math; namespace phpOMS\Math;
/**
* Well known functions class.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Functions class Functions
{ {
public static function getGammaInteger(\int $k) /**
* Calculate gammar function value.
*
* Example: (7)
*
* @param int $k Variable
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getGammaInteger(\int $k) : \int
{ {
return self::fact($k-1); return self::fact($k - 1);
} }
public static function fact(\int $n, \int $start = 1) /**
* Calculate gammar function value.
*
* Example: (7, 2)
*
* @param int $n Factorial upper bound
* @param int $start Factorial starting value
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function fact(\int $n, \int $start = 1) : \int
{ {
$fact = 1; $fact = 1;
for($i = $start; $i < $n; $i++) { for ($i = $start; $i < $n + 1; $i++) {
$fact *= $i; $fact *= $i;
} }
return $fact; return $fact;
} }
/**
* Calculate binomial coefficient
*
* Algorithm optimized for large factorials without the use of big int or string manipulation.
*
* Example: (7, 2)
*
* @param int $n
* @param int $k
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function binomialCoefficient(\int $n, \int $k) : \int
{
$max = max([$k, $n - $k]);
$min = min([$k, $n - $k]);
$fact = 1;
$range = array_reverse(range(1, $min));
for ($i = $max + 1; $i < $n + 1; $i++) {
$div = 1;
foreach ($range as $key => $d) {
if ($i % $d === 0) {
$div = $d;
unset($range[$key]);
break;
}
}
$fact *= $i / $div;
}
$fact2 = 1;
foreach ($range as $d) {
$fact2 *= $d;
}
return $fact / $fact2;
}
} }

View File

@ -1,13 +1,60 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Optimization\TSP; namespace phpOMS\Math\Optimization\TSP;
/**
* TSP solution with brute force.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class BruteForce class BruteForce
{ {
/**
* City limit (for factorial calculation).
*
* @var float
* @since 1.0.0
*/
const LIMIT = 22; const LIMIT = 22;
/**
* City pool.
*
* @var CityPool
* @since 1.0.0
*/
private $cityPool = null; private $cityPool = null;
/**
* Constructor.
*
* @param CityPool $pool City pool
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(CityPool $pool) public function __construct(CityPool $pool)
{ {
$this->cityPool = $pool; $this->cityPool = $pool;
@ -17,6 +64,16 @@ class BruteForce
} }
} }
/**
* Calculate best routes.
*
* @param int $limit Amount of best routes
*
* @return Population
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getSolution(\int $limit = 1) : Population public function getSolution(\int $limit = 1) : Population
{ {
$population = new Population($this->cityPool, $limit, true); $population = new Population($this->cityPool, $limit, true);
@ -27,6 +84,18 @@ class BruteForce
return $population; return $population;
} }
/**
* Bruteforce best solutions.
*
* @param array $cities Cities
* @param Tour $tour Current (potential) optimal tour
* @param Population $population Population of tours
*
* @return Population
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function bruteForce(array $cities, Tour $tour, Population $population) private function bruteForce(array $cities, Tour $tour, Population $population)
{ {
if (count($cities) === 0) { if (count($cities) === 0) {

View File

@ -1,15 +1,69 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Optimization\TSP; namespace phpOMS\Math\Optimization\TSP;
use phpOMS\Math\Shape\D3\Sphere; use phpOMS\Math\Shape\D3\Sphere;
/**
* City class.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class City class City
{ {
/**
* City name
*
* @var string
* @since 1.0.0
*/
private $name = ''; private $name = '';
private $long = 0.0;
private $lat = 0.0;
/**
* City longitude
*
* @var float
* @since 1.0.0
*/
private $long = 0.0;
/**
* City latitude
*
* @var float
* @since 1.0.0
*/
private $lat = 0.0;
/**
* Constructor.
*
* @param float $lat Latitude
* @param float $long Longitude
* @param string $name City name
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(\float $lat, \float $long, \string $name) public function __construct(\float $lat, \float $long, \string $name)
{ {
$this->long = $long; $this->long = $long;
@ -17,26 +71,70 @@ class City
$this->name = $name; $this->name = $name;
} }
/**
* Get longitude.
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getLongitude() : \float public function getLongitude() : \float
{ {
return $this->long; return $this->long;
} }
/**
* Get latitude.
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getLatitude() : \float public function getLatitude() : \float
{ {
return $this->lat; return $this->lat;
} }
/**
* Get name.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getName() : \string public function getName() : \string
{ {
return $this->name; return $this->name;
} }
/**
* Is equals to.
*
* @param City $city City
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function equals(City $city) : \bool public function equals(City $city) : \bool
{ {
return $this->name === $city->getName() && $this->lat === $city->getLatitude() && $this->long === $city->getLatitude(); return $this->name === $city->getName() && $this->lat === $city->getLatitude() && $this->long === $city->getLatitude();
} }
/**
* Distance to city
*
* @param City $city City
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getDistanceTo(City $city) : \float public function getDistanceTo(City $city) : \float
{ {
return Sphere::distance2PointsOnSphere($this->lat, $this->long, $city->getLatitude(), $city->getLongitude()); return Sphere::distance2PointsOnSphere($this->lat, $this->long, $city->getLatitude(), $city->getLongitude());

View File

@ -1,31 +1,106 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Optimization\TSP; namespace phpOMS\Math\Optimization\TSP;
/**
* City pool.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class CityPool implements \Countable class CityPool implements \Countable
{ {
/**
* Cities
*
* @var array
* @since 1.0.0
*/
private $cities = []; private $cities = [];
public function __construct($cities = []) /**
* Constructor.
*
* @param City[] $cities Cities
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(array $cities = [])
{ {
$this->cities = $cities; $this->cities = $cities;
} }
/**
* Add city.
*
* @param City $city City
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function addCity(City $city) public function addCity(City $city)
{ {
$this->cities[$city->getName() . $city->getLatitude() . $city->getLongitude()] = $city; $this->cities[$city->getName() . $city->getLatitude() . $city->getLongitude()] = $city;
} }
public function getCity($index) : City /**
* Get city.
*
* @param int $index City index
*
* @return City
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCity(\int $index) : City
{ {
return array_values($this->cities)[$index]; return array_values($this->cities)[$index];
} }
/**
* Get cities.
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCities() : array public function getCities() : array
{ {
return $this->cities; return $this->cities;
} }
/**
* Has city.
*
* @param City $city City
*
* @return \bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function hasCity(City $city) : \bool public function hasCity(City $city) : \bool
{ {
foreach ($this->cities as $c) { foreach ($this->cities as $c) {
@ -37,6 +112,14 @@ class CityPool implements \Countable
return false; return false;
} }
/**
* Count cities
*
* @return \int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function count() : \int public function count() : \int
{ {
return count($this->cities); return count($this->cities);

View File

@ -1,20 +1,89 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Optimization\TSP; namespace phpOMS\Math\Optimization\TSP;
/**
* TSP solution by genetic algorithm.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class GA class GA
{ {
const MUTATION = 15; /* 1000 = 100% */ /**
const TOURNAMENT = 5; * Mutation percentage
const ELITISM = true; *
* @var int
* @since 1.0.0
*/
const MUTATION = 15; /* 1000 = 100% */
/**
* Tournaments
*
* @var int
* @since 1.0.0
*/
const TOURNAMENT = 5;
/**
* Elitism
*
* @var bool
* @since 1.0.0
*/
const ELITISM = true;
/**
* City pool
*
* @var CityPool
* @since 1.0.0
*/
private $cityPool = null; private $cityPool = null;
/**
* Constructor.
*
* @param CityPool $pool City pool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(CityPool $pool) public function __construct(CityPool $pool)
{ {
$this->cityPool = $pool; $this->cityPool = $pool;
} }
/**
* Evolve population.
*
* @param Population $population Population to eveolve
*
* @return Population
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function evolvePopulation(Population $population) : Population public function evolvePopulation(Population $population) : Population
{ {
$shift = self::ELITISM ? 1 : 0; $shift = self::ELITISM ? 1 : 0;
@ -39,6 +108,17 @@ class GA
return $newPopulation; return $newPopulation;
} }
/**
* Crossover tours
*
* @param Tour $tour1 Tour 1
* @param Tour $tour2 Tour 2
*
* @return Tour
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function crossover(Tour $tour1, Tour $tour2) : Tour public function crossover(Tour $tour1, Tour $tour2) : Tour
{ {
$child = new Tour($this->cityPool, false); $child = new Tour($this->cityPool, false);
@ -72,6 +152,16 @@ class GA
return $child; return $child;
} }
/**
* Mutate tour
*
* @param Tour $tour Tour to mutate
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function mutate(Tour $tour) private function mutate(Tour $tour)
{ {
$count = $tour->count(); $count = $tour->count();
@ -91,6 +181,16 @@ class GA
} }
} }
/**
* Find fittest
*
* @param Population $population Population to evaluate
*
* @return Tour
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function tournamentSelection(Population $population) : Tour private function tournamentSelection(Population $population) : Tour
{ {
$tournament = new Population($this->cityPool, self::TOURNAMENT, false); $tournament = new Population($this->cityPool, self::TOURNAMENT, false);

View File

@ -1,41 +1,126 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Optimization\TSP; namespace phpOMS\Math\Optimization\TSP;
/**
* Population class.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Population implements \Countable class Population implements \Countable
{ {
/**
* Tours
*
* @var array
* @since 1.0.0
*/
private $tours = []; private $tours = [];
public function __construct(CityPool $pool, \int $size, \bool $initialise = false) /**
* Constructor.
*
* @param CityPool $pool City pool
* @param int $size Population size
* @param bool $initialize Initialize with random tours
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(CityPool $pool, \int $size, \bool $initialize = false)
{ {
if ($initialise) { if ($initialize) {
for ($i = 0; $i < $size; $i++) { for ($i = 0; $i < $size; $i++) {
$this->tours[] = new Tour($pool, true); $this->tours[] = new Tour($pool, true);
} }
} }
} }
/**
* Insert Tour at position.
*
* @param int $index Position to insert at
* @param Tour $tour Tour to insert
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function insertTourAt(\int $index, Tour $tour) public function insertTourAt(\int $index, Tour $tour)
{ {
$this->tours = array_slice($this->tours, 0, $index) + [$tour] + array_slice($this->tours, $index); $this->tours = array_slice($this->tours, 0, $index) + [$tour] + array_slice($this->tours, $index);
} }
/**
* Set tour at position
*
* @param int $index Position to set/replace
* @param Tour $tour Tour to set
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setTour(\int $index, Tour $tour) public function setTour(\int $index, Tour $tour)
{ {
$this->tours[$index] = $tour; $this->tours[$index] = $tour;
asort($this->tours); asort($this->tours);
} }
/**
* Add tour
*
* @param Tour $tour Tour to add
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function addTour(Tour $tour) public function addTour(Tour $tour)
{ {
$this->tours[] = $tour; $this->tours[] = $tour;
} }
/**
* Get tour
*
* @param int $index Index of tour
*
* @return null|Tour
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getTour(\int $index) public function getTour(\int $index)
{ {
return $this->tours[$index] ?? null; return $this->tours[$index] ?? null;
} }
/**
* Get fittest tour
*
* @return Tour
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getFittest() : Tour public function getFittest() : Tour
{ {
$fittest = $this->tours[0]; $fittest = $this->tours[0];
@ -50,6 +135,14 @@ class Population implements \Countable
return $fittest; return $fittest;
} }
/**
* Get unfittest tour
*
* @return Tour
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getUnfittest() : Tour public function getUnfittest() : Tour
{ {
$unfittest = $this->tours[0]; $unfittest = $this->tours[0];
@ -64,6 +157,14 @@ class Population implements \Countable
return $unfittest; return $unfittest;
} }
/**
* Get tour count
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function count() : \int public function count() : \int
{ {
return count($this->tours); return count($this->tours);

View File

@ -1,30 +1,108 @@
<?php <?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Optimization\TSP; namespace phpOMS\Math\Optimization\TSP;
/**
* Tour class.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Tour implements \Countable class Tour implements \Countable
{ {
private $cities = []; /**
private $fitness = 0.0; * Cities
*
* @var City[]
* @since 1.0.0
*/
private $cities = [];
/**
* Tour fitness
*
* @var City[]
* @since 1.0.0
*/
private $fitness = 0.0;
/**
* Tour distance
*
* @var City[]
* @since 1.0.0
*/
private $distance = 0.0; private $distance = 0.0;
/**
* City pool
*
* @var CityPool
* @since 1.0.0
*/
private $cityPool = null; private $cityPool = null;
public function __construct(CityPool $pool, \bool $initialise = false) /**
* Constructor.
*
* @param CityPool $pool City pool
* @param bool $initialize Initialize with random tours
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(CityPool $pool, \bool $initialize = false)
{ {
$this->cityPool = $pool; $this->cityPool = $pool;
if ($initialise) { if ($initialize) {
$this->cities = $this->cityPool->getCities(); $this->cities = $this->cityPool->getCities();
shuffle($this->cities); shuffle($this->cities);
} }
} }
/**
* Get city.
*
* @param int $index Index
*
* @return null|City
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCity($index) public function getCity($index)
{ {
return array_values($this->cities)[$index] ?? null; return array_values($this->cities)[$index] ?? null;
} }
/**
* Get fitness.
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getFitness() : \float public function getFitness() : \float
{ {
if ($this->fitness === 0.0 && ($distance = $this->getDistance()) !== 0.0) { if ($this->fitness === 0.0 && ($distance = $this->getDistance()) !== 0.0) {
@ -34,6 +112,14 @@ class Tour implements \Countable
return $this->fitness; return $this->fitness;
} }
/**
* Add city to tour.
*
* @param City $city City
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function addCity(City $city) public function addCity(City $city)
{ {
$this->cities[] = $city; $this->cities[] = $city;
@ -42,6 +128,15 @@ class Tour implements \Countable
$this->distance = 0.0; $this->distance = 0.0;
} }
/**
* Set city
*
* @param int $index Index to set/replace
* @param City $city City
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setCity(\int $index, City $city) public function setCity(\int $index, City $city)
{ {
$this->cities[$index] = $city; $this->cities[$index] = $city;
@ -51,6 +146,14 @@ class Tour implements \Countable
$this->distance = 0.0; $this->distance = 0.0;
} }
/**
* Get tour distance
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getDistance() : \float public function getDistance() : \float
{ {
if ($this->distance === 0.0) { if ($this->distance === 0.0) {
@ -70,6 +173,16 @@ class Tour implements \Countable
return $this->distance; return $this->distance;
} }
/**
* Has city.
*
* @param City $city City
*
* @return \bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function hasCity(City $city) : \bool public function hasCity(City $city) : \bool
{ {
foreach ($this->cities as $c) { foreach ($this->cities as $c) {
@ -81,6 +194,14 @@ class Tour implements \Countable
return false; return false;
} }
/**
* Get city count
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function count() : \int public function count() : \int
{ {
return count($this->cities); return count($this->cities);

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Circle shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Circle class Circle
{ {

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Ellipse shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Ellipse class Ellipse
{ {

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Rectangle shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Rectangle class Rectangle
{ {

View File

@ -15,7 +15,18 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
class Rectangle /**
* Trapezoid shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Trapezoid
{ {
/** /**

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Triangle shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Triangle class Triangle
{ {

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Cone shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Cone class Cone
{ {

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Cuboid shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Cuboid class Cuboid
{ {

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Cylinder shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Cylinder class Cylinder
{ {

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Rectangular pyramid shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class RectangularPyramid class RectangularPyramid
{ {

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Shape\D3; namespace phpOMS\Math\Shape\D3;
/**
* Sphere shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Sphere class Sphere
{ {
@ -25,7 +36,7 @@ class Sphere
* @param float $longStart Longitude of start point in deg * @param float $longStart Longitude of start point in deg
* @param float $latEnd Latitude of target point in deg * @param float $latEnd Latitude of target point in deg
* @param float $longEnd Longitude of target point in deg * @param float $longEnd Longitude of target point in deg
* @param float $radius Sphere radius * @param float $radius Sphere radius (6371000 = earth)
* *
* @return float Distance between points in meter * @return float Distance between points in meter
* *

View File

@ -15,6 +15,17 @@
*/ */
namespace phpOMS\Math\Algebra; namespace phpOMS\Math\Algebra;
/**
* Tetraedron shape.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Tetrahedron class Tetrahedron
{ {

View File

@ -0,0 +1,172 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Math\Stochastic\Distribution;
/**
* Uniform (discrete) distribution.
*
* @category Framework
* @package phpOMS\DataStorage\Database
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class UniformDistributionDiscrete
{
/**
* Get probability mass function.
*
* @param \float $a
* @param float $b
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getPmf(\float $a, \float $b) : \float
{
return 1 / ($b - $a + 1);
}
/**
* Get cumulative distribution function.
*
* @param float $k
* @param float $a
* @param float $b
*
* @return float
*
* @throws
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getCdf(\float $k, \float $a, \float $b) : \float
{
if ($k > $b || $k < $a) {
throw new \Exception('Out of bounds');
}
return (floor($k) - $a + 1) / ($b - $a + 1);
}
/**
* Get moment generating function.
*
* @param int $t
* @param float $a
* @param float $b
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getMgf(\int $t, \float $a, \float $b) : \float
{
return (exp($a * $t) - exp(($b + 1) * $t)) / (($b - $a + 1) * (1 - exp($t)));
}
/**
* Get skewness.
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getSkewness() : \float
{
return 0.0;
}
/**
* Get Ex. kurtosis.
*
* @param float $a
* @param float $b
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getExKurtosis(\float $a, \float $b) : \float
{
$n = ($b - $a + 1);
return -6 / 5 * ($n ** 2 + 1) / ($n ** 2 - 1);
}
/**
* Get expected value.
*
* @param float $a
* @param float $b
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getMedian(\float $a, \float $b) : \float
{
return ($a + $b) / 2;
}
/**
* Get expected value.
*
* @param float $a
* @param float $b
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getMean(\float $a, \float $b) : \float
{
return ($a + $b) / 2;
}
/**
* Get variance.
*
* @param float $a
* @param float $b
*
* @return float
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getVariance(\float $a, \float $b) : \float
{
return (($b - $a + 1) ** 2 - 1) / 12;
}
public static function getRandom()
{
}
}

View File

@ -15,11 +15,8 @@
*/ */
namespace phpOMS\Uri; namespace phpOMS\Uri;
/** /**
* Uri interface. * Uri exception.
*
* Used in order to create and evaluate a uri
* *
* @category Uri * @category Uri
* @package Framework * @package Framework