Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsGeneticAlgorithmMainLoop.cs @ 12136

Last change on this file since 12136 was 12136, checked in by pfleck, 9 years ago

#2350

  • Worked on AlpsSsGeneticAlgorithmMainLoop.
  • Added SteadyStateMatingPoolCreator, RandomLayerProcessor and RandomIntAssigner.
File size: 10.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 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.SteadyState {
32  [Item("AlpsSsGeneticAlgorithmMainLoop", "An ALPS steady-state genetic algorithm main loop operator.")]
33  [StorableClass]
34  public class AlpsSsGeneticAlgorithmMainLoop : AlgorithmOperator {
35    #region Parameter Properties
36    public ValueLookupParameter<BoolValue> MaximizationParameter {
37      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
38    }
39    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
40      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
42    public ILookupParameter<IntValue> MaximumIterationsParameter {
43      get { return (ILookupParameter<IntValue>)Parameters["MaximumIterations"]; }
44    }
45    public ILookupParameter<IOperator> AnalyzerParameter {
46      get { return (ILookupParameter<IOperator>)Parameters["Analyzer"]; }
47    }
48    public ILookupParameter<IOperator> LayerAnalyzerParameter {
49      get { return (ILookupParameter<IOperator>)Parameters["LayerAnalyzer"]; }
50    }
51    #endregion
52
53    [StorableConstructor]
54    private AlpsSsGeneticAlgorithmMainLoop(bool deserializing)
55      : base(deserializing) { }
56    private AlpsSsGeneticAlgorithmMainLoop(AlpsSsGeneticAlgorithmMainLoop original, Cloner cloner)
57      : base(original, cloner) { }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new AlpsSsGeneticAlgorithmMainLoop(this, cloner);
60    }
61
62    public AlpsSsGeneticAlgorithmMainLoop()
63      : base() {
64      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
65      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
66      Parameters.Add(new LookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations that the algorithm should process."));
67      Parameters.Add(new LookupParameter<IOperator>("Analyzer", "The operator used to the analyze all individuals."));
68      Parameters.Add(new LookupParameter<IOperator>("LayerAnalyzer", "The operator used to analyze each layer."));
69
70      var variableCreator = new VariableCreator() { Name = "Initialize" };
71      var randomScopeProcessor = new RandomLayerProcessor() { Name = "Select a layer" }; // TODO LayerSubScopeProcessor for Array conversion
72      var isLayerZeroComperator = new Comparator() { Name = "IsLayerZero = Layer == 0" };
73      var isLayerZeroBranch = new ConditionalBranch() { Name = "IsLayerZero?" };
74      var isDoInitBranch = new ConditionalBranch() { Name = "DoInit?" };
75      var setTargetIndedxToNextInit = new Assigner() { Name = "TargetIndex = NextInit" };
76      var incrementNextInit = new IntCounter() { Name = "Incr. NextInit" };
77      var checkInitFinished = new Comparator() { Name = "DoInit = NextInit >= PopulationSize" };
78      var createWorkingScope = new BestSelector();
79      var workingScopeProcessor = new SubScopesProcessor() { Name = "Working Scope Processor" };
80      var createRandomIndividual = new SolutionsCreator() { Name = "Create random Individual" };
81      var initializeAgeProcessor = new UniformSubScopesProcessor();
82      var initializeAge = new Assigner() { Name = "Initialize Age" };
83      var selectRandomTargetIndex = new RandomIntAssigner();
84      var copyLayer = new BestSelector();
85      var copyLayerProcessor = new SubScopesProcessor();
86      var matingPoolCreator = new SteadyStateMatingPoolCreator() { Name = "Create MatingPool" };
87      var matingPoolSize = new SubScopesCounter() { Name = "MatingPoolSize" };
88      var matingPoolSizeMin2 = new Comparator() { Name = "ValidParents = MatingPoolSize >= 2" };
89      var validParentsBranch = new ConditionalBranch() { Name = "ValidParents?" };
90      var mainOperator = new EmptyOperator(); // TODO
91      var reactivateInit = new Assigner() { Name = "DoInit = true" };
92      var resetNextIndex = new Assigner() { Name = "NextInit = 1" };
93      var resetTargetIndex = new Assigner() { Name = "TargetIndex = 0" };
94      var clearMatingPool = new SubScopesRemover() { Name = "Clear WorkingScope" };
95      var tryMoveUp = new EmptyOperator() { Name = "Try Move Up" }; // TODO
96      var setNewIndividual = new EmptyOperator() { Name = "Set New Individual" };
97      var iterationsComparator = new Comparator() { Name = "Iterations >= MaximumIterations" };
98      var terminateBranch = new ConditionalBranch() { Name = "Terminate?" };
99
100
101      OperatorGraph.InitialOperator = variableCreator;
102
103      variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("DoInit", new BoolValue(false)));
104      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NextInit", new IntValue(0)));
105      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OpenLayers", new IntValue(1)));
106      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("TargetIndex", new IntValue(0)));
107      variableCreator.Successor = randomScopeProcessor;
108
109      randomScopeProcessor.Operator = isLayerZeroComperator;
110
111      isLayerZeroComperator.LeftSideParameter.ActualName = "Layer";
112      isLayerZeroComperator.RightSideParameter.Value = new IntValue(0);
113      isLayerZeroComperator.ResultParameter.ActualName = "IsLayerZero";
114      isLayerZeroComperator.Comparison = new Comparison(ComparisonType.Equal);
115      isLayerZeroComperator.Successor = isLayerZeroBranch;
116
117      isLayerZeroBranch.ConditionParameter.ActualName = "IsLayerZero";
118      isLayerZeroBranch.TrueBranch = isDoInitBranch;
119      isLayerZeroBranch.FalseBranch = selectRandomTargetIndex;
120      isLayerZeroBranch.Successor = tryMoveUp;
121
122      isDoInitBranch.ConditionParameter.ActualName = "DoInit";
123      isDoInitBranch.TrueBranch = setTargetIndedxToNextInit;
124      isDoInitBranch.FalseBranch = selectRandomTargetIndex;
125
126      setTargetIndedxToNextInit.LeftSideParameter.ActualName = "TargetIndex";
127      setTargetIndedxToNextInit.RightSideParameter.ActualName = "NextInit";
128      setTargetIndedxToNextInit.Successor = incrementNextInit;
129
130      incrementNextInit.ValueParameter.ActualName = "NextInit";
131      incrementNextInit.Increment = new IntValue(1);
132      incrementNextInit.Successor = checkInitFinished;
133
134      checkInitFinished.LeftSideParameter.ActualName = "NextInit";
135      checkInitFinished.RightSideParameter.ActualName = "PopulationSize";
136      checkInitFinished.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
137      checkInitFinished.ResultParameter.ActualName = "DoInit";
138      checkInitFinished.Successor = createWorkingScope;
139
140      createWorkingScope.NumberOfSelectedSubScopesParameter.Value = new IntValue(0);
141      createWorkingScope.CopySelected = new BoolValue(false);
142      createWorkingScope.Successor = workingScopeProcessor;
143
144      workingScopeProcessor.Operators.Add(createRandomIndividual);
145      workingScopeProcessor.Operators.Add(new EmptyOperator());
146
147      createRandomIndividual.NumberOfSolutions = new IntValue(1);
148      createRandomIndividual.Successor = initializeAgeProcessor;
149
150      initializeAgeProcessor.Operator = initializeAge;
151
152      initializeAge.LeftSideParameter.ActualName = "EvalsCreated";
153      initializeAge.RightSideParameter.ActualName = "EvaluatedSolutions";
154
155      selectRandomTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
156      selectRandomTargetIndex.MinimumParameter.Value = new IntValue(0);
157      selectRandomTargetIndex.MaximumParameter.ActualName = "PopulationSize";
158      selectRandomTargetIndex.Successor = copyLayer;
159
160      copyLayer.NumberOfSelectedSubScopesParameter.ActualName = "LayerPopulationSize";
161      copyLayer.CopySelected = new BoolValue(true);
162      copyLayer.Successor = copyLayerProcessor;
163
164      copyLayerProcessor.Operators.Add(new EmptyOperator());
165      copyLayerProcessor.Operators.Add(matingPoolCreator);
166
167      matingPoolCreator.Successor = matingPoolSize;
168
169      matingPoolSize.ValueParameter.ActualName = "MatingPoolSize";
170      matingPoolSize.Successor = matingPoolSizeMin2;
171
172      matingPoolSizeMin2.LeftSideParameter.ActualName = "MatingPoolSize";
173      matingPoolSizeMin2.RightSideParameter.Value = new IntValue(2);
174      matingPoolSizeMin2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
175      matingPoolSizeMin2.ResultParameter.ActualName = "ValidParents";
176      matingPoolSizeMin2.Successor = validParentsBranch;
177
178      validParentsBranch.TrueBranch = mainOperator;
179      validParentsBranch.FalseBranch = reactivateInit;
180
181      reactivateInit.Successor = resetNextIndex;
182      reactivateInit.LeftSideParameter.ActualName = "DoInit";
183      reactivateInit.RightSideParameter.Value = new BoolValue(true);
184
185      resetNextIndex.Successor = resetTargetIndex;
186      resetNextIndex.LeftSideParameter.ActualName = "NextIndex";
187      resetNextIndex.RightSideParameter.Value = new IntValue(1);
188
189      resetTargetIndex.Successor = clearMatingPool;
190      resetTargetIndex.LeftSideParameter.ActualName = "TargetIndex";
191      resetTargetIndex.RightSideParameter.Value = new IntValue(0);
192
193      clearMatingPool.Successor = createRandomIndividual;
194
195      tryMoveUp.Successor = setNewIndividual;
196
197      setNewIndividual.Successor = iterationsComparator;
198
199      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
200      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
201      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
202      iterationsComparator.ResultParameter.ActualName = "Terminate";
203      iterationsComparator.Successor = terminateBranch;
204
205      terminateBranch.ConditionParameter.ActualName = "Terminate";
206      terminateBranch.FalseBranch = randomScopeProcessor;
207
208
209    }
210
211  }
212}
Note: See TracBrowser for help on using the repository browser.