フィルターのクリア

finding maximum power using a single-objective optimization algorithm

5 ビュー (過去 30 日間)
Moussa
Moussa 2023 年 12 月 10 日
編集済み: Torsten 2023 年 12 月 10 日
The following equation represents the power across a 2-ohm resistor in a circuit as a function of R1, R2, R3, V1, and V2.
power(R1, R2, R3, V1, V2) = 2*((280*R1 + 200*R2 + 270*R3 + 100*V1 + 20*R1*R2 + 78*R1*R3 + 60*R2*R3 + 60*R1*V1 + 10*R1*V2 + 50*R2*V1 + 30*R3*V1 + 4*R1*R2*R3 + 5*R1*R2*V1 + 17*R1*R3*V1 + 2*R1*R3*V2 + 15*R2*R3*V1 + R1*R2*R3*V1 + 900)/(520*R1 + 450*R2 + 210*R3 + 45*R1*R2 + 149*R1*R3 + 135*R2*R3 + 9*R1*R2*R3 + 700) - (220*R1 + 795*R3 + 100*V1 + 53*R1*R3 + 10*R1*V1 + 45*R1*V2 + 30*R3*V1 + 2*R1*R3*V1 + 9*R1*R3*V2 + 2650)/(520*R1 + 450*R2 + 210*R3 + 45*R1*R2 + 149*R1*R3 + 135*R2*R3 + 9*R1*R2*R3 + 700))^2
I should use a single-purpose optimization algorithm to find values for R1, R2, R3, V1, and V2 so that the power across the resistor is maximized. I employed Genetic Algorithm; its code, provided below, is capable of finding the maximum power, which is approximately 12 W.
clear;
close all;
clc;
% Genetic Algorithm Parameters
popSize = 20;
numGenerations = 50;
% Variable Bounds
lb = [0 0 0 0 0]; % Lower bounds for R1, R2, R3, V1, V2
ub = [5 5 5 10 10]; % Upper bounds for R1, R2, R3, V1, V2
% Modified Cost Function (negative of the function you want to maximize)
costFunction = @(x) -2*((280*x(1) + 200*x(2) + 270*x(3) + 100*x(4) + 20*x(1)*x(2) + 78*x(1)*x(3) + 60*x(2)*x(3) + 60*x(1)*x(4) + 10*x(1)*x(5) + 50*x(2)*x(4) + 30*x(3)*x(4) + 4*x(1)*x(2)*x(3) + 5*x(1)*x(2)*x(4) + 17*x(1)*x(3)*x(4) + 2*x(1)*x(3)*x(5) + 15*x(2)*x(3)*x(4) + x(1)*x(2)*x(3)*x(4) + 900)/(520*x(1) + 450*x(2) + 210*x(3) + 45*x(1)*x(2) + 149*x(1)*x(3) + 135*x(2)*x(3) + 9*x(1)*x(2)*x(3) + 700) - (220*x(1) + 795*x(3) + 100*x(4) + 53*x(1)*x(3) + 10*x(1)*x(4) + 45*x(1)*x(5) + 30*x(3)*x(4) + 2*x(1)*x(3)*x(4) + 9*x(1)*x(3)*x(5) + 2650)/(520*x(1) + 450*x(2) + 210*x(3) + 45*x(1)*x(2) + 149*x(1)*x(3) + 135*x(2)*x(3) + 9*x(1)*x(2)*x(3) + 700))^2;
% Run Genetic Algorithm
options = optimoptions(@ga, 'PopulationSize', popSize, 'MaxGenerations', numGenerations, 'PlotFcn', @gaplotbestf);
[x, fval, exitflag, output] = ga(costFunction, 5, [], [], [], [], lb, ub, [], options);
ga stopped because it exceeded options.MaxGenerations.
%x = fmincon(costFunction,(lb+ub)/2,[],[],[],[],lb,ub)
% Display Results
fprintf('Optimal values:\n');
Optimal values:
fprintf('R1 = %.4f ohms\n', x(1));
R1 = 0.0000 ohms
fprintf('R2 = %.4f ohms\n', x(2));
R2 = 0.0000 ohms
fprintf('R3 = %.4f ohms\n', x(3));
R3 = 0.2739 ohms
fprintf('V1 = %.4f volts\n', x(4));
V1 = 6.3453 volts
fprintf('V2 = %.4f volts\n', x(5));
V2 = 2.7447 volts
fprintf('Maximum Power: %.4f\n', fval);
Maximum Power: -12.5000
However, when I want to use the following code, as opposed to the one above that utilizes built-in functions such as optimoptions and ga, it produces a maximum power value of approximately 0.
clear;
close all;
clc;
% Genetic Algorithm Parameters
popSize = 20;
numGenerations = 50;
% Variable Bounds
lb = [0 0 0 0 0]; % Lower bounds for R1, R2, R3, V1, V2
ub = [5 5 5 10 10]; % Upper bounds for R1, R2, R3, V1, V2
% Cost Function
costFunction = @(x) -2*((280*x(1) + 200*x(2) + 270*x(3) + 100*x(4) + 20*x(1)*x(2) + 78*x(1)*x(3) + 60*x(2)*x(3) + 60*x(1)*x(4) + 10*x(1)*x(5) + 50*x(2)*x(4) + 30*x(3)*x(4) + 4*x(1)*x(2)*x(3) + 5*x(1)*x(2)*x(4) + 17*x(1)*x(3)*x(4) + 2*x(1)*x(3)*x(5) + 15*x(2)*x(3)*x(4) + x(1)*x(2)*x(3)*x(4) + 900)/(520*x(1) + 450*x(2) + 210*x(3) + 45*x(1)*x(2) + 149*x(1)*x(3) + 135*x(2)*x(3) + 9*x(1)*x(2)*x(3) + 700) - (220*x(1) + 795*x(3) + 100*x(4) + 53*x(1)*x(3) + 10*x(1)*x(4) + 45*x(1)*x(5) + 30*x(3)*x(4) + 2*x(1)*x(3)*x(4) + 9*x(1)*x(3)*x(5) + 2650)/(520*x(1) + 450*x(2) + 210*x(3) + 45*x(1)*x(2) + 149*x(1)*x(3) + 135*x(2)*x(3) + 9*x(1)*x(2)*x(3) + 700))^2;
% Initialize Population
population = lb + (ub - lb) .* rand(popSize, 5);
% Run Genetic Algorithm
fitnessHistory = zeros(numGenerations, 1); % Store fitness values at each generation
for generation = 1:numGenerations
% Evaluate Fitness
fitness = zeros(popSize, 1);
for i = 1:popSize
fitness(i) = costFunction(population(i, :));
end
% Store best fitness of the generation
fitnessHistory(generation) = max(fitness);
% Select Parents (Tournament Selection)
parentIndices = zeros(popSize, 2);
for i = 1:popSize
tournamentIndices = randi([1, popSize], 1, 2);
[~, winnerIndex] = max(fitness(tournamentIndices));
parentIndices(i, :) = tournamentIndices(winnerIndex);
end
% Crossover (Single Point)
crossoverPoints = randi([1, 4], 1, popSize);
offspring = zeros(popSize, 5);
for i = 1:popSize
crossoverPoint = crossoverPoints(i);
offspring(i, 1:crossoverPoint) = population(parentIndices(i, 1), 1:crossoverPoint);
offspring(i, crossoverPoint+1:end) = population(parentIndices(i, 2), crossoverPoint+1:end);
end
% Mutation
mutationMask = rand(popSize, 5) < 0.1; % Mutation probability = 0.1
mutationChange = (ub - lb) .* rand(popSize, 5);
offspring = offspring + mutationMask .* mutationChange;
% Clip offspring to bounds
offspring = max(min(offspring, ub), lb);
% Replace old population with offspring
population = offspring;
end
% Find the best individual
bestIndividualIndex = find(fitness == max(fitness), 1);
bestIndividual = population(bestIndividualIndex, :);
% Display Results
fprintf('Optimal values:\n');
Optimal values:
fprintf('R1 = %.4f ohms\n', bestIndividual(1));
R1 = 5.0000 ohms
fprintf('R2 = %.4f ohms\n', bestIndividual(2));
R2 = 1.4218 ohms
fprintf('R3 = %.4f ohms\n', bestIndividual(3));
R3 = 5.0000 ohms
fprintf('V1 = %.4f volts\n', bestIndividual(4));
V1 = 4.8466 volts
fprintf('V2 = %.4f volts\n', bestIndividual(5));
V2 = 5.1458 volts
fprintf('Maximum Power: %.4f\n', costFunction(bestIndividual));
Maximum Power: -0.0000
% Plot Fitness vs Iteration
figure;
plot(1:numGenerations, fitnessHistory, 'LineWidth', 2);
title('Fitness vs Iteration');
xlabel('Iteration');
ylabel('Fitness Value');
grid on;
What is wrong with the second code?

回答 (1 件)

Torsten
Torsten 2023 年 12 月 10 日
編集済み: Torsten 2023 年 12 月 10 日
You want to minimize -f. Thus instead of
% Store best fitness of the generation
fitnessHistory(generation) = max(fitness);
you have to use
% Store best fitness of the generation
fitnessHistory(generation) = min(fitness);
Maybe there are other lines in your code as well that have to be changed.

カテゴリ

Help Center および File ExchangeNonlinear Optimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by