필터 지우기
필터 지우기

problem with genetic algorithm

조회 수: 3 (최근 30일)
Ahmed Mostafa
Ahmed Mostafa 2024년 4월 24일
댓글: Torsten 2024년 4월 24일
my 6 variables optimization problem always shows this error.
"Optimization terminated: average change in the fitness value less than options. FunctionTolerance and constraint violation is less than options.ConstraintTolerance."
I've tried smaller Constraint Tolerance, increasing population and increasing stall generation but still the same error appears.
and one more thing
Every time I try to run the code this message appears several times and it takes me around five times pressing “run” button.

답변 (1개)

John D'Errico
John D'Errico 2024년 4월 24일
편집: John D'Errico 2024년 4월 24일
Clearly, your constraint function is not evaluating as a real number. It is likely complex, based on the error message. (Though I'm not sure if it might be nan or inf. I think the message generated is different in those cases.) Regardless, LEARN TO USE THE DEBUGGER! We cannot tell what you have done wrong. We don't see your code!
help dbstop
DBSTOP Set breakpoints The DBSTOP function is used to temporarily stop the execution of a program and give the user an opportunity to examine the local workspace. There are several forms to this command. They are: (1) DBSTOP in FILE at LINENO (2) DBSTOP in FILE at LINENO@ (3) DBSTOP in FILE at LINENO@N (4) DBSTOP in FILE at SUBFUN (5) DBSTOP in FILE (6) DBSTOP in FILE at LINENO if EXPRESSION (7) DBSTOP in FILE at LINENO@ if EXPRESSION (8) DBSTOP in FILE at LINENO@N if EXPRESSION (9) DBSTOP in FILE at SUBFUN if EXPRESSION (10) DBSTOP in FILE if EXPRESSION (11) DBSTOP if error (12) DBSTOP if caught error (13) DBSTOP if warning (14) DBSTOP if naninf or DBSTOP if infnan (15) DBSTOP if error IDENTIFIER (16) DBSTOP if caught error IDENTIFIER (17) DBSTOP if warning IDENTIFIER FILE is the name of the file in which you want the MATLAB to stop, specified as a character vector or string scalar. FILE can include a full or partial path to the file (see PARTIALPATH). You can specify a file that is not on the current path by using the keyword -completenames in the command, and specifying FILE as a fully qualified file name. (On Windows, this is a file name that begins with \\ or with a drive letter followed by a colon. On Unix, this is a file name that begins with / or ~.) You can also include a filemarker in FILE to specify the path to a particular subfunction or to a nested function within the same file. LINENO is a line number within FILE, N is an integer specifying the Nth anonymous function on the line, and SUBFUN is the name of a subfunction within FILE. EXPRESSION is an evaluatable conditional expression, specified as a character vector or string scalar. IDENTIFIER is a MATLAB Message Identifier (see help for ERROR for a description of message identifiers). The AT and IN keywords are optional. The forms behave as follows: (1) Stops at line LINENO in the specified file. (2) Stops just after any call to the first anonymous function in the specified line number. (3) As (2), but just after any call to the Nth anonymous function. (4) Stops at the specified subfunction in the specified file. (5) Stops at the first executable line in the specified file. (6-10) As (1)-(5), except that execution stops only if EXPRESSION evaluates to true. EXPRESSION is evaluated (as if by EVAL) in the workspace of the program being debugged. Evaluation takes place when MATLAB encounters the breakpoint. EXPRESSION must evaluate to a scalar logical value (true or false). (11) Causes a stop in any function causing a run-time error that would not be detected within a TRY...CATCH block. You cannot resume execution after an uncaught run-time error. (12) Causes a stop in any function, causing a run-time error that would be detected within a TRY...CATCH block. You can resume execution after a caught run-time error. (13) Causes a stop in any function causing a run-time warning. (14) Causes a stop in any function where an infinite value (Inf) or Not-a-Number (NaN) is detected. (15-17) As (11)-(13), except that MATLAB only stops on an error or warning whose message identifier is IDENTIFIER. (If IDENTIFIER is specified as 'all', then these uses behave exactly like (11)-(13).) When MATLAB reaches a breakpoint, it enters debug mode. The prompt changes to a K>> and, depending on the "Open Files when Debugging" setting in the Debug menu, the debugger window may become active. Any MATLAB command is allowed at the prompt. To resume execution, use DBCONT or DBSTEP. To exit from the debugger, use DBQUIT. See also DBCONT, DBSTEP, DBCLEAR, DBTYPE, DBSTACK, DBUP, DBDOWN, DBSTATUS, DBQUIT, ERROR, EVAL, LOGICAL, PARTIALPATH, TRY, WARNING. Documentation for dbstop doc dbstop
You will probably want t oset a breakpoint using a command like:
dbstop if error
Then look to see what happened. Think about if possibly you need to make the constraints tighter on the problem.
I'm sorry, but if you want a better answer than this, then you need to show your code. Show how it is called.
As far as the first, THAT IS NOT AN ERROR! It merely says it thinks it did as well as it could. Terminated is a synonym for done.
  댓글 수: 2
Ahmed Mostafa
Ahmed Mostafa 2024년 4월 24일
this is the GA solver code
clc,clear,clf
N_v=6;
%no linear constraint
A = [];
b = [];
Aeq = [];
beq = [];
%lower adn upper bondray
%lb = [-1*(10^-8) -1*(10^-8) -1*(10^-8)];
lb = [-0.001 -0.001 -0.001 0 0 0];
ub = [0.0001 0.0001 0.0001 0.1 0.1 0.1];
con = @constr;
% constrains
fun = @obj;
options = optimoptions('ga','PopulationSize',600,'MaxStallGenerations',75,'MaxGenerations',3000,'ConstraintTolerance',1e-4,"FunctionTolerance",1e-6,...
'UseParallel',true,'PlotFcn',{@gaplotbestfun,'gaplotbestindiv','gaplotscorediversity', @gaplotstopping});
[x,fval] = ga(fun,N_v,A,b,Aeq,beq,lb,ub,con,options);
the const and obj fn is very long to post
Torsten
Torsten 2024년 4월 24일
Before calling "ga", insert the command
[c,ceq] = constr((lb+ub)/2)
and check whether c and/or ceq have complex-valued entries.
If yes, you will have to change "constr" to avoid this.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by