optlens 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. optlens-0.0.1/LICENSE +201 -0
  2. optlens-0.0.1/NOTICE +12 -0
  3. optlens-0.0.1/PKG-INFO +232 -0
  4. optlens-0.0.1/README.md +191 -0
  5. optlens-0.0.1/pyproject.toml +57 -0
  6. optlens-0.0.1/setup.cfg +4 -0
  7. optlens-0.0.1/src/optlens/__init__.py +16 -0
  8. optlens-0.0.1/src/optlens/backends.py +734 -0
  9. optlens-0.0.1/src/optlens/checks.py +92 -0
  10. optlens-0.0.1/src/optlens/closest.py +52 -0
  11. optlens-0.0.1/src/optlens/context.py +175 -0
  12. optlens-0.0.1/src/optlens/diagnose.py +546 -0
  13. optlens-0.0.1/src/optlens/loaders.py +360 -0
  14. optlens-0.0.1/src/optlens/lpfile.py +342 -0
  15. optlens-0.0.1/src/optlens/mcp_server.py +392 -0
  16. optlens-0.0.1/src/optlens/model.py +345 -0
  17. optlens-0.0.1/src/optlens/outliers.py +437 -0
  18. optlens-0.0.1/src/optlens/prompts.py +112 -0
  19. optlens-0.0.1/src/optlens/py.typed +0 -0
  20. optlens-0.0.1/src/optlens/sensitivity.py +161 -0
  21. optlens-0.0.1/src/optlens/session.py +1857 -0
  22. optlens-0.0.1/src/optlens/structure.py +47 -0
  23. optlens-0.0.1/src/optlens/workspace.py +408 -0
  24. optlens-0.0.1/src/optlens.egg-info/PKG-INFO +232 -0
  25. optlens-0.0.1/src/optlens.egg-info/SOURCES.txt +55 -0
  26. optlens-0.0.1/src/optlens.egg-info/dependency_links.txt +1 -0
  27. optlens-0.0.1/src/optlens.egg-info/entry_points.txt +2 -0
  28. optlens-0.0.1/src/optlens.egg-info/requires.txt +21 -0
  29. optlens-0.0.1/src/optlens.egg-info/top_level.txt +1 -0
  30. optlens-0.0.1/tests/test_agent_tools.py +106 -0
  31. optlens-0.0.1/tests/test_checks.py +67 -0
  32. optlens-0.0.1/tests/test_closest.py +70 -0
  33. optlens-0.0.1/tests/test_context.py +92 -0
  34. optlens-0.0.1/tests/test_crossed_bounds.py +35 -0
  35. optlens-0.0.1/tests/test_fix_menu.py +72 -0
  36. optlens-0.0.1/tests/test_flag_fixes.py +57 -0
  37. optlens-0.0.1/tests/test_gurobi_unusable.py +146 -0
  38. optlens-0.0.1/tests/test_import_rule.py +29 -0
  39. optlens-0.0.1/tests/test_licensed_solver.py +96 -0
  40. optlens-0.0.1/tests/test_mcp_server.py +214 -0
  41. optlens-0.0.1/tests/test_model_data.py +50 -0
  42. optlens-0.0.1/tests/test_multi_model.py +70 -0
  43. optlens-0.0.1/tests/test_outliers.py +175 -0
  44. optlens-0.0.1/tests/test_plan_language.py +157 -0
  45. optlens-0.0.1/tests/test_plugin.py +55 -0
  46. optlens-0.0.1/tests/test_proof.py +63 -0
  47. optlens-0.0.1/tests/test_quadratic.py +68 -0
  48. optlens-0.0.1/tests/test_ranging.py +43 -0
  49. optlens-0.0.1/tests/test_relax_integral.py +43 -0
  50. optlens-0.0.1/tests/test_repair_changes.py +167 -0
  51. optlens-0.0.1/tests/test_resume.py +44 -0
  52. optlens-0.0.1/tests/test_scale_answers.py +65 -0
  53. optlens-0.0.1/tests/test_solver_routing.py +140 -0
  54. optlens-0.0.1/tests/test_starting_plan.py +103 -0
  55. optlens-0.0.1/tests/test_versions.py +174 -0
  56. optlens-0.0.1/tests/test_worker_pool.py +59 -0
  57. optlens-0.0.1/tests/test_workspace.py +222 -0
optlens-0.0.1/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
optlens-0.0.1/NOTICE ADDED
@@ -0,0 +1,12 @@
1
+ optlens
2
+ Copyright 2026 Jessica Dong
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (LICENSE).
5
+
6
+ Test models derived from Gurobi modeling-examples
7
+ (https://github.com/Gurobi/modeling-examples), Copyright Gurobi Optimization, LLC, licensed under the Apache
8
+ License, Version 2.0:
9
+ tests/fixtures/milp_tutorial__0_RAP.mps the resource assignment problem of the milp_tutorial
10
+ example, written as an MPS file
11
+ tests/fixtures/ex_milp_tutorial__rhs_tighten__0.mps the same model with one constraint limit tightened so
12
+ that it is infeasible (changed for this project)
optlens-0.0.1/PKG-INFO ADDED
@@ -0,0 +1,232 @@
1
+ Metadata-Version: 2.4
2
+ Name: optlens
3
+ Version: 0.0.1
4
+ Summary: Solver-agnostic diagnostics for LP/MILP models: IIS, feasibility relaxation, repair menus, sensitivity, what-if.
5
+ Author: Jessica Dong
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://optlens.dev
8
+ Project-URL: Repository, https://github.com/jjd-lab/optlens
9
+ Project-URL: Issues, https://github.com/jjd-lab/optlens/issues
10
+ Project-URL: Changelog, https://github.com/jjd-lab/optlens/blob/main/CHANGELOG.md
11
+ Keywords: optimization,linear programming,MILP,infeasibility,IIS,debugging,MCP,Claude Code
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
19
+ Classifier: Typing :: Typed
20
+ Requires-Python: >=3.12
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ License-File: NOTICE
24
+ Requires-Dist: numpy
25
+ Requires-Dist: scipy
26
+ Requires-Dist: highspy
27
+ Provides-Extra: scip
28
+ Requires-Dist: pyscipopt; extra == "scip"
29
+ Provides-Extra: gurobi
30
+ Requires-Dist: gurobipy; extra == "gurobi"
31
+ Provides-Extra: pulp
32
+ Requires-Dist: pulp; extra == "pulp"
33
+ Provides-Extra: pyomo
34
+ Requires-Dist: pyomo>=6.7; extra == "pyomo"
35
+ Requires-Dist: packaging; extra == "pyomo"
36
+ Provides-Extra: mcp
37
+ Requires-Dist: mcp>=2.2; extra == "mcp"
38
+ Requires-Dist: anyio; extra == "mcp"
39
+ Requires-Dist: jsonschema; extra == "mcp"
40
+ Dynamic: license-file
41
+
42
+ # optlens
43
+
44
+ A solver-agnostic debugger and explainer for LP and MILP models. Give it a model that is infeasible, or that solves
45
+ to an answer you don't trust, and it finds the conflicting constraints, the smallest changes that fix them, and what
46
+ each change costs. Every result comes back as text an agent or a person can read: in your own Python, in Claude Code
47
+ through the plugin, or in any MCP client.
48
+
49
+ optlens works on a model that is already built. It does not write models.
50
+
51
+ ## Install
52
+
53
+ Python 3.12 or later:
54
+
55
+ ```bash
56
+ pip install "optlens[scip,mcp]" # core: numpy, scipy, HiGHS
57
+ ```
58
+
59
+ To run the quickstart below or the tests, clone the repository and install it from the clone
60
+ (`git clone https://github.com/jjd-lab/optlens && cd optlens && pip install ".[scip,mcp]"`). On a network that cannot
61
+ reach GitHub (a proxy, a VPN), copy the source over another way (a zip of the repository) and install from that folder:
62
+ `pip install "<folder>[scip,mcp]"`, and for the Claude Code plugin `claude plugin marketplace add <folder>`.
63
+
64
+ | extra | adds |
65
+ |---|---|
66
+ | `scip` | SCIP (pyscipopt): a second open-source solver, with a native MIP IIS |
67
+ | `gurobi` | gurobipy: Gurobi, bring your own license. Its version must match your Gurobi: a Compute Server or token server rejects a newer client ("No compatible runtime available"), so install that major version, e.g. `pip install "gurobipy==12.*"`. `open_model` shows the installed gurobipy version |
68
+ | `pyomo`, `pulp` | load Pyomo and PuLP models |
69
+ | `mcp` | the MCP server `optlens-mcp` and the Claude Code plugin |
70
+
71
+ ## Quickstart
72
+
73
+ ```python
74
+ import optlens as od
75
+ from optlens.session import Session, Version
76
+
77
+ if __name__ == "__main__": # solves run in worker processes, which start by re-importing this script
78
+ md = od.load("tests/fixtures/ex_milp_tutorial__rhs_tighten__0.mps") # LP or MPS
79
+ s = Session({"v0": Version(md, None, "original model")})
80
+ print(s.get_model_overview()) # size, status (here INFEASIBLE), constraint families
81
+ print(s.compute_iis()) # the conflicting constraints, grouped by family
82
+ print(s.fix_menu()) # the smallest change per family that restores feasibility
83
+ print(s.modify_and_resolve(changes=[{"action": "set_rhs", "name": "resource[Monika]", "upper": 1}]))
84
+ ```
85
+
86
+ Save it as a file and run it from the repository root. Keep the `if __name__ == "__main__":` guard in any script that
87
+ solves: solves run in worker processes, and on Windows (which starts them with `spawn`) each one re-imports the script,
88
+ so without the guard the script re-runs in every solver process. Every `Session` method returns text, and `optlens.session.TOOLS` holds the matching JSON schemas for an
89
+ agent. A session also covers feasibility relaxation, what-if edits as versions, why-not questions, sensitivity and
90
+ marginal values, suspicious data values, and comparisons between versions or models.
91
+
92
+ A model built in Pyomo, gurobipy or PuLP loads directly, from the object or from the file that builds it. The file
93
+ is run without its `if __name__ == "__main__":` block and stops at its first solve call (`optimize()`, `solve()`),
94
+ whose model is the one read; nothing is solved, so a large gurobipy model loads on Gurobi's size-limited license.
95
+
96
+ ```python
97
+ md = od.from_object(model) # a Pyomo ConcreteModel, gurobipy Model or PuLP LpProblem
98
+ md = od.load("my_model.py") # the model it solves, else its one model or build_model()
99
+ md = od.load("my_model.py:make_scenario") # a named model, or a function with no arguments that returns one
100
+ ```
101
+
102
+ LP files written by Gurobi (bracketed names, which HiGHS rejects) load without gurobipy.
103
+
104
+ Quadratic objectives (QP, MIQP) are supported: a convex QP solves on HiGHS, and a mixed-integer or non-convex one goes
105
+ to SCIP automatically. Every question works as for a linear model except sensitivity ranges (and shadow prices for a
106
+ non-convex objective). Quadratic constraints, indicator and other general constraints, and SOS
107
+ are rejected when the model loads. The full list of questions, formats and solvers: [optlens.dev/ask](https://optlens.dev/ask/).
108
+
109
+ ## Solvers
110
+
111
+ HiGHS comes with the core; SCIP and Gurobi are extras. Pick one with `OPTLENS_SOLVER` (`highs`, `scip`, `gurobi`, or
112
+ `auto`, the default), `open_model`'s `solver` in the MCP server, or `Session(..., prefer=...)`. Under `auto` without
113
+ Gurobi, each model goes to HiGHS or SCIP (a larger MIP's first solve races both).
114
+
115
+ - **Every solve has a hard time limit.** Solvers do not always honour their own (SCIP once ran 862 s on a 30 s
116
+ limit), so each solve runs in a worker process that is stopped at the limit.
117
+ - **Gurobi does every step itself.** A Gurobi session solves, computes IIS, relaxes, ranges and checks on Gurobi; no
118
+ step is handed to another solver. If you chose Gurobi and it cannot run a model (not installed, or over the
119
+ size-limited license), the tool stops and says so instead of switching. Under `auto` it uses Gurobi when it can
120
+ run here (checked once with a one-variable solve: an installed gurobipy with no usable license, or a version your
121
+ license server rejects, is left out, and `open_model` says why) and falls back to HiGHS or SCIP for a model Gurobi
122
+ cannot run, and says so.
123
+ - **HiGHS and SCIP stand in for each other** where only one can do a step (HiGHS has no MIP IIS; SCIP does), or
124
+ when one gives no verdict within its limit (the other tries once), and the result names the solver that did it.
125
+ - **Every IIS is checked.** A solver's IIS whose constraints are feasible on their own is rejected and rebuilt by
126
+ removing constraints one at a time on the same solver. This guards against a known gurobipy 13.0.3 bug: `computeIIS` leaves out a
127
+ one-variable row on a binary whose fractional limit rounds it to 0 (`160 open <= 100` with `open >= 1` returns
128
+ `open >= 1` alone, which is feasible).
129
+ - **Gurobi is tested on small models and on one large one** (a 279k-row hotel model: solves, sensitivity, IIS,
130
+ relaxations and repair menus). Other large models are untested; reports are welcome.
131
+
132
+ On large models (hundreds of thousands of constraints) the IIS search starts near the conflict, from HiGHS's infeasibility
133
+ proof, instead of searching the whole model.
134
+
135
+ ## Use it with your agent
136
+
137
+ Install as above (it puts `optlens-mcp` on your PATH), then connect once; the agent gets the 22 tools and the method
138
+ (open the model first, lead with the cause, every number from a solve, re-solve before recommending a fix) with them.
139
+
140
+ | agent | connect |
141
+ |---|---|
142
+ | **Claude Code** | `claude plugin marketplace add jjd-lab/optlens` then `claude plugin install optlens@optlens` (or the same as `/plugin` commands in a session) |
143
+ | **Codex CLI** | `codex mcp add optlens -- optlens-mcp` |
144
+ | **Cursor** | in `.cursor/mcp.json` (or `~/.cursor/mcp.json`): `{"mcpServers": {"optlens": {"command": "optlens-mcp"}}}` |
145
+ | **VS Code (Copilot)** | in `.vscode/mcp.json`: `{"servers": {"optlens": {"type": "stdio", "command": "optlens-mcp"}}}` |
146
+ | **Claude Desktop** | in `claude_desktop_config.json` (Settings > Developer > Edit Config): `{"mcpServers": {"optlens": {"command": "optlens-mcp"}}}` |
147
+ | **Any other MCP client** | run `optlens-mcp` as a stdio server |
148
+
149
+ The command must be on the PATH the agent sees; give the full path to `optlens-mcp` (for example `.venv/bin/optlens-mcp`)
150
+ if it is not. `OPTLENS_SOLVER` (`highs`, `scip`, `gurobi` or `auto`) chooses the solver once.
151
+
152
+ **Time limits.** Solves stop at 45 s, so a call answers within a minute. `OPTLENS_CALL_LIMIT` (seconds, at least 30)
153
+ is the most one tool call may take: a tool's `time_limit` may ask for up to 15 s less, and the agent asks for more only
154
+ when a solve stopped at its limit while still improving (results say whether it was). Set it to what your client waits
155
+ for one tool call; `open_model` states the limits in force, and a result cut short by one says so.
156
+
157
+ | client | waits for a tool call | `OPTLENS_CALL_LIMIT` |
158
+ |---|---|---|
159
+ | Claude Code (plugin) | 10 minutes, set by the plugin (calls past 2 minutes move to the background) | 110, set by the plugin |
160
+ | Claude Code (`claude mcp add`) | `MCP_TOOL_TIMEOUT` (about 28 hours unless set; some environments set 60 s) | 60 unless set; to allow more, add `"timeout": 600000` to the server's entry and set 110 |
161
+ | Claude Desktop, MCP TypeScript SDK clients | 60 s | 60 (the default) |
162
+ | other clients | see the client's settings | 60 unless the client allows more |
163
+
164
+ Large MIPs may not finish within any of these; results then report the plan found, its bound and its gap.
165
+
166
+ Then ask: *"Why is `plan.mps` infeasible, and what fixes it?"* The agent opens the model, writes its context once
167
+ (what each constraint and variable family means, as JSON in `.optlens/context/` that you can review and commit), and
168
+ works through the tools. For several steps or many solves it uses `run_python`, a persistent Python process with the
169
+ engine preloaded as `session` and the model loaded once.
170
+
171
+ The Claude Code plugin is the same server plus a skill that carries the same method ([plugin/README.md](https://github.com/jjd-lab/optlens/blob/main/plugin/README.md)).
172
+
173
+ **Security.** optlens runs code on your machine, with your permissions, and has no sandbox: `run_python` executes
174
+ the code the agent writes (its process starts without your API keys and tokens, but can read and write whatever your
175
+ user can; in Claude Code you approve each call), and opening a `.py` model runs that file up to its first solve call.
176
+ Open only models and code you trust. To report a vulnerability, see [SECURITY.md](https://github.com/jjd-lab/optlens/blob/main/SECURITY.md).
177
+
178
+ ## Try it
179
+
180
+ To try the plugin without touching your own Claude Code setup, build a clean one in a folder of its own: its own venv
181
+ (optlens from GitHub), its own Claude config with only the optlens plugin, and a hotel week whose data load went wrong
182
+ ([packs/hotel/examples/try_week](https://github.com/jjd-lab/optlens/blob/main/packs/hotel/examples/try_week/README.md)),
183
+ with five questions to ask:
184
+
185
+ ```bash
186
+ git clone https://github.com/jjd-lab/optlens && optlens/scripts/sandbox.sh ~/optlens-try
187
+ ~/optlens-try/start.sh # log in on the first start; the questions are in ~/optlens-try/HOW-TO.md
188
+ ```
189
+
190
+ It needs bash, Python 3.12+ and Claude Code (macOS or Linux; on Windows, WSL, or the PowerShell steps in
191
+ [plugin/README.md](https://github.com/jjd-lab/optlens/blob/main/plugin/README.md#windows)). `--project PATH` puts your own model
192
+ there instead. It is a separate setup, not a security sandbox (see Security
193
+ above); delete the folder to remove it.
194
+
195
+ The same week in a clean Claude Code with the plugin (rendered from a recorded session; each tool call shows its real
196
+ duration):
197
+
198
+ ![Claude Code with the optlens plugin finds why the week's hotel plan is infeasible, checks the fix, and ranks the business rules by the revenue they cost](https://raw.githubusercontent.com/jjd-lab/optlens/main/docs/plugin-agent-demo.svg)
199
+
200
+ ## Hotel pack
201
+
202
+ [packs/hotel/](https://github.com/jjd-lab/optlens/blob/main/packs/hotel/README.md) is a synthetic hotel revenue-management model (room type × night × length of stay
203
+ × booking window × rate tier) with its generator, its document, domain notes and helpers. It builds models from a
204
+ few thousand to 837,000 constraints, for trying optlens on something realistic and large.
205
+
206
+ ## Tests
207
+
208
+ ```bash
209
+ python -m unittest discover -s tests -t . # the engine, from the repo root
210
+ cd packs/hotel && python -m unittest discover -s tests -t . # the hotel pack
211
+ ```
212
+
213
+ ## Contact
214
+
215
+ Questions, feedback on your own models, or access to **optchat**, the chat agent built on optlens for business users
216
+ (available on request): **hello@optlens.dev**. Bugs and feature requests: GitHub issues.
217
+
218
+ A planner's session with optchat on the hotel pack's model (a 14-night plan, after a data load typed one night's group
219
+ target as 1,200 instead of 120). Each answer ends with the engine calls, time and cost it took; waiting time is cut
220
+ to two seconds:
221
+
222
+ ![A planner asks why the week's plan is infeasible, approves the fix, and asks which rule costs the most revenue](https://raw.githubusercontent.com/jjd-lab/optlens/main/docs/planner-agent-demo.svg)
223
+
224
+ ## Contributing
225
+
226
+ Issues with your own models are the most useful contribution; see
227
+ [CONTRIBUTING.md](https://github.com/jjd-lab/optlens/blob/main/CONTRIBUTING.md) and the
228
+ [code of conduct](https://github.com/jjd-lab/optlens/blob/main/CODE_OF_CONDUCT.md).
229
+
230
+ ## License
231
+
232
+ Apache-2.0 ([LICENSE](https://github.com/jjd-lab/optlens/blob/main/LICENSE)); third-party credits in [NOTICE](https://github.com/jjd-lab/optlens/blob/main/NOTICE).
@@ -0,0 +1,191 @@
1
+ # optlens
2
+
3
+ A solver-agnostic debugger and explainer for LP and MILP models. Give it a model that is infeasible, or that solves
4
+ to an answer you don't trust, and it finds the conflicting constraints, the smallest changes that fix them, and what
5
+ each change costs. Every result comes back as text an agent or a person can read: in your own Python, in Claude Code
6
+ through the plugin, or in any MCP client.
7
+
8
+ optlens works on a model that is already built. It does not write models.
9
+
10
+ ## Install
11
+
12
+ Python 3.12 or later:
13
+
14
+ ```bash
15
+ pip install "optlens[scip,mcp]" # core: numpy, scipy, HiGHS
16
+ ```
17
+
18
+ To run the quickstart below or the tests, clone the repository and install it from the clone
19
+ (`git clone https://github.com/jjd-lab/optlens && cd optlens && pip install ".[scip,mcp]"`). On a network that cannot
20
+ reach GitHub (a proxy, a VPN), copy the source over another way (a zip of the repository) and install from that folder:
21
+ `pip install "<folder>[scip,mcp]"`, and for the Claude Code plugin `claude plugin marketplace add <folder>`.
22
+
23
+ | extra | adds |
24
+ |---|---|
25
+ | `scip` | SCIP (pyscipopt): a second open-source solver, with a native MIP IIS |
26
+ | `gurobi` | gurobipy: Gurobi, bring your own license. Its version must match your Gurobi: a Compute Server or token server rejects a newer client ("No compatible runtime available"), so install that major version, e.g. `pip install "gurobipy==12.*"`. `open_model` shows the installed gurobipy version |
27
+ | `pyomo`, `pulp` | load Pyomo and PuLP models |
28
+ | `mcp` | the MCP server `optlens-mcp` and the Claude Code plugin |
29
+
30
+ ## Quickstart
31
+
32
+ ```python
33
+ import optlens as od
34
+ from optlens.session import Session, Version
35
+
36
+ if __name__ == "__main__": # solves run in worker processes, which start by re-importing this script
37
+ md = od.load("tests/fixtures/ex_milp_tutorial__rhs_tighten__0.mps") # LP or MPS
38
+ s = Session({"v0": Version(md, None, "original model")})
39
+ print(s.get_model_overview()) # size, status (here INFEASIBLE), constraint families
40
+ print(s.compute_iis()) # the conflicting constraints, grouped by family
41
+ print(s.fix_menu()) # the smallest change per family that restores feasibility
42
+ print(s.modify_and_resolve(changes=[{"action": "set_rhs", "name": "resource[Monika]", "upper": 1}]))
43
+ ```
44
+
45
+ Save it as a file and run it from the repository root. Keep the `if __name__ == "__main__":` guard in any script that
46
+ solves: solves run in worker processes, and on Windows (which starts them with `spawn`) each one re-imports the script,
47
+ so without the guard the script re-runs in every solver process. Every `Session` method returns text, and `optlens.session.TOOLS` holds the matching JSON schemas for an
48
+ agent. A session also covers feasibility relaxation, what-if edits as versions, why-not questions, sensitivity and
49
+ marginal values, suspicious data values, and comparisons between versions or models.
50
+
51
+ A model built in Pyomo, gurobipy or PuLP loads directly, from the object or from the file that builds it. The file
52
+ is run without its `if __name__ == "__main__":` block and stops at its first solve call (`optimize()`, `solve()`),
53
+ whose model is the one read; nothing is solved, so a large gurobipy model loads on Gurobi's size-limited license.
54
+
55
+ ```python
56
+ md = od.from_object(model) # a Pyomo ConcreteModel, gurobipy Model or PuLP LpProblem
57
+ md = od.load("my_model.py") # the model it solves, else its one model or build_model()
58
+ md = od.load("my_model.py:make_scenario") # a named model, or a function with no arguments that returns one
59
+ ```
60
+
61
+ LP files written by Gurobi (bracketed names, which HiGHS rejects) load without gurobipy.
62
+
63
+ Quadratic objectives (QP, MIQP) are supported: a convex QP solves on HiGHS, and a mixed-integer or non-convex one goes
64
+ to SCIP automatically. Every question works as for a linear model except sensitivity ranges (and shadow prices for a
65
+ non-convex objective). Quadratic constraints, indicator and other general constraints, and SOS
66
+ are rejected when the model loads. The full list of questions, formats and solvers: [optlens.dev/ask](https://optlens.dev/ask/).
67
+
68
+ ## Solvers
69
+
70
+ HiGHS comes with the core; SCIP and Gurobi are extras. Pick one with `OPTLENS_SOLVER` (`highs`, `scip`, `gurobi`, or
71
+ `auto`, the default), `open_model`'s `solver` in the MCP server, or `Session(..., prefer=...)`. Under `auto` without
72
+ Gurobi, each model goes to HiGHS or SCIP (a larger MIP's first solve races both).
73
+
74
+ - **Every solve has a hard time limit.** Solvers do not always honour their own (SCIP once ran 862 s on a 30 s
75
+ limit), so each solve runs in a worker process that is stopped at the limit.
76
+ - **Gurobi does every step itself.** A Gurobi session solves, computes IIS, relaxes, ranges and checks on Gurobi; no
77
+ step is handed to another solver. If you chose Gurobi and it cannot run a model (not installed, or over the
78
+ size-limited license), the tool stops and says so instead of switching. Under `auto` it uses Gurobi when it can
79
+ run here (checked once with a one-variable solve: an installed gurobipy with no usable license, or a version your
80
+ license server rejects, is left out, and `open_model` says why) and falls back to HiGHS or SCIP for a model Gurobi
81
+ cannot run, and says so.
82
+ - **HiGHS and SCIP stand in for each other** where only one can do a step (HiGHS has no MIP IIS; SCIP does), or
83
+ when one gives no verdict within its limit (the other tries once), and the result names the solver that did it.
84
+ - **Every IIS is checked.** A solver's IIS whose constraints are feasible on their own is rejected and rebuilt by
85
+ removing constraints one at a time on the same solver. This guards against a known gurobipy 13.0.3 bug: `computeIIS` leaves out a
86
+ one-variable row on a binary whose fractional limit rounds it to 0 (`160 open <= 100` with `open >= 1` returns
87
+ `open >= 1` alone, which is feasible).
88
+ - **Gurobi is tested on small models and on one large one** (a 279k-row hotel model: solves, sensitivity, IIS,
89
+ relaxations and repair menus). Other large models are untested; reports are welcome.
90
+
91
+ On large models (hundreds of thousands of constraints) the IIS search starts near the conflict, from HiGHS's infeasibility
92
+ proof, instead of searching the whole model.
93
+
94
+ ## Use it with your agent
95
+
96
+ Install as above (it puts `optlens-mcp` on your PATH), then connect once; the agent gets the 22 tools and the method
97
+ (open the model first, lead with the cause, every number from a solve, re-solve before recommending a fix) with them.
98
+
99
+ | agent | connect |
100
+ |---|---|
101
+ | **Claude Code** | `claude plugin marketplace add jjd-lab/optlens` then `claude plugin install optlens@optlens` (or the same as `/plugin` commands in a session) |
102
+ | **Codex CLI** | `codex mcp add optlens -- optlens-mcp` |
103
+ | **Cursor** | in `.cursor/mcp.json` (or `~/.cursor/mcp.json`): `{"mcpServers": {"optlens": {"command": "optlens-mcp"}}}` |
104
+ | **VS Code (Copilot)** | in `.vscode/mcp.json`: `{"servers": {"optlens": {"type": "stdio", "command": "optlens-mcp"}}}` |
105
+ | **Claude Desktop** | in `claude_desktop_config.json` (Settings > Developer > Edit Config): `{"mcpServers": {"optlens": {"command": "optlens-mcp"}}}` |
106
+ | **Any other MCP client** | run `optlens-mcp` as a stdio server |
107
+
108
+ The command must be on the PATH the agent sees; give the full path to `optlens-mcp` (for example `.venv/bin/optlens-mcp`)
109
+ if it is not. `OPTLENS_SOLVER` (`highs`, `scip`, `gurobi` or `auto`) chooses the solver once.
110
+
111
+ **Time limits.** Solves stop at 45 s, so a call answers within a minute. `OPTLENS_CALL_LIMIT` (seconds, at least 30)
112
+ is the most one tool call may take: a tool's `time_limit` may ask for up to 15 s less, and the agent asks for more only
113
+ when a solve stopped at its limit while still improving (results say whether it was). Set it to what your client waits
114
+ for one tool call; `open_model` states the limits in force, and a result cut short by one says so.
115
+
116
+ | client | waits for a tool call | `OPTLENS_CALL_LIMIT` |
117
+ |---|---|---|
118
+ | Claude Code (plugin) | 10 minutes, set by the plugin (calls past 2 minutes move to the background) | 110, set by the plugin |
119
+ | Claude Code (`claude mcp add`) | `MCP_TOOL_TIMEOUT` (about 28 hours unless set; some environments set 60 s) | 60 unless set; to allow more, add `"timeout": 600000` to the server's entry and set 110 |
120
+ | Claude Desktop, MCP TypeScript SDK clients | 60 s | 60 (the default) |
121
+ | other clients | see the client's settings | 60 unless the client allows more |
122
+
123
+ Large MIPs may not finish within any of these; results then report the plan found, its bound and its gap.
124
+
125
+ Then ask: *"Why is `plan.mps` infeasible, and what fixes it?"* The agent opens the model, writes its context once
126
+ (what each constraint and variable family means, as JSON in `.optlens/context/` that you can review and commit), and
127
+ works through the tools. For several steps or many solves it uses `run_python`, a persistent Python process with the
128
+ engine preloaded as `session` and the model loaded once.
129
+
130
+ The Claude Code plugin is the same server plus a skill that carries the same method ([plugin/README.md](https://github.com/jjd-lab/optlens/blob/main/plugin/README.md)).
131
+
132
+ **Security.** optlens runs code on your machine, with your permissions, and has no sandbox: `run_python` executes
133
+ the code the agent writes (its process starts without your API keys and tokens, but can read and write whatever your
134
+ user can; in Claude Code you approve each call), and opening a `.py` model runs that file up to its first solve call.
135
+ Open only models and code you trust. To report a vulnerability, see [SECURITY.md](https://github.com/jjd-lab/optlens/blob/main/SECURITY.md).
136
+
137
+ ## Try it
138
+
139
+ To try the plugin without touching your own Claude Code setup, build a clean one in a folder of its own: its own venv
140
+ (optlens from GitHub), its own Claude config with only the optlens plugin, and a hotel week whose data load went wrong
141
+ ([packs/hotel/examples/try_week](https://github.com/jjd-lab/optlens/blob/main/packs/hotel/examples/try_week/README.md)),
142
+ with five questions to ask:
143
+
144
+ ```bash
145
+ git clone https://github.com/jjd-lab/optlens && optlens/scripts/sandbox.sh ~/optlens-try
146
+ ~/optlens-try/start.sh # log in on the first start; the questions are in ~/optlens-try/HOW-TO.md
147
+ ```
148
+
149
+ It needs bash, Python 3.12+ and Claude Code (macOS or Linux; on Windows, WSL, or the PowerShell steps in
150
+ [plugin/README.md](https://github.com/jjd-lab/optlens/blob/main/plugin/README.md#windows)). `--project PATH` puts your own model
151
+ there instead. It is a separate setup, not a security sandbox (see Security
152
+ above); delete the folder to remove it.
153
+
154
+ The same week in a clean Claude Code with the plugin (rendered from a recorded session; each tool call shows its real
155
+ duration):
156
+
157
+ ![Claude Code with the optlens plugin finds why the week's hotel plan is infeasible, checks the fix, and ranks the business rules by the revenue they cost](https://raw.githubusercontent.com/jjd-lab/optlens/main/docs/plugin-agent-demo.svg)
158
+
159
+ ## Hotel pack
160
+
161
+ [packs/hotel/](https://github.com/jjd-lab/optlens/blob/main/packs/hotel/README.md) is a synthetic hotel revenue-management model (room type × night × length of stay
162
+ × booking window × rate tier) with its generator, its document, domain notes and helpers. It builds models from a
163
+ few thousand to 837,000 constraints, for trying optlens on something realistic and large.
164
+
165
+ ## Tests
166
+
167
+ ```bash
168
+ python -m unittest discover -s tests -t . # the engine, from the repo root
169
+ cd packs/hotel && python -m unittest discover -s tests -t . # the hotel pack
170
+ ```
171
+
172
+ ## Contact
173
+
174
+ Questions, feedback on your own models, or access to **optchat**, the chat agent built on optlens for business users
175
+ (available on request): **hello@optlens.dev**. Bugs and feature requests: GitHub issues.
176
+
177
+ A planner's session with optchat on the hotel pack's model (a 14-night plan, after a data load typed one night's group
178
+ target as 1,200 instead of 120). Each answer ends with the engine calls, time and cost it took; waiting time is cut
179
+ to two seconds:
180
+
181
+ ![A planner asks why the week's plan is infeasible, approves the fix, and asks which rule costs the most revenue](https://raw.githubusercontent.com/jjd-lab/optlens/main/docs/planner-agent-demo.svg)
182
+
183
+ ## Contributing
184
+
185
+ Issues with your own models are the most useful contribution; see
186
+ [CONTRIBUTING.md](https://github.com/jjd-lab/optlens/blob/main/CONTRIBUTING.md) and the
187
+ [code of conduct](https://github.com/jjd-lab/optlens/blob/main/CODE_OF_CONDUCT.md).
188
+
189
+ ## License
190
+
191
+ Apache-2.0 ([LICENSE](https://github.com/jjd-lab/optlens/blob/main/LICENSE)); third-party credits in [NOTICE](https://github.com/jjd-lab/optlens/blob/main/NOTICE).