Errors in Simulated Annealing (simulannealbnd) algorithm.

Hi guys!
I have a problem applying this optimization algorithm to my function.
My function, basically, is a Forex Trading algorithm that takes as input a 10-parameter X vector which must be optimized through simulated annealing, in fact the data on which to perform the trading analysis are passed to the program as global variables .
Once I try to perform the optimization I get this series of errors that I can't understand the source of the problem in order to solve them.
I would be very grateful if you would help me with this.

 Accepted Answer

Giuseppe - the error message is telling you that the code in your ForexFun is NOT using positive integers or logical values when indexing into arrays at the line
if askW(idx)>askW(idx-X(9)) + min(MFL(idx-1),MFL(idx-2))
Presumably idx is an integer and that askW and MFL are arrays (or is one a function?). Since X is the ten element array and you are subtracting the ninth element from idx, what guarantees are there that idx - X(9) is a positive integer? Are you assuming that this is the case or maybe you need to round up to the nearest integer? Or maybe the error is with something else in this line. I recommend that you put a break point at this l ine or add some logging (via fprintf) to write out the indices before this line of code so that you can get an idea of what is happening.

11 Comments

Hi!
Thank you for your interest, I appreciate it very much.
Could you precisely give me the command to print the index before and after the error, please?
Could you please also indicate a link or some other source to better understand how to debug a procedure?
I will really appreciate that, Thank you.
Giuseppe - see Debug a MATLAB program for details on debugging. For printing your indices, I would add this line before the if statement:
fprintf('My indices are: idx=%f, idx-X(9)=%f, idx-1=%f, idx-2=%f', idx, idx-X(9), idx-1, idx-2);
if askW(idx)>askW(idx-X(9)) + min(MFL(idx-1),MFL(idx-2))
% etc.
end
Yes, thank you, the result to your instruction is:
My indices are: idx=7.000000, idx-X(9)=2.000000, idx-1=6.000000, idx-2=5.000000
My indices are: idx=7.709432, idx-X(9)=2.861608, idx-1=6.709432, idx-2=5.709432
The first row of indices is correct, performing the calculation manually is exactly what it should be.
But, I don't know why the second row of indexes also appears and every time I run, the first row remains the same while the second row is different from the previous one.
Actually the second row of indexes shouldn't appear ... I think that's the problem where the procedure hangs.
What do you think about it?
You would need to show how idx is initialized. What line or lines of code do that? Can you copy and paste your ForexFun function here?
So X is the input to your ForexFun and that is an array produced (on each iteration) by the simulated annealing algorithm. In your main function, you are probably passing in the initial array x0 which may or may not be all integers (unfortunately my version of MATLAB can't read the mlx file) so I can't verify that. Here is the code where you initialize the idx0.
idx0=2+max(X(9),X(10));
Long=0;
Short=0;
for id2=idx0:length(askW)
if and(Long==0,Short==0)
fprintf('My indices are: id2=%f, id2-X(9)=%f, id2-1=%f, id2-2=%f', id2, id2-X(9), id2-1, id2-2);
if askW(id2)>askW(id2-X(9))+min(MFL(id2-1),MFL(id2-2))
Note how you use the maximun of the ninth and tenth elements of X. What guarantee is there that this is a integer? I suspect that the first time this function is called, that the maximum value is 5 (which is the ninth element of X) so that when we add two to it we get 7, subtract X(9) we get 5, etc.
My indices are: idx=7.000000, idx-X(9)=2.000000, idx-1=6.000000, idx-2=5.000000
On the second iteration, the inputs are not integers so the maximum value is 5.709432 (probably X(10)), then we subract X(9) to get 2.861608, etc.
My indices are: idx=7.709432, idx-X(9)=2.861608, idx-1=6.709432, idx-2=5.709432
And so because idx is not an integer, then we observe the error. I think that you need to determine how the code should handle these values. What does the 10-parameter X vector represent? Do non-integer values make sense? Does it make sense to use these values as indices into an array?
The vector X contains the fundamental parameters to ensure that the forex trading trategy is implemented.
In vector X the first 8 components are real and continuous numbers, instead the last 2 components must be whole and discrete numbers.
Is there a way to impose this so that the optimization procedure once it reads the lower bound and the upper bound can understand that X (9) and X (10) must be whole numbers?
Here is the code of the main.mix file:
%MAIN
%input X=[pL qL pS qS DeltaStopL DeltaTargetL DeltaStopS DeltaTargetS DeltaL DeltaS]
clear all
close all
clc
%Optimize the X vector on the first window of data and call the ForexFun with Xnew vector
%What changes?
global askW bidW
f=@ForexFun;
X=[16 1 1 1 16 1 5 5 5 5];
l=[2,0,0,0,2,0,1,1,1,1];
u=[30,1,1,1,30,1,10,10,10,10];
datiUniti=readtable('datiUniti.xlsx');
datiUniti.Properties.VariableNames{1} = 'ask';
datiUniti.Properties.VariableNames{2} = 'bid';
ask=datiUniti.ask;
bid=datiUniti.bid;
profitto=[];
M=floor(length(ask)/90);
for idx=1:90:90 %91:90:180 1:90:M*90 take the first window of data
askW=ask(idx:idx+89); %in the first window you have 90 ask and 90 bid
bidW=bid(idx:idx+89);
%simulated annealing to optimize X
[Xnew,fval,exitflag]=simulannealbnd(f,X,l,u);
ProfittoTOT = ForexFun(X); %to call the function delet %
profitto=[profitto ProfittoTOT];
end
%guadagnoF=sum(profitto);
It isn't clear to me from the simulated annealing options if you can "force" some of the variables to be integers. You may need to add to round (up or down?) the last two values of the X array to ensure that they are integers and are valid as array indices.
I get it, I'll try to round values of interest to not to create problems in the simulation.
Thank you so much for your interest in my problem, I appreciate it very much. I appreciate your commitment to helping me understand the various problems, surely now I can proceed safely.
Thank you!
Glad that it worked out! It's been years since I've thought about simulated annearling. :)
Yes, in my simulation your solution is acceptable!
I think this is the only way to "impose" integer variables in simulated annealing.

Sign in to comment.

More Answers (0)

Asked:

on 25 May 2020

Commented:

on 28 May 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!