Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add(
self,
name: str = "",
lb: numbers.Real = 0.0,
ub: numbers.Real = mip.INF,
obj: numbers.Real = 0.0,
var_type: str = mip.CONTINUOUS,
column: "mip.Column" = None,
) -> "mip.Var":
if not name:
name = "var({})".format(len(self.__vars))
if var_type == mip.BINARY:
lb = 0.0
ub = 1.0
new_var = mip.Var(self.__model, len(self.__vars))
self.__model.solver.add_var(obj, lb, ub, var_type, column, name)
self.__vars.append(new_var)
return new_var
useLazy = int(argv[5])
useHeur = int(argv[6])
start = time()
inst = tsplib95.load_problem(argv[1])
V = [n-1 for n in inst.get_nodes()]
n, c = len(V), [[0.0 for j in V] for i in V]
for (i, j) in product(V, V):
if i != j:
c[i][j] = inst.wfunc(i+1, j+1)
model = Model()
# binary variables indicating if arc (i,j) is used on the route or not
x = [[model.add_var(var_type=BINARY, name='x(%d,%d)' % (i, j)) for j in V]
for i in V]
# continuous variable to prevent subtours: each city will have a
# different sequential id in the planned route except the first one
y = [model.add_var(name='y(%d)' % i) for i in V]
# objective function: minimize the distance
model.objective = minimize(xsum(c[i][j]*x[i][j] for i in V for j in V))
# constraint : leave each city only once
for i in V:
model += xsum(x[i][j] for j in set(V) - {i}) == 1
# constraint : enter each city only once
for i in V:
model += xsum(x[j][i] for j in set(V) - {i}) == 1
[97],
[]]
# number of nodes and list of vertices
n, V = len(dists), range(len(dists))
# distances matrix
c = [[0 if i == j
else dists[i][j-i-1] if j > i
else dists[j][i-j-1]
for j in V] for i in V]
model = Model()
# binary variables indicating if arc (i,j) is used on the route or not
x = [[model.add_var(var_type=BINARY) for j in V] for i in V]
# continuous variable to prevent subtours: each city will have a
# different sequential id in the planned route except the first one
y = [model.add_var() for i in V]
# objective function: minimize the distance
model.objective = minimize(xsum(c[i][j]*x[i][j] for i in V for j in V))
# constraint : leave each city only once
for i in V:
model += xsum(x[i][j] for j in set(V) - {i}) == 1
# constraint : enter each city only once
for i in V:
model += xsum(x[j][i] for j in set(V) - {i}) == 1
[0, 2, 3, 0, 0, 0, 3, 0], # 2
[0, 0, 0, 3, 2, 0, 0, 2], # 3
[2, 0, 0, 2, 3, 2, 0, 0], # 4
[2, 2, 0, 0, 2, 3, 2, 0], # 5
[0, 2, 2, 0, 0, 2, 3, 0], # 6
[0, 0, 0, 2, 0, 0, 0, 3]] # 7
N = range(len(r))
# in complete applications this upper bound should be obtained from a feasible
# solution produced with some heuristic
U = range(sum(d[i][j] for (i, j) in product(N, N)) + sum(el for el in r))
m = Model()
x = [[m.add_var('x({},{})'.format(i, c), var_type=BINARY)
for c in U] for i in N]
z = m.add_var('z')
m.objective = minimize(z)
for i in N:
m += xsum(x[i][c] for c in U) == r[i]
for i, j, c1, c2 in product(N, N, U, U):
if i != j and c1 <= c2 < c1+d[i][j]:
m += x[i][c1] + x[j][c2] <= 1
for i, c1, c2 in product(N, U, U):
if c1 < c2 < c1+d[i][i]:
m += x[i][c1] + x[i][c2] <= 1
times = [[2, 1, 2],
[1, 2, 2],
[1, 2, 1]]
M = sum(times[i][j] for i in range(n) for j in range(m))
machines = [[2, 0, 1],
[1, 2, 0],
[2, 1, 0]]
model = Model('JSSP')
c = model.add_var(name="C")
x = [[model.add_var(name='x({},{})'.format(j+1, i+1))
for i in range(m)] for j in range(n)]
y = [[[model.add_var(var_type=BINARY, name='y({},{},{})'.format(j+1, k+1, i+1))
for i in range(m)] for k in range(n)] for j in range(n)]
model.objective = c
for (j, i) in product(range(n), range(1, m)):
model += x[j][machines[j][i]] - x[j][machines[j][i-1]] >= \
times[j][machines[j][i-1]]
for (j, k) in product(range(n), range(n)):
if k != j:
for i in range(m):
model += x[j][i] - x[k][i] + M*y[j][k][i] >= times[k][i]
model += -x[j][i] + x[k][i] - M*y[j][k][i] >= times[j][i] - M
for j in range(n):
model += c - x[j][machines[j][m - 1]] >= times[j][machines[j][m - 1]]
def var_get_var_type(self, var: "Var") -> str:
isInt = cbclib.Osi_isInteger(self.osi, var.idx)
if isInt:
lb = self.var_get_lb(var)
ub = self.var_get_ub(var)
if abs(lb) <= 1e-15 and abs(ub - 1.0) <= 1e-15:
return BINARY
return INTEGER
return CONTINUOUS
"""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
# latitude and longitude
coord = [(rad(x), rad(y)) for (x, y) in coord]
# distances in an upper triangular matrix
# number of nodes and list of vertices
n, V = len(coord), set(range(len(coord)))
# distances matrix
c = [[0 if i == j else dist(coord[i], coord[j]) for j in V] for i in V]
model = Model()
# binary variables indicating if arc (i,j) is used on the route or not
x = [[model.add_var(var_type=BINARY) for j in V] for i in V]
# continuous variable to prevent subtours: each city will have a
# different sequential id in the planned route except the first one
y = [model.add_var() for i in V]
# objective function: minimize the distance
model.objective = minimize(xsum(c[i][j] * x[i][j] for i in V for j in V))
# constraint : leave each city only once
for i in V:
model += xsum(x[i][j] for j in V - {i}) == 1
# constraint : enter each city only once
for i in V:
model += xsum(x[j][i] for j in V - {i}) == 1
from networkx import minimum_cut, DiGraph
from mip import Model, xsum, BINARY, OptimizationStatus, CutType
N = ["a", "b", "c", "d", "e", "f", "g"]
A = { ("a", "d"): 56, ("d", "a"): 67, ("a", "b"): 49, ("b", "a"): 50,
("f", "c"): 35, ("g", "b"): 35, ("g", "b"): 35, ("b", "g"): 25,
("a", "c"): 80, ("c", "a"): 99, ("e", "f"): 20, ("f", "e"): 20,
("g", "e"): 38, ("e", "g"): 49, ("g", "f"): 37, ("f", "g"): 32,
("b", "e"): 21, ("e", "b"): 30, ("a", "g"): 47, ("g", "a"): 68,
("d", "c"): 37, ("c", "d"): 52, ("d", "e"): 15, ("e", "d"): 20,
("d", "b"): 39, ("b", "d"): 37, ("c", "f"): 35, }
Aout = {n: [a for a in A if a[0] == n] for n in N}
Ain = {n: [a for a in A if a[1] == n] for n in N}
m = Model()
x = {a: m.add_var(name="x({},{})".format(a[0], a[1]), var_type=BINARY) for a in A}
m.objective = xsum(c * x[a] for a, c in A.items())
for n in N:
m += xsum(x[a] for a in Aout[n]) == 1, "out({})".format(n)
m += xsum(x[a] for a in Ain[n]) == 1, "in({})".format(n)
newConstraints = True
while newConstraints:
m.optimize(relax=True)
print("status: {} objective value : {}".format(m.status, m.objective_value))
G = DiGraph()
for a in A:
G.add_edge(a[0], a[1], capacity=x[a].x)