How to terminate GA when a criteria is reached

I am using GA to find a local min of a function. I want to terminate iterations when fitness functions reaches a preset value.
x = ga(@AECMS,nvars,[],[],[],[],LB,UB,[],options);
function socf=AECMS(s)
sim HYB_VEH
socf=(soc_sim-soc_des)^2
It should stop when socf<A. BTW, this is different from TolFun or TolX

 Accepted Answer

Use an options structure with an output function and set the state StopFlag to a non-empty string http://www.mathworks.com/help/gads/genetic-algorithm-options.html#f17837

7 Comments

Thanks for your answer. I added this part to my code:
options=gaoptimset('OutputFcns',@stop_iter);
function state= stop_iter(options,state,flag)
if socf<1e-4
state.StopFlag=1;
end
With this option, the iteration still doesn't stop. Is there any mistake?
It's the objective function. I am trying to find local min of this function
x = ga(@AECMS,nvars,[],[],[],[],LB,UB,[],options);
function socf=AECMS(s)
sim HYB_VEH
socf=(soc_sim-soc_des)^2
You must be using a To Workspace block. Checking around, I see that when sim() is used, the workspace written to is the workspace of the function that calls sim(), so soc_sim and soc_des must be local to your AECMS. We will not be able to access those once your AECMS returns.
You will need to be careful that you do not add an "end" corresponding to the "function" for your AECMS function, or else you will get an error about being unable to add variables to a static workspace. Or you could use the matching "end" if you initialize all of the To Workspace variables before you call sim()
function socf = AECMS(s)
soc_sim = [];
soc_des = [];
sim('HYB_VEH'); %uses To Workspace block
socf = (soc_sim - soc_des).^2;
end
-----
As I study GA's output functions more, I see that they are not called every time the objective function is called: they are called once per iteration, where an iteration might include evaluation over each member of the entire population. By examining http://www.mathworks.com/help/gads/creating-a-custom-plot-function.html I see that in the output function, we need to look at the State input's "Score" vector:
function state = stop_iter(options,state,flag)
best_socf = min(state.Score); %find best of this generation
if best_socf < 1e-4
state.StopFlag = 1;
end
Formally the output function should have three outputs. The informal descriptions I read say that only the first one is required, but those informal descriptions could be wrong or reflect an older version. Please go back to the documentation for the output function and alter your output function to return options and the options changed argument. Sorry, I am in a waiting room and cannot presently research what the options changed output should look like.
Perfect. It finally worked. You were right. The output function should have three outputs.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!