3

I have written my code in python3 and solved it using Gekko solver.

After 10000 iterations, I am getting the error maximum iteration reached and solution not found. So can I get the value of decision variables after the 10000th iteration? I mean even when the maximum iteration is reached the solver must have a value of decision variable in the last iteration. so I want to access that values. how can I do that?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

3

You can access the solution by setting debug=0 when you call solve:

m.solve(debug=0)

The default is debug=1 that throws an exception when there is an error such as reaching the maximum number of iterations. Here is an example that normally solves in 5 iterations. With m.options.max_iter=4, it reaches the maximum iterations before a successful solution is reached.

from gekko import GEKKO
m = GEKKO(remote=False) # create GEKKO model
x = m.Var(value=0)      # define new variable, initial value=0
y = m.Var(value=1)      # define new variable, initial value=1
m.Equations([x + 2*y==0, x**2+y**2==1]) # equations

m.options.MAX_ITER=4
m.solve(debug=0)        # solve

print([x.value[0],y.value[0]]) # print solution

With debug=0, the incorrect answer at iteration 4 is returned where the solver almost completed the solution.

[-0.89473125735, 0.44736562868]

I recommend this article if you need suggestions on initializing your problem to help you find a successful solution:

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • I am having a couple of questions more.  1) I am solving an MINLP problem with APOPT Solver. And my decision variables are defined as integer. I have retrieved the result of 10,000th iteration as you suggested. but the Decision variables values are non-integer. So why APOPT Solver is calculating a non-integer solution? 2) I am running the code for "m.options.MAX_ITER=100" and using m = GEKKO() i.e. using remote server. But my code is still running for 10000th iterations. So is there any issue or bug with Gekko library or I am making some coding mistake?thank you in advance. – kinshuk agrawal Apr 13 '20 at 17:40
  • There is an option on what is classified as an integer. The default tolerance is any number within 0.05 of an integer value. If it reached the maximum (10000 iterations) then it likely isn't converged. Does the iteration summary report that any integer solutions were found? There is more information on how APOPT does branch and bound here: https://apmonitor.com/wiki/index.php/Main/IntegerBinaryVariables and https://apmonitor.com/me575/index.php/Main/DiscreteOptimization – John Hedengren Apr 14 '20 at 04:42
0

Question: 1) I am solving an MINLP problem with APOPT Solver. And my decision variables are defined as integer. I have retrieved the result of 10,000th iteration as you suggested. but the Decision variables values are non-integer. So why APOPT Solver is calculating a non-integer solution?

Answer: There is an option on what is classified as an integer. The default tolerance is any number within 0.05 of an integer value. you can change this by: m.solver_options = ['minlp_integer_tol 1']

2) I am running the code for "m.options.MAX_ITER=100" and using m = GEKKO() i.e. using remote server. But my code is still running for 10000th iterations.

Answer: Can do it alternatively by: m.solver_options = ['minlp_maximum_iterations 100']

Thanks a lot to Prof. John Hedengren for the prompt replies.

Gekko