-1

Apopt Gekko solves but doesn't optimize binary list with constraints.

Anyone ever got into it?

I got a code that takes 2 tables froem excel and try to optimize containers over trucks with several constraints. It seems to work fine with the constraints but I can see very easily that it doesn't optimize the container loading on the trucks...

any ideas?

import numpy as np
from gekko import GEKKO

containers = [[1, 10, 0, 0, 0, 0, 1], [2, 20, 1, 0, 0, 0, 1], [3, 40, 0, 1, 
0, 0, 2], [4, 40, 0, 0, 0, 0, 3], [5, 10, 1, 0, 0, 0, 4], [6, 20, 0, 0, 0, 0, 
1], [7, 20, 0, 0, 0, 0, 1], [8, 40, 0, 0, 0, 0, 1], [9, 10, 1, 0, 0, 0, 1], 
[10, 10, 0, 0, 0, 0, 1], [11, 40, 0, 0, 0, 0, 1], [12, 40, 1, 0, 0, 0, 1], 
[13, 10, 0, 0, 0, 0, 1], [14, 20, 1, 0, 0, 0, 1], [15, 40, 1, 0, 0, 0, 1], 
[16, 40, 0, 0, 0, 0, 1], [17, 20, 0, 1, 0, 0, 1], [18, 10, 0, 0, 0, 0, 1], 
[19, 20, 0, 0, 0, 0, 1], [20, 10, 0, 0, 0, 0, 1], [21, 40, 0, 0, 0, 0, 1], 
[22, 40, 0, 0, 0, 0, 1], [23, 20, 0, 0, 0, 0, 1], [24, 10, 0, 0, 0, 0, 1], 
[25, 20, 0, 0, 0, 0, 1], [26, 40, 0, 0, 0, 0, 1], [27, 40, 0, 0, 0, 0, 1]]
trucks = [[11, 'D2', 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [22, 'D3', 
40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [33, 'E1', 40, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0], [44, 'D4', 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
[45, 'D5', 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [46, 'D6', 40, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [47, 'D7', 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0], [48, 'D8', 40, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [49, 
'D9', 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [50, 'D10', 40, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0], [51, 'D11', 40, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0], [52, 'D12', 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
m=GEKKO()
m.options.SOLVER = 1
match_matrix = []

#Creating the match matrix vars
for i in range(len(trucks)):
    match_matrix.append([])
    for j in range(len(containers)):
        match_matrix[i].append(m.Var(integer=True,lb=0,ub=1))

#Constraint - total loading size not larger than truck size
for i in range(len(trucks)):
    load_size = 0
    truck_size = trucks[i][2]
    for j in range(len(containers)):
        container_size = containers[j][1]
        load_size = load_size + match_matrix[i][j] * container_size
    m.Equation(load_size<=truck_size)

#Constraint - each container will be loaded once (maximum)
for j in range(len(containers)):
    number_of_loads = 0
    for i in range(len(trucks)):
        number_of_loads += match_matrix[i][j]
    m.Equation(number_of_loads<=1)


#Creating the objective
total_containers_size_on_truck = 0
for i in range(len(trucks)):
    for j in range(len(containers)):
        container_size = containers[j][1]
        total_containers_size_on_truck += match_matrix[i][j] * 
        container_size

#solving
m.Obj(-total_containers_size_on_truck)
m.options.SOLVER = 1
m.solve()


#Printing

for i in range(len(trucks)):
    output = "Truck %d (size: %d):" % (i+1, trucks[i][2])
    for j in range(len(containers)):
        if match_matrix[i][j].value[0] == 1:
            output += "%d(%d), " % (j+1, containers[j][1])
    print (output)

So the solver solves the problem but doesn't maximize maxi..

Any ideas?

  • Thanks for reaching out for help on your problem. Could you post the code that you are working with so that we can help? – John Hedengren Apr 26 '20 at 05:56
  • Thanks for posting your code. Do you have a simple table that could take the place of `trucks.xlsx` so that the code can run and show your issue? You could create a small table that has 3 trucks with loading constraints for each. – John Hedengren Apr 26 '20 at 18:26
  • containers file https://drive.google.com/file/d/1cRrulkLCXjMHvt3dku13hR3FaUqlgFSR/view?usp=sharing trucks file https://drive.google.com/file/d/1BMGMypDZqqFYJ6ldkoK_BnVK4UInwnfA/view?usp=sharing – Cheetaa Tiger Apr 26 '20 at 19:19
  • It would be better if you could create sample data in the original question. I think most people are not going to want to download a document from the Internet, especially with the possibility of macros in Excel. – John Hedengren Apr 29 '20 at 04:37
  • 1
    Thank you for wanting to help! I just edited the post :) – Cheetaa Tiger Apr 29 '20 at 15:34
  • Thanks for including images. Even better is if you can include some matrices at the top of your post similar to https://stackoverflow.com/questions/61469170/gekko-cant-find-solution-of-a-small-problem and https://stackoverflow.com/questions/61109450/trajectory-planner-with-gekko-is-not-able-to-handle-given-goal-velocities Here are some tips for creating a Minimal, Reproducible problem to show the issue that you are facing: https://stackoverflow.com/help/minimal-reproducible-example Otherwise, I'd need to type in the matrix from your image. – John Hedengren Apr 29 '20 at 16:50
  • It doesn't need to be the full matrix, but possibly just for 2-3 trucks to demonstrate what you are dealing with on the problem. – John Hedengren Apr 29 '20 at 16:52
  • 1
    Thanks for helping me learning the business around here..I've just edited the post - Adding the data on the code by lists + minimized the code. Thanks again! – Cheetaa Tiger Apr 29 '20 at 19:28
  • 1
    Also, I've added more than 2-3 trucks and containers in order to demonstrate the problem I am experiencing.. Thanks :) – Cheetaa Tiger Apr 29 '20 at 19:42

1 Answers1

0

You may need to adjust the APOPT solver settings to get a better solution. Here are options for APOPT that I recommend for your problem.

m.solver_options = ['minlp_gap_tol 0.5',\
                    'minlp_maximum_iterations 10000',\
                    'minlp_max_iter_with_int_sol 10000']

With these options, the problem converges in 952 iterations with the following result:

--Integer Solution:  -3.10E+02 Lowest Leaf:  -4.80E+02 Gap:   4.30E-01
Iter:   952 I:  0 Tm:      0.01 NLPi:    2 Dpth:   40 Lvs:  498 Obj: -3.10E+02 Gap:  4.30E-01
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :    20.8784000000014      sec
 Objective      :   -310.000000000000     
 Successful solution
 ---------------------------------------------------

Truck 1 (size: 40):13(10), 17(20), 
Truck 2 (size: 40):3(40), 
Truck 3 (size: 40):5(10), 
Truck 4 (size: 40):26(40), 
Truck 5 (size: 40):10(10), 19(20), 
Truck 6 (size: 40):25(20), 
Truck 7 (size: 40):14(20), 24(10), 
Truck 8 (size: 40):20(10), 
Truck 9 (size: 40):6(20), 9(10), 
Truck 10 (size: 40):23(20), 
Truck 11 (size: 40):2(20), 
Truck 12 (size: 40):1(10), 7(20),

Even with decreasing the MINLP gap tolerance, the solver continues for 10,000 iterations but does not find a better solution. If this isn't the optimal solution, you may try to give it more iterations to find a better solution or else give a better initial guess.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • Thanks for your super quick response! Tried to open it for 100K and unfortunately it still doesn't t work.. You can see it has some 40 ft containers left aside that it won't load for some reason... – Cheetaa Tiger Apr 29 '20 at 20:00
  • Could you explain the output? I'm not sure what I'm looking at in the printed results. – TexasEngineer Apr 29 '20 at 20:25
  • I think the output is Truck # (size: truck size): Container index(amount), Container index(amount) but I'll need @Cheetaa Tiger to confirm. – John Hedengren Apr 29 '20 at 20:46
  • 1
    Its Turck # (Size: total truck size for loading): Container index (container size) Thanks guys – Cheetaa Tiger Apr 29 '20 at 20:49
  • Tried with 100k iterations, did a bit better but still not optimum. The solving time is a bit to slow for me, guess I will be needing to find another sloving engine.. – Cheetaa Tiger Apr 29 '20 at 20:59
  • Good idea to look around. There are also some other options in APOPT that may make it faster. If your problem is Mixed Integer Linear (instead of nonlinear) then you'll see a big improvement with a solver like Gurobi or CPLEX (both commercial solvers). – John Hedengren Apr 30 '20 at 01:27