# How can I run GA without using Optimization tool box

7 views (last 30 days)
Matlab Noob on 17 Mar 2016
Commented: Matlab Noob on 18 Mar 2016
My question is that I want to run this code without using Optimization tool box.
function F = fitm(x)
q = reshape(x(1:6),3,2);
k = reshape(x(7:12),3,2);
m = reshape(x(13:end),3,1);
intcon = 13:15;
lb = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1];
ub = [inf, inf, inf, inf, inf, inf, 1, 1, 1, 1, 1, 1, inf, inf, inf];
%Const
A =0;
B = 0;
J = 2;
I = 3;
a = [3 2; 3 1; 1.1 9];
d = [1850 2000; 3000 2100; 1300 900 ];
hr = [2.4 1.3; 3.8 4.5; 3.6 4.2];
so = [3.5 1.5; 2.5 0.3; 0.6 6.3];
t = [1 1; 3 1; 1 3];
E = [2 2; 3 2; 2 3];
Y = [3 3; 3 3; 3 3];
S=100;
hv = [3.5; 4; 5];
C = [1; 5; 2];
Cv = [2.4; 3; 2;];
Cf = [3; 1.2; 2;];
s = [0.3; 0.1; 0.2;];
the = [0.01; 0.03; 0.02];
P = [1000; 8000; 9000];
dv = [sum(d(1,:)); sum(d(2,:)); sum(d(3,:))];
Q = [sum(q(1,:)); sum(q(2,:)); sum(q(3,:))];
for i=1:I
A = A + (S*dv(i)/(Q(i)*m(i))+(hv(i)*Q(i)*((m(i)-1)*(1-dv(i)/P(i))+dv(i)/P(i)))/2+...
dv(i)*(C(i)*m(i)+m(i)*Q(i)*Cv(i)+Cf(i))/(Q(i)*m(i))+(s(i)*m(i)*dv(i)*Q(i)*the(i))/2);
for j=1:J
B = B + (a(i,j)*d(i,j)/q(i,j)+(hr(i,j)*((1-k(i,j))^2)*q(i,j))/2+...
k(i,j)^2*so(i,j)*q(i,j)/2+(m(i)*t(i,j)*d(i,j))/q(i,j)+(m(i)*E(i,j)*d(i,j))/q(i,j)+...
Y(i,j)*d(i,j));
end
end
F = A + B;
x = ga(F,15,[],[],[],[],[],lb,ub,intcon);
disp(x)
end
It is worked using GA Optimization tool box.
I added ' x = ga(F,15,[],[],[],[],[],lb,ub,intcon);' However, matlab shows me 'Not enough input arguments. Error in fitm (line 2) q = reshape(x(1:6),3,2);' How can I fix it...?

Alan Weiss on 18 Mar 2016
Your question "How can I run GA without using Optimization tool box" does not make sense to me, given that your stated question is about an error in your fitness function. I suggest that you edit your post.
The problem is that you put the ga call inside your fitness function call. You also put the lower and upper bounds inside your fitness function. These lines do not belong there. Take the lines
lb = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1];
ub = [inf, inf, inf, inf, inf, inf, 1, 1, 1, 1, 1, 1, inf, inf, inf];
intcon = 13:15;
x = ga(F,15,[],[],[],[],[],lb,ub,intcon);
out of your fitness function. Then, to call ga at the command line, enter the following at the command line
lb = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1];
ub = [inf, inf, inf, inf, inf, inf, 1, 1, 1, 1, 1, 1, inf, inf, inf];
intcon = 13:15;
x = ga(@fitm,15,[],[],[],[],[],lb,ub,intcon);
The difference is between a fitness function and the procedure to minimize the function. You have to separate these things.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Matlab Noob on 18 Mar 2016
Thank you, I got a result.
But,I saw some code that the ga call inside m-file within function. Is it a different case of mine?

### Categories

Find more on Problem-Based Optimization Setup 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!