コード例 #1
0
ファイル: mip.py プロジェクト: vkinoshita/tsp
def solve_tsp_by_mip(tsp_matrix):
    start = time()
    matrix_of_distances = get_matrix_of_distances(tsp_matrix)
    length = len(tsp_matrix)

    model = Model(solver_name='gurobi')
    model.verbose = 1

    x = [[model.add_var(var_type=BINARY) for j in range(length)]
         for i in range(length)]

    y = [model.add_var() for i in range(length)]

    model.objective = xsum(matrix_of_distances[i][j] * x[i][j]
                           for j in range(length) for i in range(length))

    for i in range(length):
        model += xsum(x[j][i] for j in range(length) if j != i) == 1
        model += xsum(x[i][j] for j in range(length) if j != i) == 1

    for i in range(1, length):
        for j in [x for x in range(1, length) if x != i]:
            model += y[i] - (length + 1) * x[i][j] >= y[j] - length

    model.optimize(max_seconds=300)

    arcs = [(i, j) for i in range(length) for j in range(length)
            if x[i][j].x >= 0.99]

    best_distance = calculate_total_dist_by_arcs(matrix_of_distances, arcs)
    time_diff = time() - start
    return arcs, time_diff, best_distance
コード例 #2
0
ファイル: mip.py プロジェクト: vkinoshita/tsp
def solve_tsp_by_mip_with_sub_cycles_2(tsp_matrix):
    start = time()
    matrix_of_distances = get_matrix_of_distances(tsp_matrix)
    total_length = len(tsp_matrix)
    best_distance = sys.float_info.max

    found_cycles = []
    arcs = [(i, i + 1) for i in range(total_length - 1)]

    iteration = 0

    model = Model(solver_name='gurobi')
    model.verbose = 0

    x = [[model.add_var(var_type=BINARY) for j in range(total_length)]
         for i in range(total_length)]

    y = [model.add_var() for i in range(total_length)]

    model.objective = xsum(matrix_of_distances[i][j] * x[i][j]
                           for j in range(total_length)
                           for i in range(total_length))

    for i in range(total_length):
        model += (xsum(x[i][j] for j in range(0, i)) +
                  xsum(x[j][i] for j in range(i + 1, total_length))) == 2

    while len(found_cycles) != 1:
        model.optimize(max_seconds=300)

        arcs = [(i, j) for i in range(total_length)
                for j in range(total_length) if x[i][j].x >= 0.99]
        best_distance = calculate_total_dist_by_arcs(matrix_of_distances, arcs)

        found_cycles = get_cycle(arcs)

        for cycle in found_cycles:
            points = {}
            for arc in cycle:
                points = {*points, arc[0]}
                points = {*points, arc[1]}
            cycle_len = len(cycle)
            model += xsum(x[arc[0]][arc[1]]
                          for arc in permutations(points, 2)) <= cycle_len - 1

        # plot_connected_tsp_points_from_arcs(tsp_matrix, arcs, '../images/mip_xql662/{}'.format(iteration))
        print(iteration)
        iteration += 1

    time_diff = time() - start
    return arcs, time_diff, best_distance
コード例 #3
0
def test_tsp_mipstart(solver: str):
    """tsp related tests"""
    announce_test("TSP - MIPStart", solver)
    N = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    n = len(N)
    i0 = N[0]

    A = {
        ('a', 'd'): 56,
        ('d', 'a'): 67,
        ('a', 'b'): 49,
        ('b', 'a'): 50,
        ('d', 'b'): 39,
        ('b', 'd'): 37,
        ('c', 'f'): 35,
        ('f', 'c'): 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
    }

    # input and output arcs per node
    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(solver_name=solver)
    m.verbose = 0

    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 i in N:
        m += xsum(x[a] for a in Aout[i]) == 1, 'out({})'.format(i)
        m += xsum(x[a] for a in Ain[i]) == 1, 'in({})'.format(i)

    # continuous variable to prevent subtours: each
    # city will have a different "identifier" in the planned route
    y = {i: m.add_var(name='y({})'.format(i), lb=0.0) for i in N}

    # subtour elimination
    for (i, j) in A:
        if i0 not in [i, j]:
            m.add_constr(y[i] - (n + 1) * x[(i, j)] >= y[j] - n)

    route = ['a', 'g', 'f', 'c', 'd', 'e', 'b', 'a']
    m.start = [(x[route[i - 1], route[i]], 1.0) for i in range(1, len(route))]
    m.optimize()

    check_result("mip model status", m.status == OptimizationStatus.OPTIMAL)
    check_result("mip model objective",
                 (abs(m.objective_value - 262)) <= 0.0001)
    print('')
コード例 #4
0
ファイル: tsp-lazy.py プロジェクト: tommyod/python-mip
print('solving TSP with {} cities'.format(len(N)))

model = Model()

# binary variables indicating if arc (i,j) is used on the route or not
x = {
    a: model.add_var('x({},{})'.format(a[0], a[1]), var_type=BINARY)
    for a in A
}

# continuous variable to prevent subtours: each
# city will have a different "identifier" in the planned route
y = {i: model.add_var(name='y({})') for i in N}

# objective function: minimize the distance
model.objective = minimize(xsum(A[a] * x[a] for a in A))

# constraint : enter each city coming from another city
for i in N:
    model += xsum(x[a] for a in OUT[i]) == 1

# constraint : leave each city coming from another city
for i in N:
    model += xsum(x[a] for a in IN[i]) == 1

# no subtours of size 2
for a in A:
    if (a[1], a[0]) in A.keys():
        model += x[a] + x[a[1], a[0]] <= 1

# computing farthest point for each point
コード例 #5
0
def cg():
    """
    Simple column generation implementation for a Cutting Stock Problem
    """

    L = 250  # bar length
    m = 4  # number of requests
    w = [187, 119, 74, 90]  # size of each item
    b = [1, 2, 2, 1]  # demand for each item

    # creating models and auxiliary lists
    master = Model()
    lambdas = []
    constraints = []

    # creating an initial pattern (which cut one item per bar)
    # to provide the restricted master problem with a feasible solution
    for i in range(m):
        lambdas.append(master.add_var(obj=1, name='lambda_%d' %
                                      (len(lambdas) + 1)))

    # creating constraints
    for i in range(m):
        constraints.append(master.add_constr(lambdas[i] >= b[i], 
                                             name='i_%d' % (i + 1)))

    # creating the pricing problem
    pricing = Model(SOLVER)

    # creating pricing variables
    a = []
    for i in range(m):
        a.append(pricing.add_var(obj=0, var_type=INTEGER, name='a_%d' % (i + 1)))

    # creating pricing constraint
    pricing.add_constr(xsum(w[i] * a[i] for i in range(m)) <= L, 'bar_length')

    pricing.write('pricing.lp')

    new_vars = True
    while new_vars:

        ##########
        # STEP 1: solving restricted master problem
        ##########

        master.optimize()

        # printing dual values
        print_solution(master)
        print('pi = ', end='')
        print([constraints[i].pi for i in range(m)])
        print('')

        ##########
        # STEP 2: updating pricing objective with dual values from master
        ##########

        pricing.objective = 1
        for i in range(m):
            a[i].obj = -constraints[i].pi

        # solving pricing problem
        pricing.optimize()

        # printing pricing solution
        z_val = pricing.objective_value
        print('Pricing:')
        print('    z =  {z_val}'.format(**locals()))
        print('    a = ', end='')
        print([v.x for v in pricing.vars])
        print('')

        ##########
        # STEP 3: adding the new columns
        ##########

        # checking if columns with negative reduced cost were produced and
        # adding them into the restricted master problem
        if 1 + pricing.objective_value < - EPS:
            coeffs = [a[i].x for i in range(m)]
            column = Column(constraints, coeffs)
            lambdas.append(master.add_var(obj=1, column=column, name='lambda_%d' % (len(lambdas) + 1)))

            print('new pattern = {coeffs}'.format(**locals()))

        # if no column with negative reduced cost was produced, then linear
        # relaxation of the restricted master problem is solved
        else:
            new_vars = False

        pricing.write('pricing.lp')

    print_solution(master)
コード例 #6
0
print('solving TSP with {} cities'.format(len(N)))

model = Model()

# binary variables indicating if arc (i,j) is used on the route or not
x = {
    a: model.add_var('x({},{})'.format(a[0], a[1]), var_type=BINARY)
    for a in A.keys()
}

# continuous variable to prevent subtours: each
# city will have a different "identifier" in the planned route
y = {i: model.add_var(name='y({})') for i in N}

# objective function: minimize the distance
model.objective = minimize(xsum(A[a] * x[a] for a in A.keys()))

# constraint : enter each city coming from another city
for i in N:
    model += xsum(x[a] for a in OUT[i]) == 1

# constraint : leave each city coming from another city
for i in N:
    model += xsum(x[a] for a in IN[i]) == 1

# subtour elimination
for (i, j) in [a for a in A.keys() if n0 not in [a[0], a[1]]]:
    model += \
        y[i] - (n+1)*x[(i, j)] >= y[j]-n, 'noSub({},{})'.format(i, j)

print('model has {} variables, {} of which are integral and {} rows'.format(
コード例 #7
0
ファイル: jssp.py プロジェクト: tekgrizzly/python-mip
inst = JSSPInstance(argv[1])
n, m, machines, times, M = inst.n, inst.m, inst.machines, inst.times, inst.M

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]]

model.optimize()
コード例 #8
0
ファイル: knapsack.py プロジェクト: s-c-e/python-mip
"""0/1 Knapsack example"""

from mip.model import Model, xsum, maximize
from mip.constants import BINARY

p = [10, 13, 18, 31, 7, 15]
w = [11, 15, 20, 35, 10, 33]
c = 47
n = len(w)

m = Model('knapsack')

x = [m.add_var(var_type=BINARY) for i in range(n)]

m.objective = maximize(xsum(p[i] * x[i] for i in range(n)))

m += xsum(w[i] * x[i] for i in range(n)) <= c

m.optimize()

selected = [i for i in range(n) if x[i].x >= 0.99]
print('selected items: {}'.format(selected))
コード例 #9
0
ファイル: rcpsp.py プロジェクト: s-c-e/python-mip
     [5, 2], [2, 5], [0, 0]]

c = [6, 8]

S = [[0, 1], [0, 2], [0, 3], [1, 4], [1, 5], [2, 9], [2, 10], [3, 8], [4, 6],
     [4, 7], [5, 9], [5, 10], [6, 8], [6, 9], [7, 8], [8, 11], [9, 11],
     [10, 11]]

(R, J, T) = (range(len(c)), range(len(p)), range(sum(p)))

model = Model()

x = [[model.add_var(name='x({},{})'.format(j, t), var_type=BINARY) for t in T]
     for j in J]

model.objective = xsum(x[len(J) - 1][t] * t for t in T)

for j in J:
    model += xsum(x[j][t] for t in T) == 1

for (r, t) in product(R, T):
    model += xsum(u[j][r] * x[j][t2] for j in J
                  for t2 in range(max(0, t - p[j] + 1), t + 1)) <= c[r]

for (j, s) in S:
    model += xsum(t * x[s][t] - t * x[j][t] for t in T) >= p[j]

model.optimize()

print('Schedule: ')
for (j, t) in product(J, T):
コード例 #10
0
import bmcp_greedy
from mip.model import Model, xsum, minimize
from mip.constants import MINIMIZE, BINARY

data = bmcp_data.read('P1.col')
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)]

m = Model(sense=MINIMIZE)

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]
コード例 #11
0
            model.add_cut(cut)
        return


inst = TSPData(argv[1])
n, d = inst.n, inst.d

model = Model()

x = [[
    model.add_var(name='x({},{})'.format(i, j), var_type=BINARY)
    for j in range(n)
] for i in range(n)]
y = [model.add_var(name='y({})'.format(i), lb=0.0, ub=n) for i in range(n)]

model.objective = xsum(d[i][j] * x[i][j] for j in range(n) for i in range(n))

for i in range(n):
    model += xsum(x[j][i] for j in range(n) if j != i) == 1
    model += xsum(x[i][j] for j in range(n) if j != i) == 1
for (i, j) in [(i, j) for (i, j) in product(range(1, n), range(1, n))
               if i != j]:
    model += y[i] - (n + 1) * x[i][j] >= y[j] - n

F = []
for i in range(n):
    (md, dp) = (0, -1)
    for j in [k for k in range(n) if k != i]:
        if d[i][j] > md:
            (md, dp) = (d[i][j], j)
    F.append((i, dp))