coin-or / CyLP

A Python interface to CLP, CBC, and CGL to solve LPs and MIPs.
Other
182 stars 69 forks source link

Inconsistent indexing for initial variable bounds #192

Open emunsing opened 1 year ago

emunsing commented 1 year ago

It appears that the s.variables[i].toInd value is computed differently for the first variable created (i==0) than it is treated for variable created later.

The indexing for the i=0 variable goes from fromInd==0 to toInd==len(var)-1, while for a subsequent variable where the indexing goes from fromInd=j to toind==j + len(var)

Example:

import numpy as np
from cylp.cy import CyClpSimplex

s = CyClpSimplex()
x = s.addVariable('x', 3)
y = s.addVariable('y', 2)
s += 1 <= x <= 2
s += 3 <= y <= 4

print(s.variablesLower[s.variables[0].fromInd: s.variables[0].toInd])
print(s.variablesLower[s.variables[1].fromInd: s.variables[1].toInd])

[Out:] 
[1. 1.]
[3. 3.]

Even more succinctly, this behavior is immediately visible on initializing the initial variable:

s = CyClpSimplex()
x = s.addVariable('x', 3)
print(s.variablesLower[s.variables[0].fromInd: s.variables[0].toInd])

I would expect there to be three entries for the bounds on x.

emunsing commented 1 year ago

A workaround that appears to be robust is to index using the variable dim:

[s.variables[0].fromInd: s.variables[0].fromInd + s.variables[0].dim]