How to use the mip.minimize function in mip

To help you get started, we’ve selected a few mip examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github coin-or / python-mip / test / mip_rcpsp.py View on Github external
sd = sum(dur[j] for j in J)
    vt = CONTINUOUS if relax else BINARY
    x = [
        {
            t: mip.add_var("x(%d,%d)" % (j, t), var_type=vt)
            for t in range(EST[j], sd + 1)
        }
        for j in J
    ]
    TJ = [set(x[j].keys()) for j in J]
    T = set()
    for j in J:
        T = T.union(TJ[j])

    if sense == MINIMIZE:
        mip.objective = minimize(xsum(t * x[J[-1]][t] for t in TJ[-1]))
    else:
        mip.objective = maximize(xsum(t * x[J[-1]][t] for t in TJ[-1]))

    # one time per job
    for j in J:
        mip += xsum(x[j][t] for t in TJ[j]) == 1, "selTime(%d)" % j

    # precedences
    for (u, v) in S:
        mip += (
            xsum(t * x[v][t] for t in TJ[v])
            >= xsum(t * x[u][t] for t in TJ[u]) + dur[u],
            "prec(%d,%d)" % (u, v),
        )

    # resource usage
github coin-or / python-mip / examples / tsp-cuts.py View on Github external
Arcs = [(i, j) for (i, j) in product(V, V) if i != j]

# distance matrix
c = [[round(sqrt((p[i][0]-p[j][0])**2 + (p[i][1]-p[j][1])**2)) 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, j) in Arcs))

# 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

# (weak) subtour elimination
# subtour elimination
for (i, j) in product(V - {0}, V - {0}):
    if i != j:
        model += y[i] - (n+1)*x[i][j] >= y[j]-n

# no subtours of size 2
github coin-or / python-mip / examples / tsp-lazy-ulysses22.py View on Github external
# 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]

# 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

model.lazy_constrs_generator = SubTourLazyGenerator(x)

# optimizing
model.optimize(max_seconds=70)

# checking if a solution was found
if model.num_solutions:
github coin-or / python-mip / examples / tsp-mipstart.py View on Github external
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

# subtour elimination
for (i, j) in set(product(set(V) - {0}, set(V) - {0})):
    model += y[i] - (n+1)*x[i][j] >= y[j]-n

# running a best insertion heuristic to obtain an initial feasible solution:
# test every node j not yet inserted in the route at every intermediate
# position p and select the pair (j, p) that results in the smallest cost
github coin-or / python-mip / examples / tsp-compact-ulysses22.py View on Github external
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

# subtour elimination
for (i, j) in product(V - {0}, V - {0}):
    if i != j:
        model += y[i] - (n + 1) * x[i][j] >= y[j] - n

# optimizing
model.threads = 4
github coin-or / python-mip / examples / apps / tsp / tsp.py View on Github external
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

if not useLazy:
    # (weak) subtour elimination
    for (i, j) in set(product(set(V) - {0}, set(V) - {0})):
        model += y[i] - (n+1)*x[i][j] >= y[j]-n

# no subtours of size 2
for (i, j) in product(V, V):
github coin-or / python-mip / examples / tsp-compact.py View on Github external
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 V - {i}) == 1

# constraint : enter each city only once
for i in V:
    model += xsum(x[j][i] for j in V - {i}) == 1

# subtour elimination
for (i, j) in product(V - {0}, V - {0}):
    if i != j:
        model += y[i] - (n+1)*x[i][j] >= y[j]-n

# optimizing
model.optimize()
github coin-or / python-mip / bench / bmcp.py View on Github external
from mip import Model, xsum, minimize, MINIMIZE, BINARY

data = bmcp_data.read(argv[1])
ncol = int(argv[3])
N, r, d = data.N, data.r, data.d
U = list(range(ncol))

st = time()
m = Model(solver_name=argv[2])

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

for i, c in product(N, U):
    m += z >= (c + 1) * x[i][c]
ed = time()
github coin-or / python-mip / docs / podes / tsp-lazy.py View on Github external
# nr. de pontos e conjunto de pontos
n, V = len(dists), set(range(len(dists)))

# matriz com tempos
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()

# variáveis 0/1 indicando se um arco (i,j) participa da rota ou não
x = [[model.add_var(var_type=BINARY, name='x(%d,%d)' % (i, j)) for j in V] for i in V]

# função objetivo: minimizar tempo
model.objective = minimize(xsum(c[i][j]*x[i][j] for i in V for j in V))

# restrição: selecionar arco de saída da cidade
for i in V:
    model += xsum(x[i][j] for j in V - {i}) == 1

# restrição: selecionar arco de entrada na cidade
for i in V:
    model += xsum(x[j][i] for j in V - {i}) == 1

# chamada da otimização com limite tempo de 30 segundos
model.lazy_constrs_generator = SubTourLazyGenerator()
model.optimize(max_seconds=30)

# verificando se ao menos uma solução foi encontrada e a imprimindo
if model.num_solutions:
    out.write('route with total distance %g found: %s'
github coin-or / python-mip / examples / apps / bmcp / bmcp.py View on Github external
import bmcp_greedy
from mip import Model, xsum, minimize, MINIMIZE, BINARY

data = bmcp_data.read(argv[1])
N, r, d = data.N, data.r, data.d
S = bmcp_greedy.build(data)
C, U = S.C, [i for i in range(S.u_max+1)]

st = time()
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

for i, c in product(N, U):
    m += z >= (c+1)*x[i][c]
ed = time()