Genetic Algorithm with nonlinear Constraints and Vectorization

3 次查看(过去 30 天)
Hi everyone,
I'm trying to solve an optimization problem with GA:
I have 100 binary variables writen in a vector k (1x100).
min: cost*k', where cost is a vector (1x100)
constraint: quantile(A*k', 0.05) > P, where A is a matrix with 10000x100 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

采纳的回答

Alan Weiss
Alan Weiss 2017-12-5
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
  1 个评论
Stefan M
Stefan M 2017-12-6
Hi Alan,
thanks for your answer. It works perfectly! I've read the documentation but did not look at the constraint function to closely because it returned a value without giving an error.
Thanks again very much for finding my error.

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2017-12-4
编辑:Matt J 2017-12-4
See if this modification makes a difference.
function kosten = costFunc(k, cost)
if isempty(k),
kosten=[];
else
kosten = cost*k';
end
end
  3 个评论
Matt J
Matt J 2017-12-5
编辑:Matt J 2017-12-5
The best thing would be to attach a .mat file to your original post with the variables A,P, and cost so that we can try the optimization ourselves.
Stefan M
Stefan M 2017-12-5
I attached a file. The example is only for N=19, but everything else is the same

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by