Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve fails on problem with only 1 variable #22

Open
snorfalorpagus opened this issue Dec 17, 2014 · 2 comments
Open

Solve fails on problem with only 1 variable #22

snorfalorpagus opened this issue Dec 17, 2014 · 2 comments

Comments

@snorfalorpagus
Copy link

The following code runs correctly for count = 2, but fails with count = 1.

#!/usr/bin/env python

from cylp.cy import CyClpSimplex
from cylp.py.modeling.CyLPModel import CyLPArray

count = 2

s = CyClpSimplex()
x = s.addVariable('x', count)

s += 0.0 <= x <= 10.0

s.objective = CyLPArray([20]*count) * x
s.optimizationDirection = 'max'

status = s.primal()
result = s.primalVariableSolution['x']

print(status)
print(result)

The error raised:

meh.py:9: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  x = s.addVariable('x', count)
meh.py:12: FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.
  s += 0.0 <= x <= 10.0
Traceback (most recent call last):
  File "meh.py", line 14, in <module>
    s.objective = CyLPArray([20]*count) * x
  File "CyClpSimplex.pyx", line 141, in cylp.cy.CyClpSimplex.CyClpSimplex.objective.__set__ (cylp/cy/CyClpSimplex.cpp:4559)
  File "CyClpSimplex.pyx", line 1192, in cylp.cy.CyClpSimplex.CyClpSimplex.setObjectiveArray (cylp/cy/CyClpSimplex.cpp:17461)
ValueError: Buffer has wrong number of dimensions (expected 1, got 0)

This can be worked around by adding another variable, which can be completely unconstrained and not included in the objective function definition. However, this shouldn't be required.

@dafinguzman
Copy link

I had a similar issue and worked around it by deleting an "np.squeeze" operation in CyLPModel.py, which transforms an np.array([[0]]) into np.array(0).

I guess deleting that operation in the function def objective(...): could make your case work.

@sdementen
Copy link
Contributor

sdementen commented Jan 28, 2022

I found another example that leads to a very similar error message but removing the 2 np.squeeze at the beginning of the CyLPConstraint.perform() did not solve it:

from cylp.cy.CyClpSimplex import CyClpSimplex

s = CyClpSimplex()
x = s.addVariable("x", 1)

s += x >= 1
s.objective = x.sum()
s.primal()
Traceback (most recent call last):
  File "...", line 7, in <module>
    s.objective = x.sum()
  File "cylp/cy/CyClpSimplex.pyx", line 147, in cylp.cy.CyClpSimplex.CyClpSimplex.objective.__set__
  File "cylp/cy/CyClpSimplex.pyx", line 1207, in cylp.cy.CyClpSimplex.CyClpSimplex.setObjectiveArray
ValueError: Buffer has wrong number of dimensions (expected 1, got 0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants