The Solution class contains the results of solving an optimization problem.
solution = prob.solve()
2 Accessing Optimal Values
2.1 Dictionary-Style Access
Get optimal values by variable name:
from optyx import Variable, Problemx = Variable("x", lb=0)y = Variable("y", lb=0)solution = ( Problem() .minimize(x**2+ y**2) .subject_to(x + y >=1) .solve())# Access by nameprint(f"x* = {solution['x']:.4f}")print(f"y* = {solution['y']:.4f}")
x* = 0.5000
y* = 0.5000
2.2 All Values
Get the full dictionary of optimal values:
print(solution.values)
{'x': 0.5, 'y': 0.4999999999999999}
3 Properties
Property
Type
Description
.status
SolverStatus
Solution status
.objective_value
float
Optimal objective value
.values
dict[str, float]
All optimal variable values
.solve_time
float
Time to solve (seconds)
.iterations
int
Number of solver iterations
.message
str
Solver message
.mip_gap
float \| None
Relative optimality gap (MILP only)
.best_bound
float \| None
Best dual bound (MILP only)
.constraint_violation
float \| None
Maximum measured bound or constraint violation
.feasibility_tolerance
float \| None
Tolerance used by post-solve feasibility validation
.is_optimal
bool
True if status is OPTIMAL
.feasibility_checked
bool
True when explicit feasibility evidence is available
.is_feasible
bool
True only when validation was performed and violation is within tolerance
is_feasible is deliberately independent of termination status. A MAX_ITERATIONS or TERMINATED result is feasible only when its returned candidate has passed the same post-solve feasibility check. Older serialized solutions without feasibility metadata load successfully but do not claim feasibility.
from optyx import Variable, Problem, SolverStatusx = Variable("x", lb=0, ub=1)# Impossible constraintssolution = ( Problem() .minimize(x) .subject_to(x >=2) # But x ≤ 1! .solve())print(f"Status: {solution.status}")if solution.status != SolverStatus.OPTIMAL:print(f"Problem: {solution.message}")
Status: SolverStatus.INFEASIBLE
Problem: The problem is infeasible. (HiGHS Status 8: model_status is Infeasible; primal_status is None) No feasible solution exists.
5.2 Defensive Programming
from optyx import Variable, Problem, SolverStatusdef safe_solve(prob):"""Solve with error handling.""" solution = prob.solve()if solution.status == SolverStatus.OPTIMAL:return solutionelif solution.status == SolverStatus.MAX_ITERATIONS:print("Warning: Reached iteration limit, solution may not be optimal")return solutionelse:raiseRuntimeError(f"Solver failed: {solution.message}")x = Variable("x", lb=0)prob = Problem().minimize(x**2).subject_to(x >=1)try: sol = safe_solve(prob)print(f"x* = {sol['x']:.4f}")exceptRuntimeErroras e:print(e)