?? galincon.m
字號:
function [x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,GenomeLength, ... Aineq,bineq,Aeq,beq,lb,ub,options,output,Iterate)%GALINCON Genetic algorithm linear constrained solver.% GALINCON solves problems of the form:% min F(X) subject to: A*x <= b% X Aeq*x = beq% LB <= X <= UB% Private function to GA% Copyright 2005-2007 The MathWorks, Inc.% $Revision: 1.1.6.11 $ $Date: 2007/12/10 21:35:48 $% Initialize output argsx = []; fval = []; exitFlag = [];LinearConstr = options.LinearConstr;% Create initial state: population, scores, status datastate = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);% Determine who is the callercallStack = dbstack;caller = callStack(2).file(1:end-2);% Set state for plot and output functions (only gacon will have% 'interrupt' state)if ~strcmp(caller,'gacon') currentState = 'init';else currentState = 'interrupt';end% Give the plot/output Fcns a chance to do any initialization they need.state = gadsplot(options,state,currentState,'Genetic Algorithm');[state,options] = gaoutput(FitnessFcn,options,state,currentState);% Setup display headerif options.Verbosity > 1 fprintf('\n Best Mean Stall\n'); fprintf('Generation f-count f(x) f(x) Generations\n');end% Set state for plot and output functions (only gacon will have% 'interrupt' state)if ~strcmp(caller,'gacon') currentState = 'iter';else currentState = 'interrupt';end% Run the main loop until some termination condition becomes truewhile isempty(exitFlag) state.Generation = state.Generation + 1; % Repeat for each subpopulation (element of the populationSize vector) offset = 0; totalPop = options.PopulationSize; % Each sub-population loop for pop = 1:length(totalPop) populationSize = totalPop(pop); thisPopulation = 1 + (offset:(offset + populationSize - 1)); population = state.Population(thisPopulation,:); score = state.Score( thisPopulation ); % Empty population is also possible if isempty(thisPopulation) continue; end [score,population,state] = stepGA(score,population,options,state,GenomeLength,FitnessFcn); % Store the results for this sub-population state.Population(thisPopulation,:) = population; state.Score(thisPopulation) = score; offset = offset + populationSize; end % Remember the best score best = min(state.Score); generation = state.Generation; state.Best(generation) = best; % Keep track of improvement in the best if((generation > 1) && isfinite(best)) if(state.Best(generation-1) > best) state.LastImprovement = generation; state.LastImprovementTime = cputime; end end % Do any migration state = migrate(FitnessFcn,GenomeLength,options,state); % Update the Output state = gadsplot(options,state,currentState,'Genetic Algorithm'); [state,options,optchanged] = gaoutput(FitnessFcn,options,state,currentState); if optchanged options.LinearConstr = LinearConstr; end % Check to see if any stopping criteria have been met [exitFlag,output.message] = isItTimeToStop(options,state);end % End while loop% Find and return the best solution[fval,best] = min(state.Score);x = state.Population(best,:);% Update output structureoutput.generations = state.Generation;output.funccount = state.FunEval;output.maxconstraint = 0.0;population = state.Population;scores = state.Score;% Call hybrid functionif ~isempty(options.HybridFcn) if strcmpi(options.PopulationType,'doubleVector') [x,fval] = callHybridFunction; else warning('gads:galincon:notValidHybrid','''HybridFcn'' can only be used with ''doubleVector'' population; ignoring ''HybridFcn'' option'); endend% Set state for plot and output functions (only gacon will have% 'interrupt' state)if ~strcmp(caller,'gacon') currentState = 'done';else currentState = 'interrupt';end% give the Output functions a chance to finish upgadsplot(options,state,currentState,'Genetic Algorithm');gaoutput(FitnessFcn,options,state,currentState);%-----------------------------------------------------------------% Hybrid function function [xhybrid,fhybrid] = callHybridFunction xhybrid = x; fhybrid = fval; % Who is the hybrid function if isa(options.HybridFcn,'function_handle') hfunc = func2str(options.HybridFcn); else hfunc = options.HybridFcn; end % Inform about hybrid scheme if options.Verbosity > 1 fprintf('%s%s%s\n','Switching to the hybrid optimization algorithm (',upper(hfunc),').'); end % Create functions handle to be passed to hybrid function FitnessHybridFcn = @(x) FitnessFcn(x,options.FitnessFcnArgs{:}); ConstrHybridFcn = []; if ~any(strcmpi(hfunc,{'fmincon', 'patternsearch'})) msg = sprintf('%s is not a constrained solver',upper(hfunc)); msg = [msg, sprintf('\n%s',' using constrained solver FMINCON as hybrid function.')]; warning('gads:galincon:unconstrainedHybridFcn',msg); hfunc = 'fmincon'; end [x_temp,f_temp,funccount,message,e] = callHybrid(hfunc,FitnessHybridFcn,x,options.HybridFcnArgs,Aineq,bineq,Aeq,beq,lb,ub,ConstrHybridFcn); output.funccount = output.funccount + funccount; output.message = sprintf([output.message '\n', message '\n']); % Check for exitflag and fval if f_temp < fhybrid && e > 0 fhybrid = f_temp; xhybrid = x_temp; end % Inform about hybrid scheme termination if options.Verbosity > 1 fprintf('%s%s\n',upper(hfunc), ' terminated.'); end end % End of callHybridFunctionend % End of galincon
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -