Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/04/15 14:07:38 (10 years ago)
Author:
pfleck
Message:

#2350

  • Implemented AlpsSsGeneticAlgorithm.
  • Created empty AlpsSsGeneticAlgorithmMainLoop.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsGeneticAlgorithm.cs

    r12119 r12120  
    2121
    2222using System;
     23using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    2526using HeuristicLab.Data;
     27using HeuristicLab.Operators;
    2628using HeuristicLab.Optimization;
     29using HeuristicLab.Optimization.Operators;
    2730using HeuristicLab.Parameters;
    2831using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.PluginInfrastructure;
     33using HeuristicLab.Random;
     34using HeuristicLab.Selection;
    2935
    3036namespace HeuristicLab.Algorithms.ALPS.SteadyState {
     
    3440  public class AlpsSsGeneticAlgorithm : Alps {
    3541    #region Parameter Properties
    36     // TODO
     42    private IValueParameter<IntArray> PopulationSizeParameter {
     43      get { return (IValueParameter<IntArray>)Parameters["PopulationSize"]; }
     44    }
     45    private IValueParameter<IntValue> MaximumGenerationsParameter {
     46      get { return (IValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
     47    }
     48    public IConstrainedValueParameter<ISelector> SelectorParameter {
     49      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
     50    }
     51    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
     52      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
     53    }
     54    private IValueParameter<PercentValue> MutationProbabilityParameter {
     55      get { return (IValueParameter<PercentValue>)Parameters["MutationProbability"]; }
     56    }
     57    public IConstrainedValueParameter<IManipulator> MutatorParameter {
     58      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
     59    }
     60    private IValueParameter<IntValue> ElitesParameter {
     61      get { return (IValueParameter<IntValue>)Parameters["Elites"]; }
     62    }
     63    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
     64      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
     65    }
    3766    #endregion
    3867
    3968    #region Properties
    40     // TODO
     69    public IntArray PopulationSize {
     70      get { return PopulationSizeParameter.Value; }
     71      set { PopulationSizeParameter.Value = value; }
     72    }
     73    public IntValue MaximumGenerations {
     74      get { return MaximumGenerationsParameter.Value; }
     75      set { MaximumGenerationsParameter.Value = value; }
     76    }
     77
     78    public ISelector Selector {
     79      get { return SelectorParameter.Value; }
     80      set { SelectorParameter.Value = value; }
     81    }
     82    public ICrossover Crossover {
     83      get { return CrossoverParameter.Value; }
     84      set { CrossoverParameter.Value = value; }
     85    }
     86    public PercentValue MutationProbability {
     87      get { return MutationProbabilityParameter.Value; }
     88      set { MutationProbabilityParameter.Value = value; }
     89    }
     90    public IManipulator Mutator {
     91      get { return MutatorParameter.Value; }
     92      set { MutatorParameter.Value = value; }
     93    }
     94    public IntValue Elites {
     95      get { return ElitesParameter.Value; }
     96      set { ElitesParameter.Value = value; }
     97    }
     98    public bool ReevaluteElites {
     99      get { return ReevaluateElitesParameter.Value.Value; }
     100      set { ReevaluateElitesParameter.Value.Value = value; }
     101    }
    41102    #endregion
    42103
     
    54115    public AlpsSsGeneticAlgorithm()
    55116      : base() {
    56 
    57       // TODO
    58 
     117      Parameters.Add(new ValueParameter<IntArray>("PopulationSize", "The size of the population of solutions each layer.", new IntArray(new[] { 100 })));
     118      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
     119      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
     120      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
     121      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
     122      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
     123      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
     124      Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
     125      AgeInheritance = new ReductionOperation(ReductionOperations.Min);
     126
     127      var randomCreator = new RandomCreator();
     128      var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
     129      var layer0Processor = new LayerUniformSubScopesProcessor();
     130      var layer0VariableCreator = new VariableCreator();
     131      var layer0SolutionsCreator = new SolutionsCreator();
     132      var initializeAgeProcessor = new UniformSubScopesProcessor();
     133      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
     134      var initializeLocalEvaluatedSolutions = new SubScopesCounter() { Name = "Initialize LayerEvaluatedSolutions" };
     135      var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
     136      var resultsCollector = new ResultsCollector();
     137      var mainLoop = new AlpsSsGeneticAlgorithmMainLoop();
     138
     139      OperatorGraph.InitialOperator = randomCreator;
     140
     141      randomCreator.SeedParameter.Value = null;
     142      randomCreator.SetSeedRandomlyParameter.Value = null;
     143      randomCreator.Successor = layer0Creator;
     144
     145      layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
     146      layer0Creator.Successor = layer0Processor;
     147
     148      layer0Processor.Operator = layer0VariableCreator;
     149      layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
     150
     151      layer0VariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
     152      layer0VariableCreator.Successor = layer0SolutionsCreator;
     153
     154      layer0SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
     155      layer0SolutionsCreator.Successor = initializeAgeProcessor;
     156
     157      initializeAgeProcessor.Operator = initializeAge;
     158      initializeAgeProcessor.Successor = initializeLocalEvaluatedSolutions;
     159
     160      initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
     161
     162      initializeLocalEvaluatedSolutions.ValueParameter.ActualName = "LayerEvaluatedSolutions";
     163
     164      initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
     165      initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
     166      initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
     167      initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
     168      initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
     169
     170      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
     171      resultsCollector.Successor = mainLoop;
     172
     173      foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
     174        SelectorParameter.ValidValues.Add(selector);
     175      var tournamentSelector = SelectorParameter.ValidValues.OfType<TournamentSelector>().FirstOrDefault();
     176      if (tournamentSelector != null) {
     177        tournamentSelector.GroupSizeParameter.Value = new IntValue(5);
     178        SelectorParameter.Value = tournamentSelector;
     179      }
     180
     181      ParameterizeSelectors();
    59182      Initialize();
    60183    }
    61184
    62185    #region Events
     186    protected override void OnProblemChanged() {
     187      base.OnProblemChanged();
     188      ParameterizeSolutionsCreator();
     189      ParameterizeMainLoop();
     190      ParameterizeSelectors();
     191      ParameterizeIterationBasedOperators();
     192      UpdateCrossovers();
     193      UpdateMutators();
     194    }
     195    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
     196      base.Problem_SolutionCreatorChanged(sender, e);
     197      ParameterizeSolutionsCreator();
     198    }
     199    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
     200      base.Problem_EvaluatorChanged(sender, e);
     201      ParameterizeSolutionsCreator();
     202      ParameterizeMainLoop();
     203      ParameterizeSelectors();
     204    }
     205    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
     206      base.Problem_OperatorsChanged(sender, e);
     207      ParameterizeIterationBasedOperators();
     208      UpdateCrossovers();
     209      UpdateMutators();
     210    }
     211    protected override void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
     212      base.Evaluator_QualityParameter_ActualNameChanged(sender, e);
     213      ParameterizeMainLoop();
     214      ParameterizeSelectors();
     215    }
    63216    #endregion
    64217
    65218    #region Parameterization
    66219    private void Initialize() {
    67       // TODO
    68     }
     220      //
     221    }
     222    private void ParameterizeSolutionsCreator() {
     223      //MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     224      //MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
     225    }
     226    private void ParameterizeMainLoop() {
     227      //MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     228      //MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     229      //MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     230      //MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     231      //MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     232      //MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
     233    }
     234    private void ParameterizeSelectors() {
     235      foreach (var selector in SelectorParameter.ValidValues) {
     236        selector.CopySelected = new BoolValue(true);
     237        // Explicit setting of NumberOfSelectedSubScopesParameter is not required anymore because the NumberOfSelectedSubScopesCalculator calculates it itself
     238        //selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize - Elites.Value));
     239        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
     240        ParameterizeStochasticOperatorForLayer(selector);
     241      }
     242      if (Problem != null) {
     243        foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
     244          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     245          selector.MaximizationParameter.Hidden = true;
     246          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     247          selector.QualityParameter.Hidden = true;
     248        }
     249      }
     250    }
     251    private void ParameterizeIterationBasedOperators() {
     252      if (Problem != null) {
     253        foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
     254          @operator.IterationsParameter.ActualName = "Generations";
     255          @operator.IterationsParameter.Hidden = true;
     256          @operator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
     257          @operator.MaximumIterationsParameter.Hidden = true;
     258        }
     259      }
     260    }
     261
     262    protected override void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
     263      var stochasticOperator = @operator as IStochasticOperator;
     264      if (stochasticOperator != null) {
     265        stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.Name;
     266        stochasticOperator.RandomParameter.Hidden = true;
     267      }
     268    }
     269
    69270    #endregion
    70271
    71272    #region Updates
     273    private void UpdateCrossovers() {
     274      var oldCrossover = CrossoverParameter.Value;
     275      var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
     276      CrossoverParameter.ValidValues.Clear();
     277      foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
     278        ParameterizeStochasticOperatorForLayer(crossover);
     279        CrossoverParameter.ValidValues.Add(crossover);
     280      }
     281      if (oldCrossover != null) {
     282        var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
     283        if (crossover != null)
     284          CrossoverParameter.Value = crossover;
     285        else
     286          oldCrossover = null;
     287      }
     288      if (oldCrossover == null && defaultCrossover != null)
     289        CrossoverParameter.Value = defaultCrossover;
     290    }
     291    private void UpdateMutators() {
     292      var oldMutator = MutatorParameter.Value;
     293      MutatorParameter.ValidValues.Clear();
     294      foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
     295        ParameterizeStochasticOperatorForLayer(mutator);
     296        MutatorParameter.ValidValues.Add(mutator);
     297      }
     298      if (oldMutator != null) {
     299        var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
     300        if (mutator != null)
     301          MutatorParameter.Value = mutator;
     302      }
     303    }
    72304    #endregion
    73305  }
Note: See TracChangeset for help on using the changeset viewer.