Optimization Problem cost function won't minimize after generations

1 次查看(过去 30 天)
Hi I have a matlab project where the best cost value should be getting smaller after each generation but Its not. Ive tried to do alot of debugging but nothing has worked. The plot just hoover's around the same value, meaning its not optimizing.
clear; clc; close all;
%% Creating Initial Random population
R = 20000 + (50000-20000)*rand(1);
C = 0.0001 +(0.001-0.0001)*rand(1);
Vs = 5 +(20-5)*rand(1);
tc = 1+(8-2)*rand(1);
RandPopulation = zeros(100,4);
%% Step 2 Calculate cost function
for i = 1:100
R1 = 20000 + (50000-20000)*rand(1);
C1 = 0.0001 +(0.001-0.0001)*rand(1);
Vs1 = 5 +(20-5)*rand(1);
tc1 = 1+(8-2)*rand(1);
RandPopulation( i,1) = R1;
RandPopulation( i,2) = C1;
RandPopulation( i,3) = Vs1;
RandPopulation( i,4) = tc1;
end
dt=0.01;
t=0:dt:10;
n=size(t);
A= zeros(100,1); %cost function A
for j= 1:100
R = RandPopulation( j,1);
C = RandPopulation( j,2);
Vs = RandPopulation( j,3);
tc = RandPopulation( j,4);
Vc=zeros(1,n(2));
Vc(1)=0;
%% EULER SOLUTION
% Charge
limit = round(tc/dt);
for i=2:limit+1
Vc(i)=Vc(i-1)+(Vs/(R*C)-Vc(i-1)/(R*C))*dt;
end
%Discharge
for i=(limit+2):n(2)
Vc(i)=Vc(i-1)+(-Vc(i-1)/(R*C))*dt;
end
A(j) = ( Vc(1)-0)^2 + (Vc(200)-3.27)^2 + (Vc(400)-5.79)^2 + (Vc(600)-7.70)^2 + (Vc(800)-6.64)^2 + (Vc(1000)-5.09)^2 ;
end
B= A(1:100);
RandPopulation = [RandPopulation B];
%% Execute a for loop that will plot the top 10 cost function values,
%% for each generation vs each generation of matrix
%% for 100 generations
for k= 1:100
%% Step 3 Sort Your Matrix
SortPopulation = sortrows(RandPopulation,5);
%% Creating 2nd generation of Empty Population
NewPopulation = zeros(100,5);
%% Step 4 Create 1st generation of 20 Children from top 40 performers
ChildPopulation = zeros(20,5);
j=1;
for i = 1:1:20
phi= rand(1);
R1 = SortPopulation(i,1);
R2 = SortPopulation(i+1,1);
AvgR = (phi*R1) + (R2*(1-phi));
phi = rand(1);
C1 = SortPopulation(i,2);
C2 = SortPopulation(i+1,2);
AvgC = (phi*C1) + (C2*(1-phi));
phi = rand(1);
Vs1 = SortPopulation(i,3);
Vs2 = SortPopulation(i+1,3);
AvgVs = (phi*Vs1) + (Vs2*(1-phi));
phi = rand(1);
tc1 = SortPopulation(i,4);
tc2 = SortPopulation(i+1,4);
Avgtc = (phi*tc1) + (tc2*(1-phi));
phi = rand(1);
B1= SortPopulation(i,5);
B2= SortPopulation(i+1,5);
AvgB = (phi*B1) + (B2*(1-phi));
ChildPopulation(j,1)= AvgR;
ChildPopulation(j,2)= AvgC;
ChildPopulation(j,3)= AvgVs;
ChildPopulation(j,4)= Avgtc;
ChildPopulation(j,5)= AvgB;
j=j+1;
end
%% Step 5 Creating 60 New Strings
NewRandPopulation = zeros(60,4);
for i = 1:60
R1 = 20000 + (50000-20000)*rand(1);
C1 = 0.0001 +(0.001-0.0001)*rand(1);
Vs1 = 5 +(20-5)*rand(1);
tc1 = 1+(8-2)*rand(1);
NewRandPopulation( i,1) = R1;
NewRandPopulation( i,2) = C1;
NewRandPopulation( i,3) = Vs1;
NewRandPopulation( i,4) = tc1;
end
dt=0.01;
t=0:dt:10;
n=size(t);
A2= zeros(60,1); %cost function2
for j= 1:60
R = NewRandPopulation( j,1);
C = NewRandPopulation( j,2);
Vs = NewRandPopulation( j,3);
tc = NewRandPopulation( j,4);
Vc=zeros(1,n(2));
Vc(1)=0;
%% EULER SOLUTION2
% Charge
limit = round(tc/dt);
for i=2:limit+1
Vc(i)=Vc(i-1)+(Vs/(R*C)-Vc(i-1)/(R*C))*dt;
end
%Discharge
for i=(limit+2):n(2)
Vc(i)=Vc(i-1)+(-Vc(i-1)/(R*C))*dt;
end
%% COST FUNCTION2
%func
A2(j) = ( Vc(1)-0)^2 + (Vc(200)-3.27)^2 + (Vc(400)-5.79)^2 + (Vc(600)-7.70)^2 + (Vc(800)-6.64)^2 + (Vc(1000)-5.09)^2 ;
end
B2= A2(1:60);
NewRandPopulation = [NewRandPopulation B2];
%% Step 6 combine the matrices keep the top 20 Parents
NewPopulation(21:40,:) = ChildPopulation;
NewPopulation(41:100,:) = NewRandPopulation;
NewPopulation(1:20,:) = SortPopulation(1:20,:);
SortNewPopulation = sortrows(NewPopulation,5);
%% Overwriting the old population with the new population (possibly needing consideration of for loop)
BestCost(k) = SortNewPopulation(1,5);
gen(k)=k+1;
end
y= BestCost
x = gen
plot(gen,BestCost)
ylabel('Best cost function')
xlabel('generations')
%%NewPopulation = RandPopulation;
%end
%plot(Gens,BestCost);
%xlabel('generations')
%ylabel('Best cost function values per generation')

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear Programming and Mixed-Integer Linear Programming 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by