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
0 件のコメント
採用された回答
Catalytic
2024 年 2 月 19 日
編集済み: Catalytic
2024 年 2 月 19 日
Follow this example -
2 件のコメント
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 Exchange で Genetic Algorithm についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!