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

조회 수: 14 (최근 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