-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from chrhansk/feature-solver-refactor
Move status / result classes into dedicated files
- Loading branch information
Showing
6 changed files
with
135 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import numpy as np | ||
|
||
from pygradflow.status import SolverStatus | ||
|
||
|
||
class SolverResult: | ||
""" | ||
The result of a solution of a :py:class:`pygradflow.problem.Problem` | ||
instance with a :py:class:`pygradflow.solver.Solver` | ||
""" | ||
|
||
def __init__( | ||
self, | ||
x: np.ndarray, | ||
y: np.ndarray, | ||
d: np.ndarray, | ||
status: SolverStatus, | ||
iterations: int, | ||
num_accepted_steps: int, | ||
total_time: float, | ||
dist_factor: float, | ||
): | ||
self._x = x | ||
self._y = y | ||
self._d = d | ||
self._status = status | ||
self.iterations = iterations | ||
self.num_accepted_steps = num_accepted_steps | ||
self.total_time = total_time | ||
self.dist_factor = dist_factor | ||
|
||
@property | ||
def status(self) -> SolverStatus: | ||
""" | ||
The status of the solve as a :py:class:`pygradflow.solver.SolverStatus` | ||
""" | ||
return self._status | ||
|
||
@property | ||
def x(self) -> np.ndarray: | ||
""" | ||
The primal solution :math:`x \\in \\mathbb{R}^{n}` | ||
""" | ||
return self._x | ||
|
||
@property | ||
def y(self) -> np.ndarray: | ||
""" | ||
The dual solution :math:`y \\in \\mathbb{R}^{m}` | ||
""" | ||
return self._y | ||
|
||
@property | ||
def d(self) -> np.ndarray: | ||
""" | ||
The dual solution :math:`d \\in \\mathbb{R}^{n}` | ||
with respect to the variable bounds | ||
""" | ||
return self._d | ||
|
||
def __repr__(self) -> str: | ||
return "SolverResult(status={0})".format(self.status) | ||
|
||
@property | ||
def success(self): | ||
return SolverStatus.success(self.status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from enum import Enum, auto | ||
|
||
|
||
class SolverStatus(Enum): | ||
Optimal = auto() | ||
""" | ||
The algorithm has converged to a solution satisfying | ||
the optimality conditions according to given tolerances | ||
""" | ||
|
||
IterationLimit = auto() | ||
""" | ||
Reached the iteration limit precribed by the algorithmic | ||
parameters | ||
""" | ||
|
||
TimeLimit = auto() | ||
""" | ||
Reached the time limit precribed by the algorithmic | ||
parameters | ||
""" | ||
|
||
Unbounded = auto() | ||
""" | ||
Problem appearst unbounded (found feasible point with extremely | ||
small objective value) | ||
""" | ||
|
||
LocallyInfeasible = auto() | ||
""" | ||
Local infeasibility detected (found infeasible point being | ||
a local minimum with respect to constraint violation) | ||
""" | ||
|
||
@staticmethod | ||
def short_name(status): | ||
return { | ||
SolverStatus.Optimal: "optimal", | ||
SolverStatus.IterationLimit: "iteration_limit", | ||
SolverStatus.TimeLimit: "time_limit", | ||
SolverStatus.Unbounded: "unbounded", | ||
SolverStatus.LocallyInfeasible: "infeasible", | ||
}[status] | ||
|
||
@staticmethod | ||
def description(status): | ||
return { | ||
SolverStatus.Optimal: "Converged to first-order optimal solution", | ||
SolverStatus.IterationLimit: "Reached iteration limit", | ||
SolverStatus.TimeLimit: "Reached time limit", | ||
SolverStatus.Unbounded: "Problem appears unbounded", | ||
SolverStatus.LocallyInfeasible: "Local infeasibility detected", | ||
}[status] | ||
|
||
@staticmethod | ||
def success(status): | ||
""" | ||
Returns | ||
------- | ||
bool | ||
Whether the status indicates a successful solve | ||
""" | ||
return status == SolverStatus.Optimal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pygradflow" | ||
version = "0.4.19" | ||
version = "0.4.20" | ||
description = "PyGradFlow is a simple implementation of the sequential homotopy method to be used to solve general nonlinear programs." | ||
authors = ["Christoph Hansknecht <[email protected]>"] | ||
readme = "README.md" | ||
|