"Failure in initial user-supplied nonlinear constraint function evaluation" in a ga() with integer variables and non-linear inequality constraints

(Edit: Please read answers/comments before pointing out code mistakes.)
I am trying to run a GA where the solutions are an array of integer values that are either 0 or 1. The inequalities are not dependent only on the solution variable(s), but also additional variables that I pass along.
Simplyfing things a bit, the setup is:
Each solution "x" contains 100 values. "y" contains two structs. The first 50 values of "x" refer to y(1).data and the other 50 refer to y(2).data. Therefore, when checking the inequalities, the same constraints are applied to each part of "x" (in this case, two parts of 50 values each).
The solutions are subject to ga_constraints function below, where non-linear inequalities are set.
constraints.var1 = 60;
constraints.var2 = 30;
% GA setup (using MATLAB nomenclature for the variables and functions)
ObjectiveFunction = @(x) ga_fitness(x,y,z);
ConstraintFunction = @(x) ga_constraints(x,y,constraints);
nvars = 100; % size of a solution
lb = zeros(1,nvars);
ub = ones(1,nvars);
IntCon = linspace(1,nvars,nvars);
% GA
x = ga(ObjectiveFunction,nvars,[],[],[],[],lb,ub,ConstraintFunction,IntCon);
And the constraint function is something like this:
function c = ga_constraints(x,y,constraints)
% Constraints apply to each satellite separetely
for i = 1:length(y)
% x is split into 5 segments called "x_segment"
% so x_segment is either x[1:50] or x[51:100]
% calculate a value that depends on x_segment and the pertinent structure in y
% aux_min_val = function(x_segment,y)
% Constraint 1
c(i) = - aux_min_val + constraints.var1;
% Constraint 2, basically keeps a maximum number of "ones" in the solution
c(i + length(y)) = sum(x_segment) - constraints.var2;
end
end
For an example of having only y(1) and y(2), the output "c" will be a vector of 4 values.
The error is:
Error using ga (line 393)
Too many output arguments.
Error in ga_main (line 62)
x = ga(ObjectiveFunction,nvars,...
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
Could someone point me to the right direction to investigate what's wrong? I don't know what to do with the error since it doesn't give me any more information than that.
Thanks!

 Respuesta aceptada

Nonlinear constraint function must have two outputs: c and ceq . Yours only has one.

4 comentarios

I found that " In particular, ga does not accept any equality constraints when there are integer variables. "
Should I just leave it as ceq=0 (or ceq=zeros(size(c))) inside the function, instead of omitting it?
Doing this, I get the same error, but the cause seems to be different:
Error using ga (line 393)
Matrix dimensions must agree.
...
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
Both c and ceq would have dimension 4x1. Is this infeasible?
ceq = []
Not enough of the error message was included, and not enough of the constraint function was included, for it to be obvious to me what the problem is.
dbstop if error
and run, and probe around to see what the problem is.
Thank you for the tip on how to probe the constraint function with the error.
There was a 1xn array that should have been nx1 array inside.
I will choose your answer as correct, although I think this question+answer will not be very useful for future users, since the problem was completely a mistake unrelated to the actual ga functionality.
However, that is information too ;-) People who have the same message and find this posting will see what kind of triggers there can be for the message, and can see the debugging steps.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2018b

Preguntada:

el 2 de Jul. de 2020

Comentada:

el 2 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by