Metadata-Version: 2.4
Name: kos-connectors
Version: 1.0.0b1
Summary: KOS (Katsenia Optimization Solver) for Pyomo, PuLP and CVXPY: LP and MILP, with the same result as KOS directly
Author-email: Katsenia SpA <support@katsenia.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://katsenia.com
Project-URL: Documentation, https://katsenia.com/help.html
Keywords: optimization,linear programming,mixed-integer programming,solver,pyomo,pulp,cvxpy
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: kos-solver>=1.0.0b1
Requires-Dist: numpy
Provides-Extra: pyomo
Requires-Dist: pyomo>=6.9.2; extra == "pyomo"
Provides-Extra: pulp
Requires-Dist: pulp>=3.3.1; extra == "pulp"
Provides-Extra: cvxpy
Requires-Dist: cvxpy>=1.7.1; extra == "cvxpy"
Requires-Dist: scipy; extra == "cvxpy"
Provides-Extra: all
Requires-Dist: pyomo>=6.9.2; extra == "all"
Requires-Dist: pulp>=3.3.1; extra == "all"
Requires-Dist: cvxpy>=1.7.1; extra == "all"
Requires-Dist: scipy; extra == "all"
Dynamic: license-file

# KOS connectors: Pyomo, PuLP and CVXPY

Use KOS (the Katsenia Optimization Solver) from the modeling layer where your model already lives:

| modeling layer | import | how to solve |
|---|---|---|
| [Pyomo](https://www.pyomo.org) (6.9.2 or later) | `import kos_pyomo` | `pyo.SolverFactory("kos").solve(model)` |
| [PuLP](https://coin-or.github.io/pulp/) (3.3.1 or later) | `from kos_pulp import KOS` | `prob.solve(KOS())` |
| [CVXPY](https://www.cvxpy.org) (1.7.1 or later) | `from kos_cvxpy import KOS` | `prob.solve(solver=KOS())` |

The connectors solve LPs and MILPs: a linear objective, linear constraints, and continuous, integer and binary
variables. Anything else (nonlinear or quadratic terms, conic constraints, SOS, logical or disjunctive constraints)
raises an error that says what is not supported.

## Installation

```
pip install --pre "kos-connectors[pyomo]"     # or [pulp], [cvxpy], [all]
```

`kos-connectors` is pure Python, for Python 3.9 or later; it depends on `kos-solver` (the solver, a compiled package
for Linux on x86_64 and aarch64, imported as `kos`) and, through the extras, on the modeling layer. The Community
edition of `kos-solver` on PyPI solves models of up to 2,000 rows and 2,000 columns; a larger model raises
`kos.Error` with the code `size_limit` (see https://katsenia.com/pricing.html); in Pyomo, it ends with
the termination condition `licensingProblems` (Pyomo's condition for a license of the wrong type for the problem),
with the KOS message in `results.extra_info.kos_error`. `kos_connectors.edition()` says which edition is installed.
The limit counts the rows and columns KOS receives: with CVXPY, the rows of its conic form (bounds given as
`cp.Variable(n, bounds=[l, u])` are column bounds; `x >= l` and `x <= u` as constraints are rows).

## The same result as KOS directly

Each connector hands KOS the model exactly as the modeling layer states it: the same columns and rows, in the same
order, with the same coefficients, bounds, objective constant and integrality. The model goes to KOS as exact LP text
(every number written so that it reads back as the same double), so what KOS computes through a connector (the
status, the objective, the values, the duals, the reduced costs, the iterations) is the same, bit for bit, as KOS
directly (`kos.Model.read`) on a file with that model and the same parameters; `solver.kos_model` is that KOS model.
Every connector can write that file (`write_model` / `writeModel`), and our tests compare the two runs bit for bit on
the netlib LPs, the netlib infeasible LPs and a bank of small MILPs.

What the modeling layer shows:

- **Pyomo**: KOS's values, duals, reduced costs and `incumbent_objective`, unchanged.
- **PuLP**: KOS's values (`varValue`), `pi` and `dj`, unchanged; PuLP computes `constraint.slack` from KOS's row
  activity (the right-hand side minus the activity); the objective PuLP shows (`value(prob.objective)`,
  `LpSolveStats.objective`) is PuLP's own evaluation at those values, which can differ from KOS's in the last bits:
  KOS's is `solver.kos_model.objective`.
- **CVXPY**: `problem.solution.opt_val` is KOS's objective of the minimization form (negated by CVXPY when
  maximizing); `problem.value` is CVXPY's own evaluation, and CVXPY may project `x.value` onto the variable bounds (a
  value 1e-13 below a bound of 0 shows as 0.0). KOS's values are in `solver.kos_model.solution`.

What each modeling layer hands its solvers:

- **Pyomo**: the active constraints in their order (as `(lower, body, upper)`, with the constant of the body moved to
  the bounds); the variables they and the objective use, in declaration order; fixed variables as constants.
- **PuLP**: PuLP 4, the variables the problem uses, in creation order; PuLP 3, `LpProblem.variables()`, which PuLP
  sorts by name. The constraints in their order, each `expression + constant <sense> 0` as
  `expression <sense> -constant`. PuLP has no ranged constraints.
- **CVXPY**: its conic form (`minimize c^T x + d` subject to `A x + s = b`, `s` in the zero cone or the nonnegative
  orthant, and the variable bounds), with `d` as the objective constant, so that a MIP gap is relative to the true
  objective. A `>=` constraint reaches KOS as a `<=` row with its signs changed.

## Parameters

Every KOS parameter can be set by name (`kos.parameters()` lists them; `"profile"` sets a KOS profile), after the
standard options of the modeling layer and in the order given:

| | standard options mapped to KOS | KOS parameters |
|---|---|---|
| Pyomo | `time_limit` → `time_limit`, `threads` → `threads`, `rel_gap` → `mip_relative_gap`, `abs_gap` → `mip_absolute_gap` (classic interface: `timelimit`) | `solver_options={...}` (classic interface: `options={...}`) |
| PuLP | `timeLimit` → `time_limit`, `gapRel` → `mip_relative_gap`, `gapAbs` → `mip_absolute_gap`, `threads` → `threads`, `maxNodes` → `node_limit`, `mip=False` solves the LP relaxation | any other keyword argument, and `options=["name=value", ...]` |
| CVXPY | (none) | the constructor, `KOS(method="primal", presolve="on")`, then the keyword arguments of `solve`; `method` only in the constructor (CVXPY's `solve` takes `method` for itself) |

```python
pyo.SolverFactory("kos").solve(m, options={"profile": "stable", "primal_tolerance": 1e-9})
prob.solve(KOS(msg=False, timeLimit=60, gapRel=1e-6, presolve="on"))
prob.solve(solver=KOS(), time_limit=60, mip_relative_gap=1e-6)
```

An unknown name or a value out of range raises `kos.Error` (`unknown_parameter` or `invalid_value`) before solving.
KOS has no MIP start in its public API: PuLP's `warmStart` is ignored with a warning.

## Duals and reduced costs

KOS's duals `y` are the change of the objective per unit increase of the active bound of each row, in the sense of
the model, and its reduced costs are `d = c - A^T y`. Each connector reports them in the convention of its modeling
layer; they exist for an LP with a solution, not for a MILP.

| | duals | reduced costs |
|---|---|---|
| Pyomo (`model.dual`, `model.rc`, `get_duals`, `get_reduced_costs`) | `y`: minimizing, a binding `<=` constraint has a dual `<= 0` and a `>=` one `>= 0`; maximizing, the opposite | `d` (a fixed variable is a constant for KOS: it has no reduced cost, and asking for it raises `ValueError`) |
| PuLP (`constraint.pi`, `variable.dj`) | `y`: minimizing, `<=` gives `pi <= 0`; maximizing, `>= 0` | `d` |
| CVXPY (`constraint.dual_value`) | the multipliers of CVXPY's minimization form, `>= 0` for inequalities: `-y` of the model KOS receives (KOS gets the minimization form, also when maximizing) | (CVXPY has none) |

PuLP's `constraint.slack` is the right-hand side minus the activity.

## Statuses

The KOS status is always available: `results.extra_info.kos_status` (Pyomo), `solver.kos_status` (PuLP),
`problem.solver_stats.extra_stats["kos_status"]` (CVXPY), and `solver.kos_model` is the `kos.Model` of the last solve.

| KOS | Pyomo `TerminationCondition` | PuLP 4 `LpSolveStatus` | CVXPY |
|---|---|---|---|
| `optimal` | `convergenceCriteriaSatisfied` | `Optimal` | `OPTIMAL` |
| `optimal_flagged` | `unknown` (solution status `unknown`; `feasible` in Pyomo 6.9, which has no `unknown`) | `NumericalError` (with a solution) | `OPTIMAL_INACCURATE` |
| `infeasible` | `provenInfeasible` | `Infeasible` | `INFEASIBLE` |
| `dual_infeasible` | `unbounded` with a feasible point, else `infeasibleOrUnbounded` | `Unbounded` with a feasible point, else `Undefined` | `UNBOUNDED` with a feasible point, else `INFEASIBLE_OR_UNBOUNDED` |
| `unbounded` | `unbounded` | `Unbounded` | `UNBOUNDED` |
| `infeasible_or_unbounded` | `infeasibleOrUnbounded` | `Undefined` | `INFEASIBLE_OR_UNBOUNDED` |
| `iteration_limit` | `iterationLimit` | `IterationLimit` | `USER_LIMIT` or error |
| `time_limit` | `maxTimeLimit` | `TimeLimit` | `USER_LIMIT` or error |
| `objective_limit` | `objectiveLimit` | `GapLimit` | `USER_LIMIT` or error |
| `work_limit` | `maxTimeLimit` | `TimeLimit` | `USER_LIMIT` or error |
| `node_limit` | `iterationLimit` | `NodeLimit` | `USER_LIMIT` or error |
| `solution_limit` | `unknown` | `SolutionLimit` | `USER_LIMIT` or error |
| `target_reached` | `objectiveLimit` | `GapLimit` | `USER_LIMIT` or error |
| `gap_after_nodes` | `iterationLimit` (solution status `feasible`) | `GapLimit` | `USER_LIMIT` or error |
| `interrupted` | `interrupted` | `Interrupted` | `USER_LIMIT` or error |
| `no_verdict` | `error` | `NumericalError` | error |

Only `optimal` is `convergenceCriteriaSatisfied` in Pyomo: every limit, also the gap target by node count
(`gap_after_nodes`), is a limit, and `pyo.check_optimal_termination` is false for them. `dual_infeasible` means that
the LP has no finite optimum: it is unbounded if it has a feasible point, and it may also be infeasible; the connectors
report it as unbounded only when KOS's certificate of the solve has a feasible point (a ray and a point within the
primal tolerance). To know it, the connector writes that certificate after the solve, to a temporary folder that it
removes: extra work, bounded by the KOS parameter `certificate_work_limit`, that `time_limit` does not count and
Ctrl-C does not stop. In CVXPY, "`USER_LIMIT` or error" is `USER_LIMIT` when there is a solution (a MILP incumbent) and
`cvxpy.error.SolverError`, with the KOS status and its meaning in the message, when there is none. With PuLP 3, the
statuses are PuLP 3's `LpStatus` and `sol_status`: a limit with a solution is (`Optimal`, `IntegerFeasible`), PuLP 3's
convention, without one (`Not Solved`, `No Solution Found`); an LP optimum that KOS flags is (`Undefined`,
`IntegerFeasible`), and `dual_infeasible` without a feasible point is (`Undefined`, `No Solution Found`).

Pyomo follows the rules of its solver interfaces: by default a status other than `convergenceCriteriaSatisfied`
raises `NoOptimalSolutionError`, and `load_solutions=True` without a solution raises `NoFeasibleSolutionError`
(`raise_exception_on_nonoptimal_result=False` and `load_solutions=False` return the results instead). With no active
objective, `incumbent_objective` is `None`.

## Stopping a solve

Ctrl-C during a solve stops KOS cleanly: the result has the status `interrupted`, with the best MILP solution found so
far, if any (a second Ctrl-C does not wait for KOS). From another thread, `solver.interrupt()` does the same.

## License

Apache License 2.0 (see `LICENSE` and `NOTICE`). Copyright 2026 Katsenia SpA. KOS itself (the `kos-solver` package)
is licensed separately: see https://katsenia.com/license.html.
