MATLAB: Genetic Algorithm with nonlinear Constraints and Vectorization

genetic algorithmGlobal Optimization ToolboxnonlinearOptimization Toolboxvectorization

Hi everyone,
I'm trying to solve an optimization problem with GA:
I have 100 binary variables writen in a vector k (1×100).
min: cost*k', where cost is a vector (1×100)
constraint: quantile(A*k', 0.05) > P, where A is a matrix with 10000×100 and P is a fixed value
Solving the problem without vectorization works perfectly, but if I'm setting 'UseVectorized' to true, it gives the following errors:
Warning: This concatenation operation includes an empty array with an incorrect number of rows.
Concatenation including empty arrays will require all arrays to have the same number of rows in a future release.
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
Here is the compact code for my problem:
N = 100;
ObjectiveFunction = @(k)costFunc(k, cost);
nvars = N; % Number of variables
LB = zeros(1, N); % Lower bound
UB = ones(1, N); % Upper bound
ConstraintFunction = @(k)powerConstraint(k, A, P);
IntCon = 1:N;
options = optimoptions(@ga,'MaxStallGenerations',20,'FunctionTolerance',1e-10,...
'MaxGenerations',300, 'PopulationSize', 400, ...
'UseVectorized', true);
[k_best,kosten_best] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
ConstraintFunction, IntCon, options);
function kosten = costFunc(k, cost)
kosten = cost*k';
end
function [c, ceq] = powerConstraint (k, A, P)
P_risk = quantile(A*k', 0.05);
c = P - P_risk;
ceq = [];
end
I discovered that Matlab exits with the Error, after an empty vector k is passed to the objective function.
EDIT: I attached a .mat file with an example for A, P and cost. The example is a little smaller, so one has to set N=19.
Thanks in advance for your help

Best Answer

Thank you for including a file so we could reproduce the issue.
The problem is that the quantile function returns a row vector, as documented. But a vectorized ga call wants a column, as documented but perhaps not documented so clearly. So change the constraint function to return a column:
function [c, ceq] = powerConstraint (k, A, P)
P_risk = quantile(A*k', 0.05);
P_risk = P_risk';
c = P - P_risk;
ceq = [];
end
Alan Weiss
MATLAB mathematical toolbox documentation