フィルターのクリア

Genetic Algorithm with nonlinear Constraints and Vectorization

1 回表示 (過去 30 日間)
Stefan M
Stefan M 2017 年 12 月 4 日
コメント済み: Stefan M 2017 年 12 月 6 日
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 Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by