Please help me to using genetic algorithm
1 view (last 30 days)
Show older comments
I write this code but I want to solve this problem with 'ga' not with 'intlinprog' solver!
Can anyone guide me?
costprob = optimproblem;
% Indices
k = 15;
j = 2;
f = 10;
l = 5;
r0 = 6;
r = 6;
% Parameters
cr0 = 0 + 1*rand(1,r0);
dr0f = 0 + 1*rand(r0,f);
csl = 0 + 1*rand(1,l);
DE = 200 + 100*rand(1,1);
csur0f = 2000 + 1000*rand(r0,f);
ctl = 1000 + 1000*rand(1,l);
cvl = 10 + 10*rand(1,l);
cpjk = 0 + 1*rand(j,k);
corj = 0 + 1*rand(r,j);
pr0f = 0 + 1*rand(r0,f);
vjk = 0 + 1*rand(j,k);
cvjrk = 0 + 1*rand(j,r,k);
M = 10000000000000;
% Variables
xl = optimvar('xl',1,l,'LowerBound',0);
yr0f = optimvar('yr0f',r0,f,'Type','integer','LowerBound',0,'UpperBound',1);
xx1r0f = optimvar('xx1r0f',r0,f,'LowerBound',0);
xx2r0f = optimvar('xx2r0f',r0,f,'LowerBound',0);
yjk1 = optimvar('yjk1',j,k,'Type','integer','LowerBound',0,'UpperBound',1);
yl2 = optimvar('yl2',1,l,'Type','integer','LowerBound',0,'UpperBound',1);
zjkr = optimvar('zjkr',j,k,r,'LowerBound',0);
wrj = optimvar('wrj',r,j,'LowerBound',0);
% Objective function
objfun1 = sum(sum(dr0f.*xx1r0f,2).*cr0',1);
objfun2 = sum(sum(corj.*wrj,2),1);
objfun3 = sum(sum(pr0f.*xx1r0f,2),1);
objfun4 = sum(sum(cpjk.*yjk1,2),1);
objfun5 = sum(csl.*xl,2);
costprob.Objective = objfun1 + objfun2 + objfun3 + objfun4 + objfun5;
% Constraints
cons1 = sum(xl,2) >= DE;
cons2 = sum(xl,2)*ones(j,1,r) == sum(zjkr,2);
cons3 = xx1r0f <= csur0f.*yr0f;
cons4 = xl <= ctl.*yl2;
cons5 = xl >= cvl.*yl2;
cons6 = sum(yjk1,2) == ones(j,1);
cons7 = squeeze(sum(zjkr,3)) <= M*yjk1;
cons8 = (1-dr0f).*xx1r0f == xx2r0f;
costprob.Constraints.cons1 = cons1;
costprob.Constraints.cons2 = cons2;
costprob.Constraints.cons3 = cons3;
costprob.Constraints.cons4 = cons4;
costprob.Constraints.cons5 = cons5;
costprob.Constraints.cons6 = cons6;
costprob.Constraints.cons7 = cons7;
costprob.Constraints.cons8 = cons8;
2 Comments
Accepted Answer
Matt J
on 21 Oct 2019
Edited: Matt J
on 21 Oct 2019
You can use prob2struct to obtain most of your problem parameters in solver form,
problem=prob2struct(costprob);
problem=rmfield(problem,'solver');
problem.nvars=numel(problem.lb);
problem.fitnessfcn=@(x) dot(problem.f,x);
x=ga(problem);
However your problem, as currently formulated, has both integer and equality constraints, which ga cannot handle. See here, for guidelines on how to rewrite the problem without equality constraints:
5 Comments
More Answers (0)
See Also
Categories
Find more on Genetic Algorithm 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!