Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if v.name.startswith('x(') and v.x >= 0.99]
U = [int(v.name.split('(')[1].split(',')[0]) for v, f in r]
V = [int(v.name.split(')')[0].split(',')[1]) for v, f in r]
N, cp = set(U+V), CutPool()
# output nodes for each node
out = defaultdict(lambda: list())
for i in range(len(U)):
out[U[i]].append(V[i])
for n in N:
S = set(subtour(N, out, n))
if S:
arcsInS = [(v, f) for i, (v, f) in enumerate(r)
if U[i] in S and V[i] in S]
if sum(f for v, f in arcsInS) >= (len(S)-1)+1e-4:
cut = xsum(1.0*v for v, fm in arcsInS) <= len(S)-1
cp.add(cut)
for cut in cp.cuts:
model += cut
def get_objective(self) -> LinExpr:
self.flush_cols()
attr = "Obj".encode("utf-8")
# st = GRBsetdblattrarray(self._model, attr,
# 0, num_vars, zeros)
obj = ffi.new("double[]", [0.0 for i in range(self.num_cols())])
st = GRBgetdblattrarray(self._model, attr, 0, self.num_cols(), obj)
if st != 0:
raise ParameterNotAvailable("Error getting objective function")
obj_expr = xsum(
obj[i] * self.model.vars[i]
for i in range(self.num_cols())
if abs(obj[i] > 1e-20)
)
obj_expr.sense = self.get_objective_sense
return obj_expr
def generate_constrs(self, m_: Model):
xf, cp, Gl = m_.translate(self.x), CutPool(), nx.DiGraph()
Ar = [(i, j) for (i, j) in Arcs if xf[i][j] and xf[i][j].x >= 1e-4]
for (u, v) in Ar:
Gl.add_edge(u, v, capacity=xf[u][v].x)
for (u, v) in F:
val, (S, NS) = nx.minimum_cut(Gl, u, v)
if val <= 0.99:
aInS = [(xf[i][j], xf[i][j].x)
for (i, j) in Ar if i in S and j in S]
if sum(f for v, f in aInS) >= (len(S)-1)+1e-4:
cut = xsum(1.0*v for v, fm in aInS) <= len(S)-1
cp.add(cut)
if len(cp.cuts) > 32:
for cut in cp.cuts:
m_ += cut
return
for cut in cp.cuts:
m_ += cut
def generate_constrs(self, model: Model):
x_, N, cp = model.translate(self._x), range(len(self._x)), CutPool()
outa = [[j for j in N if x_[i][j].x >= 0.99] for i in N]
for node in N:
S = set(subtour(N, outa, node))
if S:
AS = [(i, j) for (i, j) in product(S, S) if i != j]
cut = xsum(x_[i][j] for (i, j) in AS) <= len(S)-1
cp.add(cut)
for cut in cp.cuts:
model += cut
"""0/1 Knapsack example"""
from mip import Model, xsum, maximize, BINARY
p = [10, 13, 18, 31, 7, 15]
w = [11, 15, 20, 35, 10, 33]
c, I = 47, range(len(w))
m = Model("knapsack")
x = [m.add_var(var_type=BINARY) for i in I]
m.objective = maximize(xsum(p[i] * x[i] for i in I))
m += xsum(w[i] * x[i] for i in I) <= c
m.optimize()
selected = [i for i in I if x[i].x >= 0.99]
print("selected items: {}".format(selected))
# sanity tests
from mip import OptimizationStatus
assert m.status == OptimizationStatus.OPTIMAL
assert round(m.objective_value) == 41
assert round(m.constrs[0].slack) == 1
def generate_constrs(self, model: Model):
xf, V_, cp, G = model.translate(self.x), self.V, CutPool(), nx.DiGraph()
for (u, v) in [(k, l) for (k, l) in product(V_, V_) if k != l and xf[k][l]]:
G.add_edge(u, v, capacity=xf[u][v].x)
for (u, v) in F:
val, (S, NS) = nx.minimum_cut(G, u, v)
if val <= 0.99:
aInS = [(xf[i][j], xf[i][j].x)
for (i, j) in product(V_, V_) if i != j and xf[i][j] and i in S and j in S]
if sum(f for v, f in aInS) >= (len(S)-1)+1e-4:
cut = xsum(1.0*v for v, fm in aInS) <= len(S)-1
cp.add(cut)
if len(cp.cuts) > 256:
for cut in cp.cuts:
model += cut
return
for cut in cp.cuts:
model += cut
x = {(i, j): m.add_var() for (i, j) in product(F, C)}
# satisfy demand
for j in C:
m += xsum(x[(i, j)] for i in F) == d[j]
# SOS type 2 to model installation costs for each installed plant
y = {i: m.add_var() for i in F}
for f in F:
D = 6 # nr. of discretization points, increase for more precision
v = [c[f] * (v / (D - 1)) for v in range(D)] # points
# non-linear function values for points in v
vn = [0 if k == 0 else 1520 * log(v[k]) for k in range(D)]
# w variables
w = [m.add_var() for v in range(D)]
m += xsum(w) == 1 # convexification
# link to z vars
m += z[f] == xsum(v[k] * w[k] for k in range(D))
# link to y vars associated with non-linear cost
m += y[f] == xsum(vn[k] * w[k] for k in range(D))
m.add_sos([(w[k], v[k]) for k in range(D)], 2)
# plant capacity
for i in F:
m += z[i] >= xsum(x[(i, j)] for j in C)
# objective function
m.objective = minimize(
xsum(dist[i, j] * x[i, j] for (i, j) in product(F, C)) + xsum(y[i] for i in F) )
m.optimize()