Optimization not working because fitness function must be a function handle
18 views (last 30 days)
Show older comments
% 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));
0 Comments
Answers (1)
Matt J
on 4 Jun 2023
Edited: Matt J
on 4 Jun 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 Comment
Steven Lord
on 4 Jun 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])
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])
See Also
Categories
Find more on Multiobjective Optimization 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!