Scaled function for optimization
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
clear
if true
% Unscaled formulation:
% min (x^3 - y)^2 - x
% subject to
% x^2 + 3150 - y <= 0
% -15 <= x <= 15
% -4000 <= y <= 4000
obj = @(x) (x(1)^3 - x(2))^2 - x(1);
nonlcon = @(x) deal(x(1)^2 + 3150 - x(2), []);
lb = [-15, -4000];
ub = [15, 4000];
% Solve the problem a few times
fprintf("Unscaled optimisation results:\n");
x_unscaled = zeros(5,2);
fval_unscaled = zeros(5,1);
for i = 1:5
options = optimoptions('ga','Display','none','ConstraintTolerance',1e-6,'PlotFcn', @gaplotbestf);
[x_unscaled(i,:), fval_unscaled(i)] = ga(obj, 2, [], [], [], [], lb, ub, nonlcon, options);
fprintf("Objective value: %0.3f, x = %0.3f, y = %0.3f\n",fval_unscaled(i), x_unscaled(i,1), x_unscaled(i,2));
end
end
% Scaled formulation:
% Substitute w = x/15 and z = y/4000
% and multiply the objective and constraints by reasonable factors
% (so that all of their terms are somewhere around the order of 1)
% to obtain...
% min 0.01*(((15*w)^3 - (4000*z))^2 - 15*w)
% subject to
% 0.001*((15*w)^2 + 3150 - (4000*z)) <= 0
% -1 <= w <= 1
% -1 <= z <= 1
if true
obj = @(x) 0.01*(((15*x(1))^3 - (4000*x(2)))^2 - (15*x(1)));
nonlcon = @(x) deal(0.001*((15*x(1))^2 + 3150 - (4000*x(2))), []);
lb = [-1, -1];
ub = [1, 1];
% Solve the problem a few times
fprintf("Scaled optimisation results:\n");
x_scaled = zeros(5,2);
fval_scaled = zeros(5,1);
for i = 1:5
options = optimoptions('ga','Display','none','ConstraintTolerance',1e-6,'PlotFcn', @gaplotbestf);
[x_scaled(i,:), fval_scaled(i)] = ga(obj, 2, [], [], [], [], lb, ub, nonlcon, options);
% Rescale for x and y afterwards
fval_scaled(i,1) = fval_scaled(i,1) * 100;
x_scaled(i,1) = x_scaled(i,1) * 15;
x_scaled(i,2) = x_scaled(i,2) * 4000;
fprintf("Objective value: %0.3f, x = %0.3f, y = %0.3f\n",fval_scaled(i), x_scaled(i,1), x_scaled(i,2));
end
end
From the run, I got this solution. But, I do not understand the coding, especially the 0.01 and 0.001 which multiply in the scaled objective function and constraint. How does it come from?
0 commentaires
Réponses (1)
Torsten
le 16 Mai 2023
Déplacé(e) : Torsten
le 16 Mai 2023
From the run, I got this solution. But, I do not understand the coding, especially the 0.01 and 0.001 which multiply in the scaled objective function and constraint. How does it come from?
Did you read the program documentation ?
% Substitute w = x/15 and z = y/4000
% and multiply the objective and constraints by reasonable factors
% (so that all of their terms are somewhere around the order of 1)
0 commentaires
Voir également
Catégories
En savoir plus sur Genetic Algorithm dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!