Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def eventinit(self):
self.model.catchEvent(SCIP_EVENTTYPE.NODEFOCUSED, self)
def eventinit(self):
calls.append('eventinit')
self.model.catchEvent(SCIP_EVENTTYPE.FIRSTLPSOLVED, self)
def eventexit(self):
calls.append('eventexit')
self.model.dropEvent(SCIP_EVENTTYPE.FIRSTLPSOLVED, self)
def flp(I, J, M, d,f, c=None, monolithic=False):
"""flp -- model for the capacitated facility location problem
Parameters:
- I: set of customers
- J: set of facilities
- d[i]: demand for customer i
- M[j]: capacity of facility j
- f[j]: fixed cost for using a facility in point j
- c[i,j]: unit cost of servicing demand point i from facility j
Returns a model, ready to be solved.
"""
master = Model("flp-master")
# creating the problem
y = {}
for j in J:
y["y(%d)"%j] = master.addVar(vtype="B", name="y(%s)"%j)
if monolithic:
x = {}
demand = {}
capacity = {}
for j in J:
for i in I:
x[i, j] = master.addVar(vtype="C", name="x(%s,%s)" % (i, j))
for i in I:
demand[i] = master.addCons(quicksum(x[i, j] for j in J) >= d[i], "Demand(%s)" % i)
def test_reopt(self):
m = Model()
m.enableReoptimization()
x = m.addVar(name="x", ub=5)
y = m.addVar(name="y", lb=-2, ub=10)
m.addCons(2 * x + y >= 8)
m.setObjective(x + y)
m.optimize()
print("x", m.getVal(x))
print("y", m.getVal(y))
self.assertEqual(m.getVal(x), 5.0)
self.assertEqual(m.getVal(y), -2.0)
m.freeReoptSolve()
m.addCons(y <= 3)
def flp(I, J, M, d,f, c=None, monolithic=False):
"""flp -- model for the capacitated facility location problem
Parameters:
- I: set of customers
- J: set of facilities
- d[i]: demand for customer i
- M[j]: capacity of facility j
- f[j]: fixed cost for using a facility in point j
- c[i,j]: unit cost of servicing demand point i from facility j
Returns a model, ready to be solved.
"""
master = Model("flp-master")
# creating the problem
y = {}
for j in J:
y["y(%d)"%j] = master.addVar(vtype="B", name="y(%s)"%j)
if monolithic:
x = {}
demand = {}
capacity = {}
for j in J:
for i in I:
x[i, j] = master.addVar(vtype="C", name="x(%s,%s)" % (i, j))
for i in I:
demand[i] = master.addCons(quicksum(x[i, j] for j in J) >= d[i], "Demand(%s)" % i)
def test_largequadratic():
# inspired from performance issue on
# http://stackoverflow.com/questions/38434300
m = Model("dense_quadratic")
dim = 200
x = [m.addVar("x_%d" % i) for i in range(dim)]
expr = quicksum((i+j+1)*x[i]*x[j]
for i in range(dim)
for j in range(dim))
cons = expr <= 1.0
# upper triangle, diagonal
assert len(cons.expr.terms) == dim * (dim-1) / 2 + dim
m.addCons(cons)
# TODO: what can we test beyond the lack of crashes?
y["y(%d)"%j] = master.addVar(vtype="B", name="y(%s)"%j)
if monolithic:
x = {}
demand = {}
capacity = {}
for j in J:
for i in I:
x[i, j] = master.addVar(vtype="C", name="x(%s,%s)" % (i, j))
for i in I:
demand[i] = master.addCons(quicksum(x[i, j] for j in J) >= d[i], "Demand(%s)" % i)
for j in J:
print(j, M[j])
capacity[j] = master.addCons(quicksum(x[i, j] for i in I) <= M[j] * y["y(%d)"%j], "Capacity(%s)" % j)
master.addCons(quicksum(y["y(%d)"%j]*M[j] for j in J)
- quicksum(d[i] for i in I) >= 0)
master.setObjective(
quicksum(f[j]*y["y(%d)"%j] for j in J) + (0 if not monolithic else
quicksum(c[i, j] * x[i, j] for i in I for j in J)),
"minimize")
master.data = y
return master
m.addCons(quicksum(v for v in more_vars[50::2]) <= (40 - i) * quicksum(v for v in more_vars[405::2]))
m.addCons(r1 >= x0)
m.addCons(r2 >= -x0)
m.addCons(y0 == r1 +r2)
#m.addCons(t * l + l * u >= 4)
m.addCons(t + l + 7* u <= 300)
m.addCons(t >= quicksum(v for v in more_vars[::3]) - 10 * more_vars[5] + 5* more_vars[9])
m.addCons(more_vars[3] >= l + 2)
m.addCons(7 <= quicksum(v for v in more_vars[::4]) - x0)
m.addCons(quicksum(v for v in more_vars[::2]) + l <= quicksum(v for v in more_vars[::4]))
m.setObjective(t - quicksum(j*v for j, v in enumerate(more_vars[20:-40])))
#m.addCons(t >= r1 * (r1 - x0) + r2 * (r2 + x0))
my_branchrule = MyBranching(m, x0)
m.includeBranchrule(my_branchrule, "test branch", "test branching and probing and lp functions",
priority=10000000, maxdepth=3, maxbounddist=1)
m.optimize()
print("x0", m.getVal(x0))
print("r1", m.getVal(r1))
print("r2", m.getVal(r2))
print("y0", m.getVal(y0))
print("t", m.getVal(t))
assert my_branchrule.was_called_val
assert my_branchrule.was_called_int
def benderscutexec(self, solution, probnumber, enfotype):
subprob = self.model.getBendersSubproblem(probnumber, benders=self.benders)
membersubprob = self.benders.subprob
# checking whether the subproblem is already optimal, i.e. whether a cut
# needs to be generated
if self.model.checkBendersSubproblemOptimality(solution, probnumber,
benders=self.benders):
return {"result" : SCIP_RESULT.FEASIBLE}
# testing whether the dual multipliers can be found for the retrieved
# subproblem model. If the constraints don't exist, then the subproblem
# model is not correct.
# Also checking whether the dual multiplier is the same between the
# member subproblem and the retrieved subproblem`
lhs = 0
for i in self.I:
subprobcons = self.benders.demand[i]
try:
dualmult = subprob.getDualMultiplier(subprobcons)
lhs += dualmult*self.d[i]
except:
print("Subproblem constraint <%d> does not exist in the "\
"subproblem."%subprobcons.name)
assert False