-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex; | ||
|
||
use Simplex\Math\Vector; | ||
use Simplex\Math\Fraction; | ||
use Simplex\Task\InitialTask; | ||
use Simplex\Task\StandardTask; | ||
|
||
|
||
final class Dumper | ||
{ | ||
public static function dump(mixed $v): void | ||
{ | ||
if ($v instanceof Fraction) { | ||
self::dumpFraction($v); | ||
|
||
} elseif ($v instanceof Vector) { | ||
self::dumpVector($v); | ||
|
||
} elseif ($v instanceof InitialTask) { | ||
self::dumpInitialTask($v); | ||
|
||
} elseif ($v instanceof StandardTask) { | ||
// self::dumpStandardTask($v); | ||
|
||
} else { | ||
throw new \InvalidArgumentException(sprintf('Cannot dump variable of type %s', is_object($v) ? get_class($v) : gettype($v))); | ||
} | ||
} | ||
|
||
|
||
private static function dumpFraction(Fraction $f): void | ||
{ | ||
echo $f; | ||
} | ||
|
||
|
||
private static function dumpVector(Vector $v): void | ||
{ | ||
echo '[', implode(', ', $v->toArray()), ']'; | ||
} | ||
|
||
|
||
private static function dumpInitialTask(InitialTask $t): void | ||
{ | ||
echo 'f: '; // TODO | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex; | ||
|
||
use Simplex\Math\Vector; | ||
use Simplex\Tableau\Tableau; | ||
|
||
|
||
final class Solution | ||
{ | ||
/** @var Tableau[] */ | ||
private array $tableaus; | ||
|
||
private Vector $values; | ||
|
||
private ?Vector $alternativeValues; | ||
|
||
|
||
/** @param Tableau[] $tableaus */ | ||
public function __construct(array $tableaus, Vector $values, ?Vector $alternativeValues) | ||
{ | ||
$this->values = $values; | ||
$this->tableaus = $tableaus; | ||
$this->alternativeValues = $alternativeValues; | ||
} | ||
|
||
|
||
/** @return Tableau[] */ | ||
public function getTableaus(): array | ||
{ | ||
return $this->tableaus; | ||
} | ||
|
||
|
||
public function getValues(): Vector | ||
{ | ||
return $this->values; | ||
} | ||
|
||
|
||
public function getAlternativeValues(): ?Vector | ||
{ | ||
return $this->alternativeValues; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex; | ||
|
||
use Simplex\Math\Vector; | ||
use Simplex\Task\InitialTask; | ||
|
||
|
||
final class Solver | ||
{ | ||
private readonly int $maxSteps; | ||
|
||
|
||
public function __construct(int $maxSteps = 128) | ||
{ | ||
$this->maxSteps = $maxSteps; | ||
} | ||
|
||
|
||
public function maximize(InitialTask $task): Solution | ||
{ | ||
$standardTask = $task->toStandardTask(); | ||
$tableau = $standardTask->toTableau(); | ||
|
||
dumpe($tableau, ':OOO'); | ||
|
||
$tableaus = []; | ||
while (count($tableaus) < $this->maxSteps) { | ||
$tableaus[] = $tableau; | ||
|
||
dumpe($tableau, ':OOO'); | ||
} | ||
|
||
dumpe($standardTask); | ||
|
||
// TODO: throw exception when solution not found in under $maxSteps steps? | ||
return new Solution([], new Vector([]), null); | ||
} | ||
|
||
|
||
public function minimize(InitialTask $task): Solution | ||
{ | ||
// TODO | ||
|
||
return new Solution([], new Vector([]), null); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex\Tableau; | ||
|
||
use Simplex\Math\Fraction; | ||
|
||
|
||
final class Tableau | ||
{ | ||
/** @var array<int, array<int, Fraction>> */ | ||
private array $rows = []; | ||
|
||
/** @var array<int, true> */ | ||
private readonly array $surplusVars; | ||
Check failure on line 25 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
Check failure on line 25 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.3
Check failure on line 25 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
|
||
|
||
private readonly bool $hasSurplusVarInBasis; | ||
|
||
|
||
/** @param array<int, true> $surplusVars */ | ||
public function __construct(array $surplusVars, bool $hasSurplusVarInBasis) | ||
{ | ||
$this->surplusVars = $surplusVars; | ||
$this->hasSurplusVarInBasis = $hasSurplusVarInBasis; | ||
} | ||
|
||
|
||
/** @param array<int, Fraction> $values */ | ||
public function addRow(int $varIdx, array $values): void | ||
{ | ||
$this->rows[$varIdx] = $values; | ||
} | ||
|
||
|
||
public function isFinished(): bool | ||
{ | ||
$lastRow = $this->getLastRow(); | ||
|
||
if ($lastRow === null) { | ||
// TODO: no solution | ||
return true; | ||
} | ||
|
||
if ($this->hasSurplusVarInBasis) { | ||
foreach ($lastRow as $value) { | ||
if ($value->isNegative()) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
reset($lastRow); | ||
$keyVarIdx = key($lastRow); | ||
$keyVal = current($lastRow); | ||
|
||
foreach ($lastRow as $varIdx => $value) { | ||
if ($value->isLowerThan($keyVal)) { | ||
Check failure on line 67 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
Check failure on line 67 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.3
Check failure on line 67 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
|
||
$keyVal = $value; | ||
$keyVarIdx = $varIdx; | ||
} | ||
} | ||
|
||
if ($keyVal->isNegative()) { | ||
Check failure on line 73 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
Check failure on line 73 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.3
Check failure on line 73 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
|
||
foreach ($this->rows as $row) { | ||
if ($row[$keyVarIdx]->isPositive()) { | ||
return false; | ||
} | ||
} | ||
|
||
// TODO: no solution | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
/** @return array<int, Fraction>|null */ | ||
private function getLastRow(): ?array | ||
{ | ||
return end($this->rows) ?: null; | ||
Check failure on line 90 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
Check failure on line 90 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.3
Check failure on line 90 in src/Simplex/Tableau/Tableau.php GitHub Actions / PHPStan - 8.2
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex\Task\Constraint; | ||
|
||
use Simplex\Math\Vector; | ||
use Simplex\Math\Fraction; | ||
|
||
|
||
abstract class Constraint | ||
{ | ||
private readonly Vector $coeffs; | ||
private readonly Fraction $value; | ||
|
||
|
||
/** @param Vector|array<int, Fraction|string|int|float> $coeffs */ | ||
public function __construct(Vector|array $coeffs, Fraction|string|int|float $value) | ||
{ | ||
$this->coeffs = Vector::create($coeffs); | ||
$this->value = Fraction::create($value); | ||
} | ||
|
||
|
||
public function getCoeffs(): Vector | ||
{ | ||
return $this->coeffs; | ||
} | ||
|
||
|
||
public function getValue(): Fraction | ||
{ | ||
return $this->value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex\Task\Constraint; | ||
|
||
|
||
final class Equal extends Constraint | ||
{} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex\Task\Constraint; | ||
|
||
|
||
final class GreaterOrEqual extends Constraint | ||
{} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the Simplex-Calculator library | ||
* | ||
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz) | ||
* | ||
* @license MIT | ||
* @link https://github.com/uestla/Simplex-Calculator | ||
*/ | ||
|
||
namespace Simplex\Task\Constraint; | ||
|
||
|
||
final class LowerOrEqual extends Constraint | ||
{} |