Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
uestla committed Oct 31, 2024
1 parent bb72253 commit dd97e5b
Show file tree
Hide file tree
Showing 16 changed files with 754 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Simplex/Dumper.php
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
}
}
56 changes: 56 additions & 0 deletions src/Simplex/Solution.php
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;
}
}
59 changes: 59 additions & 0 deletions src/Simplex/Solver.php
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);
}
}
92 changes: 92 additions & 0 deletions src/Simplex/Tableau/Tableau.php
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

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Property Simplex\Tableau\Tableau::$surplusVars is never read, only written.

Check failure on line 25 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Property Simplex\Tableau\Tableau::$surplusVars is never read, only written.

Check failure on line 25 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Property Simplex\Tableau\Tableau::$surplusVars is never read, only written.

Check failure on line 25 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Property Simplex\Tableau\Tableau::$surplusVars is never read, only written.

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

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Parameter #1 $a of method Simplex\Math\Fraction::isLowerThan() expects Simplex\Math\Fraction, Simplex\Math\Fraction|false given.

Check failure on line 67 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Parameter #1 $a of method Simplex\Math\Fraction::isLowerThan() expects Simplex\Math\Fraction, Simplex\Math\Fraction|false given.

Check failure on line 67 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Parameter #1 $a of method Simplex\Math\Fraction::isLowerThan() expects Simplex\Math\Fraction, Simplex\Math\Fraction|false given.

Check failure on line 67 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Parameter #1 $a of method Simplex\Math\Fraction::isLowerThan() expects Simplex\Math\Fraction, Simplex\Math\Fraction|false given.
$keyVal = $value;
$keyVarIdx = $varIdx;
}
}

if ($keyVal->isNegative()) {

Check failure on line 73 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Cannot call method isNegative() on Simplex\Math\Fraction|false.

Check failure on line 73 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Cannot call method isNegative() on Simplex\Math\Fraction|false.

Check failure on line 73 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Cannot call method isNegative() on Simplex\Math\Fraction|false.

Check failure on line 73 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Cannot call method isNegative() on Simplex\Math\Fraction|false.
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

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.

Check failure on line 90 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.

Check failure on line 90 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.2

Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.

Check failure on line 90 in src/Simplex/Tableau/Tableau.php

View workflow job for this annotation

GitHub Actions / PHPStan - 8.3

Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.
}
}
44 changes: 44 additions & 0 deletions src/Simplex/Task/Constraint/Constraint.php
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;
}
}
18 changes: 18 additions & 0 deletions src/Simplex/Task/Constraint/Equal.php
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
{}
18 changes: 18 additions & 0 deletions src/Simplex/Task/Constraint/GreaterOrEqual.php
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
{}
18 changes: 18 additions & 0 deletions src/Simplex/Task/Constraint/LowerOrEqual.php
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
{}
Loading

0 comments on commit dd97e5b

Please sign in to comment.