2

i'm trying to write facility location MIP solution with OR tools. i translated a Scip solution, from here:

https://scipbook.readthedocs.io/en/latest/flp.html but im getting a table with only zeros means a no solution.. is the framing of the problem or/and the way I'm using OR-tools here supposed to work?

    def or_tools_scip_mine(facilities, customers, time_limit=None):

    import numpy
    import datetime

    if time_limit is None:
        time_limit = 1000 * 60  # 1 minute


    solver = pywraplp.Solver.CreateSolver('SCIP')


    customer_count = range(len(customers))
    facility_count = range(len(facilities))
    x =[[] for _ in range(len(customers))]
    y = []
    facility_capacities=[facilities[i][2] for i in facility_count]
    facility_setup_costs = [facilities[i][1] for i in facility_count]
    demands=[customers[i][1] for i in customer_count]
    c=dist_matrix(facilities,customers)

    for j in facility_count:
        y.append(solver.BoolVar("y(%s)" % j))
        for i in customer_count:
            x[i].append(solver.BoolVar("x(%s,%s)" % (i, j)))

    for i in customer_count:
        solver.Add(solver.Sum(x[i][j] for j in facility_count) <= demands[i])#, "Demand(%s)" % i
    for j in facility_count:
        solver.Add(solver.Sum(x[i][j] for i in customer_count) <= facility_capacities[j] * y[j])#, "Capacity(%s)" % j)
    for j in facility_count:
        for i in customer_count:
            solver.Add(x[i][j] <= demands[i] * y[j])
    a=solver.Sum((facility_setup_costs[j] * y[j] for j in facility_count))
    b=solver.Sum((c[i, j] * x[i][j] for i in customer_count for j in facility_count))
    func_=solver.Sum([a,b])
    solver.Minimize(func_)

    solver.set_time_limit(time_limit)
    result_status = solver.Solve()
    print(result_status)
    val = solver.Objective().Value()

    x_val = [[] for _ in range(len(customers))]  
    solution = []
    for j in range(len(facilities)):
        for i in range(len(customers)):
            x_val[i].append(int(x[i][j].solution_value()))
    x_val = numpy.array(x_val)
    for j in range(len(customers)):
        solution.append(numpy.where(x_val[:, j] == 1)[0][0])

    return val, solution

the  Error:
solution.append(numpy.where(x_val[:, j] == 1)[0][0])
IndexError: index 0 is out of bounds for axis 0 with size 0

1 Answers1

1

Add solver.EnableOutput() after CreateSolver line in your code. This will give you more insight what is going on.

Below are the helping solver parameters and status. If you need more details click on this documentation link

    print('Number of variables = %d' % solver.NumVariables())
    print('Number of constraints = %d' % solver.NumConstraints())                
    print('The Optimal Objective value =',solver.Objective().Value())
    print('Total Iterations:',solver.iterations())
    print('Total Nodes:',solver.nodes())
    print('Total number of Variables:',solver.NumVariables())
    print(pywraplp.Solver.FEASIBLE)
    print(pywraplp.Solver.MODEL_INVALID)
    print(pywraplp.Solver.OPTIMAL)
    print(pywraplp.Solver.INFEASIBLE)
    print(pywraplp.Solver.UNBOUNDED)
Chinmay T
  • 745
  • 1
  • 9
  • 17