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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Optimization.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Selection;
|
---|
30 |
|
---|
31 | namespace 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 numberOfSubScopesBranch = new ConditionalBranch() { Name = "PlusSelection?" };
|
---|
43 | var numberOfSelectedSubScopesPlusCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = PopulationSize * 2" };
|
---|
44 | var numberOfSelectedSubScopesCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = (PopulationSize - Elites) * 2" };
|
---|
45 | var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };
|
---|
46 |
|
---|
47 | // Set new initial operator
|
---|
48 | mainLoop.OperatorGraph.InitialOperator = numberOfSubScopesBranch;
|
---|
49 |
|
---|
50 | numberOfSubScopesBranch.ConditionParameter.ActualName = "PlusSelection";
|
---|
51 | numberOfSubScopesBranch.TrueBranch = numberOfSelectedSubScopesPlusCalculator;
|
---|
52 | numberOfSubScopesBranch.FalseBranch = numberOfSelectedSubScopesCalculator;
|
---|
53 | numberOfSubScopesBranch.Successor = selector;
|
---|
54 |
|
---|
55 | numberOfSelectedSubScopesPlusCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
56 | numberOfSelectedSubScopesPlusCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
|
---|
57 | numberOfSelectedSubScopesPlusCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 2 * toint");
|
---|
58 |
|
---|
59 | numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
60 | numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Elites"));
|
---|
61 | numberOfSelectedSubScopesCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
|
---|
62 | numberOfSelectedSubScopesCalculator.ExpressionParameter.Value = new StringValue("PopulationSize Elites - 2 * toint");
|
---|
63 |
|
---|
64 | // Use Elitism or Plus-Selection as replacement strategy
|
---|
65 | var selectedProcessor = (SubScopesProcessor)selector.Successor;
|
---|
66 | var elitismReplacement = selectedProcessor.Successor;
|
---|
67 | selectedProcessor.Successor = replacementBranch;
|
---|
68 | replacementBranch.ConditionParameter.ActualName = "PlusSelection";
|
---|
69 | replacementBranch.FalseBranch = elitismReplacement;
|
---|
70 |
|
---|
71 | // Plus selection replacement
|
---|
72 | var replacementMergingReducer = new MergingReducer();
|
---|
73 | var replacementBestSelector = new BestSelector();
|
---|
74 | var replacementRightReducer = new RightReducer();
|
---|
75 | replacementBranch.TrueBranch = replacementMergingReducer;
|
---|
76 |
|
---|
77 | replacementMergingReducer.Successor = replacementBestSelector;
|
---|
78 |
|
---|
79 | replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = "PopulationSize";
|
---|
80 | replacementBestSelector.CopySelected = new BoolValue(false);
|
---|
81 | replacementBestSelector.Successor = replacementRightReducer;
|
---|
82 |
|
---|
83 | replacementRightReducer.Successor = null;
|
---|
84 |
|
---|
85 | // Increment ages of all individuals after replacement
|
---|
86 | var incrementAgeProcessor = new UniformSubScopesProcessor();
|
---|
87 | var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
|
---|
88 | replacementBranch.Successor = incrementAgeProcessor;
|
---|
89 | incrementAgeProcessor.Operator = ageIncrementor;
|
---|
90 | incrementAgeProcessor.Successor = null;
|
---|
91 | ageIncrementor.ValueParameter.ActualName = "Age";
|
---|
92 | ageIncrementor.Increment = new DoubleValue(1.0);
|
---|
93 |
|
---|
94 | // Insert AgeCalculator between crossover and its successor
|
---|
95 | var crossoverSuccessor = crossover.Successor;
|
---|
96 | var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };
|
---|
97 | crossover.Successor = ageCalculator;
|
---|
98 |
|
---|
99 | ageCalculator.ParameterToReduce.ActualName = "Age";
|
---|
100 | ageCalculator.TargetParameter.ActualName = "Age";
|
---|
101 | ageCalculator.WeightParameter.ActualName = "AgeInheritance";
|
---|
102 | ageCalculator.Successor = crossoverSuccessor;
|
---|
103 |
|
---|
104 | // When counting the evaluated solutions, write in LayerEvaluatedSolutions
|
---|
105 | subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
106 | subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
|
---|
107 |
|
---|
108 | // Instead of generational loop after merging of elites, stop
|
---|
109 | elitesMerger.Successor = null;
|
---|
110 |
|
---|
111 | // Parameterize
|
---|
112 | foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
|
---|
113 | stochasticOperator.RandomParameter.ActualName = "LocalRandom";
|
---|
114 | foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
|
---|
115 | stochasticBranch.RandomParameter.ActualName = "LocalRandom";
|
---|
116 |
|
---|
117 | // Remove unused operators
|
---|
118 | var usedOperators = mainLoop.OperatorGraph.Iterate();
|
---|
119 | var unusedOperators = mainLoop.OperatorGraph.Operators.Except(usedOperators);
|
---|
120 | foreach (var op in unusedOperators.ToList())
|
---|
121 | mainLoop.OperatorGraph.Operators.Remove(op);
|
---|
122 |
|
---|
123 | return mainLoop;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | } |
---|