Guest User

Untitled

a guest
Feb 16th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.60 KB | None | 0 0
  1. function [x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,GenomeLength, ...
  2.     Aineq,bineq,Aeq,beq,lb,ub,options,output,Iterate)
  3. %GALINCON Genetic algorithm linear constrained solver.
  4. %   GALINCON solves problems of the form:
  5. %           min F(X)    subject to:      A*x <= b
  6. %            X                          Aeq*x = beq
  7. %                                      LB <= X <= UB
  8. %   Private function to GA
  9.  
  10. %   Copyright 2005-2011 The MathWorks, Inc.
  11. %   $Revision: 1.1.6.6 $  $Date: 2011/05/09 00:50:54 $
  12.  
  13. % Initialize output args
  14. x = []; fval = []; exitFlag = [];
  15. LinearConstr = options.LinearConstr;
  16.  
  17. % Create initial state: population, scores, status data
  18. state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
  19. % Determine who is the caller
  20. callStack = dbstack;
  21. caller = callStack(2).file(1:end-2);
  22.  
  23. % Set state for plot and output functions (only gacon will have
  24. % 'interrupt' state)
  25. if ~strcmp(caller,'gacon')
  26.     currentState = 'init';
  27. else
  28.     currentState = 'interrupt';
  29. end
  30. % Give the plot/output Fcns a chance to do any initialization they need.
  31. state = gadsplot(options,state,currentState,'Genetic Algorithm');
  32. [state,options] = gaoutput(FitnessFcn,options,state,currentState);
  33.  
  34. % Setup display header
  35. if  options.Verbosity > 1
  36.     fprintf('\n                               Best           Mean      Stall\n');
  37.     fprintf('Generation      f-count        f(x)           f(x)    Generations\n');
  38. end
  39.  
  40. % Set state for plot and output functions (only gacon will have
  41. % 'interrupt' state)
  42. if ~strcmp(caller,'gacon')
  43.     currentState = 'iter';
  44. else
  45.     currentState = 'interrupt';
  46. end
  47. % Run the main loop until some termination condition becomes true
  48. while isempty(exitFlag)
  49.     state.Generation = state.Generation + 1;
  50.     % Repeat for each subpopulation (element of the populationSize vector)
  51.     offset = 0;
  52.     totalPop = options.PopulationSize;
  53.     % Each sub-population loop
  54.     for pop = 1:length(totalPop)
  55.         populationSize =  totalPop(pop);
  56.         thisPopulation = 1 + (offset:(offset + populationSize - 1));
  57.         population = state.Population(thisPopulation,:);
  58.         score = state.Score( thisPopulation );
  59.         % Empty population is also possible
  60.         if isempty(thisPopulation)
  61.             continue;
  62.         end
  63.         [score,population,state] = stepGA(score,population,options,state,GenomeLength,FitnessFcn);
  64.         % Store the results for this sub-population
  65.         state.Population(thisPopulation,:) = population;
  66.         state.Score(thisPopulation) = score;
  67.         offset = offset + populationSize;
  68.     end
  69.  
  70.     % Remember the best score
  71.     best = min(state.Score);
  72.     generation = state.Generation;
  73.     state.Best(generation) = best;
  74.     % Keep track of improvement in the best
  75.     if((generation > 1) && isfinite(best))
  76.         if(state.Best(generation-1) > best)
  77.             state.LastImprovement = generation;
  78.             state.LastImprovementTime = cputime;
  79.         end
  80.     end
  81.     % Do any migration
  82.     state = migrate(FitnessFcn,GenomeLength,options,state);
  83.     % Update the Output
  84.     state = gadsplot(options,state,currentState,'Genetic Algorithm');
  85.     [state,options,optchanged] = gaoutput(FitnessFcn,options,state,currentState);
  86.     if optchanged
  87.         options.LinearConstr = LinearConstr;
  88.     end
  89.     % Check to see if any stopping criteria have been met
  90.     [exitFlag,output.message] = isItTimeToStop(options,state);
  91. end % End while loop
  92.  
  93. % Find and return the best solution
  94. [fval,best] = min(state.Score);
  95. x = state.Population(best,:);
  96.  
  97. % Update output structure
  98. output.generations = state.Generation;
  99. output.funccount   = state.FunEval;
  100. output.maxconstraint = norm([Aeq*x'-beq; max([Aineq*x' - bineq;x' - ub(:);lb(:) - x'],0)],Inf);
  101. population = state.Population;
  102. scores = state.Score;
  103.  
  104. % Call hybrid function
  105. if ~isempty(options.HybridFcn)
  106.     if  strcmpi(options.PopulationType,'doubleVector')
  107.         [x,fval] = callHybridFunction;
  108.     else
  109.         warning(message('globaloptim:galincon:notValidHybrid'));
  110.     end
  111. end
  112. % Set state for plot and output functions (only gacon will have
  113. % 'interrupt' state)
  114. if ~strcmp(caller,'gacon')
  115.     currentState = 'done';
  116. else
  117.     currentState = 'interrupt';
  118. end
  119. % give the Output functions a chance to finish up
  120. gadsplot(options,state,currentState,'Genetic Algorithm');
  121. gaoutput(FitnessFcn,options,state,currentState);
  122.  
  123. %-----------------------------------------------------------------
  124. % Hybrid function
  125.     function [xhybrid,fhybrid] = callHybridFunction
  126.         xhybrid = x;
  127.         fhybrid = fval;
  128.         % Who is the hybrid function
  129.         if isa(options.HybridFcn,'function_handle')
  130.             hfunc = func2str(options.HybridFcn);
  131.         else
  132.             hfunc = options.HybridFcn;
  133.         end
  134.         % Inform about hybrid scheme
  135.         if   options.Verbosity > 1
  136.             fprintf('%s%s%s\n','Switching to the hybrid optimization algorithm (',upper(hfunc),').');
  137.         end
  138.         % Create functions handle to be passed to hybrid function
  139.         FitnessHybridFcn = @(x) FitnessFcn(x,options.FitnessFcnArgs{:});
  140.         ConstrHybridFcn = [];
  141.         if ~any(strcmpi(hfunc,{'fmincon', 'patternsearch'}))
  142.             warning(message('globaloptim:galincon:unconstrainedHybridFcn',upper(hfunc),'FMINCON'));
  143.             hfunc = 'fmincon';
  144.         end
  145.         [x_temp,f_temp,funccount,theMessage,conviol_temp] = ...
  146.             callHybrid(hfunc,FitnessHybridFcn,x,options.HybridFcnArgs,Aineq,bineq,Aeq,beq,lb,ub,ConstrHybridFcn);
  147.         output.funccount = output.funccount + funccount;
  148.         output.message   = sprintf([output.message '\n', theMessage '\n']);
  149.         hybridPointFeasible = isHybridPointFeasible(conviol_temp, ...
  150.             hfunc, options.HybridFcnArgs{:});
  151.         % We have to recheck whether the final point returned from GA is
  152.         % feasible because GA asserts that the final point is always
  153.         % feasible. If a user supplies their own operators, this may not be
  154.         % the case. We could replace this code if output.maxconstraint
  155.         % reflects the true constraint violation.
  156.         tol = max(sqrt(eps),sqrt(options.TolCon));        
  157.         gaPointFeasible = isTrialFeasible(x(:), Aineq, bineq, ...
  158.             Aeq, beq, lb, ub, tol);
  159.         if hybridPointFeasible && (~gaPointFeasible || f_temp < fhybrid)
  160.             fhybrid = f_temp;
  161.             xhybrid = x_temp;
  162.         end
  163.         % Inform about hybrid scheme termination
  164.         if  options.Verbosity > 1
  165.             fprintf('%s%s\n',upper(hfunc), ' terminated.');
  166.         end
  167.     end % End of callHybridFunction
  168. end  % End of galincon
Advertisement
Add Comment
Please, Sign In to add comment