Skip to content

Commit

Permalink
Solver: added solutions as class properties
Browse files Browse the repository at this point in the history
  • Loading branch information
uestla committed Mar 12, 2023
1 parent 1cd9ca2 commit fcd7eed
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions Simplex/Solver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ final class Solver
/** @var int */
private $maxSteps;

/** @var array<string, Fraction> */
private $solution;

/** @var array<int, array<string, Fraction>>|null */
private $alternativeSolutions;


/** @param int $maxSteps */
public function __construct(Task $task, $maxSteps = 16)
Expand Down Expand Up @@ -70,14 +76,7 @@ public function getSteps()
/** @return array<string, Fraction>|false|null */
public function getSolution()
{
// find first table with a solution in steps
foreach ($this->steps as $step) {
if ($step->isSolved()) {
return $step->getSolution();
}
}

return null;
return $this->solution;
}


Expand All @@ -100,21 +99,7 @@ public function getSolutionValue(array $solution)
/** @return array<string, Fraction>|null */
public function getAlternativeSolutions()
{
$first = false;
$alternatives = array();

foreach ($this->steps as $step) {
if ($step->isSolved()) {
if (!$first) {
$first = true;
continue ;
}

$alternatives[] = $step->getSolution();
}
}

return $first ? $alternatives : null;
return $this->alternativeSolutions;
}


Expand All @@ -140,8 +125,16 @@ private function solve(Task $task)
}
}

$this->solution = $tbl->getSolution();

if ($tbl->hasAlternativeSolution()) {
$this->steps[] = $tbl->getAlternativeSolution();
$altSolutionTbl = $tbl->getAlternativeSolution();

$this->steps[] = $altSolutionTbl;
$this->alternativeSolutions = array($altSolutionTbl->getSolution());

} elseif ($tbl->hasSolution()) {
$this->alternativeSolutions = array();
}
}

Expand Down

0 comments on commit fcd7eed

Please sign in to comment.