For loop with globalsolve terminates after timelimit is reached

In order to optimize a large number of datasets I integrated the GlobalSolve command from the OptimizationToolbox
in a for loop. The code (in short form) looks like this:

for i from  1 to 100 do
data(i):= "read data from external file":
Z:= "function to optimize based on data(i)":
with(GlobalOptimization):
infolevel(GlobalOptimization):=4
B:=GlobalSolve(Z, a=0..2, b=0..2, c=0..2, timelimit=6000):
->Solution of optimization is exported to external file
end do:

Unfortnuately the optimization procedure does not always retrun a solution within the given timelimit. Whenever this
happens, the for loop is exited and the calculation is stopped (although i has not reached 100). I tried to add the GetLastSolution()-Command to avoid the error and added

GetLastSolution()

after "B:=GlobalSolve(Z, a=0..2, b=0..2, c=0..2, timelimit=6000)" but this does not solve the problem, because the code
is halted before GetLastSolution is executed. Does anybody know how I can use the GetLastSolution-Command (or something
else) so that the loop is not stopped?

 

Axel Vogt's picture

Exception Handling - the try Statement

It has nothing to do with the package, perhaps you want the following code snippet:
  endless:=proc() while true do end do end proc; # endless waiting loop
  for i from 1 to 3 do
    try
      timelimit( 0.5, endless());
    catch "time expired":
      print(i, "can not wait"); #print(lastexception);
    finally
      print("going ahead ...", exp(i));
    end try;
  ``; # prints a blank line 
  end do;
                          1, "can not wait"
                      "going ahead ...", exp(1)

                          2, "can not wait"
                      "going ahead ...", exp(2)

                          3, "can not wait"
                      "going ahead ...", exp(3)

Hallo Axel, I changed the

Hallo Axel,

I changed the code to:

for i from  1 to 100 do
data(i):= "read data from external file":
Z:= "function to optimize based on data(i)":
with(GlobalOptimization):
infolevel(GlobalOptimization):=4
try
B:=GlobalSolve(Z, a=0..2, b=0..2, c=0..2, timelimit=6000):
catch:
B:=GetLastSolution();
end try;

solutionvector:=matrix(1,3) #vector containing calculated values for 3 variables)
solutionvector:=[eval(a,B[2]),eval(b,B[2]),eval(c,B[2])];

end do:

But now I can no longer evaluate the solution vector (which made no problems before adding the try-sequence). The error code is:

Error, invalid input: eval expects its 2nd argument to be of type (iteger, equation, set(equation)), but recieved B[2].
Obviously the catch-procedure does not return values in the same format as before.  Do you have any idea how I can solve this?
Many thanks for your help.
Frank

Axel Vogt's picture

you have to look closer

you have to look closer into try .. catch - if the procedure fails, you have to skip it ... it needs some patients to use it, try with simple examples first ... so it is only a sketch what I have posted

for your actual problem I would use timelimit = 2 sec (!) or less and 'i' only up to 3 or so, so see what happens (it will fail) and how you handle the case of failings

anyway: I do not have the Toolbox ...

Edited to add an example

  semiendless:=proc(n) while type(n,'even') do end do; return(n); end proc;
  # endless waiting loop if n is even, otherwise return input
  for i from 0 to 5 do
    try
      k:=timelimit( 0.5, semiendless(i));
    catch: #"time expired":
      print(i, "can not wait"); # error processing
      next
    finally
      # do nothing 
    end try;
  print(`this is ok, result`=[i,k^2]); # do it here
  end do;
 
                         0, "can not wait"
                     this is ok, result = [1, 1]
                          2, "can not wait"
                     this is ok, result = [3, 9]
                          4, "can not wait"
                     this is ok, result = [5, 25]
This way you can treat the good and bad results.

 

Hallo Axel, I changed the

Hallo Axel,

I changed the code to:

for i from  1 to 100 do
data(i):= "read data from external file":
Z:= "function to optimize based on data(i)":
with(GlobalOptimization):
infolevel(GlobalOptimization):=4
try
B:=GlobalSolve(Z, a=0..2, b=0..2, c=0..2, timelimit=6000):
catch:
B:=GetLastSolution();
end try;

solutionvector:=matrix(1,3) #vector containing calculated values for 3 variables)
solutionvector:=[eval(a,B[2]),eval(b,B[2]),eval(c,B[2])];

end do:

But now I can no longer evaluate the solution vector (which made no problems before adding the try-sequence). The error code is:

Error, invalid input: eval expects its 2nd argument to be of type (iteger, equation, set(equation)), but recieved B[2].
Obviously the catch-procedure does not return values in the same format as before.  Do you have any idea how I can solve this?
Many thanks for your help.
Frank

I solved the problem.

I solved the problem.

try
B:=GlobalSolve(Z, a=0..2, b=0..2, c=0..2):
solutionvector:=matrix(1,3):
solutionvector:=[eval(a,B[2]),eval(b,B[2]),eval(c,B[2])]:
catch:
B:=GetLastSolution():
solutionvector:=matrix(1,3):
solutionvector:=[eval(B[2,1]),eval(B[2,2]),eval(B[2,3])];
end try;

Thank you very much.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}