|
| 1 | +--- |
| 2 | +name: pyoptinterface-expert |
| 3 | +description: Specialized guidance for modeling mathematical optimization problems using the pyoptinterface_native library. Use when building optimization models (LP, QP, MIP, NLP), interfacing with solvers (Gurobi, HiGHS, Ipopt, etc.), and performing matrix-based or nonlinear modeling in Python. |
| 4 | +--- |
| 5 | + |
| 6 | +# PyOptInterface Expert Skill |
| 7 | + |
| 8 | +This skill provides expert guidance for using `pyoptinterface` (POI), a high-performance Python modeling interface for mathematical optimization solvers. |
| 9 | + |
| 10 | +## Core Concepts |
| 11 | + |
| 12 | +POI follows a common modeling pattern: |
| 13 | +1. Create a solver-specific model (e.g., `poi.highs.Model()`). |
| 14 | +2. Add variables using `model.add_variable()` or `model.add_m_variables()`. |
| 15 | +3. Set the objective using `model.set_objective()`. |
| 16 | +4. Add constraints using `model.add_linear_constraint()`, `model.add_quadratic_constraint()`, or `model.add_nl_constraint()`. |
| 17 | +5. Call `model.optimize()` and retrieve results. |
| 18 | + |
| 19 | +## Key Workflows |
| 20 | + |
| 21 | +### 1. Basic Modeling |
| 22 | +```python |
| 23 | +import pyoptinterface as poi |
| 24 | +from pyoptinterface import highs |
| 25 | + |
| 26 | +model = highs.Model() |
| 27 | +x = model.add_variable(lb=0, name="x") |
| 28 | +y = model.add_variable(lb=0, name="y") |
| 29 | + |
| 30 | +model.set_objective(x + 2 * y, poi.ObjectiveSense.Minimize) |
| 31 | +model.add_linear_constraint(x + y >= 10, name="c1") |
| 32 | + |
| 33 | +model.optimize() |
| 34 | +status = model.get_model_attribute(poi.ModelAttribute.TerminationStatus) |
| 35 | +if status == poi.TerminationStatusCode.OPTIMAL: |
| 36 | + print(f"x: {model.get_variable_attribute(x, poi.VariableAttribute.Value)}") |
| 37 | +``` |
| 38 | + |
| 39 | +### 2. Matrix-based Modeling |
| 40 | +Use `add_m_variables` and `add_m_linear_constraints` for performance with NumPy/SciPy. |
| 41 | +```python |
| 42 | +import numpy as np |
| 43 | +x = model.add_m_variables((10,), lb=0) |
| 44 | +A = np.random.rand(5, 10) |
| 45 | +b = np.ones(5) |
| 46 | +model.add_m_linear_constraints(A, x, poi.Leq, b) |
| 47 | +``` |
| 48 | + |
| 49 | +### 3. Nonlinear Modeling |
| 50 | +Nonlinear expressions must be wrapped in `nl.graph()` context. |
| 51 | +```python |
| 52 | +from pyoptinterface import nl |
| 53 | +with nl.graph(): |
| 54 | + model.add_nl_objective(x * x + nl.sin(y)) |
| 55 | + model.add_nl_constraint(x * y >= 5) |
| 56 | +``` |
| 57 | + |
| 58 | +## Reference Documentation |
| 59 | +- API Reference: [references/api.md](references/api.md) |
| 60 | +- Modeling Examples: [references/examples.md](references/examples.md) |
| 61 | + |
| 62 | +## Best Practices |
| 63 | +- Use `poi.quicksum()` for large summations. |
| 64 | +- Check `TerminationStatus` before accessing variable values. |
| 65 | +- For large-scale models, prefer the Matrix API (`add_m_...`). |
0 commit comments