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

4 次查看(过去 30 天)
(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!

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by