-
Notifications
You must be signed in to change notification settings - Fork 271
BUG: _VarArray
can't handle MatrixVar
#1044
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
Changes from 23 commits
885865a
9451cc6
7ee3a6f
2cde7a6
50bb995
d72f951
110679f
f181cf4
0346c52
68f2e44
5580e12
8fc900c
d52eb54
8424c6d
710a836
3b64e79
2be1117
7277e55
09e3a0f
c749183
cba76c2
44de7b7
1fd24a9
1a3d2e1
e17f68b
d280764
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -308,7 +308,7 @@ | |||||||||||||||||||||||||||
if rc == SCIP_OKAY: | ||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||
elif rc == SCIP_ERROR: | ||||||||||||||||||||||||||||
raise Exception('SCIP: unspecified error!') | ||||||||||||||||||||||||||||
elif rc == SCIP_NOMEMORY: | ||||||||||||||||||||||||||||
raise MemoryError('SCIP: insufficient memory error!') | ||||||||||||||||||||||||||||
elif rc == SCIP_READERROR: | ||||||||||||||||||||||||||||
|
@@ -2499,21 +2499,19 @@ | |||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
def __cinit__(self, object vars): | ||||||||||||||||||||||||||||
if isinstance(vars, Variable): | ||||||||||||||||||||||||||||
Zeroto521 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
self.size = 1 | ||||||||||||||||||||||||||||
self.ptr = <SCIP_VAR**> malloc(sizeof(SCIP_VAR*)) | ||||||||||||||||||||||||||||
self.ptr[0] = (<Variable>vars).scip_var | ||||||||||||||||||||||||||||
vars = [vars] | ||||||||||||||||||||||||||||
elif isinstance(vars, (list, tuple, MatrixVariable)): | ||||||||||||||||||||||||||||
if (ndim := np.ndim(vars)) != 1: | ||||||||||||||||||||||||||||
raise ValueError(f"Expected a 1D array, but got a {ndim}D array.") | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
if not isinstance(vars, (list, tuple)): | ||||||||||||||||||||||||||||
raise TypeError("Expected Variable or list of Variable, got %s." % type(vars)) | ||||||||||||||||||||||||||||
self.size = len(vars) | ||||||||||||||||||||||||||||
if self.size == 0: | ||||||||||||||||||||||||||||
self.ptr = NULL | ||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||
self.ptr = <SCIP_VAR**> malloc(self.size * sizeof(SCIP_VAR*)) | ||||||||||||||||||||||||||||
for i, var in enumerate(vars): | ||||||||||||||||||||||||||||
if not isinstance(var, Variable): | ||||||||||||||||||||||||||||
raise TypeError("Expected Variable, got %s." % type(var)) | ||||||||||||||||||||||||||||
self.ptr[i] = (<Variable>var).scip_var | ||||||||||||||||||||||||||||
raise TypeError(f"Expected Variable or list of Variable, got {type(vars)}.") | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
self.size = len(vars) | ||||||||||||||||||||||||||||
self.ptr = <SCIP_VAR**> malloc(self.size * sizeof(SCIP_VAR*)) if self.size else NULL | ||||||||||||||||||||||||||||
for i, var in enumerate(vars): | ||||||||||||||||||||||||||||
if not isinstance(var, Variable): | ||||||||||||||||||||||||||||
raise TypeError(f"Expected Variable, got {type(var)}.") | ||||||||||||||||||||||||||||
self.ptr[i] = (<Variable>var).scip_var | ||||||||||||||||||||||||||||
|
self.size = len(vars) | |
self.ptr = <SCIP_VAR**> malloc(self.size * sizeof(SCIP_VAR*)) if self.size else NULL | |
for i, var in enumerate(vars): | |
if not isinstance(var, Variable): | |
raise TypeError(f"Expected Variable, got {type(var)}.") | |
self.ptr[i] = (<Variable>var).scip_var | |
if vars: | |
self.size = len(vars) | |
self.ptr = <SCIP_VAR**> malloc(self.size * sizeof(SCIP_VAR*)) | |
for i, var in enumerate(vars): | |
if not isinstance(var, Variable): | |
raise TypeError(f"Expected Variable, got {type(var)}.") | |
self.ptr[i] = (<Variable>var).scip_var |
Together with https://github.com/scipopt/PySCIPOpt/pull/1044/files#r2281629510.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot that this line has a problem. @DominikKamp
numpy.ndarray
will raise an error if the size is larger than 2.
if vars:
In my local model, it raised an error with the head of the commit building package.
from pyscipopt import Model
model = Model()
vars = model.addMatrixVar(3, vtype="B")
model.addConsKnapsack(vars, [1, 2, 3], 5)
model.setObjective(vars.sum(), "maximize")
model.optimize()
# Traceback (most recent call last):
# line 5, in <module>
# model.addConsKnapsack(vars, [1, 2, 3], 5)
# File "src/pyscipopt/scip.pxi", line 6463, in pyscipopt. scip.Model.addConsKnapsack
# File "src/pyscipopt/scip.pxi", line 2512, in pyscipopt.scip._VarArray.__cinit__
# ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
It should be kept original, in my mind. Since self.size
and self.ptr
are both initiated at the top.
self.size = len(vars)
self.ptr = <SCIP_VAR**> malloc(self.size * sizeof(SCIP_VAR*)) if self.size else NULL
for i, var in enumerate(vars):
if not isinstance(var, Variable):
raise TypeError(f"Expected Variable, got {type(var)}.")
self.ptr[i] = (<Variable>var).scip_var
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So a matrix variable is an ndarray and not a list. I would suggest pulling the initialization of self.size
by len(vars)
down here and update the condition to self.size
. We do not need to enumerate an empty something.
Uh oh!
There was an error while loading. Please reload this page.