MATLAB Answers

How to increase accuracy of optimization using genetic algorithm of the following code? Value obtained by ga = -2360 (approx.) and Actual value is = -2390. Percentage of accuracy by ga = 98.7%

6 views (last 30 days)
clc
clear
price=[49.52, 47.94, 46.24, 43.34]';
inFlow=[1070,1070,1070 1070]';
x0 = [inFlow; zeros(size(inFlow))];
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);
DefineConstraints;
X = sym('x',[2*N,1]); % turbine flow and spill flow
F = sym('F',[N,1]); % flow into the reservoir
P = sym('P',[N,1]); % price
s0 = sym('s0'); % initial storage
c = sym({'c1','c2','c3','c4'}.','real'); % constants
TotFlow = X(1:N)+X(N+1:end);
S = cell(N,1);
S{1} = s0 + 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)*([s0; S(1:end-1)]+ S)/2+c(3);
MWh = k.*X(1:N)/c(4);
R = P.*MWh;
totR = -sum(R);
totR = subs(totR,[c;s0;P;F],[C';stor0;price;inFlow]);
matlabFunction(totR,'vars',{X.'},'file','objFcn_ga','Optimize',false);
PopulationSize_Data = 200;
EliteCount_Data = 2;
CrossoverFraction_Data = 0.4;
MaxGenerations_Data = 5000;
MaxStallGenerations_Data = 500;
InitialPopulationMatrix_Data = zeros(1,2*N);
FunctionTolerance_Data=1e-06;
ConstraintTolerance_Data=1e-3;
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize', PopulationSize_Data);
options = optimoptions(options,'EliteCount', EliteCount_Data);
options = optimoptions(options,'CrossoverFraction', CrossoverFraction_Data);
options = optimoptions(options,'MaxGenerations', MaxGenerations_Data);
options = optimoptions(options,'MaxStallGenerations', MaxStallGenerations_Data);
options = optimoptions(options,'FunctionTolerance', FunctionTolerance_Data);
options = optimoptions(options,'ConstraintTolerance', ConstraintTolerance_Data);
options = optimoptions(options,'InitialPopulationMatrix', InitialPopulationMatrix_Data);
options = optimoptions(options,'SelectionFcn', @selectionroulette);
options = optimoptions(options,'CrossoverFcn', @crossoversinglepoint);
options = optimoptions(options,'MutationFcn', @mutationadaptfeasible);
options = optimoptions(options,'Display', 'iter');
options = optimoptions(options,'PlotFcn', { @gaplotbestf });
tic
[x3,fval3] = ga(@objFcn_ga,2*N,A,b,Aeq,beq,LB,UB,[],options);
toc
x3
fval3
%Define constraint
% Save the following script in the matlab file DefineConstraints.m
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];
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);

Accepted Answer

Alan Weiss
Alan Weiss on 5 May 2021
You are getting a pretty good answer from the wrong solver. Your objective and constraints are all smooth, so you should not be using a Global Optimization Toolbox solver, unless you want to try MultiStart or GlobalSearch. I suggest that you use fmincon for this kind or problem. Here is what I did based on your setup:
rng default
x0 = 1e4*rand(size(LB))';
tic
[x3,fval3] = fmincon(@objFcn_ga,x0,A,b,Aeq,beq,LB,UB);
toc
x3
fval3
Here is what I got:
Local minimum possible. Constraints satisfied.
...
Elapsed time is 0.280546 seconds.
x3 =
1.0e+03 *
1.7600 1.2600 0.7600 0.5000 0.0000 0.0000 0.0000 0.0000
fval3 =
-2.3909e+03
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by