How can I use the Genetic Algorithm (GA) to minimize a cost function of two variables C(T,K) where T is a real an K an integer.

12 views (last 30 days)

Accepted Answer

John D'Errico
John D'Errico on 20 Jan 2023
Trivial.
  1. Write your function as an objective function. I won't do that for you, but just start at the beginning and write it.
  2. Set up any constraints.
  3. Call GA.
And while you may think of it as a 2 variable problem, you have only ONE variable, a vector of length 2. One of those variables is constrained to be integer.
For example:
a = 1.25;
b = 100;
obj = @(xy) (a - xy(1)).^2 + b*(-1/8 + xy(1)-(xy(2)/10).^2).^2;
This is just effectively the Rosenbrock function, with a tweak or two in there. I'll constrain y to be an integer.
intcon = 2 % So only y is constrained to be integer
intcon = 2
lb = [-20,-20];
ub = [20 20];
[xysol,fval,exitflag] = ga(obj,2,[],[],[],[],lb,ub,[],intcon)
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
xysol = 1×2
1.1262 10.0000
fval = 0.0155
exitflag = 1
fsurf(@(x,y) obj([x,y]),[0 2 8 13])
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!