source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainOperator.cs @ 13113

Last change on this file since 13113 was 13113, checked in by pfleck, 7 years ago

#2269 Some small changes.

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Algorithms.GeneticAlgorithm;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.ALPS {
32  public static class AlpsGeneticAlgorithmMainOperator {
33    public static GeneticAlgorithmMainLoop Create() {
34      var mainLoop = new GeneticAlgorithmMainLoop();
35
36      var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
37      var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
38      var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>().First();
39      var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
40
41      // Operator starts with calculating number of selected scopes base on plus/comma-selection replacement scheme
42      var numberOfSelectedSubScopesCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = 2 * (PopulationSize - (PlusSelection ? 0 : Elites))" };
43      var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };
44
45      // Set new initial operator
46      mainLoop.OperatorGraph.InitialOperator = numberOfSelectedSubScopesCalculator;
47
48      numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
49      numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Elites"));
50      numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<BoolValue>("PlusSelection"));
51      numberOfSelectedSubScopesCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
52      numberOfSelectedSubScopesCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 0 Elites PlusSelection if - 2 * toint");
53      numberOfSelectedSubScopesCalculator.Successor = selector;
54
55      // Use Elitism or Plus-Selection as replacement strategy
56      var selectedProcessor = (SubScopesProcessor)selector.Successor;
57      var elitismReplacement = selectedProcessor.Successor;
58      selectedProcessor.Successor = replacementBranch;
59      replacementBranch.ConditionParameter.ActualName = "PlusSelection";
60      replacementBranch.FalseBranch = elitismReplacement;
61
62      // Plus selection replacement
63      var replacementMergingReducer = new MergingReducer();
64      var replacementBestSelector = new BestSelector();
65      var replacementRightReducer = new RightReducer();
66      replacementBranch.TrueBranch = replacementMergingReducer;
67
68      replacementMergingReducer.Successor = replacementBestSelector;
69
70      replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = "PopulationSize";
71      replacementBestSelector.CopySelected = new BoolValue(false);
72      replacementBestSelector.Successor = replacementRightReducer;
73
74      replacementRightReducer.Successor = null;
75
76      // Increment ages of all individuals after replacement
77      var incrementAgeProcessor = new UniformSubScopesProcessor();
78      var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
79      replacementBranch.Successor = incrementAgeProcessor;
80      incrementAgeProcessor.Operator = ageIncrementor;
81      incrementAgeProcessor.Successor = null;
82      ageIncrementor.ValueParameter.ActualName = "Age";
83      ageIncrementor.Increment = new DoubleValue(1.0);
84
85      // Insert AgeCalculator between crossover and its successor
86      var crossoverSuccessor = crossover.Successor;
87      var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };
88      crossover.Successor = ageCalculator;
89
90      ageCalculator.ParameterToReduce.ActualName = "Age";
91      ageCalculator.TargetParameter.ActualName = "Age";
92      ageCalculator.WeightParameter.ActualName = "AgeInheritance";
93      ageCalculator.Successor = crossoverSuccessor;
94
95      // When counting the evaluated solutions, write in LayerEvaluatedSolutions
96      subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
97      subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
98
99      // Instead of generational loop after merging of elites, stop
100      elitesMerger.Successor = null;
101
102      // Parameterize
103      foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
104        stochasticOperator.RandomParameter.ActualName = "LocalRandom";
105      foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
106        stochasticBranch.RandomParameter.ActualName = "LocalRandom";
107
108      // Remove unused operators
109      var usedOperators = mainLoop.OperatorGraph.Iterate();
110      var unusedOperators = mainLoop.OperatorGraph.Operators.Except(usedOperators);
111      foreach (var op in unusedOperators.ToList())
112        mainLoop.OperatorGraph.Operators.Remove(op);
113
114      return mainLoop;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.