Optimization not working because fitness function must be a function handle

22 vues (au cours des 30 derniers jours)
xu
xu le 4 Juin 2023
Modifié(e) : Matt J le 4 Juin 2023
% Define the objective functions
objFun1 = @(x) -0.0365*x(1) + 0.18*x(2) - 0.389; % F(1)
objFun2 = @(x) 0.0435*x(1) - 0.266*x(2) + 4.2*x(3) + 0.019*x(1)*x(2) - 0.3*x(1)*x(3) - 0.2485; % F(2)
% Define the constraint functions
constraintFun = @(x) deal([], []); % No constraints in this case
% Define the lower and upper bounds
lb = [6; 1; 0.05]; % Lower bounds
ub = [22; 5; 0.45]; % Upper bounds
% Define the problem structure
problem.objective = {@(x) -objFun1(x), @(x) objFun2(x)}; % Multi-objective function to minimize F(1) and maximize -F(2)
problem.x0 = [6; 1; 0.05]; % Initial guess
problem.lb = lb; % Lower bounds
problem.ub = ub; % Upper bounds
problem.nonlcon = constraintFun; % Nonlinear constraints
% Set the options for gamultiobj
options = optimoptions('gamultiobj', 'Display', 'iter', 'PlotFcn', @gaplotpareto);
% Solve the multi-objective optimization problem
[x, fval, exitflag, output] = gamultiobj(problem, options);
% Display the Pareto front solutions
fprintf('Pareto Front Solutions:\n');
for i = 1:numel(fval)
fprintf('Solution %d:\n', i);
fprintf('F(1): %.4f\n', -fval(i, 1)); % Multiply by -1 to get the maximum value of F(1)
fprintf('F(2): %.4f\n', fval(i, 2));
fprintf('x1: %.4f\n', x(i, 1));
fprintf('x2: %.4f\n', x(i, 2));
fprintf('x3: %.4f\n', x(i, 3));
fprintf('-------------------------\n');
end
% Find the optimal solution based on preferences
optimalIndex = find(fval(:, 1) == max(fval(:, 1))); % Find the solution with the maximum value of F(1)
optimalSolution = x(optimalIndex, :);
fprintf('Optimal Solution:\n');
fprintf('F(1): %.4f\n', -fval(optimalIndex, 1)); % Multiply by -1 to get the maximum value of F(1)
fprintf('F(2): %.4f\n', fval(optimalIndex, 2));
fprintf('x1: %.4f\n', optimalSolution(1));
fprintf('x2: %.4f\n', optimalSolution(2));
fprintf('x3: %.4f\n', optimalSolution(3));

Réponses (1)

Matt J
Matt J le 4 Juin 2023
Modifié(e) : Matt J le 4 Juin 2023
problem.objective = ...
@(x) [+0.0365*x(1) - 0.18*x(2) + 0.389;
0.0435*x(1) - 0.266*x(2) + 4.2*x(3) + 0.019*x(1)*x(2) - 0.3*x(1)*x(3) - 0.2485];
  1 commentaire
Steven Lord
Steven Lord le 4 Juin 2023
Or to reuse the previous definition of the two objective functions:
% Define the objective functions
objFun1 = @(x) -0.0365*x(1) + 0.18*x(2) - 0.389; % F(1)
objFun2 = @(x) 0.0435*x(1) - 0.266*x(2) + 4.2*x(3) + 0.019*x(1)*x(2) - 0.3*x(1)*x(3) - 0.2485; % F(2)
objective = @(x) [-objFun1(x); objFun2(x)];
To check:
objective([1, 2, 3])
ans = 2×1
0.0655 11.0010
problem.objective = ...
@(x) [-0.0365*x(1) + 0.18*x(2) - 0.389;
0.0435*x(1) - 0.266*x(2) + 4.2*x(3) + 0.019*x(1)*x(2) - 0.3*x(1)*x(3) - 0.2485];
problem.objective([1, 2, 3])
ans = 2×1
-0.0655 11.0010

Connectez-vous pour commenter.

Catégories

En savoir plus sur Multiobjective Optimization 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!

Translated by