Genetic Algorithm code with Array as variable

19 ビュー (過去 30 日間)
Set Eng
Set Eng 2020 年 10 月 6 日
コメント済み: Walter Roberson 2020 年 10 月 6 日
Hi,
I want to make my code eccept input as array
can you help me
================================================================
clc;
clear;
close all;
%% Problem Definition
problem.CostFunction = @ forcast_Linear;
problem.nVar = 8 ;%[A B C D E F Ts Tw . ]
problem.VarMin = [ 1 1 60 30 360 -60 30 18 ];
problem.VarMax = [40 365 90 40 400 -30 34 22 ];
%% GA Parameters
params.MaxIt = 100;
params.nPop = 1000;
params.beta = 1;
params.pC = 1;
params.gamma = 0.1;
params.mu = 0.02;
params.sigma = 0.1;
%% Run GA
out = RunGA(problem, params);
%% Results
%figure;
% plot(out.bestcost, 'LineWidth', 2);
semilogy(out.bestcost, 'LineWidth', 2);
xlabel('Generation'); %Iterations ??? ???????
ylabel('Best Cost');
grid on;
=====================================
function z = forcast_Linear(x)
%% varibles
A = x(:,1)';
B = x(:,2)';
C = x(:,3)' ;
D = x(:,4)';
E = x(:,5)';
F = x(:,6)';
Ts =x(:,7)';
Tw =x(:,8)';
%% input
I = [1 2 3 4 5 6 7 8 9 10 ]% ; Day sequence
L = [585 511 583 535 530 487 440 360 465 457]% ; Actual load
T = [15 21 21 20 23 25 22 26 23 23] ;% temp.
%% problem
y =(T >= Tw).* (T <= Ts).*(A+( B.*(I./365) ))+(T > Ts).* ( A+( B.*(I./365) )+C+D.*(I./365))+ (T< Tw).*(A+( B.*(I./365) )+E+F.*(I./365))
e = (T >= Tw).*(T <= Ts).*((L - y)/L) +(T > Ts).*((L - y)/L)+ (T< Tw).*((L - y)/L)
z = abs(e)
disp('___________________________________________________')
end
=============================================
function out = RunGA(problem, params)
% Problem
CostFunction = problem.CostFunction;
nVar = problem.nVar;
VarSize = [1, nVar];
VarMin = problem.VarMin;
VarMax = problem.VarMax;
% Params
MaxIt = params.MaxIt;
nPop = params.nPop;
beta = params.beta;
pC = params.pC;
nC = round(pC*nPop/2)*2;
gamma = params.gamma;
mu = params.mu;
sigma = params.sigma;
% Template for Empty Individuals
empty_individual.Position = [];
empty_individual.Cost = [];
% Best Solution Ever Found
bestsol.Cost = inf;
% Initialization
pop = repmat(empty_individual, nPop, 1);
for i = 1:nPop
% Generate Random Solution
pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Evaluate Solution
pop(i).Cost = CostFunction(pop(i).Position);
% Compare Solution to Best Solution Ever Found
if pop(i).Cost < bestsol.Cost
bestsol = pop(i);
end
end
% Best Cost of Iterations
bestcost = nan(MaxIt, 1);
% Main Loop
for it = 1:MaxIt
% Selection Probabilities
c = [pop.Cost];
avgc = mean(c);
if avgc ~= 0
c = c/avgc;
end
probs = exp(-beta*c);
% Initialize Offsprings Population
popc = repmat(empty_individual, nC/2, 2);
% Crossover
for k = 1:nC/2
% Select Parents
p1 = pop(RouletteWheelSelection(probs));
p2 = pop(RouletteWheelSelection(probs));
% Perform Crossover
[popc(k, 1).Position, popc(k, 2).Position] = ...
UniformCrossover(p1.Position, p2.Position, gamma);
end
% Convert popc to Single-Column Matrix
popc = popc(:);
% Mutation
for l = 1:nC
% Perform Mutation
popc(l).Position = Mutate(popc(l).Position, mu, sigma);
% Check for Variable Bounds
popc(l).Position = max(popc(l).Position, VarMin);
popc(l).Position = min(popc(l).Position, VarMax);
% Evaluation
popc(l).Cost = CostFunction(popc(l).Position);
% Compare Solution to Best Solution Ever Found
if popc(l).Cost < bestsol.Cost
bestsol = popc(l);
end
end
% Merge and Sort Populations
pop = SortPopulation([pop; popc]);
% Remove Extra Individuals
pop = pop(1:nPop);
% Update Best Cost of Iteration
bestcost(it) = bestsol.Cost;
% Display Itertion Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(bestcost(it))]);
end
% Results
out.pop = pop;
out.bestsol = bestsol;
out.bestcost = bestcost;
end
============================================
  3 件のコメント
Set Eng
Set Eng 2020 年 10 月 6 日
when I run it , Matlab gives me this error ::
----------------------------------------------
Index exceeds matrix dimensions.
Error in RunGA (line 65)
p1 = pop(RouletteWheelSelection(probs));
Error in app1 (line 28)
out = RunGA(problem, params);
Set Eng
Set Eng 2020 年 10 月 6 日
but when The input (T,L,I) is scalar number ، It doesn't show that sentence and the program is running, but I need to enter it as an array for speed.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 10 月 6 日
It appears to me that you would like to be able to pass an array of positions to forcast_Linear, each row being a different position.
First of all, you need to modify forcast_linear to remove the transposes. The resulting z value will be column vector, one entry per original input row.
function z = forcast_Linear(x)
%% varibles
A = x(:,1); %no transpose
B = x(:,2);
C = x(:,3) ;
D = x(:,4);
E = x(:,5);
F = x(:,6);
Ts =x(:,7);
Tw =x(:,8);
%% input
I = [1 2 3 4 5 6 7 8 9 10 ]% ; Day sequence
L = [585 511 583 535 530 487 440 360 465 457]% ; Actual load
T = [15 21 21 20 23 25 22 26 23 23] ;% temp.
%% problem
y =(T >= Tw).* (T <= Ts).*(A+( B.*(I./365) ))+(T > Ts).* ( A+( B.*(I./365) )+C+D.*(I./365))+ (T< Tw).*(A+( B.*(I./365) )+E+F.*(I./365));
e = (T >= Tw).*(T <= Ts).*((L - y)/L) +(T > Ts).*((L - y)/L)+ (T< Tw).*((L - y)/L);
z = abs(e);
end
  1 件のコメント
Walter Roberson
Walter Roberson 2020 年 10 月 6 日
Next you need to modify your cost loop.
pop = repmat(empty_individual, nPop, 1);
VarSize(1) = nPop;
Positions = unifrnd(VarMin, VarMax, VarSize);
Costs = CostFunction(Positions); %vector, nPop x 1
%distribute costs into appropriate structure entry.
%each one will receive a scalar cost
Costs_cell = num2cell(Costs);
[pop.Cost] = Costs_cell{:};
[bestcost, bestidx] = min(Costs);
if bestcost < bestcol.Cost
bestsol = pop(bestidx);
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by