1 solution.Solution
solution.Solution(
status,
objective_value=None,
values=dict(),
multipliers=None,
iterations=None,
message='',
solve_time=None,
mip_gap=None,
best_bound=None,
constraint_violation=None,
feasibility_tolerance=None,
_raw_x=None,
_raw_layout_signature=None,
)Result of solving an optimization problem.
1.1 Attributes
| Name | Type | Description |
|---|---|---|
| status | SolverStatus | Solver termination status. |
| objective_value | float | None | Optimal objective function value (None if not solved). |
| values | dict[str, float] | Dictionary mapping variable names to optimal values. |
| multipliers | dict[str, float] | None | Lagrange multipliers for constraints (if available). |
| iterations | int | None | Number of solver iterations. |
| message | str | Solver message or error description. |
| solve_time | float | None | Time taken to solve (seconds). |
| constraint_violation | float | None | Maximum post-solve feasibility violation. None means feasibility was not checked. |
| feasibility_tolerance | float | None | Base absolute tolerance used for the feasibility check. None means feasibility was not checked. |
1.2 Example
solution = problem.solve() if solution.is_optimal: … print(f”Optimal value: {solution.objective_value}“) … print(f”x = {solution[‘x’]}“)
1.3 Methods
| Name | Description |
|---|---|
| from_json | Create solution from JSON string or file path. |
| get | Get the optimal value of a variable with a default. |
| print_vars | Pretty-print variable values. |
| to_dict | Convert solution to dictionary. |
| to_json | Convert solution to JSON string or save to file. |
1.3.1 from_json
solution.Solution.from_json(json_str_or_path)Create solution from JSON string or file path.
1.3.1.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| json_str_or_path | str | JSON string or path to JSON file. | required |
1.3.1.2 Returns
| Name | Type | Description |
|---|---|---|
| Solution | Solution object. |
1.3.2 get
solution.Solution.get(var, default=None)Get the optimal value of a variable with a default.
For scalar Variable: returns float. For VectorVariable: returns 1D numpy array. For MatrixVariable: returns 2D numpy array. For VariableDict: returns a dictionary keyed by its logical keys.
1.3.2.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| var | Variable | VectorVariable | MatrixVariable | VariableDict | str | Variable, VectorVariable, MatrixVariable, VariableDict, or variable name. | required |
| default | float | NDArray[np.floating] | dict[str, float] | None | Value to return if variable not found. | None |
1.3.2.2 Returns
| Name | Type | Description |
|---|---|---|
| float | NDArray[np.floating] | dict[str, float] | None | The optimal value(s) or default. |
1.3.3 print_vars
solution.Solution.print_vars()Pretty-print variable values.
1.3.4 to_dict
solution.Solution.to_dict()Convert solution to dictionary.
1.3.5 to_json
solution.Solution.to_json(path=None)Convert solution to JSON string or save to file.
1.3.5.1 Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| path | str | None | Optional file path to save JSON to. | None |
1.3.5.2 Returns
| Name | Type | Description |
|---|---|---|
| str | JSON string if path is None, otherwise empty string. |