Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CellularGeneticAlgorithm/HeuristicLab.Algorithms.CellularGeneticAlgorithm/3.4/CellularGeneticAlgorithmMainLoop.cs @ 13042

Last change on this file since 13042 was 7540, checked in by svonolfe, 13 years ago

Added first working version of cellular GA (#1781)

File size: 10.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Operators;
8using HeuristicLab.Parameters;
9using HeuristicLab.Data;
10using HeuristicLab.Common;
11using HeuristicLab.Optimization.Operators;
12using HeuristicLab.Selection;
13
14namespace HeuristicLab.Algorithms.CellularGeneticAlgorithm {
15  /// <summary>
16  /// An operator which represents the main loop of a genetic algorithm.
17  /// </summary>
18  [Item("CellularGeneticAlgorithmMainLoop", "An operator which represents the main loop of a cellular genetic algorithm.")]
19  [StorableClass]
20  public sealed class CellularGeneticAlgorithmMainLoop : AlgorithmOperator {
21     #region Parameter properties
22    public ValueLookupParameter<IRandom> RandomParameter {
23      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
24    }
25    public ValueLookupParameter<BoolValue> MaximizationParameter {
26      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
27    }
28    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
29      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
30    }
31    public ValueLookupParameter<IOperator> SelectorParameter {
32      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
33    }
34    public ValueLookupParameter<IOperator> CrossoverParameter {
35      get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
36    }
37    public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
38      get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
39    }
40    public ValueLookupParameter<IOperator> MutatorParameter {
41      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
42    }
43    public ValueLookupParameter<IOperator> EvaluatorParameter {
44      get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
45    }
46    public ValueLookupParameter<IntValue> ElitesParameter {
47      get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
48    }
49    public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
50      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
51    }
52    public ValueLookupParameter<VariableCollection> ResultsParameter {
53      get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
54    }
55    public ValueLookupParameter<IOperator> AnalyzerParameter {
56      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
57    }
58    public ValueLookupParameter<IOperator> NeighborhoodSelectorParameter {
59      get { return (ValueLookupParameter<IOperator>)Parameters["NeighborhoodSelector"]; }
60    }
61    public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
62      get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
63    }
64    public ValueLookupParameter<IntValue> PopulationSizeParameter {
65      get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
66    }
67    private ScopeParameter CurrentScopeParameter {
68      get { return (ScopeParameter)Parameters["CurrentScope"]; }
69    }
70
71    public IScope CurrentScope {
72      get { return CurrentScopeParameter.ActualValue; }
73    }
74    #endregion
75
76    [StorableConstructor]
77    private CellularGeneticAlgorithmMainLoop(bool deserializing) : base(deserializing) { }
78    private CellularGeneticAlgorithmMainLoop(CellularGeneticAlgorithmMainLoop original, Cloner cloner)
79      : base(original, cloner) {
80    }
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new CellularGeneticAlgorithmMainLoop(this, cloner);
83    }
84    public CellularGeneticAlgorithmMainLoop()
85      : base() {
86      Initialize();
87    }
88
89    private void Initialize() {
90      #region Create parameters
91      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
92      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
93      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
94      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
95      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
96      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
97      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
98      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."));
99      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
100      Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
101      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
102      Parameters.Add(new ValueLookupParameter<IOperator>("NeighborhoodSelector", "The neighborhood selector."));
103      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
104      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
105      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
106      #endregion
107
108      #region Create operators
109      VariableCreator variableCreator = new VariableCreator();
110      ResultsCollector resultsCollector1 = new ResultsCollector();
111      Placeholder analyzer1 = new Placeholder();
112      UniformSubScopesProcessor scp = new UniformSubScopesProcessor();
113      Placeholder neighborhoodSelector = new Placeholder();
114      Placeholder selector = new Placeholder();
115      Placeholder crossover = new Placeholder();
116      StochasticBranch stochasticBranch = new StochasticBranch();
117      Placeholder mutator = new Placeholder();
118      SubScopesRemover subScopesRemover = new SubScopesRemover();
119      Placeholder evaluator = new Placeholder();
120      BestSelector bestSelector = new BestSelector();
121      IntCounter intCounter = new IntCounter();
122      Comparator comparator = new Comparator();
123      Placeholder analyzer2 = new Placeholder();
124      ConditionalBranch conditionalBranch = new ConditionalBranch();
125     
126      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class CellularGeneticAlgorithm expects this to be called Generations
127
128      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
129      resultsCollector1.ResultsParameter.ActualName = "Results";
130
131      analyzer1.Name = "Analyzer";
132      analyzer1.OperatorParameter.ActualName = "Analyzer";
133
134      scp.Parallel.Value = true;
135
136      neighborhoodSelector.Name = "NeighborhoodSelector";
137      neighborhoodSelector.OperatorParameter.ActualName = "NeighborhoodSelector";
138
139      selector.Name = "Selector";
140      selector.OperatorParameter.ActualName = "Selector";
141
142      crossover.Name = "Crossover";
143      crossover.OperatorParameter.ActualName = "Crossover";
144
145      stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
146      stochasticBranch.RandomParameter.ActualName = "Random";
147
148      mutator.Name = "Mutator";
149      mutator.OperatorParameter.ActualName = "Mutator";
150
151      subScopesRemover.RemoveAllSubScopes = true;
152
153      evaluator.Name = "Evaluator";
154      evaluator.OperatorParameter.ActualName = "Evaluator";
155
156      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
157      bestSelector.MaximizationParameter.Hidden = true;
158      bestSelector.QualityParameter.ActualName = QualityParameter.ActualName;
159      bestSelector.QualityParameter.Hidden = true;       
160      bestSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
161
162      intCounter.Increment = new IntValue(1);
163      intCounter.ValueParameter.ActualName = "Generations";
164
165      comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
166      comparator.LeftSideParameter.ActualName = "Generations";
167      comparator.ResultParameter.ActualName = "Terminate";
168      comparator.RightSideParameter.ActualName = "MaximumGenerations";
169
170      analyzer2.Name = "Analyzer";
171      analyzer2.OperatorParameter.ActualName = "Analyzer";
172
173      conditionalBranch.ConditionParameter.ActualName = "Terminate";
174      #endregion
175
176      #region Create operator graph
177      OperatorGraph.InitialOperator = variableCreator;
178      variableCreator.Successor = resultsCollector1;
179      resultsCollector1.Successor = analyzer1;
180      analyzer1.Successor = scp;
181      scp.Operator = neighborhoodSelector;
182      SubScopesProcessor sp = new SubScopesProcessor();
183      neighborhoodSelector.Successor = sp;
184
185      SubScopesProcessor sp2 = new SubScopesProcessor();
186      sp.Operators.Add(sp2);
187      sp2.Operators.Add(new EmptyOperator());
188
189      SubScopesProcessor sp3 = new SubScopesProcessor();
190      sp2.Operators.Add(sp3);
191      sp3.Operators.Add(new EmptyOperator());
192      sp3.Operators.Add(selector);
193      selector.Successor = new RightReducer();
194
195      MergingReducer mr = new MergingReducer();
196      sp3.Successor = mr;
197
198      mr.Successor = crossover;
199      crossover.Successor = stochasticBranch;
200      stochasticBranch.FirstBranch = mutator;
201      stochasticBranch.SecondBranch = null;
202      stochasticBranch.Successor = subScopesRemover;
203      subScopesRemover.Successor = evaluator;
204
205      sp2.Successor = bestSelector;
206      bestSelector.Successor = new RightReducer();
207
208      sp.Successor = new RightReducer();
209
210      MergingReducer mr2 = new MergingReducer();
211      scp.Successor = mr2;
212
213      mr2.Successor = intCounter;
214      intCounter.Successor = comparator;
215      comparator.Successor = analyzer2;
216      analyzer2.Successor = conditionalBranch;
217      conditionalBranch.FalseBranch = scp;
218      conditionalBranch.TrueBranch = null;
219      conditionalBranch.Successor = null;
220      #endregion
221    }
222
223    public override IOperation Apply() {
224      if (CrossoverParameter.ActualValue == null)
225        return null;
226      return base.Apply();
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.