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?
Exception Handling - the try Statement
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;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
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
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.