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

Answers (1)

You should show us the code for slope_functn as that is the function that will need to return a scalar value.

13 Comments

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
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.
Ok... So what should I do? Do you mean that simulated annealing cannot be done on this function?
Thanks
Atanu
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.
Then can you send me a better code for my problem?
You haven't described what it is you are trying to minimize.
I am trying to minimize the deltaslope which is described in slope_functn.
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.
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?
At the end of your slope_functn put
deltaslope = sqrt(dot(deltaslope,deltaslope));
I had the same problem and it works, really thanks
deltaslope = sum(deltaslope); %no need to take sqrt() for optimization purposes

Sign in to comment.

Asked:

on 11 Apr 2011

Commented:

on 5 Jun 2020

Community Treasure Hunt

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

Start Hunting!