How to restructure my objective function to optimise using genetic algorithm?
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Catalytic
on 19 Feb 2024
Edited: Catalytic
on 19 Feb 2024
Follow this example -
2 Comments
Matt J
on 19 Feb 2024
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.
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!