How to add some special constraints in ga optimization?

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

The answer to your specific question is yes, of course, you can use if-else or any other MATLAB statement in a nonlinear constraint function.
As far as your constraints, it seems to me that constraint 1 states that for any i >= 5, x(i) can be 1 only if all previous x(j) = 1 for j < i. Is this true? My statement depends on all x(j) = 0 or 1. So the first x(i-1) that = 0 causes the sum x(i-5:i-1) to be 4, and makes x(i) = 0, and this continues.
If this is true, then constraint 2 is superfluous.
In your constraint 3, it seems to me impossible that "the sum of X(i-6:i-1)>6" because all the x(i) are either 0 or 1, so the sum is bounded above by 6.
Alan Weiss
MATLAB mathematical toolbox documentation

More Answers (1)

YING
YING on 24 Jan 2013
Edited: YING on 24 Jan 2013
Dear Alan Weiss:
Thank you for your reply and sorry for the mis-understanding. to make my self clear I am going to give examples:
For the constraint 1: if x(14) is 0, then x(15) has to be 0 only if x(10:14) are NOT ALL '0's, which mean that sum of (1-x(10:14))<5.
Similar for the the constraint 2: if x(10) is 1, then x(11) has to be 1 only if x(3:10) are NOT ALL '1's, which mean that sum of x(3:10)<8.
For the constraint 3, I made mistake last time and I already corrected it, here is the example:
if x(8:15) are [1 1 0 0 0 0 0 0] then S(16)=1000, because there are 6 consecutive '0's, on the other hand, if x(8:15) are [1 0 0 0 0 0 0 0] then S(16)=2000, because there are more than 6 consecutive '0's.
I try to program a nonlinear constraint function for constraint 1 and 2, but it returns an error says: "Subscripted assignment dimension mismatch."
lower bound of x is 0, the upper bound of x is 1, Intcon=[1:24]
function [c,ceq] = nonconstr(x)
ceq=[];
n=1;
for i=2:24
gap_1=min(8,i);
gap_0=min(5,i);
v_1=zeros(24,1); v_1(t-gap_1:i-1)=1;
v_0=zeros(24,1); v_0(t-gap_0:i-1)=1;
if x(i-1)==1 & x*v_1<8
c(n)=-x(i)+1;
n=n+1;
elseif x(i-1)==0 & (1-x)*v_0<5
c(n)=x(i);
n=n+1;
end
end
Regards
Ying

1 Comment

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

Sign in to comment.

Products

Asked:

on 22 Jan 2013

Community Treasure Hunt

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

Start Hunting!