sagemath / sage

Main repository of SageMath. Now open for Issues and Pull Requests.
https://www.sagemath.org
Other
1.19k stars 411 forks source link

Meta-ticket: Use Python optimization interfaces: CVXPY, SCIP, or-tools, PuLP, Pyomo, cylp... #26511

Open mkoeppe opened 5 years ago

mkoeppe commented 5 years ago

The purpose of this ticket is to

SageMath status quo: Front end

SageMath status quo: Back ends

SageMath tickets and tasks

Related:

Key Python software (solver-independent)

Selection criteria for solver-independent interfaces:

cvxpy - Meta-ticket #33920

or-tools - #33493

conv_opt - https://github.com/KarrLab/conv_opt

PuLP - https://github.com/coin-or/pulp

Pyomo

YAPOSIB - Python interface to COIN OSI, using Boost::Python

Python MIP (Mixed-Integer Linear Programming) Tools (new 2018)

PICOS - a user friendly Python API to several conic and integer programming solvers, very much like YALMIP or CVX under MATLAB.

PAO

C, C++ solver abstractions

or-tools (#33493) ​linear_solver wrapper

COIN-OR OSI

Key Python software (solver-dependent)

The Python interfaces that target only one solver are usually careful in not having more restrictive licenses than the targeted solver.

scipy.optimize (vendors HiGHS)

highspy (pybind11-based interface to HiGHS)

Interfaces to GLPK

cylp

cbcpy (LGPL; new August 2019 according to ​https://pypi.org/project/cbcpy/; no activity since then)

Gurobi, CPLEX

python-qsoptex

SCIP, PySCIPOpt (now open source)

Possible integration routes via Julia

CC: a.mason@auckland.ac.nz @jiawei-wang-ucd @yuan-zhou @dimpase @dcoudert @sagetrac-tmonteil @kiwifb

Component: numerical

Issue created by migration from https://trac.sagemath.org/ticket/26511

mkoeppe commented 5 years ago

Description changed:

--- 
+++ 
@@ -1,8 +1,74 @@
-https://github.com/coin-or/pulp
+The purpose of this ticket is to 
+- connect [SageMath](../wiki/SageMath) to interfaces to optimization solvers that are maintained outside of the Sage project,
+- integrate the related developer and user communities.

-It has 
-- some API-based and some file-based interfaces to various solvers (including COIN OSI via yaposib),
-- a basic modeling system (need to compare its capabilities with sage frontend)
+**Status quo in Sage:**
+- Frontend class MixedIntegerLinearProgram
+    - mutable (can call add_constraint, set_integer, new_variable etc. and then re-solve)
+    - solver-independent and solver-specific parameters (solver_parameter)
+    - widely used in Sage code
+    - MIPVariable - indexed by arbitrary objects
+    - some connections to Polyhedron class and to InteractiveLPProblem (didactical code)
+- Backends, using Cython, with varying degrees of implementation quality
+    - GLPK backend - complete, includes support for tableau data and GLPK's exact rational mode
+    - COIN backend - very basic, no support for setting solver parameters such as time limit
+    - CPLEX backend, Gurobi backend, CVXOPT backend, PPL backend
+    - InteractiveLP backend - basic, provide LP only for algebraic LPs
+- Very small developer community

-https://github.com/coin-or/yaposib
+**Tickets:**

+- Template for minimal Python-based backends (interactivelp_backend.pyx is implemented in Cython just because a Cython template was available)
+- Replace basic Cython-based COIN backend by full-featured PuLP backend (gives access to many solvers)
+- #19219 MILP: Add CyLP backend -- Replace basic Cython-based COIN backend by full-featured cylp backend (gives detailed access to CLP, including tableau data)
+- PuLP backend implementation using Sage backends... to make exact backends such as InteractiveLP, PPL backend available to PuLP frontend users 
+   - can PuLP handle non-floating point data (for example for exact computation over rationals or number fields if the backend allows it?
+   - this could be added to PuLP, rather than to Sage.
+- Pyomo backend implementation using Sage backends... to make exact backends available to Pyomo frontend users 
+  - see above
+- Check feasibility of replacing front end MixedIntegerLinearProgram by PuLP frontend or Pyomo frontend
+   - can PuLP handle Sage's number types such as RDF and ZZ?
+   - can Pyomo handle Sage's number types such as RDF and ZZ?
+
+Related:
+- #20302 Meta-ticket: Improvements to MixedIntegerLinearProgram, its backends, and InteractiveLinearProgram
+
+.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  
+
+References:
+
+**Key Python software (solver-independent):**
+
+**PuLP**
+- https://github.com/coin-or/pulp
+- Installation with `sage -pip install pulp` works
+- Frontend: a basic modeling system 
+   - example: https://github.com/coin-or/pulp/blob/master/examples/WhiskasModel1.py
+   - need to compare its capabilities with the sage frontend
+  - Backends: some API-based and some file-based interfaces to various solvers, including:
+   - COIN_CMD (COIN CBC via command line), 
+   - COINMP_DLL (CBC via COINMP C library API), 
+   - YAPOSIB (Python interface to COIN OSI, see below), 
+   - see [solvers.py](https://github.com/coin-or/pulp/blob/master/src/pulp/solvers.py)
+  - is it under active development??
+
+**Pyomo**
+- http://www.pyomo.org/ 
+- installation with `sage -pip install pyomo` works
+- some file-based, some API-based interfaces (direct/persistent) - see [solvers directory](https://github.com/Pyomo/pyomo/tree/master/pyomo/solvers/plugins/solvers)
+- big system, under active development
+
+**YAPOSIB** - Python interface to COIN OSI, using Boost::Python
+- https://github.com/coin-or/yaposib
+- Installation with `sage -i boost && sage -pip install yaposib` works
+- **incompatible open source license** - EPL 1.0
+- therefore we cannot use it as the basis for our solver-independent code
+- could still be useful to be used via PuLP for solvers that don't have a direct PuLP backend
+
+**Key Python software (solver-dependent):**
+
+**cylp**
+- CyLP is a Python interface to COIN-OR’s Linear and mixed-integer program solvers (CLP, CBC, and CGL). CyLP’s unique feature is that you can use it to alter the solution process of the solvers from within Python. For example, you may define cut generators, branch-and-bound strategies, and primal/dual Simplex pivot rules completely in Python.
+- is it maintained?? https://github.com/coin-or/CyLP/issues
+
+
edd8e884-f507-429a-b577-5d554626c0fe commented 5 years ago
comment:2

This was in my plans for sage days84 at Olot, but i did not go further since then. Also, there is Numberjack, which seems pretty general. Looking quickly at the repositories, it seems that neither Numberjack nor PuLP were modified recently (though we could contact the authors to see if they have plans for the future).

dcoudert commented 5 years ago
comment:3

I have the same feeling that PuLP has only minimal maintenance these days. Also, there is a significant effort in the OR community with Julia http://www.juliaopt.org/.

mkoeppe commented 5 years ago
comment:4

Andrew Mason commented by email: PuLP is actively developed. It had a release last week. https://github.com/coin-or/pulp I know the developer Stuart Mitchell if you have any questions.

mkoeppe commented 5 years ago

Description changed:

--- 
+++ 
@@ -50,7 +50,7 @@
    - COINMP_DLL (CBC via COINMP C library API), 
    - YAPOSIB (Python interface to COIN OSI, see below), 
    - see [solvers.py](https://github.com/coin-or/pulp/blob/master/src/pulp/solvers.py)
-  - is it under active development??
+  - **PuLP is currently looking for a co-maintainer** (https://github.com/coin-or/pulp/issues/183)

 **Pyomo**
 - http://www.pyomo.org/ 
mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -32,7 +32,7 @@

 Related:
 - #20302 Meta-ticket: Improvements to MixedIntegerLinearProgram, its backends, and InteractiveLinearProgram
-
+- #28175 Move sage optimization backends to separate Cython packages to remove OptionalExtension problems
 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  

 References:
mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -65,6 +65,9 @@
 - therefore we cannot use it as the basis for our solver-independent code
 - could still be useful to be used via PuLP for solvers that don't have a direct PuLP backend

+**Python MIP (Mixed-Integer Linear Programming) Tools**
+- https://pypi.org/project/mip/
+
 **Key Python software (solver-dependent):**

 **cylp**
mkoeppe commented 4 years ago
comment:7

Added Python-MIP

mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -65,8 +65,10 @@
 - therefore we cannot use it as the basis for our solver-independent code
 - could still be useful to be used via PuLP for solvers that don't have a direct PuLP backend

-**Python MIP (Mixed-Integer Linear Programming) Tools**
+**Python MIP (Mixed-Integer Linear Programming) Tools** (new 2018)
 - https://pypi.org/project/mip/
+- https://github.com/coin-or/python-mip
+- requires Python 3.5 or newer

 **Key Python software (solver-dependent):**
mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -68,7 +68,8 @@
 **Python MIP (Mixed-Integer Linear Programming) Tools** (new 2018)
 - https://pypi.org/project/mip/
 - https://github.com/coin-or/python-mip
-- requires Python 3.5 or newer
+- Eclipse Public License 2.0 (considered **GPL**-incompatible https://www.gnu.org/licenses/license-list.en.html#GPLCompatibleLicenses)
+- **requires Python 3.5 or newer**

 **Key Python software (solver-dependent):**
mkoeppe commented 4 years ago
comment:10

https://pypi.org/project/mip/ looks good. Too bad we can't use it because its license, Eclipse Public License 2.0, is used without a Secondary Licenses Notice that would make it instantly GPL-compatible.

dimpase commented 4 years ago
comment:11

Replying to @mkoeppe:

https://pypi.org/project/mip/ looks good. Too bad we can't use it because its license, Eclipse Public License 2.0, is used without a Secondary Licenses Notice that would make it instantly GPL-compatible.

it's py3 only, by the way.

W.r.t. licenses, maybe we can ask the authors for a fix?

mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -73,8 +73,19 @@

 **Key Python software (solver-dependent):**

+**GLPK** 
+- it looks like ​https://github.com/biosustain/swiglpk/releases is the best maintained. Excluding sage and cvxopt.
+- see https://en.wikibooks.org/wiki/GLPK/Python for a list of other glpk bindings
+
 **cylp**
 - CyLP is a Python interface to COIN-OR’s Linear and mixed-integer program solvers (CLP, CBC, and CGL). CyLP’s unique feature is that you can use it to alter the solution process of the solvers from within Python. For example, you may define cut generators, branch-and-bound strategies, and primal/dual Simplex pivot rules completely in Python.
 - is it maintained?? https://github.com/coin-or/CyLP/issues

+**cbcpy** (new August 2019 according to ​https://pypi.org/project/cbcpy/) 
+- upstream devs for cbc have published their own python interface ​https://git.patrikdufresne.com/pdsl/cbcpy
+- the fact that it uses swig may be an obstacle for immediate inclusion

+**Gurobi**, **CPLEX**
+- they come with their own standard Python APIs, which we could use instead of building our own cython interface 
+- related: #28175 - where we remove these cython modules from sagelib and ship them in separate packages instead
+
mkoeppe commented 4 years ago
comment:12

Added info on cbcpy and glpk from fbissey in #28175.

mkoeppe commented 4 years ago
comment:13

Replying to @dimpase:

Replying to @mkoeppe:

https://pypi.org/project/mip/ looks good. Too bad we can't use it because its license, Eclipse Public License 2.0, is used without a Secondary Licenses Notice that would make it instantly GPL-compatible.

it's py3 only, by the way.

W.r.t. licenses, maybe we can ask the authors for a fix?

Good idea... please go ahead.

mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -18,7 +18,8 @@

 **Tickets:**

-- Template for minimal Python-based backends (interactivelp_backend.pyx is implemented in Cython just because a Cython template was available)
+- #28175 Move sage optimization backends to separate Cython packages to remove `OptionalExtension` problems
+- Template for minimal Python-based backends (`interactivelp_backend.pyx` is implemented in Cython just because a Cython template was available)
 - Replace basic Cython-based COIN backend by full-featured PuLP backend (gives access to many solvers)
 - #19219 MILP: Add CyLP backend -- Replace basic Cython-based COIN backend by full-featured cylp backend (gives detailed access to CLP, including tableau data)
 - PuLP backend implementation using Sage backends... to make exact backends such as InteractiveLP, PPL backend available to PuLP frontend users 
@@ -26,13 +27,12 @@
    - this could be added to PuLP, rather than to Sage.
 - Pyomo backend implementation using Sage backends... to make exact backends available to Pyomo frontend users 
   - see above
-- Check feasibility of replacing front end MixedIntegerLinearProgram by PuLP frontend or Pyomo frontend
+- Check feasibility of replacing front end `MixedIntegerLinearProgram` by PuLP frontend or Pyomo frontend
    - can PuLP handle Sage's number types such as RDF and ZZ?
    - can Pyomo handle Sage's number types such as RDF and ZZ?

 Related:
-- #20302 Meta-ticket: Improvements to MixedIntegerLinearProgram, its backends, and InteractiveLinearProgram
-- #28175 Move sage optimization backends to separate Cython packages to remove OptionalExtension problems
+- #20302 Meta-ticket: Improvements to `MixedIntegerLinearProgram`, its backends, and `InteractiveLinearProgram`
 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  

 References:
mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -71,6 +71,13 @@
 - Eclipse Public License 2.0 (considered **GPL**-incompatible https://www.gnu.org/licenses/license-list.en.html#GPLCompatibleLicenses)
 - **requires Python 3.5 or newer**

+**PICOS** - a user friendly Python API to several conic and integer programming solvers, very much like YALMIP or
+CVX under MATLAB.
+- runs under both Python 2 and Python 3
+- GNU GPL v3
+- https://gitlab.com/picos-api/picos
+- https://pypi.org/project/PICOS/
+
 **Key Python software (solver-dependent):**

 **GLPK** 
mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -96,3 +96,7 @@
 - they come with their own standard Python APIs, which we could use instead of building our own cython interface 
 - related: #28175 - where we remove these cython modules from sagelib and ship them in separate packages instead

+**python-qsoptex**
+- https://github.com/jonls/python-qsoptex (see #18766)
+
+
mkoeppe commented 4 years ago
comment:17

Moving some tickets to 9.2. This is not a promise that I will be working on them.

mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -99,4 +99,12 @@
 **python-qsoptex**
 - https://github.com/jonls/python-qsoptex (see #18766)

+.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  

+**Possible integration routes via Julia**
+
+- `MathOptInterface` - https://arxiv.org/pdf/2002.03447.pdf
+
+
+
+
mkoeppe commented 4 years ago

Description changed:

--- 
+++ 
@@ -3,11 +3,11 @@
 - integrate the related developer and user communities.

 **Status quo in Sage:**
-- Frontend class MixedIntegerLinearProgram
-    - mutable (can call add_constraint, set_integer, new_variable etc. and then re-solve)
+- Frontend class `MixedIntegerLinearProgram`
+    - mutable (can call `add_constraint`, `set_integer`, `new_variable` etc. and then re-solve)
     - solver-independent and solver-specific parameters (solver_parameter)
     - widely used in Sage code
-    - MIPVariable - indexed by arbitrary objects
+    - `MIPVariable` - indexed by arbitrary objects
     - some connections to Polyhedron class and to InteractiveLPProblem (didactical code)
 - Backends, using Cython, with varying degrees of implementation quality
     - GLPK backend - complete, includes support for tableau data and GLPK's exact rational mode
mkoeppe commented 3 years ago

Description changed:

--- 
+++ 
@@ -99,6 +99,13 @@
 **python-qsoptex**
 - https://github.com/jonls/python-qsoptex (see #18766)

+**PySCIPOpt**
+- #21003
+
+**Other relevant software**
+
+- PaPILO: Parallel Presolve for Integer and Linear Optimization (https://github.com/lgottwald/PaPILO) - LGPL license, C++ library; used in SCIP
+
 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  

 **Possible integration routes via Julia**
mkoeppe commented 3 years ago
comment:22

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review of ticket status, priority, and last modification date.

mkoeppe commented 3 years ago

Description changed:

--- 
+++ 
@@ -38,6 +38,8 @@
 References:

 **Key Python software (solver-independent):**
+
+**cvxpy** - #31962

 **PuLP**
 - https://github.com/coin-or/pulp
mkoeppe commented 2 years ago

Description changed:

--- 
+++ 
@@ -38,6 +38,8 @@
 References:

 **Key Python software (solver-independent):**
+
+

 **cvxpy** - #31962

@@ -82,6 +84,10 @@

 **Key Python software (solver-dependent):**

+**scipy.optimize**
+
+- #32282 Add LP solver backends for HiGHS via scipy.optimize.linprog
+
 **GLPK** 
 - it looks like ​https://github.com/biosustain/swiglpk/releases is the best maintained. Excluding sage and cvxopt.
 - see https://en.wikibooks.org/wiki/GLPK/Python for a list of other glpk bindings
mkoeppe commented 2 years ago

Description changed:

--- 
+++ 
@@ -112,7 +112,7 @@

 **Other relevant software**

-- PaPILO: Parallel Presolve for Integer and Linear Optimization (https://github.com/lgottwald/PaPILO) - LGPL license, C++ library; used in SCIP
+- PaPILO: Parallel Presolve for Integer and Linear Optimization (https://github.com/scipopt/papilo/) - LGPL license, C++ library; used in SCIP

 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  
sheerluck commented 2 years ago
comment:27

Pyomo can use SHOT sover https://github.com/coin-or/SHOT

When SHOT is configured to use all cores of cpu, solving traveling salesman problem is pretty fast.

sheerluck commented 2 years ago
comment:28

In Python3.10 they removed use_tracing and I can not build https://github.com/coin-or/CyLP anymore :(

mkoeppe commented 2 years ago
comment:29

Reported upstream?

sheerluck commented 2 years ago
comment:30

https://github.com/coin-or/CyLP/issues/132

sheerluck commented 2 years ago
comment:33

if installing CyLP for CBC is cumbersome, one can use https://github.com/coin-or/python-mip for CBC (https://docs.python-mip.com/en/latest/examples.html)

mkoeppe commented 2 years ago
comment:34

Unfortunately python-mip is encumbered with the same incompatible license as CBC itself. My plan is to target cvxpy as our main solver interface. It's well-maintained and supports lots of solver backends.

tkralphs commented 2 years ago
comment:35

Hi all, I had lost track of this discussion after the IMA workshop and am now re-engaging. @mkoeppe, I looked back at some e-mail correspondence on this topic, but didn't really find a substantive answer there as to why license compatibility is an issue here. I understand that this is something probably prescribed by the Sage organization itself and this may not be the forum for this discussion. If not, I'd be happy to engage somewhere else.

It looks to me like this is a pretty conservative interpretation. It is certainly true that you cannot distribute a single binary that mixes GPL'd and EPL'd code, or a binary that calls a shared library where the two have different licenses (although one might claim it's OK if the main binary is GPL and it's the library that is EPL). But something like python-mip is a self-contained program in the Python programming language and it's not being distributed in binary form, but source form. Yes, when it uses Cbc, it calls a shared library that is under the EPL, but since python-mip itself is also under the EPL, this doesn't create any issue. I can't really see how re-distributing python-mip is a problem, regardless of what it is being re-distributed with. It is well-known that "EPL and GPL are incomatible," but I think this incompatibility is referring to a completely different context than we have here. Can someone say exactly which license is being violated and exactly how the violation is occurring?

PuLP is even more clear cut, since it is in pure Python and only needs a self-contained Cbc binary to work. It is certainly no problem to re-distribute a statically linked Cbc binary in combination with anything. It doesn't link to anything and is itself a completely self-contained program, which can be freely re-distributed. We wouldn't claim PuLP cannot be distributed because it has the capability to use CPLEX. Further, PuLP is itself under a compatible license (MIT?).

Cvxpy may have a compatible license, but it's not useful on its own for solving LPs/MILPs without installing some solver and the open-source solver it uses for solving MILPs is Cbc through CyLP. Effectively, this just puts the onus on the user, then, to install CyLP. This doesn't really seem to be a solution.

In any case, wouldn't the legal liability derive from an owner of a GPL'd code complaining that said code was mixed illegally with non-GPL'd code? I guess that Sage itself is the entity with the GPL'd code and I would think that this could simply be allowed. There is no issue from the EPL side and in any case, the risk of legal action stemming from the re-distribution of COIN-OR code is extremely small.

Why is Sage different than, e.g., anaconda or even SciPy? Anaconda seems to mix all kinds of licenses and as far as I can tell, the onus is on the user to makes sure that the combination of packages installed on their own machine is OK. Anaconda can really be interpreted as an installer of other people's packages. But I didn't research this extensively or dig into whether anaconda actually re-distributes or just installs packages pulled from Pypi and other sources. Another data point would be Linux distros, which obviously do redistribute combinations of self-contained software packages under many different licenses, including both GPL and EPL.

It would be a shame if this license stuff gets in the way here when I think it is clear that all the players involved would approve of what is being done.

mkoeppe commented 2 years ago
comment:36

Hi Ted, thanks for joining the discussion here!

Replying to @tkralphs:

Why is Sage different than, e.g., anaconda [.... or ....] Linux distros, which obviously do redistribute combinations of self-contained software packages under many different licenses, including both GPL and EPL.

Yes, this would be "mere aggregation" https://www.gnu.org/licenses/old-licenses/gpl-2.0-faq.en.html#MereAggregation

mkoeppe commented 2 years ago
comment:37

Replying to @tkralphs:

Why is Sage different than, e.g., anaconda or even SciPy?

No difference to SciPy really. SciPy vendors a lot of packages, but all of them use a permissive license that is compatible with SciPy's own license, BSD 3-clause (https://github.com/scipy/scipy/blob/main/LICENSE.txt). It certainly does not vendor libraries with copyleft licenses such as GPL or EPL.

mkoeppe commented 2 years ago
comment:38

Replying to @tkralphs:

something like python-mip is a self-contained program in the Python programming language and it's not being distributed in binary form, but source form. Yes, when it uses Cbc, it calls a shared library that is under the EPL, but since python-mip itself is also under the EPL, this doesn't create any issue.

It would not be a problem for Sage if we just included python-mip as a package in the Sage distribution, for users to install and use.

But if we were to make substantial use of it in the Sage library, replacing our home-grown (wasn't me!) MIP interface (https://github.com/sagemath/sage-prod/blob/develop/src/sage/numerical/mip.pyx), then this use would definitely rise above "mere aggregation" of software, making the result non-distributable in binary form.

mkoeppe commented 2 years ago
comment:39

Replying to @tkralphs:

Yes, when it uses Cbc, it calls a shared library that is under the EPL, but since python-mip itself is also under the EPL, this doesn't create any issue.

Yes, there is no new problem of using python-mip as an interface to Cbc when one is already using Cbc.

However, as I wrote in our earlier discussion (with Haroldo and Túlio) in July 2021:

For Sage, I am shopping for an API offering pivot-level control over solvers that can support several backends, with the goal of replacing our home-grown (wasn't me) design. Given that Python-MIP already supports CBC and Gurobi, it is a viable candidate for this; if it weren't for the license situation [...]

mkoeppe commented 2 years ago
comment:40

Replying to @tkralphs:

PuLP is even more clear cut, since it is in pure Python and only needs a self-contained Cbc binary to work. It is certainly no problem to re-distribute a statically linked Cbc binary in combination with anything. It doesn't link to anything and is itself a completely self-contained program, which can be freely re-distributed. We wouldn't claim PuLP cannot be distributed because it has the capability to use CPLEX. Further, PuLP is itself under a compatible license (MIT?).

Yes, permissive licenses such as the one that PuLP uses (and the one that python-mip used to use...) do not create such compatibility problems. It is unproblematic to distribute PuLP with Cbc in binary form. (Obviously, you can't redistribute the combination of PuLP with CPLEX.)

It is specifically the switch of python-mip from a permissive license to the incompatible copyleft license (EPL without "secondary license notice") that created the incompatibility with GPL software.

mkoeppe commented 2 years ago
comment:41

Replying to @tkralphs:

Cvxpy may have a compatible license, but it's not useful on its own for solving LPs/MILPs without installing some solver and the open-source solver it uses for solving MILPs is Cbc through CyLP. Effectively, this just puts the onus on the user, then, to install CyLP. This doesn't really seem to be a solution.

That's not a problem for the Sage distribution, we have a working packaging infrastructure that already vendors hundreds of packages - https://repology.org/projects/?inrepo=sagemath_develop

tkralphs commented 2 years ago
comment:42

OK, thanks for clarifying, but it still looks to me like the GPL actually doesn't restrict things in the way you (and many others) are interpreting. Every argument I find that claims Python modules under incompatible licenses cannot be combined in this way takes as given that "importing a module" in Python is equivalent to "linking to a library" in a compiled language and this just isn't true in any sense. "Importing" in Python is functionally the same as "including" a source file in C/C++. You are allowed to combine GPL'd source code with other source code, no matter what the license and re-distribute the combination in source form, just not in binary form.

If you think about the philosophical underpinnings, what the GPL tries to prevent is that I modify the source code of a GPL'd code (or something linked to a GPL'd code) and then re-distribute it only in binary form, thereby profiting from a work that is derived from a GPL'd work, but not sharing the source of that derived work. The viral nature is just meant to try to ensure that everyone plays by these rules to the extent possible by preventing anyone from linking to GPL'd code in binary form without playing by the rules. But unless you distribute only a .pyc file and not a .py, this simply can't happen in Python. Although it would be possible to distribute a Python package in byte-compiled form without source (or even in some obfuscated form of source), this is not what you would be planning to do and it just doesn't run afoul of the GPL, either by the letter of the law or in spirit.

Of course, I am not a lawyer, but in my opinion, you can take python-mip, modify it as you wish, and re-distribute the modified version as an integrated part of Sage without any license issues. Here is an answer on Open Source Stack Exchange that I think is correct and supports this point of view, although everyone else seems to miss the point and disagree.

https://opensource.stackexchange.com/a/8472

mkoeppe commented 2 years ago
comment:43

I'd say this narrow view on what "linking" is amounts to wishful thinking. It's pretty clear from https://www.gnu.org/licenses/old-licenses/gpl-2.0-faq.en.html#TOCMereAggregation that the intention of the license is broader.

mkoeppe commented 2 years ago
comment:44

More importantly, as an author of GPL-protected Python software myself, I would certainly not want this interpretation to be used when a vendor of proprietary software wanted to build their product on top of my software.

sheerluck commented 2 years ago
comment:45

Thanks to both of you for detailed explanation. I was trying to use cvxpy with CBC without CyLP and ended up injecting code from https://github.com/jurasofish/mip_cvxpy/blob/develop/mip_cvxpy/__init__.py inside https://github.com/cvxpy/cvxpy/blob/master/cvxpy/reductions/solvers/conic_solvers/cbc_conif.py

mkoeppe commented 2 years ago
comment:46

I've opened #33487 for adding a package cylp

mkoeppe commented 2 years ago

Description changed:

--- 
+++ 
@@ -93,7 +93,7 @@
 - see https://en.wikibooks.org/wiki/GLPK/Python for a list of other glpk bindings

 **cylp**
-- CyLP is a Python interface to COIN-OR’s Linear and mixed-integer program solvers (CLP, CBC, and CGL). CyLP’s unique feature is that you can use it to alter the solution process of the solvers from within Python. For example, you may define cut generators, branch-and-bound strategies, and primal/dual Simplex pivot rules completely in Python.
+- #33487: CyLP is a Python interface to COIN-OR’s Linear and mixed-integer program solvers (CLP, CBC, and CGL). CyLP’s unique feature is that you can use it to alter the solution process of the solvers from within Python. For example, you may define cut generators, branch-and-bound strategies, and primal/dual Simplex pivot rules completely in Python.
 - is it maintained?? https://github.com/coin-or/CyLP/issues

 **cbcpy** (new August 2019 according to ​https://pypi.org/project/cbcpy/) 
mkoeppe commented 2 years ago
comment:47

A last, minor point on this:

Replying to @tkralphs:

unless you distribute only a .pyc file and not a .py, this simply can't happen in Python. Although it would be possible to distribute a Python package in byte-compiled form without source (or even in some obfuscated form of source)

Even for pure Python packages and even without intentional obfuscation, the Python files installed in site-packages would not be "source code" in the sense of the license, which is the "preferred form of the work for making modifications to it": The build infrastructure (setup.py, pyproject.toml) is not installed.

mkoeppe commented 2 years ago

Description changed:

--- 
+++ 
@@ -42,6 +42,8 @@

 **cvxpy** - #31962
+- use CBC via **cylp**
+- experimental CBC interface via **python-mip** - https://github.com/cvxpy/cvxpy/issues/1265, https://pypi.org/project/mip-cvxpy/

 **PuLP**
 - https://github.com/coin-or/pulp
tkralphs commented 2 years ago
comment:49

Sorry, but I can't help ranting a bit more about the GPL :-).

the intention of the license is broader

Sure, maybe, but that's not how it works. You can't take a vague and (what looks to me to be) unenforceable license and paper over it with FAQs that fill in the gaps ex post facto. The FSF consistently and loudly promotes interpretations of the GPL that seem to me to also be "wishful thinking." Intentions are not really relevant, what matters is how a court would ultimately interpret the license from a legal standpoint and what provisions of it the court would determine are actually enforceable. There are certainly plenty of people who find the FSF's claims about enforceability of certain provisions of the GPL to be pretty questionable. The license text itself is vague and its interpretation by the FSF is pretty over-reaching. It's very difficult to say one way or the other on a wide range of subtle questions around scenarios the license doesn't seem to have been designed to take into account, such as interpreted languages.

A lot of what is explained in various places about how the GPL "should" be interpreted isn't actually in the license and requires a pretty imaginative reading of the actual verbiage. But it is all a matter of opinion and the only opinion that really matters is judge and jury.

Even for pure Python packages and even without intentional obfuscation, the Python files installed in site-packages would not be "source code" in the sense of the license, which is the "preferred form of the work for making modifications to it": The build infrastructure (setup.py, pyproject.toml) is not installed.

This is a very subtle distinction that is one I really doubt would stand up to legal scrutiny. There are many subtleties around this interpretation. For one, does this mean that you can't actually grab part of a package and utilize it in another package in source form, since the partial package would not be in the "preferred form for modification"? This seems to be done quite commonly. If not, what actually defines what constitutes the "full package"? What about a case like COIN-OR, where you can't work with the build system of any of the main source package without what is clearly another completely separate package that aids in the production of the build harness (in this case, the BuildTools)? What if the original authors don't actually provide a build harness, but just distribute the source itself? What if the project does have a setup.py, but can be run without it, by just directly importing it, not installing it in site-packages? Wouldn't this then be the most convenient way of working with the code? In the C/C++ world, what if the source is provided, but without a Makefile or a CMake setup? Or what if both setups are maintained for a given project, but only one is distributed and it's not the one I would personally prefer? Who's to say what the "preferred" way of working with source is, given that this is subjective. This is a crazy mess. To me, it seems clear that the only distinction one can make very clearly is that "source" is human-readable and "object code" is not.

Putting all that aside, a Python wheel certainly can include things like setup.py if desired and those things would then be installed. Doing that would certainly seem to satisfy license requirements. There's nothing stopping you from including the entire contents of a given project's repo in its wheel, it's just not usually done that way.

There are many other aspects of the GPL that are up to interpretation and with respect to which it is anyone's guess what is really allowed. If you look at the parts of the license itself (and not the FAQ) that discuss linking, it only references linking to libraries that are "required." The word "required" seems to be intentionally chosen there. It looks as though if you distribute an optional component along with the whole work, that optional component doesn't itself need to be under the GPL. This makes sense---an optional plug-in cannot really be interpreted as part of a whole that is a derived work of the original. The argument about the enforceability of "linking" rests on the idea that the combination of the two things linked together creates something new that would be considered a derived work. Since python-mip is itself a free-standing program that works without Sage and Sage is a free-standing program that works without python-mip, this really doesn't look like "linking" in that sense. The only place that "linking" is really defined in the license itself, it is specifically mentioned that the linking is to "shared libraries and dynamically linked subprograms," which is clearly referring to code in assembly code form, not a human-readable source form.

Of course, this is all opinion, but it seems clear there is enough grey to cast doubt on almost any one fixed interpretation. Even if you take the conservative view that importing a module in Python is "linking," it would seem easy to get around this by invoking the module as a free-standing program, writing out the data to a file and then reading it back in using a separate interpreter with its own memory. Since we agree that passing data through a file rather than through memory is a legitimate way to keep the programs as separate (ala how PuLP communicates with Cbc), this is a fine workaround.

But then this opens up a whole new subtle distinction between the RAM and the disk. Is the disk not part of the computer's "memory"? It seems like it is, since when a program pages, it is using the disk as virtual memory. What then is considered communicating through "memory"? What about RAM on another processor or attached to another core? Is that "memory"? Where do we draw the line? It's almost impossible to clearly define exactly what the GPL requires in today's complex computing environments. The FSF is splitting hairs in its interpretation. It's hard to believe that they are truly trying to respect a user's "freedom" with all this. It looks to me like they are trying to forcibly impose an extreme world view on everyone else, thereby taking away freedom, to the detriment of the open source movement.

With all that said, the most important point at the end of the day is that using python-mip in the way you were hoping to use it does not violate the EPL. It only (possibly) violates the GPL. I guess the Sage foundation is the copyright holder on the GPL'd code and so could carve out an exception to the linking requirement for specific purposes. This is effectively what the secondary clause of the EPL also does. Since it is the GPL, not the EPL, that is creating the problem, why not just address it on the GPL side. Why not use the LGPL?

More importantly, as an author of GPL-protected Python software myself, I would certainly not want this interpretation to be used when a vendor of proprietary software wanted to build their product on top of my software.

How does the linking provision provide protection? It just means that no one can build on top of your software with non-GPL'd code, not that they can't do it at all. Lots of commercial products are built on GPL'd code. The protection the GPL affords you is that if someone modifies your code, they have to make the modifications open source and they can't hide from users the fact that their project is built on yours. But they can still build a commercial product around it and charge money for it.

mkoeppe commented 2 years ago
comment:50

Replying to @tkralphs:

Since we agree that passing data through a file rather than through memory is a legitimate way to keep the programs as separate (ala how PuLP communicates with Cbc)

No, we don't agree on that.

mkoeppe commented 2 years ago
comment:51

Replying to @tkralphs:

there is enough grey to cast doubt on almost any one fixed interpretation.

I agree with you on that --- which is why I will avoid building software on top of brittle interpretations such as the one that you are trying to give in order to arrive at the desired result.