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
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
Atanu 2011 年 4 月 13 日
Here is the slope_functn -------------------
function deltaslope = slope_functn(wts,EE,pE,slopedata)
slope_pred = zeros([length(slopedata) 1]);
deltaslope = zeros([length(slopedata) 1]);
dE = EE(2,:) - EE(1,:);
for j=1:length(slopedata) % loop over proteins
slope_pred(j) = sum(wts.*EE(:,j).*pE(:,j) ) .* dE(j) ;
deltaslope(j) = slopedata(j) - slope_pred(j);
end
Walter Roberson
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
Atanu 2011 年 4 月 13 日
Ok... So what should I do? Do you mean that simulated annealing cannot be done on this function?
Thanks
Atanu
Walter Roberson
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
Atanu 2011 年 4 月 13 日
Then can you send me a better code for my problem?
Walter Roberson
Walter Roberson 2011 年 4 月 13 日
You haven't described what it is you are trying to minimize.
Atanu
Atanu 2011 年 4 月 13 日
I am trying to minimize the deltaslope which is described in slope_functn.
Walter Roberson
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
Atanu 2011 年 4 月 14 日
Yes. I am trying to minimize the magnitude of the vector. Could you please tell me what should I edit in my matlab file to get the job done?
Walter Roberson
Walter Roberson 2011 年 4 月 14 日
At the end of your slope_functn put
deltaslope = sqrt(dot(deltaslope,deltaslope));
Atanu
Atanu 2011 年 4 月 18 日
It's working. Thanks a lot.
shide agani
shide agani 2015 年 11 月 22 日
I had the same problem and it works, really thanks
pratik gautam
pratik gautam 2020 年 6 月 5 日
deltaslope = sum(deltaslope); %no need to take sqrt() for optimization purposes

サインインしてコメントする。

カテゴリ

質問済み:

2011 年 4 月 11 日

コメント済み:

2020 年 6 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by