How would I get Maximization curve in GA in Matlab?

In GA toolbox in Matlab we are getting fitness curve in form of minimization. How can I get maximization (fitness function) curve?
Can anyone help me in getting GA code for optimization?

 Accepted Answer

minimize the negative of the function.
[x, fvalp] = ga(@(x) -f(x), ....)
fval = -fvalp; %maximum value of the original function

14 Comments

sholud I do this after running the GA toolbox
no this replaces running ga(@f, ....) rather than being after.
i have the same problem but i donot know where i put the code
What is your current ga call that is resulting in the function being minimized?
i use optimization toolbox not code ..... can i write code in output function to maximization (fitness function) curve
can we make online meeting to see my problem and sove it whith me
Currently you have your fitness function as @mohfun .
Instead you can use @(x)-mohfun(x) if you want to maximize.
But you have an error on line 3 of objFcn.m . You did not show us the code for that, but we can guess that you have something like
if x = 1
where you need instead
if x == 1
I am dealing with a significant computer hardware failure. I do not have the capacity to have online meetings at the moment.
now i donot have any error but iam sorry can you tell me exactely where i do @(x)-mohfun(x)
my function code
function [e] = mohfun(r)
dt = r(1);
ltt = r(2);
dss = r(3);
%% Data of cold fluid
tci=30; %Input temperature of cold fluid (C)
%tco=35; %Output temperature of cold fluid (C)
rowc=1000; %Density (kg/m3)
mioc=0.000797; %Viscosity (Pa s)
kc=.613; %Thermal conductivity (W/m K)
prc=5.83; %Prandtl number
cpc=4186; %Heat capacity (kJ/kg K)
mc=42; %Mass flow rate of cold fluid
vec=1.14; %velocity of cold fluid
%rec; %Reynold’s number OF COLD fluid
%hc; %hot stream of cold fluid
%ftc, %Darcy friction factor of cold fluid
%qc=input('Enter heat duty of cold fluid = '); %heat duty (W)
%% Data of hot fluid
thi=60; %Input temperature of hot stream (C)
%tho=45; %Output temperature of hot stream (C)
rowh=865.8; %Density (kg/m3)
mioh=0.023; %Viscosity (Pa s)
kh=0.141; %Thermal conductivity (W/m K)
prh=1205; %Prandtl number
cph=2000; %Heat capacity (kJ/kg K)
mh=17.2; %Mass flow rate
%veh=1.5; %velocity of hot fluid
%qh=input('Enter heat duty of hot fluid = '); %heat duty (W)
%pit=.005; %triangular pitch
%res %reynolds number (res) for shell side
%hs %hot stream of hot fluid
%% data of tube
%nt=input('Enter number of tube = '); %number of tube
dti=dt; %Tube inside diameter (m)
st=0.0043; %Tube thickness (m)
Rft=0.00042; %fouling factor
%dto %outer dimeter of tube
lt=ltt; %lt=input('Enter the length of tube = '); %Tubes length (m)
%% Data of shell
ds=dss; %Shell inside diameter (m)
np=1; %Number of tube passes
Rfs=0.0002; %fouling factor
%dh %hydraulic diameter
%% Equation
fprintf('dti %f \n ',dti)
fprintf('ltt %f \n ',ltt)
fprintf('dss %f \n ',dss)
dto=dti+st;
fprintf('dto %f \n ',dto)
%nt=mc/(rowc*(pi/4)*(dti^2)*vec);
v1=pi/4;
v2=dti^2;
v3=rowc*v2;
v4=v3*v1*vec;
nt=mc/v4;
ntt=nt*2;
fprintf('nt %f \n',nt)
%nt=nt1*2;
fprintf('ntt %f \n',ntt)
as=pi*dto*lt*nt*np;
fprintf('as %f \n',as)
rec = (rowc*vec*dti)/(mioc);
fprintf('rec %f \n',rec)
ftc = ((1.82*log(rec))-1.64)^(-2);
fprintf('ftc %f \n',ftc)
if rec < 2300
hc = (kc/dti)*(3.657+((0.0677*rec*prc*(dti/3.050))^1.33)/(1+0.1*prc*((rec*(dti/3.050))^0.3)));
elseif 2300 < rec && rec < 10000
hc = (kc/dti)*((1+(dti/3.050))^0.67)*(((ftc/8)*(rec-1000)*(prc))/(1+12.7*((ftc/8)^0.5)*((prc^0.667)-1)));
elseif rec > 10000
hc = (0.027)*(kc/dti)*(rec^0.8)*(prc^(1/3))*(mioc/mioc)^0.14;
end
fprintf('hc %f \n',hc)
%dh = (4*(0.43*pit^2-(pi*0.125*dto^2)))/(0.5*pi*dto);
%d1=ds^2-(dto^2*nt);
%d2=ds+(dto*nt);
%dh=d1/d2;
dh=(ds^2-(dto^2*nt))/(ds+(dto*nt));
fprintf('dh %f \n',dh)
f1=pi/4;
f2=f1*dh^2;
f3=f2*rowh;
veh=mh/f3;
fprintf('veh %f \n',veh)
res=(rowh*veh*dh)/mioh;
fprintf('res %f \n',res)
hs=(0.36*kh*res^0.55*prh^0.33)/dh;
fprintf('hs %f \n',hs)
%U = ((1/hs)+Rfs+(dto/dti)*(Rft+(1/hc)))^(-1);
U = ((1/hs)+Rfs+Rft+(1/hc))^(-1);
fprintf('U %f \n',U)
c1=mc*cpc;
fprintf('c1 %f \n',c1)
c2=mh*cph;
fprintf('c2 %f \n',c2)
if c1>c2
c_x=c1;
c_m=c2;
else
c_x=c2;
c_m=c1;
end
fprintf('c_m %f \n',c_m)
fprintf('c_x %f \n',c_x)
ntu= (as*U)/c_m;
fprintf('ntu %f \n',ntu)
cr=c_m/c_x;
fprintf('cr %f \n',cr)
%ef=1-exp(-cr^-1 * (1-exp(1-cr(ntu))));
e1=exp(-cr*ntu);
e2=1-e1;
e3=-cr^-1;
e4=e3*e2;
e5=exp(e4);
ef=1-e5;
%e7=1+e6;
%ef=1/e7;
e=-ef;
fprintf('ef %f \n',ef)
tho=thi-ef*(thi-tci);
fprintf('tho %f \n',tho)
qh=mh*cph*(thi-tho);
fprintf('qh %f \n',qh)
tco=(qh/(mc*cpc))+tci;
fprintf('tco %f \n',tco)
lt= as/(pi*dto*nt);
fprintf('lt %f \n',lt)
end
There is a data entry box labeled Fitness function where you have entered
@mohfun
Change that to
@(x)-mohfun(x)
ok it work but my curve getout minus .... i need to get it positave
i need it positve because i maxmize .... this result good ... i need this result but in curve i need fitness value in the same result but positve
You see in the Plot function section where you ask to plot best fitness? You would need to change that to a custom plot function that worked just like the plot function for best fitness but plotted the negative of the current value. See https://www.mathworks.com/help/gads/genetic-algorithm-options.html#f15210
There is no built-in way to maximize values using the solver-based approach: all you can do is minimize the negative of the function and understand that what you will get out is the location of the maxima, and that you need to take the negative of any function value you get out of your @(x)-mofun(x) function in order to know what the value would be in your original @mofun space.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!