Errors in Simulated Annealing (simulannealbnd) algorithm.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.

채택된 답변
Geoff Hayes
2020년 5월 25일
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
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
