i am a new to genetic algorithm and want to know how to write the fitness function and constarints for the genetic algorithm with default optimisation settings. i am attaching the codes written for MILP problem as below;

3 views (last 30 days)
tcost=[81 92 101 130 115;117 77 108 98 100;102 105 95 119 111;115 125 90 59 74;142 100 103 105 71;81 92 101 130 115;117 77 108 98 100;102 105 95 119 111;115 125 90 59 74;142 100 103 105 71]; demand=[12;8;14;16;7]; fixedcost=[6000,4500,6500,4100,4000,9000,6750,9750,6150,6000]; capacity=[10;10;10;10;10;20;20;20;20;20]; S=10; M=5; obj1 = zeros(1,S); obj2=zeros(S,M); for ii = 1:S obj1(ii) = fixedcost(ii); end
for ii = 1:S for jj = 1:M obj2(ii,jj) = tcost(ii,jj); end end obj=[obj1(:);obj2(:)]; matwid = length(obj); Aineq = spalloc(S,matwid,S*(M+1)); % Allocate sparse Aeq bineq=zeros(S,1); clearer1 = zeros(size(obj1)); clearer12 = clearer1(:); clearer2 = zeros(size(obj2)); clearer22 = clearer2(:); counter = 1; for ii = 1:S xtemp = clearer1; xtemp(ii)=-capacity(ii); xtemp1=clearer2; xtemp1(ii,:)=1; xtemp = sparse([xtemp(:);xtemp1(:)]); % Convert to sparse Aineq(counter,:) = xtemp'; % Fill in the row counter = counter + 1; end
Aeq = spalloc(M,matwid,S*M); % Allocate sparse Aeq beq = zeros(M,1); % Allocate bineq as full
counter = 1; for ii = 1:M xtemp = clearer2; xtemp(:,ii)=1; xtemp = sparse([clearer12;xtemp(:)]); Aeq(counter,:) = xtemp'; beq(counter)=demand(ii); counter = counter + 1; end intcon=1:S; lb=zeros(length(obj),1); ub=Inf(length(obj),1); ub(1:S)=1; x = intlinprog(obj,intcon,Aineq,bineq,Aeq,beq,lb,ub); [x,objval] = intlinprog(obj,intcon,Aineq,bineq,Aeq,beq,lb,ub);
  1 Comment
Alan Weiss
Alan Weiss on 28 Nov 2016
  1. Mark up your code using the {} Code button so that we can read it more easily.
  2. What is your question? if it is a question in how to write fitness or nonlinear constraint functions, then see Compute Objective Functions and also Write Constraints.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!