Azzera filtri
Azzera filtri

Genetic Algorithm with nonlinear Constraints and Vectorization

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Alan Weiss
Alan Weiss il 5 Dic 2017
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 Commento
Stefan M
Stefan M il 6 Dic 2017
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.

Accedi per commentare.

Più risposte (1)

Matt J
Matt J il 4 Dic 2017
Modificato: Matt J il 4 Dic 2017
See if this modification makes a difference.
function kosten = costFunc(k, cost)
if isempty(k),
kosten=[];
else
kosten = cost*k';
end
end
  3 Commenti
Matt J
Matt J il 5 Dic 2017
Modificato: Matt J il 5 Dic 2017
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 il 5 Dic 2017
I attached a file. The example is only for N=19, but everything else is the same

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by