How to add some special constraints in ga optimization?
Show older comments
Hello everyone, I am currently solving an Integer optimization problem, which involves some special constraints that I have no idea how to deal with them:
Suppose I need to minimize a function f(X), where X is an integer vector only contains "0"s and "1"s, i.e. X=[x1, x2 ... xn];
Constraint 1: for any (2 to n)-th entries of vector X, if the X(i-1) entry is 0 and the sum of (1-X(i-5:i-1))<5, then the X(i) must be 0 (of course, if i<=5, the X(i) must be 0).
Constraint 2: for any (2 to n)-th entries of vector X, if the X(i-1) entry is 1 and the sum of X(i-8:i-1)<8, then the X(i) must be 1 (of course, if i<=8, the X(i) must be 1).
There also is known vector S=[s1,s2 ... sn] in the objective function f(X), each one depends on the corresponding entry of the vector X, and such that:
Constraint 3: suppose s1 is given, for any (2 to n)-th entries of vector S, if X(i-1) is 1, then S(i)=0. If X(i-N+1:i-1) are all '0's and X(i-N) is 1, if N<=6 S(i)=1000; if N>6 S(i)=2000.
P.S. Can I use if-else conditions in the nonlinear constraint function file?
Thank you very much!!
Ying
Accepted Answer
More Answers (1)
1 Comment
Alan Weiss
on 24 Jan 2013
Ah, now I think I understand. Your x(i) are alternating blocks of 1 and 0. Constraint 1 is the length of a block of 0 must be at least 5. Constraint 2 is the length of a block of 1 must be at least 8.
I am still not sure I understand your constraint 3. Either S is given and the x have to satisfy the constraint, or this is a method of calculating S from the x vector. Either way, you simply have to calculate the S vector from any given x vector.
I think the easiest way to calculate the lengths of the blocks of 0 and 1 is to use diff and find.
u = diff(x)
zplace = find(u == -1); % locations of starts of 0 blocks
oplace = find(u == 1); % locations of starts of 1 blocks
% Assume that x(1) = 1. Then zplace(1) < oplace(1).
% The length of the first block of 0 is oplace(1) - zplace(1)
% The length of the first block of 1 is zplace(1) + 1
% zplace and oplace might have the same length, in which case
% the lengths of the 0 blocks is oplace(i) - zplace(i)
% It is possible that zplace might be longer than oplace,
% which means that the last block is zeros.
% The length of the i+1 block of 1 is zplace(i+1) - oplace(i)
I hope you see how to use this idea to calculate your constraints. You might need to check that the last block of 0 or 1 is long enough to meet your constraints, too.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Categories
Find more on Choose a Solver in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!