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

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by