Error in simulated annealing --- "your objective function must return a scalar value"
이전 댓글 표시
Hi All,
I want to minimize a function using simulated annealing tool of matlab. Here is the matlab file that I used -
clear, close all
fid = fopen('test.dat');
C = textscan(fid, '%s %f');
fclose(fid);
protname = C{1};
slopedata = C{2};
for i=1:length(slopedata)
fn = sprintf('inte_%s.dat',protname{i});
Evals{i} = load(fn);
end
Nwt = 22;
EE=zeros([Nwt length(slopedata)]);
pE=zeros([Nwt length(slopedata)]);
for i=1:length(slopedata)
[~, EE(:,i), pE(:,i)] = gethist(Evals{i},Nwt);
end
wts0 = -exp((1:Nwt)/10)'; % initial weighting function
options = saoptimset('simulannealbnd');
options = saoptimset(options,'Display','iter','TolFun',1e-12,'MaxFunEvals',2000);
deltaslope = @(wts) slope_functn(wts,EE,pE,slopedata);
[wts, resnorm, residual, exitflag] = simulannealbnd(deltaslope,wts0,[],[],options);
slope_pred = slopedata - residual;
[rr , pp] = corr(slope_pred,slopedata)
plot(slope_pred,slopedata,'ks'), hold on
axis equal,
title('slopes from data and model prediction'),
xlabel('slope-predicted'),
ylabel('slope-data'),
hold off
figure,
h=bar(1:Nwt,wts), set(h,'FaceColor',[.5 .5 .5],'EdgeColor','k'), hold on,
plot(1:Nwt,wts0,'.-'),
plot(1:Nwt,zeros(Nwt,1),'--k'),
xlabel('E (scaled)'),
title('Initial (blue) and final weighting functions in \int w(E) E P(E) dE')
hold off
And the error that I got is
??? Error using ==> samakedata at 30
Your objective function must return a scalar value.
Error in ==> simulannealcommon at 113
solverData = samakedata(solverData,problem,options);
Error in ==> simulanneal at 44
[x,fval,exitflag,output,solverData,problem,options] = ...
Error in ==> simulannealbnd at 122
[x, fval, exitflag, output] = simulanneal(FUN, x0, [], [], [], [], lb, ub,
options);
Error in ==> simanneal at 52
[wts, resnorm, residual, exitflag] =
simulannealbnd(deltaslope,wts0,[],[],options);
Error in ==> run at 74
evalin('caller',[script ';']);
Could anyone please suggets me what should I do?
Thanks in advance
답변 (1개)
Walter Roberson
2011년 4월 11일
0 개 추천
You should show us the code for slope_functn as that is the function that will need to return a scalar value.
댓글 수: 13
Atanu
2011년 4월 13일
Walter Roberson
2011년 4월 13일
The documentation is clear,
http://www.mathworks.com/help/toolbox/gads/simulannealbnd.html
x = simulannealbnd(fun,x0) starts at x0 and finds a local minimum x to the objective function specified by the function handle fun. The objective function accepts input x and returns a scalar function value evaluated at x. x0 may be a scalar or a vector.
However, the value returned by your objective function is deltaslope, which in your function is clearly the same length as slopedata rather than being a scalar.
Atanu
2011년 4월 13일
Walter Roberson
2011년 4월 13일
http://www.mathworks.com/help/toolbox/gads/bq2g2yi-3.html
=== begin quote ===
At each iteration of the simulated annealing algorithm, a new point is randomly generated. The distance of the new point from the current point, or the extent of the search, is based on a probability distribution with a scale proportional to the temperature. The algorithm accepts all new points that lower the objective, but also, with a certain probability, points that raise the objective. [...]
=== end quote ===
The fun you are providing is the objective function in the above terms. It will be evaluated at one specific multidimensional point at a time, and it has to return the "energy" (function value) associated with that multidimensional point. The objective function value is NOT anything like a jacobian where a multidimensional "direction" is to be returned, just a single value.
Atanu
2011년 4월 13일
Walter Roberson
2011년 4월 13일
You haven't described what it is you are trying to minimize.
Atanu
2011년 4월 13일
Walter Roberson
2011년 4월 13일
Your deltaslope that is returned is a vector. What does it mean to you to minimize a vector? Are you looking to minimize the magnitude of the vector, sqrt(sum(v.^2)), or are you looking to minimize sum(abs(v)), or to minimize min(v), or to minimize max(v), or something else?
Note that in all of the above possibilities, you could return the described quantity derived from the deltaslope that you calculate and simulated annealing would then automatically be minimizing for your choice of metrics.
Atanu
2011년 4월 14일
Walter Roberson
2011년 4월 14일
At the end of your slope_functn put
deltaslope = sqrt(dot(deltaslope,deltaslope));
Atanu
2011년 4월 18일
shide agani
2015년 11월 22일
I had the same problem and it works, really thanks
pratik gautam
2020년 6월 5일
deltaslope = sum(deltaslope); %no need to take sqrt() for optimization purposes
카테고리
도움말 센터 및 File Exchange에서 Simulated Annealing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!