How can I optimize this problem to find the most suitable set of variables to maximize the cost function ?

I want to maximize [(2*n*eo*10*lf)/d -(2*n*eo*3.87*lf)/d]. With constraints (i) lf/wf <= 40; (ii) den*n/2*lf*wf*10 = 3.0015e-8. The variables are n,lf,wf and d.Known constants are eo and den. I am trying to use genetic algorithm to solve this problem but don't seem to be getting a proper answer. I wrote 3 functions, 1 for cost, 1 for constraints. I am giving all the functions below. The variable list is [lf wf d n] in my code.
function eh_pc_generic
clear all; close all; clc;
options = optimset('Display','iter')
ObjectiveFunction = @eh_cost_genetic;
nvars = 4; % Number of variables
LB = [0 2e-6 0 1]; % Lower bound
UB = [Inf Inf Inf Inf]; % Upper bound
A = zeros(1,4);
A(1,1) = 1;
A(1,2) = -40;
B = 0;
ConstraintFunction = @eh_constraint_genetic;
[x,fval] = ga(ObjectiveFunction,nvars,A,B,[],[],LB,UB, ...
ConstraintFunction)
end
function c = eh_cost_genetic(x)
eo = 8.854e-12;
c = -[((2*x(4)*eo*x(1)*10))/x(3)-(2*x(4)*eo*x(1)*3.87)/x(3)]
end
function [cineq,ceq] = eh_constraint_genetic(x)
den = 2331;
cineq = [];
ceq = [(den*10*x(4)*x(2)*(x(4)/2))-3.0015e-8];
end
The three functions needs to be saved with their names.
Any help on how I can optimize these parameters subject to those constraints is highly appreciated.
Thanks

 Accepted Answer

You seem to have four variables. Your upper and lower bounds have four components each. A has four components. Yet you write nvars = 3;. You use x(1), x(3), and x(4) in eh_cost_genetic, and you use x(1), x(2), and x(4) in eh_constraint_genetic.
So the first thing to check is that you are using the indices of the x variable consistently, because you mentioned only three variables to optimize: n,lf,wf. If you have written everything else correctly, then change to nvars = 4;.
Secondly, why are you using a genetic algorithm on this problem? fmincon is much more suitable.
Alan Weiss
MATLAB mathematical toolbox documentation

2 Comments

Hello Alan, Thank you for your reply. n should be 4 as I am trying to optimize 4 variables. I have made some corrections in my original question. I do not have one of the variables in the cost function but I have it in the constraints.
I also tried with fmincon but not getting results which I can accept. I am also providing the functions with fmincon.
function eh_pc
clear all; clc; close all;
format long
options = optimset('Display','iter')
lf = 600e-6;
wf = 15e-6;
d = 5e-6;
n = 150;
x0 = [lf wf d n]';
lb = zeros(4,1);
ub = Inf(4,1);
lb(2) = 2;
lb(3) = 2;
ub(1) = 1000e-6;
% ub(5) = 40e-6;
A = zeros(1,4);
A(1,1) = 1;
A(1,2) = -40;
B = 0;Aeq = [];Beq = [];
[xfinal,cost] = fmincon('eh_cost',x0,A,B,Aeq,Beq,lb,ub,'eh_constraint',options)
end
function c = eh_cost(x)
eo = 8.854e-12;
c = -[((2*x(4)*eo*x(1)*10))/x(3)-(2*x(4)*eo*x(1)*3.87)/x(3)]
end
function [cineq,ceq] = eh_constraint(x)
den = 2331;
cineq = [];
ceq = [(den*10*x(1)*x(2)*(x(4)/2))-3.0015e-8];
end
I made some very slight modifications to your code and got what I think is a reasonable answer from fmincon. See what you think of this function and result:
function [xfinal,cost] = eh_pc(x0)
% clear all; clc; close all;
format long
options = optimset('Display','iter','Algorithm','sqp');
lf = 600e-6;
wf = 15e-6;
d = 5e-6;
n = 150;
% x0 = [lf wf d n]';
lb = zeros(4,1);
ub = Inf(4,1);
lb(2) = 2;
lb(3) = 2;
ub(1) = 1000e-6;
% ub(5) = 40e-6;
A = zeros(1,4);
A(1,1) = 1;
A(1,2) = -40;
B = 0;Aeq = [];Beq = [];
[xfinal,cost] = fmincon(@eh_cost,x0,A,B,Aeq,Beq,lb,ub,@eh_constraint,options)
function c = eh_cost(x)
eo = 8.854e-12;
c = -(((2*x(4)*eo*x(1)*10))/x(3)-(2*x(4)*eo*x(1)*3.87)/x(3));
end
function [cineq,ceq] = eh_constraint(x)
den = 2331;
cineq = [];
ceq = [(den*10*x(1)*x(2)*(x(4)/2))-3.0015e-8];
end
end
Here is what happens when you run it with the 'sqp' algorithm, which I used because your tolerances seem very tight:
lf = 600e-6;
wf = 15e-6;
d = 5e-6;
n = 150;
x0 = [lf wf d n]';
[xfinal,cost] = eh_pc(x0);
Your initial point x0 is not between bounds lb and ub; FMINCON
shifted x0 to satisfy the bounds.
Norm of First-order
Iter F-count f(x) Feasibility Steplength step optimality
0 5 -4.884752e-12 2.098e+03 8.141e-09
1 10 -4.397757e-19 1.888e-04 1.000e+00 6.000e-04 6.000e-04
2 11 -4.397757e-19 1.888e-04 7.000e-01 3.781e-11 2.199e-19
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
xfinal =
1.0e+02 *
0.000000000000540
0.020000000000000
0.020000000000000
1.499999999976000
cost =
-4.397756630048431e-19
Then, to clean up the result, run it again from the calculated point xfinal:
[xfinal,cost] = eh_pc(xfinal);
Norm of First-order
Iter F-count f(x) Feasibility Steplength step optimality
0 5 -4.397757e-19 1.888e-04 8.141e-09
1 10 -6.988695e-23 5.499e-21 1.000e+00 5.401e-11 5.401e-11
Local minimum found that satisfies the constraints.
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
xfinal =
1.0e+02 *
0.000000000000000
0.020000000000000
0.020000000000000
1.499999999976000
cost =
-6.988694660232942e-23
I hope that this gives a result that you find satisfactory.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Asked:

on 25 Aug 2015

Commented:

on 26 Aug 2015

Community Treasure Hunt

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

Start Hunting!