MATLAB: GA not adhering to specified constraints

galinear constraintsoptimization

Hi,
I'm trying to solve an optimization problem with around 80 variables. I also want to ensure that these 80 variables sum up to a certain value. So when I write my GA I do it like so:
LB = .05e-6*ones(1,nvars)
UB = 1e-6*ones(1,nvars)
Length = 25e-6;
Aeq = ones(1,nvars)
beq = [Length]
opts = gaoptimset('OutputFcns',@myOutFun);
[x,fval,exitFlag,Output,population] = ga(@grating_cost_function_arb,nvars,[],[],Aeq,beq,LB,UB,[],opts);
But when, in my cost function, I print out the generated values (and the sum of them) I get a value that's way below the specified linear equality (e.g. instead of getting a number near 25e-6 I get a number near 4.95e-6). So I thought maybe it's too hard to satisfy this linear equality so I also tried two linear inequalities like so:
LB = .05e-6*ones(1,nvars)
UB = 1e-6*ones(1,nvars)
MinLength = -24.5e-6;
MaxLength = 25e-6;
A = ones(1,nvars)
A(1,:) = -1*A(1,:)
b = [MinLength; MaxLength]
opts = gaoptimset('OutputFcns',@myOutFun);
[x,fval,exitFlag,Output,population] = ga(@grating_cost_function_arb,nvars,A,b,[],[],LB,UB,[],opts);
But when I do this I get a number way above my linear inequality range (e.g. the sum of the widths was once 43.9e-6). Is there some reason my GA is not adhering to/satisfying my linear constraints? Or is there a better way to force the GA to produce widths that sum up to a specified value?
Thanks Yanni

Best Answer

Use gaoptimset to set the ConstrainTolerance or TolCon parameter to your needs