Size of GOAL must be equal to the size of F returned by FUN Error

Below is my program that I am atttempting to use goal attainment for a multiobjective problem, but when I run the program I keep getting the error message "size of goal must be equal to the size of F returned by fun". I am unsure why it is saying this as I thought my goal was the correct size. Please help if possible.
The error occurs at this statement, which makes me feel the function isn't being called correctly:
[x,fval] = fgoalattain(@MaxFunctionProblem,x0,goal,weight,[],[],[],[],[],[],@confun,options);
function [history,searchdir] = GoalAttainP1
% Set up shared variables with OUTFUN
history.x = [];
history.fval = [];
searchdir = [];
% call optimization
x0 = [3,2,4,8,10];
goal = [-.04, -.2];
weight = abs(goal);
options = optimset('outputfcn',@outfun,'display','iter',...
'Algorithm','active-set');
[x,fval] = fgoalattain(@MaxFunctionProblem,x0,goal,weight,[],[],[],[],[],[],@confun,options);
function stop = outfun(x,optimValues,state)
stop = false;
switch state
case 'init'
hold on
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; x];
% Concatenate current search direction with
% searchdir.
searchdir = [searchdir;...
optimValues.searchdirection'];
plot(x(1),x(2),'o');
% Label points with iteration number and add title.
% Add .15 to x(1) to separate label from plotted 'o'
text(x(1)+.15,x(2),...
num2str(optimValues.iteration));
title('Sequence of Points Computed by fmincon');
case 'done'
hold off
otherwise
end
end
display(fval)
display(x)
P = x(1)
W = x(2)
U = x(3)
Wprime = x(4)
Uprime = x(5)
%data=round(data*100)/100
%str2num(sprintf('%5.2f',stop))
function [E,F] = MaxFunctionProblem(x)
P = x(1);
W = x(2);
U = x(3);
Wprime = x(4);
Uprime = x(5);
% Function Formulations
% Multiobjective function
Theta0 = .005;
Theta1 = .01;
Theta2 = 0.01;
Theta3 = 0.02;
L = 5;
i = 1.5;
e = 1.2;
v = (0.15*P);
Pprime = 0.2;
m = 0;
n = 6;
r = 0.0001;
vgam = 2;
Gr = 1/(n-m);
%Gr = ((exp(-r))*(r^(vgam-1)))/(gamma(vgam));
D1 = 0.2;
a = 2;
b = 0.2;
d = 0.1;
k = 1;
P1 = 1;
P2 = 6;
W1 = 1;
W2 = 5;
U1 = 1;
U2 = 6;
W3 = 10;
a2 = 0.01;
b2 = 0.005;
g = 0.1;
t = 0;
t2 = 0.0001;
r2 = 0.0001;
Inf = 50;
% Expected Unit Profit
% Unknown variables: P,W,U, Uprime, Unit Price
% P = unit price
% v = unit premium
% Expected Unit Production Costs
LtrEUPC = (Theta0+Theta1)*((m+n)/2);
FuncEct = @(t,L)(a2 + (b2/((LtrEUPC)^i)))*(1-(g*(t)));
ECTf = integral(FuncEct,t,L);
%%%%%%Expected Unit Profit %%%%%%
Lfun = @(t,r)(Theta0+(Theta1*r)+((Theta2+(Theta3*r)).*t)*Gr);
Lfun2 = @(t)((Theta0+(Theta1*r)+((Theta2+(Theta3*r)).*t)));
Sum1 = 0;
%j=1; %place holder for vector storage
L1 = integral2(Lfun,t2,W,r2,(U/W));
Dr = .0001;
for r = (U/W):Inf
% Gr = ((exp(-r))*(r^(vgam-1)))/(gamma(vgam));
Lq3 = integral(Lfun2,t,(U/r));
Sum1 = Sum1 + (Lq3*Gr*Dr);
end
L2 = Sum1;
Cs = 0.05*P;
ExpUnitWarCost = Cs*(L1 + L2);
ExpUnitWarCostwithoutCs = (L1 + L2);
ExpUnitProfit = (P + v) - ECTf - ExpUnitWarCost;
%%%%Expected Unit Warranty Costs with Uprime and Wprime
for r = (Uprime/Wprime):Inf
% Gr = ((exp(-r))*(r^(vgam-1)))/(gamma(vgam));
Lq3 = integral(Lfun2,t,(Uprime/r));
Sum1 = Sum1 + (Lq3*Gr*Dr);
end
L2 = Sum1;
ExpUnitWarCostPrimes = (L1 + L2);
%%%%%%Expected increase in market share %%%%%%
D = D1/((P1^(-a))*((W2 + k)^b)*U2^d);
Q = (D*(P^(-a))*((W + k)^b)*(U^d));
Qprime = (D*(P^(-a))*((Wprime + k)^b)*(((Uprime)^d)*((1 + v)^(-e))));
ExpIncMarketShare = (Pprime*Qprime) + ((1 - Pprime))*Q - Q;
%%%%%%Unit Price %%%%%
UnitPrice = P;
%%%%%%Market share without extension of warranty %%%%%%
MarShareWOExtWar = Q;
%%%%%%Expected Share under Enterprise Warranty Program %%%%%%
ExpMarkShareEWP = (Pprime*Qprime) + ((1-Pprime)*Q);
E = (-1*(ExpUnitProfit*ExpMarkShareEWP));
F = (ExpUnitWarCost + (Pprime*(Cs*(1-ExpUnitWarCostwithoutCs))*(ExpUnitWarCostPrimes-ExpUnitWarCostwithoutCs)))/P;
end
disp(['ExpUnitProfit = ',num2str(ExpUnitProfit)])
disp(['ExpMarkShareEWP = ',num2str(ExpMarkShareEWP)])
% disp(['E = ',num2str(E)])
% disp(['MaxFunctionProblem = ',num2str(MaxFunctionProblem)])
disp (['r = ',num2str(r)])
disp (['Theta0 = ',num2str(Theta0)])
disp (['Theta0 = ',num2str(Theta1)])
disp (['Theta0 = ',num2str(Theta2)])
disp (['Theta0 = ',num2str(Theta3)])
disp (['L = ',num2str(L)])
disp (['i = ',num2str(i)])
disp (['Qprime = ',num2str(Qprime)])
disp (['e = ',num2str(e)])
disp (['v = ',num2str(v)])
disp (['Pprime = ',num2str(Pprime)])
disp (['Gr = ',num2str(Gr)])
function [c, ceq] = confun(x)
P = x(1);
W = x(2);
U = x(3);
Wprime = x(4);
Uprime = x(5);
P1 = 1;
P2 = 6;
W1 = 1;
W2 = 5;
U1 = 1;
U2 = 6;
W3 = 10;
U3 = 12;
Cp = 0.05;
c = [-P + P1;
P - P2;
-W + W1;
W - W2;
-U + U1;
U - U2;
-Wprime + W
Wprime - W3
-Uprime + U
Uprime - U3
-ExpUnitProfit+(Cp*P) ];
ceq = [];
end
end

 Accepted Answer

You need to write your code to give results in the format that fgoalattain expects. It wants your objective function to be in the following form:
function F = myfun(x)
% calculate stuff here
F(1) = % your first objective function, maybe the one you call E
F(2) = % your second objective function, maybe the one you call F
However, this is such a basic answer, and your code is so complex, that I am afraid that I did not understand your problem correctly. Feel free to ask again, correcting my mistaken impression of what your problem really is.
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (1)

Without executing (and I do not have the toolbox for that) it is not obvious that your "F" will indeed be a vector of length 2 like your goal. Please put in a breakpoint at the assignment to F and test size() of the various expressions. Double-check that you want algebraic matrix multiplication ("*") operator and not the element-by-element operator ".*"; also double check that your P is scalar or else that you really do want the matrix-right-divide operation ("/") and not the element-by-element operator "./"

3 Comments

Thanks a lot. That solved the problem, but maybe I am confused on this multiobjective function syntax for goalattain. I have 2 objective functions (E and F) but apparently my vector goal size is just a [1,1] vector. I was thinking I would have 2 separate vectors since there are 2 separate objective functions. I think I am also confused on how to set up these two objective functions when I am calling for the goalattain because I have a different goal for each function.
You have goal = [-.04, -.2]; so your goal is 1 x 2, so the first output of your function needs to be of length 2.
I believe your function should only be returning one result.
I changed the goal to = [-.04] which corrected my error but since I have two goals that go with two separate functions, how do I make my output of my function have a length of 2 (which is what you are suggesting)?
I have two separate functions with two separate goal values. I do understand that there should only be one output, but how do I include my second goal value for my second function?
I believe that example only uses 1 objective function but I have 2.
Thanks, Kam

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!