Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainOperator.cs

Last change on this file was 14712, checked in by gkronber, 8 years ago

#2520 added GUIDs for (almost) all interface types (probably still too many) also added newlines at end of all files

File size: 12.9 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Selection;
30
31namespace HeuristicLab.Algorithms.ALPS {
32  [Item("AlpsGeneticAlgorithmMainOperator", "An operator that represents the core of an ALPS genetic algorithm.")]
33  [StorableType("A0114C78-C3E7-4676-9BD4-5D955284CBDC")]
34  public sealed class AlpsGeneticAlgorithmMainOperator : AlgorithmOperator {
35    #region Parameter properties
36    public IValueLookupParameter<IRandom> RandomParameter {
37      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
38    }
39
40    public IValueLookupParameter<IOperator> EvaluatorParameter {
41      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
42    }
43    public IValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
44      get { return (IValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
45    }
46    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
47      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
48    }
49    public IValueLookupParameter<BoolValue> MaximizationParameter {
50      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
51    }
52
53    public IValueLookupParameter<IntValue> PopulationSizeParameter {
54      get { return (IValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
55    }
56    public IValueLookupParameter<IOperator> SelectorParameter {
57      get { return (IValueLookupParameter<IOperator>)Parameters["Selector"]; }
58    }
59    public IValueLookupParameter<IOperator> CrossoverParameter {
60      get { return (IValueLookupParameter<IOperator>)Parameters["Crossover"]; }
61    }
62    public IValueLookupParameter<IOperator> MutatorParameter {
63      get { return (IValueLookupParameter<IOperator>)Parameters["Mutator"]; }
64    }
65    public IValueLookupParameter<PercentValue> MutationProbabilityParameter {
66      get { return (IValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
67    }
68    public IValueLookupParameter<IntValue> ElitesParameter {
69      get { return (IValueLookupParameter<IntValue>)Parameters["Elites"]; }
70    }
71    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
72      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
73    }
74    public IValueLookupParameter<BoolValue> PlusSelectionParameter {
75      get { return (IValueLookupParameter<BoolValue>)Parameters["PlusSelection"]; }
76    }
77
78    public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
79      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
80    }
81    public IValueLookupParameter<DoubleValue> AgeInheritanceParameter {
82      get { return (IValueLookupParameter<DoubleValue>)Parameters["AgeInheritance"]; }
83    }
84    public IValueLookupParameter<DoubleValue> AgeIncrementParameter {
85      get { return (IValueLookupParameter<DoubleValue>)Parameters["AgeIncrement"]; }
86    }
87    #endregion
88
89    [StorableConstructor]
90    private AlpsGeneticAlgorithmMainOperator(bool deserializing) : base(deserializing) { }
91    private AlpsGeneticAlgorithmMainOperator(AlpsGeneticAlgorithmMainOperator original, Cloner cloner)
92      : base(original, cloner) {
93    }
94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new AlpsGeneticAlgorithmMainOperator(this, cloner);
96    }
97    public AlpsGeneticAlgorithmMainOperator()
98      : base() {
99      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
100
101      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
102      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
103      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
104      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
105
106      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population of solutions in each layer."));
107      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
108      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
109      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
110      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
111      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
112      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
113      Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "Include the parents in the selection of the invividuals for the next generation."));
114
115      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The age of individuals."));
116      Parameters.Add(new ValueLookupParameter<DoubleValue>("AgeInheritance", "A weight that determines the age of a child after crossover based on the older (1.0) and younger (0.0) parent."));
117      Parameters.Add(new ValueLookupParameter<DoubleValue>("AgeIncrement", "The value the age the individuals is incremented if they survives a generation."));
118
119
120      var numberOfSelectedParentsCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedParents = 2 * (PopulationSize - (PlusSelection ? 0 : Elites))" };
121      var selector = new Placeholder() { Name = "Selector (Placeholder)" };
122      var subScopesProcessor1 = new SubScopesProcessor();
123      var childrenCreator = new ChildrenCreator();
124      var uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
125      var crossover = new Placeholder() { Name = "Crossover (Placeholder)" };
126      var stochasticBranch = new StochasticBranch() { Name = "MutationProbability" };
127      var mutator = new Placeholder() { Name = "Mutator (Placeholder)" };
128      var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };
129      var subScopesRemover = new SubScopesRemover();
130      var uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
131      var evaluator = new Placeholder() { Name = "Evaluator (Placeholder)" };
132      var subScopesCounter = new SubScopesCounter() { Name = "Increment EvaluatedSolutions" };
133      var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };
134      var replacementMergingReducer = new MergingReducer();
135      var replacementBestSelector = new BestSelector();
136      var replacementRightReducer = new RightReducer();
137      var subScopesProcessor2 = new SubScopesProcessor();
138      var bestSelector = new BestSelector();
139      var rightReducer = new RightReducer();
140      var mergingReducer = new MergingReducer();
141      var reevaluateElitesBranch = new ConditionalBranch() { Name = "Reevaluate elites ?" };
142      var incrementAgeProcessor = new UniformSubScopesProcessor();
143      var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
144
145      OperatorGraph.InitialOperator = numberOfSelectedParentsCalculator;
146
147      numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter<IntValue>(PopulationSizeParameter.Name));
148      numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter<IntValue>(ElitesParameter.Name));
149      numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter<BoolValue>(PlusSelectionParameter.Name));
150      numberOfSelectedParentsCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
151      numberOfSelectedParentsCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 0 Elites PlusSelection if - 2 * toint");
152      numberOfSelectedParentsCalculator.Successor = selector;
153
154      selector.OperatorParameter.ActualName = SelectorParameter.Name;
155      selector.Successor = subScopesProcessor1;
156
157      subScopesProcessor1.Operators.Add(new EmptyOperator());
158      subScopesProcessor1.Operators.Add(childrenCreator);
159      subScopesProcessor1.Successor = replacementBranch;
160
161      childrenCreator.ParentsPerChild = new IntValue(2);
162      childrenCreator.Successor = uniformSubScopesProcessor1;
163
164      uniformSubScopesProcessor1.Operator = crossover;
165      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
166
167      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
168      crossover.Successor = stochasticBranch;
169
170      stochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
171      stochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
172      stochasticBranch.FirstBranch = mutator;
173      stochasticBranch.SecondBranch = null;
174      stochasticBranch.Successor = ageCalculator;
175
176      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
177      mutator.Successor = null;
178
179      ageCalculator.ParameterToReduce.ActualName = AgeParameter.Name;
180      ageCalculator.TargetParameter.ActualName = AgeParameter.Name;
181      ageCalculator.WeightParameter.ActualName = AgeInheritanceParameter.Name;
182      ageCalculator.Successor = subScopesRemover;
183
184      subScopesRemover.RemoveAllSubScopes = true;
185      subScopesRemover.Successor = null;
186
187      uniformSubScopesProcessor2.Parallel.Value = true;
188      uniformSubScopesProcessor2.Operator = evaluator;
189      uniformSubScopesProcessor2.Successor = subScopesCounter;
190
191      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
192      evaluator.Successor = null;
193
194      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
195      subScopesCounter.AccumulateParameter.Value = new BoolValue(true);
196      subScopesCounter.Successor = null;
197
198      replacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
199      replacementBranch.TrueBranch = replacementMergingReducer;
200      replacementBranch.FalseBranch = subScopesProcessor2;
201      replacementBranch.Successor = incrementAgeProcessor;
202
203      replacementMergingReducer.Successor = replacementBestSelector;
204
205      replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
206      replacementBestSelector.CopySelected = new BoolValue(false);
207      replacementBestSelector.Successor = replacementRightReducer;
208
209      replacementRightReducer.Successor = reevaluateElitesBranch;
210
211      subScopesProcessor2.Operators.Add(bestSelector);
212      subScopesProcessor2.Operators.Add(new EmptyOperator());
213      subScopesProcessor2.Successor = mergingReducer;
214
215      bestSelector.CopySelected = new BoolValue(false);
216      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
217      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
218      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
219      bestSelector.Successor = rightReducer;
220
221      rightReducer.Successor = reevaluateElitesBranch;
222
223      mergingReducer.Successor = null;
224
225      reevaluateElitesBranch.ConditionParameter.ActualName = ReevaluateElitesParameter.Name;
226      reevaluateElitesBranch.TrueBranch = uniformSubScopesProcessor2;
227      reevaluateElitesBranch.FalseBranch = null;
228      reevaluateElitesBranch.Successor = null;
229
230
231      incrementAgeProcessor.Operator = ageIncrementor;
232      incrementAgeProcessor.Successor = null;
233
234      ageIncrementor.ValueParameter.ActualName = AgeParameter.Name;
235      ageIncrementor.IncrementParameter.Value = null;
236      ageIncrementor.IncrementParameter.ActualName = AgeIncrementParameter.Name;
237      ageIncrementor.Successor = null;
238    }
239  }
240}
Note: See TracBrowser for help on using the repository browser.