Using GA can I put a condition on Population?

3 views (last 30 days)
Rim Abdallah
Rim Abdallah on 4 May 2022
Commented: Rim Abdallah on 5 May 2022
I am using GA, with nvars=32 and IntCon=1:32, so 32 integers
lb=[zeros(1,16),zeros(1,16)];
ub=[60*ones(1,16),4*ones(1,16)]
I want the first 16 elements be ascending and the last 16 elements no matter the order, and both of them respect the lb and ub.
Is there any way to obligate the population be between 0-60 with ascending order for the first 16 elements? (this 16 elements represent time so I can't have one value greater than the next value).

Answers (2)

Alan Weiss
Alan Weiss on 4 May 2022
Sure, that is a simple linear inequality constraint. Probably easiest to represent using the problem-based formulation, but do what you like. In solver-based:
A = zeros(15,32);
for i = 1:15
A(i,i) = 1;
A(i,i+1) = -1; % means x(i) - x(i+1) <= 0
end
end
b = zeros(15,1);
You can use sparse matrices and MATLAB constructs to make this matrix, but this is fast enough.
Alan Weiss
MATLAB mathematical toolbox documentation

Rim Abdallah
Rim Abdallah on 5 May 2022
First thank you Alan for your answer! Unfortunatetly, I used ur code but it doesn' work.
This is my main:
z=16;
first=sort(randi([0,60],1,z));
second=randi([0 4],1,z);
lb =[zeros(1,z),zeros(1,z)];
ub =[60*ones(1,z),4*ones(1,z)];
options = optimoptions('ga','Display','iter','PopulationSize',30,'FunctionTolerance',1e-5,'InitialPopulationMatrix',[first,second]);
nvars=2*z;
IntCon=1:(2*z);
funn = @(x)funct2(x,false); % handle to can cost
conss = @(x)nonlconfunct2(x,false); % handle to can constraints
A = zeros(z,2*z);
for i = 1:z-1
A(i,i) = 1;
A(i,i+1) = -1; % means x(i) - x(i+1) <= 0
end
B = zeros(z,1);
[x,fval,exitFlag,output,population,scores] = ga(funn,nvars,A,B,[],[],lb,ub,conss,IntCon,options);
Kindly check the attachement (result of the population), the population is not in an ascending order from column 1 to z=16.
(Row 12 for example:)
36 9 3 2 8 26 25 5 23 39 42 60 60 58 60 60 0 4 4 4 4 0 4 4 4 4 0 0 2 0 0 0
The first 16 elements: 36 9 3 2 8 26 25 5 23 39 42 60 60 58 60 60 are not in an ascending order.
  2 Comments
Rim Abdallah
Rim Abdallah on 5 May 2022
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance
and constraint violation is less than options.ConstraintTolerance.
Exitflag=1
In fact my solution is correct because inside the functions I used command x1=sort(x(:,1:z),2), but I prefer to have a population with an ascending order for the first 16 elements instead of using sort and than I can reduce the time of running.

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by