Maximize f(x,y) = -x sin(4x)- 1.1y sin(2y) +1 with constraints 8≤x≤10; 10≤y≤13; x+y≤22 by writing a genetic algorithm.
1 次查看(过去 30 天)
显示 更早的评论
I'm new to Matlab and programming in general. So its been challenging.
The answer final answer was supplied, however my output does not match.
Answer:
Maximum≒23.0063 at (x,y)=(9.039,11.802)
Can anyone help me fix where i've goen wrong?
Ive attached my code files.
My output is as below.

0 个评论
回答(1 个)
Nipun
2024-5-14
Hi Shameika
I understand that you are trying to fit the given two varoable function subject to constraints in MATLAB. I recommend the following steps to define the objective function, the constraints and optimal values of x and y.
Step 1: Define the Objective Function
First, create a function myObjective in a file named myObjective.m that computes the value of (f(x,y)).
function f = myObjective(x)
% Objective function to be maximized
f = -(x(1) * sin(4*x(1)) + 1.1 * x(2) * sin(2*x(2))) + 1;
% Note: The Genetic Algorithm in MATLAB minimizes functions, so we negate
% the objective function to turn our maximization problem into a minimization problem.
end
Step 2: Define the Constraint Function
Next, define the constraint function myConstraints.m to handle the inequality constraints.
function [c, ceq] = myConstraints(x)
% Inequality constraints
c = [x(1) + x(2) - 22]; % x + y ≤ 22
% No equality constraints
ceq = [];
end
Step 3: Configure and Run the Genetic Algorithm
Now, configure the Genetic Algorithm using the ga function in MATLAB. You'll need to specify the number of variables, the bounds for (x) and (y), and any other options or constraints.
% Number of variables
nvars = 2;
% Linear inequality constraints (A*x ≤ b)
A = []; b = []; % No linear inequalities beyond what's defined in myConstraints
% Linear equality constraints (Aeq*x = beq)
Aeq = []; beq = []; % No linear equalities
% Lower and upper bounds [x_lower y_lower; x_upper y_upper]
lb = [8, 10];
ub = [10, 13];
% Nonlinear constraint function
nonlcon = @myConstraints;
% Options for the Genetic Algorithm
options = optimoptions('ga', 'Display', 'iter', 'PlotFcn', @gaplotbestf);
% Run the Genetic Algorithm
[x,fval,exitflag,output] = ga(@myObjective, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options);
% Display the results
fprintf('The optimal solution is: x = %f, y = %f\n', x(1), x(2));
fprintf('The maximum value of the function is: %f\n', -fval); % Negate fval because we minimized the negated objective
This script sets up the genetic algorithm with the specified objective function, constraints, and bounds, then runs the optimization. The ga function is used to perform the optimization, with @myObjective as the objective function to minimize and @myConstraints to handle the nonlinear constraints. The bounds for (x) and (y) are specified by lb and ub.
Hope this helps.
Regards,
Nipun
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!