필터 지우기
필터 지우기

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 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