フィルターのクリア

How to restructure my objective function to optimise using genetic algorithm?

1 回表示 (過去 30 日間)
Gauri
Gauri 2024 年 2 月 19 日
コメント済み: Matt J 2024 年 2 月 19 日
Hello
I am solving an optimisation problem where i am minimising total cost from retailer pov. Ive attached my objective function
I need to use genetic algorithm to generate values of order qty placed by retailer. I managed to get a code snippet for it. will this be alrght? how can i use the optimiser task for this purpose?
note: mainfile and ModifiedWorking are same thing in different forms
I managed to get this code:
% geneticAlgorithm.m
function optimizedQB = geneticAlgorithm(D, SB, T, hcr, scr, pcr, ecr, populationSize, mutationRate)
% Define genetic algorithm parameters
populationSize = 50;
generations = 100;
mutationRate = 0.1;
% Main loop for genetic algorithm
bestQB = zeros(1, T);
for generation = 1:generations
% Generate initial population
population = randi(SB, populationSize, T);
% Evaluate fitness (total cost) for each individual in the population
fitness = zeros(1, populationSize);
for i = 1:populationSize
QB = population(i, :);
fitness(i) = calculateTotalCost(D, SB, QB, hcr, scr, pcr, ecr);
end
% Select individuals for crossover
[~, sortedIndices] = sort(fitness);
selectedPopulation = population(sortedIndices(1:populationSize/2), :);
% Crossover
crossoverPopulation = crossover(selectedPopulation);
% Mutate
mutatedPopulation = mutate(crossoverPopulation, mutationRate, SB);
% Elitism: Replace worst individuals with the best from the previous generation
population = [population(sortedIndices(1:populationSize/2), :); mutatedPopulation];
% Find the best QB from the current generation
[~, bestIndex] = min(fitness);
bestQB = population(bestIndex, :);
end

採用された回答

Catalytic
Catalytic 2024 年 2 月 19 日
編集済み: Catalytic 2024 年 2 月 19 日
  2 件のコメント
Gauri
Gauri 2024 年 2 月 19 日
編集済み: Gauri 2024 年 2 月 19 日
thank you for your comment, but i wasnt able to draw an analogy between the article and the code i have to work with. My main doubt is how to implement genetic algorithm inside the for loop. i cant find a way to seperate the genetic algorithm outside the loop. Do you have any advice on that?
Matt J
Matt J 2024 年 2 月 19 日
Your post doesn't mention any difficulties with a for loop... I don't see why that would be the main problem. There is no fundamental difference between running ga once or within a loop. My suggestion would be that you first get ga running on a single instance of the optimization without the loop. Then, come back to us with that code if wrapping it in a loop is somehow breaking things.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by