Can anyone locate the problem why I am getting this error.
I am getting this Error using makeState (line 56). Your fitness function must return a scalar value.
3 views (last 30 days)
Show older comments
% clc
% clear
price=[49.52, 47.94, 46.24, 43.34]';
inFlow=[1070,1070,1070 1070]';
N = length(inFlow);
LB = zeros(2*N,1); % must have positive flow
UB = [25000*ones(N,1); % maximum turbine flow of 25,000 CFS
Inf(N,1)]; % no bound on spill flow
ot = ones(N,1);
b = -500*ot;
A = spdiags([-ot -ot],[0 N],N,N*2);
A2 = spdiags([ot ot -ot -ot],[0 N -1 N-1],N,N*2);
A2=full(A2);
b2 = 500*ot;
% remove the initial starting condition
A2(1,:) = [];
b2(1,:) = [];
% now add constraints for the -500 condition
A = [A; A2; -A2];
b = [b; b2; b2];
C2A = 1.98347/24; % Convert from CFS to AF/HR
stor0 = 90000; % initial vol. of water stored in the reservoir (Acre-Feet)
c = stor0 + C2A*cumsum(inFlow); % Convert CFS to AF
b = [b; 100000-c; -50000+c];
s = -C2A*sparse(tril(ones(N)));
s = [s s];
A = [A; s; -s];
Aeq = ones(1,2*N);
beq = sum(inFlow);
A=full(A);
options = optimoptions('ga','PlotFcn', @gaplotbestf);
fun=@Revenue_ga;
tic
% [x1, fval1] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,[],options)
[x1, fval1] = ga(fun,2*N,A,b,Aeq,beq,LB,UB,[],options)
toc
plotResults(price, x1, N);
function totR = Revenue_ga(X)
price=[49.52, 47.94, 46.24, 43.34]';
inFlow=[1070,1070,1070 1070]';
stor0 = 90000; % initial vol. of water stored in the reservoir (Acre-Feet)
k1 = 0.00003; % K-factor coefficient
k2 = 9; % K-factor offset
MW2kW = 1000; % MW to kW
C2A = 1.98347/24; % Convert from CFS to AF/HR
c = [C2A k1 k2 MW2kW]; % Vector of constants
N = length(inFlow);
F = inFlow;
P = price;
TotFlow = X(1:N)+X(N+1:end);
S = zeros(N, 1);
S(1) = stor0 + c(1)*(F(1)-TotFlow(1));
%
for ii = 2:N
S(ii) = S(ii-1) + c(1)*(F(ii)-TotFlow(ii));
end
%
k = c(2)*([stor0; S(1:end-1)] + S)/2 + c(3);
%
MWh = k.*X(1:N)/c(4);
%
R = MWh.*P;
%
%
totR = -sum(R).';
end
2 Comments
Accepted Answer
VBBV
on 4 May 2021
Edited: VBBV
on 4 May 2021
%if true
MWh = k'.*X(1:N).'/c(4);
From the error, the function must return a scalar which is possible with above change. MWh will be single or scalar value 1x1
The other option is to change the function line as which returns a vector as below
%f true
function [totR] = Revenue_ga(X)
1 Comment
Walter Roberson
on 4 May 2021
function [totR] = Revenue_ga(X)
makes no difference compared to
function totR = Revenue_ga(X)
The function is being called from ga() and ga() requires that a scalar is returned. If you wanted to return a vector, you would have to switch to gamultiobj() ... but that is not the real solution in this situation, the real situation is just using the transpose on the X(1:N)
More Answers (1)
Walter Roberson
on 4 May 2021
k is a 4 x 1 column vector. X is a 4 x 1 row vector. k.*X(1:N) is then (4 x 1) .* (1 x 4) which gives a 4 x 4 result.
Change
MWh = k.*X(1:N)/c(4);
to
MWh = k.*X(1:N).'/c(4);
0 Comments
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!