Skip to content

Commit c98a000

Browse files
committed
Drop Variable.create via __cinit__
Replaces the static Variable.create(SCIP_VAR*) method with direct instantiation using the Variable constructor and casting pointers to size_t. Removes the create() method from the Variable class definition and updates all usages accordingly for consistency and clarity.
1 parent 50d9d68 commit c98a000

4 files changed

Lines changed: 29 additions & 52 deletions

File tree

src/pyscipopt/propagator.pxi

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

153153
#TODO: parse bdchgidx?
154154

src/pyscipopt/reader.pxi

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

5353
PyFile = os.fdopen(fd, "w", closefd=False)
5454
PyName = name.decode('utf-8')
55-
PyBinVars = [Variable.create(vars[i]) for i in range(nbinvars)]
56-
PyIntVars = [Variable.create(vars[i]) for i in range(nbinvars, nintvars)]
57-
PyImplVars = [Variable.create(vars[i]) for i in range(nintvars, nimplvars)]
58-
PyContVars = [Variable.create(vars[i]) for i in range(nimplvars, ncontvars)]
59-
PyFixedVars = [Variable.create(fixedvars[i]) for i in range(nfixedvars)]
55+
PyBinVars = [Variable(<size_t>vars[i]) for i in range(nbinvars)]
56+
PyIntVars = [Variable(<size_t>vars[i]) for i in range(nbinvars, nintvars)]
57+
PyImplVars = [Variable(<size_t>vars[i]) for i in range(nintvars, nimplvars)]
58+
PyContVars = [Variable(<size_t>vars[i]) for i in range(nimplvars, ncontvars)]
59+
PyFixedVars = [Variable(<size_t>fixedvars[i]) for i in range(nfixedvars)]
6060
PyConss = [Constraint.create(conss[i]) for i in range(nconss)]
6161
PyReader = <Reader>readerdata
6262
#TODO: provide rational objoffsetexact and objscaleexact

src/pyscipopt/scip.pxd

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

2191-
@staticmethod
2192-
cdef create(SCIP_VAR* scipvar)
2193-
21942191
cdef class Constraint:
21952192
cdef SCIP_CONS* scip_cons
21962193
# can be used to store problem data

src/pyscipopt/scip.pxi

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ cdef class Event:
445445
Variable
446446
447447
"""
448-
cdef SCIP_VAR* var = SCIPeventGetVar(self.event)
449-
return Variable.create(var)
448+
return Variable(<size_t>SCIPeventGetVar(self.event))
450449

451450
def getNode(self):
452451
"""
@@ -567,7 +566,7 @@ cdef class Column:
567566
568567
"""
569568
cdef SCIP_VAR* var = SCIPcolGetVar(self.scip_col)
570-
return Variable.create(var)
569+
return Variable(<size_t>var)
571570

572571
def getPrimsol(self):
573572
"""
@@ -1020,7 +1019,7 @@ cdef class NLRow:
10201019
cdef SCIP_Real* lincoefs = SCIPnlrowGetLinearCoefs(self.scip_nlrow)
10211020
cdef int nlinvars = SCIPnlrowGetNLinearVars(self.scip_nlrow)
10221021
cdef int i
1023-
return [(Variable.create(linvars[i]), lincoefs[i]) for i in range(nlinvars)]
1022+
return [(Variable(<size_t>linvars[i]), lincoefs[i]) for i in range(nlinvars)]
10241023

10251024
def getLhs(self):
10261025
"""
@@ -1221,7 +1220,7 @@ cdef class BoundChange:
12211220
Variable
12221221
12231222
"""
1224-
return Variable.create(SCIPboundchgGetVar(self.scip_boundchg))
1223+
return Variable(<size_t>SCIPboundchgGetVar(self.scip_boundchg))
12251224

12261225
def getBoundchgtype(self):
12271226
"""
@@ -1489,7 +1488,7 @@ cdef class Node:
14891488
SCIPnodeGetParentBranchings(self.scip_node, branchvars, branchbounds,
14901489
boundtypes, &nbranchvars, nbranchvars)
14911490

1492-
py_variables = [Variable.create(branchvars[i]) for i in range(nbranchvars)]
1491+
py_variables = [Variable(<size_t>branchvars[i]) for i in range(nbranchvars)]
14931492
py_branchbounds = [branchbounds[i] for i in range(nbranchvars)]
14941493
py_boundtypes = [boundtypes[i] for i in range(nbranchvars)]
14951494
free(boundtypes)
@@ -1538,28 +1537,8 @@ cdef class Node:
15381537

15391538
cdef class Variable:
15401539

1541-
@staticmethod
1542-
cdef create(SCIP_VAR* scip_var):
1543-
"""
1544-
Main method for creating a Variable class. Is used instead of __init__.
1545-
1546-
Parameters
1547-
----------
1548-
scip_var : SCIP_VAR*
1549-
A pointer to the SCIP_VAR
1550-
1551-
Returns
1552-
-------
1553-
var : Variable
1554-
The Python representative of the SCIP_VAR
1555-
1556-
"""
1557-
if scip_var == NULL:
1558-
raise Warning("cannot create Variable with SCIP_VAR* == NULL")
1559-
1560-
var = Variable()
1561-
var.scip_var = scip_var
1562-
return var
1540+
def __cinit__(self, size_t scip_var):
1541+
self.scip_var = <SCIP_VAR*>scip_var
15631542

15641543
@property
15651544
def name(self):
@@ -2034,6 +2013,7 @@ cdef class Variable:
20342013
"""
20352014
return SCIPvarGetNBranchingsCurrentRun(self.scip_var, branchdir)
20362015

2016+
20372017
class MatrixVariable(MatrixExpr):
20382018

20392019
def vtype(self):
@@ -4292,7 +4272,7 @@ cdef class Model:
42924272
else:
42934273
PY_SCIP_CALL(SCIPaddVar(self._scip, scip_var))
42944274

4295-
pyVar = Variable.create(scip_var)
4275+
pyVar = Variable(<size_t>scip_var)
42964276

42974277
# store variable in the model to avoid creating new python variable objects in getVars()
42984278
assert not pyVar.ptr() in self._modelvars
@@ -4428,7 +4408,7 @@ cdef class Model:
44284408
cdef SCIP_VAR* _tvar
44294409
PY_SCIP_CALL(SCIPgetTransformedVar(self._scip, var.scip_var, &_tvar))
44304410

4431-
return Variable.create(_tvar)
4411+
return Variable(<size_t>_tvar)
44324412

44334413
def addVarLocks(self, Variable var, int nlocksdown, int nlocksup):
44344414
"""
@@ -4791,7 +4771,7 @@ cdef class Model:
47914771
vars.append(self._modelvars[ptr])
47924772
else:
47934773
# create a new variable
4794-
var = Variable.create(_vars[i])
4774+
var = Variable(<size_t>_vars[i])
47954775
assert var.ptr() == ptr
47964776
self._modelvars[ptr] = var
47974777
vars.append(var)
@@ -6542,7 +6522,7 @@ cdef class Model:
65426522
vars.append(self._modelvars[ptr])
65436523
else:
65446524
# create a new variable
6545-
var = Variable.create(_vars[i])
6525+
var = Variable(<size_t>_vars[i])
65466526
assert var.ptr() == ptr
65476527
self._modelvars[ptr] = var
65486528
vars.append(var)
@@ -6631,7 +6611,7 @@ cdef class Model:
66316611
vars.append(self._modelvars[ptr])
66326612
else:
66336613
# create a new variable
6634-
var = Variable.create(_vars[i])
6614+
var = Variable(<size_t>_vars[i])
66356615
assert var.ptr() == ptr
66366616
self._modelvars[ptr] = var
66376617
vars.append(var)
@@ -6661,7 +6641,7 @@ cdef class Model:
66616641
# check whether the corresponding variable exists already
66626642
if ptr not in self._modelvars:
66636643
# create a new variable
6664-
resultant = Variable.create(_resultant)
6644+
resultant = Variable(<size_t>_resultant)
66656645
assert resultant.ptr() == ptr
66666646
self._modelvars[ptr] = resultant
66676647
else:
@@ -7565,7 +7545,7 @@ cdef class Model:
75657545
75667546
"""
75677547
cdef SCIP_VAR* var = SCIPgetSlackVarIndicator(cons.scip_cons)
7568-
return Variable.create(var)
7548+
return Variable(<size_t>var)
75697549

75707550
def addPyCons(self, Constraint cons):
75717551
"""
@@ -8188,15 +8168,15 @@ cdef class Model:
81888168
quadterms = []
81898169

81908170
for termidx in range(nlinvars):
8191-
var = Variable.create(SCIPgetVarExprVar(linexprs[termidx]))
8171+
var = Variable(<size_t>SCIPgetVarExprVar(linexprs[termidx]))
81928172
linterms.append((var, lincoefs[termidx]))
81938173

81948174
for termidx in range(nbilinterms):
81958175
SCIPexprGetQuadraticBilinTerm(expr, termidx, &bilinterm1, &bilinterm2, &bilincoef, NULL, NULL)
81968176
scipvar1 = SCIPgetVarExprVar(bilinterm1)
81978177
scipvar2 = SCIPgetVarExprVar(bilinterm2)
8198-
var1 = Variable.create(scipvar1)
8199-
var2 = Variable.create(scipvar2)
8178+
var1 = Variable(<size_t>scipvar1)
8179+
var2 = Variable(<size_t>scipvar2)
82008180
if scipvar1 != scipvar2:
82018181
bilinterms.append((var1,var2,bilincoef))
82028182
else:
@@ -8206,7 +8186,7 @@ cdef class Model:
82068186
SCIPexprGetQuadraticQuadTerm(expr, termidx, NULL, &lincoef, &sqrcoef, NULL, NULL, &sqrexpr)
82078187
if sqrexpr == NULL:
82088188
continue
8209-
var = Variable.create(SCIPgetVarExprVar(sqrexpr))
8189+
var = Variable(<size_t>SCIPgetVarExprVar(sqrexpr))
82108190
quadterms.append((var,sqrcoef,lincoef))
82118191

82128192
return (bilinterms, quadterms, linterms)
@@ -8899,7 +8879,7 @@ cdef class Model:
88998879
if _mappedvar == NULL:
89008880
mappedvar = None
89018881
else:
8902-
mappedvar = Variable.create(_mappedvar)
8882+
mappedvar = Variable(<size_t>_mappedvar)
89038883

89048884
return mappedvar
89058885

@@ -8928,7 +8908,7 @@ cdef class Model:
89288908
_benders = benders._benders
89298909

89308910
_auxvar = SCIPbendersGetAuxiliaryVar(_benders, probnumber)
8931-
auxvar = Variable.create(_auxvar)
8911+
auxvar = Variable(<size_t>_auxvar)
89328912

89338913
return auxvar
89348914

@@ -9723,7 +9703,7 @@ cdef class Model:
97239703
PY_SCIP_CALL(SCIPgetLPBranchCands(self._scip, &lpcands, &lpcandssol, &lpcandsfrac,
97249704
&nlpcands, &npriolpcands, &nfracimplvars))
97259705

9726-
return ([Variable.create(lpcands[i]) for i in range(nlpcands)], [lpcandssol[i] for i in range(nlpcands)],
9706+
return ([Variable(<size_t>lpcands[i]) for i in range(nlpcands)], [lpcandssol[i] for i in range(nlpcands)],
97279707
[lpcandsfrac[i] for i in range(nlpcands)], nlpcands, npriolpcands, nfracimplvars)
97289708

97299709
def getNLPBranchCands(self):
@@ -9760,7 +9740,7 @@ cdef class Model:
97609740

97619741
PY_SCIP_CALL(SCIPgetPseudoBranchCands(self._scip, &pseudocands, &npseudocands, &npriopseudocands))
97629742

9763-
return ([Variable.create(pseudocands[i]) for i in range(npseudocands)], npseudocands, npriopseudocands)
9743+
return ([Variable(<size_t>pseudocands[i]) for i in range(npseudocands)], npseudocands, npriopseudocands)
97649744

97659745
def branchVar(self, Variable variable):
97669746
"""

0 commit comments

Comments
 (0)