Skip to content

Commit 0b7ee70

Browse files
committed
Revert 188b3ef "Remove Variable.create"
1 parent 2b0fa85 commit 0b7ee70

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

src/pyscipopt/propagator.pxi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ cdef SCIP_RETCODE PyPropResProp (SCIP* scip, SCIP_PROP* prop, SCIP_VAR* infervar
150150
SCIP_BOUNDTYPE boundtype, SCIP_BDCHGIDX* bdchgidx, SCIP_Real relaxedbd, SCIP_RESULT* result) noexcept with gil:
151151
cdef SCIP_PROPDATA* propdata
152152
propdata = SCIPpropGetData(prop)
153-
confvar = Variable(infervar)
153+
confvar = Variable.create(infervar)
154154

155155
#TODO: parse bdchgidx?
156156

src/pyscipopt/reader.pxi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ cdef SCIP_RETCODE PyReaderWrite (SCIP* scip, SCIP_READER* reader, FILE* file,
5151

5252
PyFile = os.fdopen(fd, "w", closefd=False)
5353
PyName = name.decode('utf-8')
54-
PyBinVars = [Variable(vars[i]) for i in range(nbinvars)]
55-
PyIntVars = [Variable(vars[i]) for i in range(nbinvars, nintvars)]
56-
PyImplVars = [Variable(vars[i]) for i in range(nintvars, nimplvars)]
57-
PyContVars = [Variable(vars[i]) for i in range(nimplvars, ncontvars)]
58-
PyFixedVars = [Variable(fixedvars[i]) for i in range(nfixedvars)]
54+
PyBinVars = [Variable.create(vars[i]) for i in range(nbinvars)]
55+
PyIntVars = [Variable.create(vars[i]) for i in range(nbinvars, nintvars)]
56+
PyImplVars = [Variable.create(vars[i]) for i in range(nintvars, nimplvars)]
57+
PyContVars = [Variable.create(vars[i]) for i in range(nimplvars, ncontvars)]
58+
PyFixedVars = [Variable.create(fixedvars[i]) for i in range(nfixedvars)]
5959
PyConss = [Constraint.create(conss[i]) for i in range(nconss)]
6060
PyReader = <Reader>readerdata
6161
result_dict = PyReader.readerwrite(PyFile, PyName, transformed, objsense, objscale, objoffset,

src/pyscipopt/scip.pxd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,9 @@ cdef class Variable:
20922092
# can be used to store problem data
20932093
cdef public object data
20942094

2095+
@staticmethod
2096+
cdef create(SCIP_VAR* scipvar)
2097+
20952098
cdef class Constraint:
20962099
cdef SCIP_CONS* scip_cons
20972100
# can be used to store problem data

src/pyscipopt/scip.pxi

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ cdef class Event:
440440
441441
"""
442442
cdef SCIP_VAR* var = SCIPeventGetVar(self.event)
443-
return Variable(var)
443+
return Variable.create(var)
444444

445445
def getNode(self):
446446
"""
@@ -561,7 +561,7 @@ cdef class Column:
561561
562562
"""
563563
cdef SCIP_VAR* var = SCIPcolGetVar(self.scip_col)
564-
return Variable(var)
564+
return Variable.create(var)
565565

566566
def getPrimsol(self):
567567
"""
@@ -964,7 +964,7 @@ cdef class NLRow:
964964
cdef SCIP_Real* lincoefs = SCIPnlrowGetLinearCoefs(self.scip_nlrow)
965965
cdef int nlinvars = SCIPnlrowGetNLinearVars(self.scip_nlrow)
966966
cdef int i
967-
return [(Variable(linvars[i]), lincoefs[i]) for i in range(nlinvars)]
967+
return [(Variable.create(linvars[i]), lincoefs[i]) for i in range(nlinvars)]
968968

969969
def getLhs(self):
970970
"""
@@ -1151,7 +1151,7 @@ cdef class BoundChange:
11511151
Variable
11521152
11531153
"""
1154-
return Variable(SCIPboundchgGetVar(self.scip_boundchg))
1154+
return Variable.create(SCIPboundchgGetVar(self.scip_boundchg))
11551155

11561156
def getBoundchgtype(self):
11571157
"""
@@ -1419,7 +1419,7 @@ cdef class Node:
14191419
SCIPnodeGetParentBranchings(self.scip_node, branchvars, branchbounds,
14201420
boundtypes, &nbranchvars, nbranchvars)
14211421

1422-
py_variables = [Variable(branchvars[i]) for i in range(nbranchvars)]
1422+
py_variables = [Variable.create(branchvars[i]) for i in range(nbranchvars)]
14231423
py_branchbounds = [branchbounds[i] for i in range(nbranchvars)]
14241424
py_boundtypes = [boundtypes[i] for i in range(nbranchvars)]
14251425
free(boundtypes)
@@ -1467,8 +1467,29 @@ cdef class Node:
14671467

14681468

14691469
cdef class Variable:
1470-
def __cinit__(self, SCIP_VAR* scip_var):
1471-
self.scip_var = scip_var
1470+
1471+
@staticmethod
1472+
cdef create(SCIP_VAR* scip_var):
1473+
"""
1474+
Main method for creating a Variable class. Is used instead of __init__.
1475+
1476+
Parameters
1477+
----------
1478+
scip_var : SCIP_VAR*
1479+
A pointer to the SCIP_VAR
1480+
1481+
Returns
1482+
-------
1483+
var : Variable
1484+
The Python representative of the SCIP_VAR
1485+
1486+
"""
1487+
if scip_var == NULL:
1488+
raise Warning("cannot create Variable with SCIP_VAR* == NULL")
1489+
1490+
var = Variable()
1491+
var.scip_var = scip_var
1492+
return var
14721493

14731494
@property
14741495
def name(self):
@@ -4031,7 +4052,7 @@ cdef class Model:
40314052
else:
40324053
PY_SCIP_CALL(SCIPaddVar(self._scip, scip_var))
40334054

4034-
pyVar = Variable(scip_var)
4055+
pyVar = Variable.create(scip_var)
40354056

40364057
# store variable in the model to avoid creating new python variable objects in getVars()
40374058
assert not pyVar.ptr() in self._modelvars
@@ -4166,7 +4187,7 @@ cdef class Model:
41664187
cdef SCIP_VAR* _tvar
41674188
PY_SCIP_CALL(SCIPgetTransformedVar(self._scip, var.scip_var, &_tvar))
41684189

4169-
return Variable(_tvar)
4190+
return Variable.create(_tvar)
41704191

41714192
def addVarLocks(self, Variable var, int nlocksdown, int nlocksup):
41724193
"""
@@ -4529,7 +4550,7 @@ cdef class Model:
45294550
vars.append(self._modelvars[ptr])
45304551
else:
45314552
# create a new variable
4532-
var = Variable(_vars[i])
4553+
var = Variable.create(_vars[i])
45334554
assert var.ptr() == ptr
45344555
self._modelvars[ptr] = var
45354556
vars.append(var)
@@ -6254,7 +6275,7 @@ cdef class Model:
62546275
vars.append(self._modelvars[ptr])
62556276
else:
62566277
# create a new variable
6257-
var = Variable(_vars[i])
6278+
var = Variable.create(_vars[i])
62586279
assert var.ptr() == ptr
62596280
self._modelvars[ptr] = var
62606281
vars.append(var)
@@ -6343,7 +6364,7 @@ cdef class Model:
63436364
vars.append(self._modelvars[ptr])
63446365
else:
63456366
# create a new variable
6346-
var = Variable(_vars[i])
6367+
var = Variable.create(_vars[i])
63476368
assert var.ptr() == ptr
63486369
self._modelvars[ptr] = var
63496370
vars.append(var)
@@ -6373,7 +6394,7 @@ cdef class Model:
63736394
# check whether the corresponding variable exists already
63746395
if ptr not in self._modelvars:
63756396
# create a new variable
6376-
resultant = Variable(_resultant)
6397+
resultant = Variable.create(_resultant)
63776398
assert resultant.ptr() == ptr
63786399
self._modelvars[ptr] = resultant
63796400
else:
@@ -7309,7 +7330,7 @@ cdef class Model:
73097330
73107331
"""
73117332
cdef SCIP_VAR* var = SCIPgetSlackVarIndicator(cons.scip_cons)
7312-
return Variable(var)
7333+
return Variable.create(var)
73137334

73147335
def addPyCons(self, Constraint cons):
73157336
"""
@@ -7932,15 +7953,15 @@ cdef class Model:
79327953
quadterms = []
79337954

79347955
for termidx in range(nlinvars):
7935-
var = Variable(SCIPgetVarExprVar(linexprs[termidx]))
7956+
var = Variable.create(SCIPgetVarExprVar(linexprs[termidx]))
79367957
linterms.append((var, lincoefs[termidx]))
79377958

79387959
for termidx in range(nbilinterms):
79397960
SCIPexprGetQuadraticBilinTerm(expr, termidx, &bilinterm1, &bilinterm2, &bilincoef, NULL, NULL)
79407961
scipvar1 = SCIPgetVarExprVar(bilinterm1)
79417962
scipvar2 = SCIPgetVarExprVar(bilinterm2)
7942-
var1 = Variable(scipvar1)
7943-
var2 = Variable(scipvar2)
7963+
var1 = Variable.create(scipvar1)
7964+
var2 = Variable.create(scipvar2)
79447965
if scipvar1 != scipvar2:
79457966
bilinterms.append((var1,var2,bilincoef))
79467967
else:
@@ -7950,7 +7971,7 @@ cdef class Model:
79507971
SCIPexprGetQuadraticQuadTerm(expr, termidx, NULL, &lincoef, &sqrcoef, NULL, NULL, &sqrexpr)
79517972
if sqrexpr == NULL:
79527973
continue
7953-
var = Variable(SCIPgetVarExprVar(sqrexpr))
7974+
var = Variable.create(SCIPgetVarExprVar(sqrexpr))
79547975
quadterms.append((var,sqrcoef,lincoef))
79557976

79567977
return (bilinterms, quadterms, linterms)
@@ -8643,7 +8664,7 @@ cdef class Model:
86438664
if _mappedvar == NULL:
86448665
mappedvar = None
86458666
else:
8646-
mappedvar = Variable(_mappedvar)
8667+
mappedvar = Variable.create(_mappedvar)
86478668

86488669
return mappedvar
86498670

@@ -8672,7 +8693,7 @@ cdef class Model:
86728693
_benders = benders._benders
86738694

86748695
_auxvar = SCIPbendersGetAuxiliaryVar(_benders, probnumber)
8675-
auxvar = Variable(_auxvar)
8696+
auxvar = Variable.create(_auxvar)
86768697

86778698
return auxvar
86788699

@@ -9398,7 +9419,7 @@ cdef class Model:
93989419
PY_SCIP_CALL(SCIPgetLPBranchCands(self._scip, &lpcands, &lpcandssol, &lpcandsfrac,
93999420
&nlpcands, &npriolpcands, &nfracimplvars))
94009421

9401-
return ([Variable(lpcands[i]) for i in range(nlpcands)], [lpcandssol[i] for i in range(nlpcands)],
9422+
return ([Variable.create(lpcands[i]) for i in range(nlpcands)], [lpcandssol[i] for i in range(nlpcands)],
94029423
[lpcandsfrac[i] for i in range(nlpcands)], nlpcands, npriolpcands, nfracimplvars)
94039424

94049425
def getNLPBranchCands(self):
@@ -9435,7 +9456,7 @@ cdef class Model:
94359456

94369457
PY_SCIP_CALL(SCIPgetPseudoBranchCands(self._scip, &pseudocands, &npseudocands, &npriopseudocands))
94379458

9438-
return ([Variable(pseudocands[i]) for i in range(npseudocands)], npseudocands, npriopseudocands)
9459+
return ([Variable.create(pseudocands[i]) for i in range(npseudocands)], npseudocands, npriopseudocands)
94399460

94409461
def branchVar(self, Variable variable):
94419462
"""

0 commit comments

Comments
 (0)