Skip to content

Commit

Permalink
Add property AMPL.solve_result_num
Browse files Browse the repository at this point in the history
  • Loading branch information
fdabrandao committed Aug 28, 2023
1 parent d00fdc0 commit 5c7ca4d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## 0.12.0 - 2023-08-XX
- Upgrade libampl to 2.2.0-20230825.
- Use x-ampl by default if available.
- Add AMPL.solve_result property.
- Add properties AMPL.solve_result/AMPL.solve_result_num.
- Add arguments to AMPL.solve to specify problem name and verbosity.
- Allow passing pandas.Series to AMPL.set_data and Parameter.set_values.
- Add AMPL.snapshot, and implement AMPL.export_model/AMPL.export_data using it.
Expand Down
44 changes: 43 additions & 1 deletion amplpy/ampl.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,10 +839,52 @@ def write(self, filename, auxfiles=""):
def solve_result(self):
"""
Return solve_result value, which indicates if the problem has been solved.
Possible values: "solved", "solved?", "infeasible", "unbounded", "limit", "failure".
+---------+-----------+---------------------------------------------+
| Number | String | Interpretation |
+=========+===========+=============================================+
| 0-99 | solved | Optimal solution found |
+---------+-----------+---------------------------------------------+
| 100-199 | solved? | Optimal solution indicated, but error likely|
+---------+-----------+---------------------------------------------+
| 200-299 | infeasible| Constraints cannot be satisfied |
+---------+-----------+---------------------------------------------+
| 300-399 | unbounded | Objective can be improved without limit |
+---------+-----------+---------------------------------------------+
| 400-499 | limit | Stopped by a limit that you set (such as on |
| | | iterations) |
+---------+-----------+---------------------------------------------+
| 500-599 | failure | Stopped by an error condition in the solver |
| | | routines |
+---------+-----------+---------------------------------------------+
"""
return self.get_value("solve_result")

@property
def solve_result_num(self):
"""
Return solve_result_num value, which indicates if the problem has been solved.
+---------+-----------+---------------------------------------------+
| Number | String | Interpretation |
+=========+===========+=============================================+
| 0-99 | solved | Optimal solution found |
+---------+-----------+---------------------------------------------+
| 100-199 | solved? | Optimal solution indicated, but error likely|
+---------+-----------+---------------------------------------------+
| 200-299 | infeasible| Constraints cannot be satisfied |
+---------+-----------+---------------------------------------------+
| 300-399 | unbounded | Objective can be improved without limit |
+---------+-----------+---------------------------------------------+
| 400-499 | limit | Stopped by a limit that you set (such as on |
| | | iterations) |
+---------+-----------+---------------------------------------------+
| 500-599 | failure | Stopped by an error condition in the solver |
| | | routines |
+---------+-----------+---------------------------------------------+
"""
return self.get_value("solve_result_num")

def _start_recording(self, filename):
"""
Start recording the session to a file for debug purposes.
Expand Down
8 changes: 8 additions & 0 deletions amplpy/tests/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def test_option_prop(self):
ampl.option["solver"] = "gurobi"
self.assertEqual(ampl.option["solver"], ampl.get_option("solver"))

def test_solve_result(self):
ampl = self.ampl
self.assertEqual(ampl.solve_result, "?")
self.assertEqual(ampl.solve_result_num, -1)
ampl.solve()
self.assertEqual(ampl.solve_result, "solved")
self.assertEqual(ampl.solve_result_num, 0)


if __name__ == "__main__":
unittest.main()

0 comments on commit 5c7ca4d

Please sign in to comment.