how to get value of decision variable after maximum iteration limit in gekko

Viewed 711

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?

2 Answers

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:

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

Related