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