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

9 views (last 30 days)
(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!

Accepted Answer

Walter Roberson
Walter Roberson on 2 Jul 2020
Nonlinear constraint function must have two outputs: c and ceq . Yours only has one.
  4 Comments
Walter Roberson
Walter Roberson on 2 Jul 2020
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.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!