Skip to content

Commit 188b3ef

Browse files
committed
Remove Variable.create
1 parent 4cbc603 commit 188b3ef

File tree

5 files changed

+81
-65
lines changed

5 files changed

+81
-65
lines changed

src/pyscipopt/propagator.pxi

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ cdef SCIP_RETCODE PyPropExec (SCIP* scip, SCIP_PROP* prop, SCIP_PROPTIMING propt
149149
cdef SCIP_RETCODE PyPropResProp (SCIP* scip, SCIP_PROP* prop, SCIP_VAR* infervar, int inferinfo,
150150
SCIP_BOUNDTYPE boundtype, SCIP_BDCHGIDX* bdchgidx, SCIP_Real relaxedbd, SCIP_RESULT* result) noexcept with gil:
151151
cdef SCIP_PROPDATA* propdata
152-
cdef SCIP_VAR* tmp
153-
tmp = infervar
154152
propdata = SCIPpropGetData(prop)
155-
confvar = Variable.create(tmp)
153+
confvar = Variable(infervar)
156154

157155
#TODO: parse bdchgidx?
158156

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.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)]
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)]
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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,14 +2067,11 @@ cdef class Node:
20672067
@staticmethod
20682068
cdef create(SCIP_NODE* scipnode)
20692069

2070-
cdef class Variable(Expr):
2070+
cdef class Variable:
20712071
cdef SCIP_VAR* scip_var
20722072
# can be used to store problem data
20732073
cdef public object data
20742074

2075-
@staticmethod
2076-
cdef create(SCIP_VAR* scipvar)
2077-
20782075
cdef class Constraint:
20792076
cdef SCIP_CONS* scip_cons
20802077
# can be used to store problem data

src/pyscipopt/scip.pxi

Lines changed: 73 additions & 52 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.create(var)
443+
return Variable(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.create(var)
564+
return Variable(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.create(linvars[i]), lincoefs[i]) for i in range(nlinvars)]
967+
return [(Variable(linvars[i]), lincoefs[i]) for i in range(nlinvars)]
968968

969969
def getLhs(self):
970970
"""
@@ -1166,7 +1166,7 @@ cdef class BoundChange:
11661166
Variable
11671167
11681168
"""
1169-
return Variable.create(SCIPboundchgGetVar(self.scip_boundchg))
1169+
return Variable(SCIPboundchgGetVar(self.scip_boundchg))
11701170

11711171
def getBoundchgtype(self):
11721172
"""
@@ -1434,7 +1434,7 @@ cdef class Node:
14341434
SCIPnodeGetParentBranchings(self.scip_node, branchvars, branchbounds,
14351435
boundtypes, &nbranchvars, nbranchvars)
14361436

1437-
py_variables = [Variable.create(branchvars[i]) for i in range(nbranchvars)]
1437+
py_variables = [Variable(branchvars[i]) for i in range(nbranchvars)]
14381438
py_branchbounds = [branchbounds[i] for i in range(nbranchvars)]
14391439
py_boundtypes = [boundtypes[i] for i in range(nbranchvars)]
14401440
free(boundtypes)
@@ -1480,44 +1480,67 @@ cdef class Node:
14801480
return (self.__class__ == other.__class__
14811481
and self.scip_node == (<Node>other).scip_node)
14821482

1483-
cdef class Variable(Expr):
1484-
"""Is a linear expression and has SCIP_VAR*"""
14851483

1486-
@staticmethod
1487-
cdef create(SCIP_VAR* scipvar):
1488-
"""
1489-
Main method for creating a Variable class. Is used instead of __init__.
1490-
1491-
Parameters
1492-
----------
1493-
scipvar : SCIP_VAR*
1494-
A pointer to the SCIP_VAR
1495-
1496-
Returns
1497-
-------
1498-
var : Variable
1499-
The Python representative of the SCIP_VAR
1500-
1501-
"""
1502-
if scipvar == NULL:
1503-
raise Warning("cannot create Variable with SCIP_VAR* == NULL")
1504-
var = Variable()
1505-
var.scip_var = scipvar
1506-
Expr.__init__(var, {Term(var) : 1.0})
1507-
return var
1484+
cdef class Variable:
1485+
def __init__(self, scip_var):
1486+
self.scip_var = scip_var
15081487

1509-
property name:
1510-
def __get__(self):
1511-
cname = bytes( SCIPvarGetName(self.scip_var) )
1512-
return cname.decode('utf-8')
1488+
@property
1489+
def name(self):
1490+
return bytes(SCIPvarGetName(self.scip_var)).decode("utf-8")
15131491

15141492
def ptr(self):
1515-
""" """
15161493
return <size_t>(self.scip_var)
15171494

15181495
def __repr__(self):
15191496
return self.name
15201497

1498+
def __add__(self, other):
1499+
return self.to_expr().__add__(other)
1500+
1501+
def __iadd__(self, other):
1502+
self = self.__add__(other)
1503+
return self
1504+
1505+
def __radd__(self, other):
1506+
return self.to_expr().__radd__(other)
1507+
1508+
def __mul__(self, other):
1509+
return self.to_expr().__mul__(other)
1510+
1511+
def __rmul__(self, other):
1512+
return self.to_expr().__rmul__(other)
1513+
1514+
def __truediv__(self, other):
1515+
return self.to_expr().__truediv__(other)
1516+
1517+
def __rtruediv__(self, other):
1518+
return self.to_expr().__rtruediv__(other)
1519+
1520+
def __pow__(self, other):
1521+
return self.to_expr().__pow__(other)
1522+
1523+
def __neg__(self):
1524+
return self.to_expr().__neg__()
1525+
1526+
def __sub__(self, other):
1527+
return self.to_expr().__sub__(other)
1528+
1529+
def __rsub__(self, other):
1530+
return self.to_expr().__rsub__(other)
1531+
1532+
def __lt__(self, other):
1533+
return self.to_expr().__lt__(other)
1534+
1535+
def __gt__(self, other):
1536+
return self.to_expr().__gt__(other)
1537+
1538+
def __eq__(self, other):
1539+
return self.to_expr().__eq__(other)
1540+
1541+
def to_expr(self):
1542+
return MonomialExpr.from_var(self)
1543+
15211544
def vtype(self):
15221545
"""
15231546
Retrieve the variables type (BINARY, INTEGER, IMPLINT or CONTINUOUS)
@@ -3594,8 +3617,7 @@ cdef class Model:
35943617
coeff = var.getObj()
35953618
if coeff != 0:
35963619
objective += coeff * var
3597-
objective.normalize()
3598-
return objective
3620+
return objective._normalize()
35993621

36003622
def addObjoffset(self, offset, solutions = False):
36013623
"""
@@ -3904,7 +3926,7 @@ cdef class Model:
39043926
else:
39053927
PY_SCIP_CALL(SCIPaddVar(self._scip, scip_var))
39063928

3907-
pyVar = Variable.create(scip_var)
3929+
pyVar = Variable(scip_var)
39083930

39093931
# store variable in the model to avoid creating new python variable objects in getVars()
39103932
assert not pyVar.ptr() in self._modelvars
@@ -4039,7 +4061,7 @@ cdef class Model:
40394061
cdef SCIP_VAR* _tvar
40404062
PY_SCIP_CALL(SCIPgetTransformedVar(self._scip, var.scip_var, &_tvar))
40414063

4042-
return Variable.create(_tvar)
4064+
return Variable(_tvar)
40434065

40444066
def addVarLocks(self, Variable var, int nlocksdown, int nlocksup):
40454067
"""
@@ -4381,7 +4403,7 @@ cdef class Model:
43814403
vars.append(self._modelvars[ptr])
43824404
else:
43834405
# create a new variable
4384-
var = Variable.create(_vars[i])
4406+
var = Variable(_vars[i])
43854407
assert var.ptr() == ptr
43864408
self._modelvars[ptr] = var
43874409
vars.append(var)
@@ -5439,7 +5461,6 @@ cdef class Model:
54395461
kwargs['removable']) )
54405462

54415463
PyCons = Constraint.create(scip_cons)
5442-
54435464
PY_SCIP_CALL( SCIPreleaseExpr(self._scip, &expr) )
54445465
for i in range(<int>len(terms)):
54455466
PY_SCIP_CALL(SCIPreleaseExpr(self._scip, &monomials[i]))
@@ -6124,7 +6145,7 @@ cdef class Model:
61246145
vars.append(self._modelvars[ptr])
61256146
else:
61266147
# create a new variable
6127-
var = Variable.create(_vars[i])
6148+
var = Variable(_vars[i])
61286149
assert var.ptr() == ptr
61296150
self._modelvars[ptr] = var
61306151
vars.append(var)
@@ -6213,7 +6234,7 @@ cdef class Model:
62136234
vars.append(self._modelvars[ptr])
62146235
else:
62156236
# create a new variable
6216-
var = Variable.create(_vars[i])
6237+
var = Variable(_vars[i])
62176238
assert var.ptr() == ptr
62186239
self._modelvars[ptr] = var
62196240
vars.append(var)
@@ -6243,7 +6264,7 @@ cdef class Model:
62436264
# check whether the corresponding variable exists already
62446265
if ptr not in self._modelvars:
62456266
# create a new variable
6246-
resultant = Variable.create(_resultant)
6267+
resultant = Variable(_resultant)
62476268
assert resultant.ptr() == ptr
62486269
self._modelvars[ptr] = resultant
62496270
else:
@@ -7181,7 +7202,7 @@ cdef class Model:
71817202
71827203
"""
71837204
cdef SCIP_VAR* var = SCIPgetSlackVarIndicator(cons.scip_cons)
7184-
return Variable.create(var)
7205+
return Variable(var)
71857206

71867207
def addPyCons(self, Constraint cons):
71877208
"""
@@ -7805,15 +7826,15 @@ cdef class Model:
78057826
quadterms = []
78067827

78077828
for termidx in range(nlinvars):
7808-
var = Variable.create(SCIPgetVarExprVar(linexprs[termidx]))
7829+
var = Variable(SCIPgetVarExprVar(linexprs[termidx]))
78097830
linterms.append((var, lincoefs[termidx]))
78107831

78117832
for termidx in range(nbilinterms):
78127833
SCIPexprGetQuadraticBilinTerm(expr, termidx, &bilinterm1, &bilinterm2, &bilincoef, NULL, NULL)
78137834
scipvar1 = SCIPgetVarExprVar(bilinterm1)
78147835
scipvar2 = SCIPgetVarExprVar(bilinterm2)
7815-
var1 = Variable.create(scipvar1)
7816-
var2 = Variable.create(scipvar2)
7836+
var1 = Variable(scipvar1)
7837+
var2 = Variable(scipvar2)
78177838
if scipvar1 != scipvar2:
78187839
bilinterms.append((var1,var2,bilincoef))
78197840
else:
@@ -7823,7 +7844,7 @@ cdef class Model:
78237844
SCIPexprGetQuadraticQuadTerm(expr, termidx, NULL, &lincoef, &sqrcoef, NULL, NULL, &sqrexpr)
78247845
if sqrexpr == NULL:
78257846
continue
7826-
var = Variable.create(SCIPgetVarExprVar(sqrexpr))
7847+
var = Variable(SCIPgetVarExprVar(sqrexpr))
78277848
quadterms.append((var,sqrcoef,lincoef))
78287849

78297850
return (bilinterms, quadterms, linterms)
@@ -8499,7 +8520,7 @@ cdef class Model:
84998520
if _mappedvar == NULL:
85008521
mappedvar = None
85018522
else:
8502-
mappedvar = Variable.create(_mappedvar)
8523+
mappedvar = Variable(_mappedvar)
85038524

85048525
return mappedvar
85058526

@@ -8528,7 +8549,7 @@ cdef class Model:
85288549
_benders = benders._benders
85298550

85308551
_auxvar = SCIPbendersGetAuxiliaryVar(_benders, probnumber)
8531-
auxvar = Variable.create(_auxvar)
8552+
auxvar = Variable(_auxvar)
85328553

85338554
return auxvar
85348555

@@ -9256,7 +9277,7 @@ cdef class Model:
92569277
PY_SCIP_CALL(SCIPgetLPBranchCands(self._scip, &lpcands, &lpcandssol, &lpcandsfrac,
92579278
&nlpcands, &npriolpcands, &nfracimplvars))
92589279

9259-
return ([Variable.create(lpcands[i]) for i in range(nlpcands)], [lpcandssol[i] for i in range(nlpcands)],
9280+
return ([Variable(lpcands[i]) for i in range(nlpcands)], [lpcandssol[i] for i in range(nlpcands)],
92609281
[lpcandsfrac[i] for i in range(nlpcands)], nlpcands, npriolpcands, nfracimplvars)
92619282

92629283
def getNLPBranchCands(self):
@@ -9293,7 +9314,7 @@ cdef class Model:
92939314

92949315
PY_SCIP_CALL(SCIPgetPseudoBranchCands(self._scip, &pseudocands, &npseudocands, &npriopseudocands))
92959316

9296-
return ([Variable.create(pseudocands[i]) for i in range(npseudocands)], npseudocands, npriopseudocands)
9317+
return ([Variable(pseudocands[i]) for i in range(npseudocands)], npseudocands, npriopseudocands)
92979318

92989319
def branchVar(self, Variable variable):
92999320
"""

src/pyscipopt/scip.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1298,7 +1298,7 @@ class VarExpr(GenExpr):
12981298
var: Incomplete
12991299
def __init__(self, *args, **kwargs) -> None: ...
13001300

1301-
class Variable(Expr):
1301+
class Variable:
13021302
data: Incomplete
13031303
name: Incomplete
13041304
def __init__(self, *args, **kwargs) -> None: ...

0 commit comments

Comments
 (0)