Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
- d[i]: demand for item i
- w[i]: unit weight for item i
- W: capacity (limit on order quantity)
Returns a model, ready to be solved.
"""
model = Model("EOQ model using SOCO")
T,c = {},{}
for i in I:
T[i] = model.addVar(vtype="C", name="T(%s)"%i) # cycle time for item i
c[i] = model.addVar(vtype="C", name="c(%s)"%i) # total cost for item i
for i in I:
model.addCons(F[i] <= c[i]*T[i])
model.addCons(quicksum(w[i]*d[i]*T[i] for i in I) <= W)
model.setObjective(quicksum(c[i] + h[i]*d[i]*T[i]*0.5 for i in I), "minimize")
model.data = T,c
return model
n - number of nodes
c[i,j] - cost for traversing arc (i,j)
Returns a model, ready to be solved.
"""
model = Model("atsp - mtz-strong")
x,u = {},{}
for i in range(1,n+1):
u[i] = model.addVar(lb=0, ub=n-1, vtype="C", name="u(%s)"%i)
for j in range(1,n+1):
if i != j:
x[i,j] = model.addVar(vtype="B", name="x(%s,%s)"%(i,j))
for i in range(1,n+1):
model.addCons(quicksum(x[i,j] for j in range(1,n+1) if j != i) == 1, "Out(%s)"%i)
model.addCons(quicksum(x[j,i] for j in range(1,n+1) if j != i) == 1, "In(%s)"%i)
for i in range(1,n+1):
for j in range(2,n+1):
if i != j:
model.addCons(u[i] - u[j] + (n-1)*x[i,j] + (n-3)*x[j,i] <= n-2, "LiftedMTZ(%s,%s)"%(i,j))
for i in range(2,n+1):
model.addCons(-x[1,i] - u[i] + (n-3)*x[i,1] <= -2, name="LiftedLB(%s)"%i)
model.addCons(-x[i,1] + u[i] + (n-3)*x[1,i] <= n-2, name="LiftedUB(%s)"%i)
model.setObjective(quicksum(c[i,j]*x[i,j] for (i,j) in x), "minimize")
model.data = x,u
return model
- 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")
subprob = Model("flp-subprob")
# creating the problem
y = {}
for j in J:
y[j] = master.addVar(vtype="B", name="y(%s)"%j)
master.setObjective(
quicksum(f[j]*y[j] for j in J),
"minimize")
master.data = y
# creating the subproblem
x,y = {},{}
for j in J:
y[j] = subprob.addVar(vtype="B", name="y(%s)"%j)
for i in I:
x[i,j] = subprob.addVar(vtype="C", name="x(%s,%s)"%(i,j))
for i in I:
subprob.addCons(quicksum(x[i,j] for j in J) == d[i], "Demand(%s)"%i)
for j in M:
subprob.addCons(quicksum(x[i,j] for i in I) <= M[j]*y[j], "Capacity(%s)"%i)
model = Model("vrp")
x = {}
for i in V:
for j in V:
if j > i and i == V[0]: # depot
x[i,j] = model.addVar(ub=2, vtype="I", name="x(%s,%s)"%(i,j))
elif j > i:
x[i,j] = model.addVar(ub=1, vtype="I", name="x(%s,%s)"%(i,j))
model.addCons(quicksum(x[V[0],j] for j in V[1:]) == 2*m, "DegreeDepot")
for i in V[1:]:
model.addCons(quicksum(x[j,i] for j in V if j < i) +
quicksum(x[i,j] for j in V if j > i) == 2, "Degree(%s)"%i)
model.setObjective(quicksum(c[i,j]*x[i,j] for i in V for j in V if j>i), "minimize")
model.hideOutput()
EPS = 1.e-6
while True:
model.optimize()
edges = []
for (i,j) in x:
if model.getVal(x[i,j]) > EPS:
if i != V[0] and j != V[0]:
edges.append((i,j))
if addcut(edges) == False:
break
return model.getObjVal(),edges
model = Model("modern diet")
# Create variables
x,y,z = {},{},{}
for j in F:
x[j] = model.addVar(vtype="I", name="x(%s)"%j)
y[j] = model.addVar(vtype="B", name="y(%s)"%j)
for i in N:
z[i] = model.addVar(lb=a[i], ub=b[i], name="z(%s)"%j)
v = model.addVar(vtype="C", name="v")
# Constraints:
for i in N:
model.addCons(quicksum(d[j][i]*x[j] for j in F) == z[i], name="Nutr(%s)"%i)
model.addCons(quicksum(c[j]*x[j] for j in F) == v, name="Cost")
for j in F:
model.addCons(y[j] <= x[j], name="Eat(%s)"%j)
# Objective:
model.setObjective(quicksum(y[j] for j in F), "maximize")
model.data = x,y,z,v
return model
"""
Ts = range(1,T+1)
model = Model("multi-item lotsizing -- facility location formulation")
y,X = {},{}
for p in P:
for t in Ts:
y[t,p] = model.addVar(vtype="B", name="y(%s,%s)"%(t,p))
for s in range(1,t+1):
X[s,t,p] = model.addVar(name="X(%s,%s,%s)"%(s,t,p))
for t in Ts:
# capacity constraints
model.addCons(quicksum(X[t,s,p] for s in range(t,T+1) for p in P) + \
quicksum(g[t,p]*y[t,p] for p in P) <= M[t],
"Capacity(%s)"%(t))
for p in P:
# demand satisfaction constraints
model.addCons(quicksum(X[s,t,p] for s in range(1,t+1)) == d[t,p], "Demand(%s,%s)"%(t,p))
# connection constraints
for s in range(1,t+1):
model.addCons(X[s,t,p] <= d[t,p] * y[s,p], "Connect(%s,%s,%s)"%(s,t,p))
C = {} # variable costs plus holding costs
for p in P:
for s in Ts:
sumC = 0
for t in range(s,T+1):
Returns the optimum objective value and the list of edges used.
"""
model = Model("TSP_lazy")
conshdlr = TSPconshdlr()
x = {}
for i in V:
for j in V:
if j > i:
x[i,j] = model.addVar(vtype = "B",name = "x(%s,%s)" % (i,j))
for i in V:
model.addCons(quicksum(x[j, i] for j in V if j < i) +
quicksum(x[i, j] for j in V if j > i) == 2, "Degree(%s)" % i)
model.setObjective(quicksum(c[i, j] * x[i, j] for i in V for j in V if j > i), "minimize")
model.data = x
return model, conshdlr