Beispiel #1
0
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
Beispiel #2
0
    def constructProblem(self):
        self.instance.print()
        self.model = Model('flow')
        self.fIdx = [[
            self.model.add_var('f({},{})'.format(i + 1, t), var_type='B')
            for i in range(self.instance.m())
        ] for t in range(self.instance.h())]
        self.xIdx = [[[
            self.model.add_var('x({},{},{})'.format(i + 1, j + 1, t))
            for t in range(self.instance.est(i, j),
                           self.instance.lst(i, j) + 1)
        ] for i in range(self.instance.m())] for j in range(self.instance.n())]
        self.eIdx = [[[
            self.model.add_var('e({},{},{})'.format(i + 1, j + 1, t))
            for t in range(self.instance.est(i, j),
                           self.instance.lst(i, j) + 1)
        ] for i in range(self.instance.m())] for j in range(self.instance.n())]
        self.cIdx = self.model.add_var('C', var_type='I')

        for aux in self.fIdx:
            for f in aux:
                print(f.name, " ", end='')
            print()
        for aux in self.xIdx:
            for aux2 in aux:
                for x in aux2:
                    print(x.name, " ", end='')
                print()
        for aux in self.xIdx:
            for aux2 in aux:
                for e in aux2:
                    print(e.name, " ", end='')
                print()
Beispiel #3
0
def kantorovich():
    """
    Simple implementation of the compact formulation from Kantorovich for the problem
    """

    N = 10  # maximum number of bars
    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 the model (note that the linear relaxation is solved)
    model = Model(SOLVER)
    x = {(i, j): model.add_var(obj=0, var_type=CONTINUOUS, name="x[%d,%d]" % (i, j)) for i in range(m) for j in range(N)}
    y = {j: model.add_var(obj=1, var_type=CONTINUOUS, name="y[%d]" % j) for j in range(N)}

    # constraints
    for i in range(m):
        model.add_constr(xsum(x[i, j] for j in range(N)) >= b[i])
        for j in range(N):
            model.add_constr(xsum(w[i] * x[i, j] for i in range(m)) <= L * y[j])

    # additional constraint to reduce symmetry
    for j in range(1, N):
        model.add_constr(y[j - 1] >= y[j])

    # optimizing the model and printing solution
    model.optimize()
    print_solution(model)
Beispiel #4
0
    def __init__(self, generators, demand):

        self.generators = generators
        self.demand = demand

        self.period = range(1, len(self.demand) + 1)

        self.model = Model(name='UnitCommitment')
        self.p, self.u = {}, {}
Beispiel #5
0
def test_queens(solver: str):
    """MIP model n-queens"""
    n = 50
    announce_test("n-Queens", solver)

    queens = Model('queens', MAXIMIZE, solver_name=solver)
    queens.verbose = 0

    x = [[
        queens.add_var('x({},{})'.format(i, j), var_type=BINARY)
        for j in range(n)
    ] for i in range(n)]

    # one per row
    for i in range(n):
        queens += xsum(x[i][j] for j in range(n)) == 1, 'row({})'.format(i)

    # one per column
    for j in range(n):
        queens += xsum(x[i][j] for i in range(n)) == 1, 'col({})'.format(j)

    # diagonal \
    for p, k in enumerate(range(2 - n, n - 2 + 1)):
        queens += xsum(x[i][j] for i in range(n) for j in range(n)
                       if i - j == k) <= 1, 'diag1({})'.format(p)

    # diagonal /
    for p, k in enumerate(range(3, n + n)):
        queens += xsum(x[i][j] for i in range(n) for j in range(n)
                       if i + j == k) <= 1, 'diag2({})'.format(p)

    queens.optimize()

    check_result("model status", queens.status == OptimizationStatus.OPTIMAL)

    # querying problem variables and checking opt results
    total_queens = 0
    for v in queens.vars:
        # basic integrality test
        assert v.x <= 0.0001 or v.x >= 0.9999
        total_queens += v.x

    # solution feasibility
    rows_with_queens = 0
    for i in range(n):
        if abs(sum(x[i][j].x for j in range(n)) - 1) <= 0.001:
            rows_with_queens += 1

    check_result("feasible solution",
                 abs(total_queens - n) <= 0.001 and rows_with_queens == n)
    print('')
Beispiel #6
0
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
Beispiel #7
0
def get_pricing(m, w, L):
    # creating the pricing problem
    pricing = Model()

    # 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')

    return a, pricing
Beispiel #8
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('')
Beispiel #9
0
from tspdata import TSPData
from sys import argv
from mip.model import Model
from mip.constants import *
from matplotlib.pyplot import plot

if len(argv) <= 1:
    print('enter instance name.')
    exit(1)

inst = TSPData(argv[1])
n = inst.n
d = inst.d
print('solving TSP with {} cities'.format(inst.n))

model = Model()

# binary variables indicating if arc (i,j) is used on the route or not
x = [[model.add_var(type=BINARY) for j in range(n)] for i in range(n)]

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

# objective funtion: minimize the distance
model += sum(d[i][j] * x[i][j] for j in range(n) for i in range(n))

# constraint : enter each city coming from another city
for i in range(n):
    model += sum(x[j][i] for j in range(n)
                 if j != i) == 1, 'enter({})'.format(i)
Beispiel #10
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)
Beispiel #11
0
            cut = xsum(x[i, j] for i in range(n)
                       for j in range(n) if i - j == k) <= 1
            if cut.violation > 0.001:
                model.add_cut(cut)

        for p, k in enumerate(range(3, n + n)):
            cut = xsum(x[i, j] for i in range(n)
                       for j in range(n) if i + j == k) <= 1
            if cut.violation > 0.001:
                model.add_cut(cut)


# number of queens
n = 60

queens = Model('queens', MAXIMIZE)

x = [[
    queens.add_var('x({},{})'.format(i, j), var_type=BINARY) for j in range(n)
] for i in range(n)]

# one per row
for i in range(n):
    queens += xsum(x[i][j] for j in range(n)) == 1, 'row({})'.format(i)

# one per column
for j in range(n):
    queens += xsum(x[i][j] for i in range(n)) == 1, 'col({})'.format(j)

queens.cuts_generator = DiagonalCutGenerator()
queens.cuts_generator.lazy_constraints = True
Beispiel #12
0
"""Job Shop Scheduling Problem Python-MIP exaxmple
   To execute it on the example instance ft03.jssp call
   python jssp.py ft03.jssp
   by Victor Silva"""

from itertools import product
from sys import argv
from jssp_instance import JSSPInstance
from mip.model import Model
from mip.constants import BINARY

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]]
Beispiel #13
0
"""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))
Beispiel #14
0
   to solve P1 instance (included in the examples) call python bmcp.py P1.col
"""

from itertools import product
import bmcp_data
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):