Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12138 was 12138, checked in by pfleck, 10 years ago

#2350

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