[17479] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HEAL.Attic;
|
---|
| 29 | using HeuristicLab.Selection;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Algorithms.SWGA {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator which represents the main loop of a genetic algorithm.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("GeneticAlgorithmMainLoop", "An operator which represents the main loop of a genetic algorithm.")]
|
---|
| 37 | [StorableType("78FFFD80-9708-4FE7-9092-16DA0B51D33B")]
|
---|
| 38 | public sealed class GeneticAlgorithmMainLoop : AlgorithmOperator {
|
---|
| 39 | #region Parameter properties
|
---|
| 40 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 41 | get { return (ValueLookupParameter<IRandom>)Parameters["GlobalRandom"]; }
|
---|
| 42 | }
|
---|
| 43 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 44 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 45 | }
|
---|
| 46 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 47 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 48 | }
|
---|
| 49 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
| 50 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
| 51 | }
|
---|
| 52 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
| 53 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
| 54 | }
|
---|
| 55 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 56 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 57 | }
|
---|
| 58 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 59 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 60 | }
|
---|
| 61 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 62 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 63 | }
|
---|
| 64 | public ValueLookupParameter<IntValue> ElitesParameter {
|
---|
| 65 | get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 66 | }
|
---|
| 67 | public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 68 | get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 69 | }
|
---|
| 70 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 71 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 72 | }
|
---|
| 73 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 74 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 75 | }
|
---|
| 76 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 77 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 78 | }
|
---|
| 79 | public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 80 | get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 81 | }
|
---|
| 82 | public ValueLookupParameter<IntValue> PopulationSizeParameter {
|
---|
| 83 | get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 84 | }
|
---|
| 85 | private ScopeParameter CurrentScopeParameter {
|
---|
| 86 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public IScope CurrentScope {
|
---|
| 90 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | public IValueLookupParameter<IOperator> TerminatorParameter {
|
---|
| 94 | get { return (IValueLookupParameter<IOperator>)Parameters["Terminator"]; }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public IValueLookupParameter<IOperator> SlidingWindowParameter {
|
---|
| 98 | get { return (IValueLookupParameter<IOperator>)Parameters["SlidingWindow"]; }
|
---|
| 99 | }
|
---|
| 100 | #endregion
|
---|
| 101 |
|
---|
| 102 | [StorableConstructor]
|
---|
| 103 | private GeneticAlgorithmMainLoop(StorableConstructorFlag _) : base(_) { }
|
---|
| 104 | private GeneticAlgorithmMainLoop(GeneticAlgorithmMainLoop original, Cloner cloner)
|
---|
| 105 | : base(original, cloner) {
|
---|
| 106 | }
|
---|
| 107 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 108 | return new GeneticAlgorithmMainLoop(this, cloner);
|
---|
| 109 | }
|
---|
| 110 | public GeneticAlgorithmMainLoop()
|
---|
| 111 | : base() {
|
---|
| 112 | Initialize();
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private void Initialize() {
|
---|
| 116 | #region Create parameters
|
---|
| 117 | Parameters.Add(new ValueLookupParameter<IRandom>("GlobalRandom", "A pseudo random number generator."));
|
---|
| 118 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 119 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 120 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 121 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
| 122 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
| 123 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 124 | 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."));
|
---|
| 125 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
| 126 | Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
|
---|
| 127 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 128 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 129 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
| 130 | Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
| 131 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
|
---|
| 132 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
|
---|
| 133 | Parameters.Add(new ValueLookupParameter<IOperator>("Terminator", "The termination criteria that defines if the algorithm should continue or stop"));
|
---|
| 134 | Parameters.Add(new ValueLookupParameter<IOperator>("SlidingWindow", "Operator which moves a sliding window over the training data."));
|
---|
| 135 | #endregion
|
---|
| 136 |
|
---|
| 137 | #region Create operators
|
---|
| 138 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 139 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 140 | Placeholder analyzer1 = new Placeholder();
|
---|
| 141 | Placeholder slidingWindow = new Placeholder() { Name = "Sliding Window (Placeholder)" };
|
---|
| 142 | Placeholder selector = new Placeholder();
|
---|
| 143 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
| 144 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
| 145 | UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
|
---|
| 146 | Placeholder crossover = new Placeholder();
|
---|
| 147 | StochasticBranch stochasticBranch = new StochasticBranch();
|
---|
| 148 | Placeholder mutator = new Placeholder();
|
---|
| 149 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 150 | UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
|
---|
| 151 | Placeholder evaluator = new Placeholder();
|
---|
| 152 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
| 153 | SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
|
---|
| 154 | BestSelector bestSelector = new BestSelector();
|
---|
| 155 | RightReducer rightReducer = new RightReducer();
|
---|
| 156 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 157 | IntCounter intCounter = new IntCounter();
|
---|
| 158 | Comparator comparator = new Comparator();
|
---|
| 159 | Placeholder analyzer2 = new Placeholder();
|
---|
| 160 | TerminationOperator termination = new TerminationOperator();
|
---|
| 161 | ConditionalBranch reevaluateElitesBranch = new ConditionalBranch();
|
---|
| 162 |
|
---|
| 163 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
|
---|
| 164 |
|
---|
| 165 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
| 166 | resultsCollector1.ResultsParameter.ActualName = "Results";
|
---|
| 167 |
|
---|
| 168 | analyzer1.Name = "Analyzer";
|
---|
| 169 | analyzer1.OperatorParameter.ActualName = "Analyzer";
|
---|
| 170 |
|
---|
| 171 | selector.Name = "Selector";
|
---|
| 172 | selector.OperatorParameter.ActualName = "Selector";
|
---|
| 173 |
|
---|
| 174 | childrenCreator.ParentsPerChild = new IntValue(2);
|
---|
| 175 |
|
---|
| 176 | crossover.Name = "Crossover";
|
---|
| 177 | crossover.OperatorParameter.ActualName = "Crossover";
|
---|
| 178 |
|
---|
| 179 | stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
|
---|
| 180 | stochasticBranch.RandomParameter.ActualName = "Random";
|
---|
| 181 |
|
---|
| 182 | mutator.Name = "Mutator";
|
---|
| 183 | mutator.OperatorParameter.ActualName = "Mutator";
|
---|
| 184 |
|
---|
| 185 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 186 |
|
---|
| 187 | uniformSubScopesProcessor2.Parallel.Value = true;
|
---|
| 188 |
|
---|
| 189 | evaluator.Name = "Evaluator";
|
---|
| 190 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
| 191 |
|
---|
| 192 | subScopesCounter.Name = "Increment EvaluatedSolutions";
|
---|
| 193 | subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
| 194 |
|
---|
| 195 | bestSelector.CopySelected = new BoolValue(false);
|
---|
| 196 | bestSelector.MaximizationParameter.ActualName = "Maximization";
|
---|
| 197 | bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
|
---|
| 198 | bestSelector.QualityParameter.ActualName = "Quality";
|
---|
| 199 |
|
---|
| 200 | intCounter.Increment = new IntValue(1);
|
---|
| 201 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
| 202 |
|
---|
| 203 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 204 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 205 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
| 206 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
| 207 |
|
---|
| 208 | analyzer2.Name = "Analyzer";
|
---|
| 209 | analyzer2.OperatorParameter.ActualName = "Analyzer";
|
---|
| 210 |
|
---|
| 211 | termination.TerminatorParameter.ActualName = TerminatorParameter.Name;
|
---|
| 212 | slidingWindow.OperatorParameter.ActualName = SlidingWindowParameter.Name;
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | reevaluateElitesBranch.ConditionParameter.ActualName = "ReevaluateElites";
|
---|
| 216 | reevaluateElitesBranch.Name = "Reevaluate elites ?";
|
---|
| 217 | #endregion
|
---|
| 218 |
|
---|
| 219 | #region Create operator graph
|
---|
| 220 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 221 | variableCreator.Successor = resultsCollector1;
|
---|
| 222 | resultsCollector1.Successor = analyzer1;
|
---|
| 223 | analyzer1.Successor = slidingWindow;
|
---|
| 224 | slidingWindow.Successor = selector;
|
---|
| 225 | selector.Successor = subScopesProcessor1;
|
---|
| 226 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
| 227 | subScopesProcessor1.Operators.Add(childrenCreator);
|
---|
| 228 | subScopesProcessor1.Successor = subScopesProcessor2;
|
---|
| 229 | childrenCreator.Successor = uniformSubScopesProcessor1;
|
---|
| 230 | uniformSubScopesProcessor1.Operator = crossover;
|
---|
| 231 | uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
|
---|
| 232 | crossover.Successor = stochasticBranch;
|
---|
| 233 | stochasticBranch.FirstBranch = mutator;
|
---|
| 234 | stochasticBranch.SecondBranch = null;
|
---|
| 235 | stochasticBranch.Successor = subScopesRemover;
|
---|
| 236 | mutator.Successor = null;
|
---|
| 237 | subScopesRemover.Successor = null;
|
---|
| 238 | uniformSubScopesProcessor2.Operator = evaluator;
|
---|
| 239 | uniformSubScopesProcessor2.Successor = subScopesCounter;
|
---|
| 240 | evaluator.Successor = null;
|
---|
| 241 | subScopesCounter.Successor = null;
|
---|
| 242 | subScopesProcessor2.Operators.Add(bestSelector);
|
---|
| 243 | subScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
| 244 | subScopesProcessor2.Successor = mergingReducer;
|
---|
| 245 | bestSelector.Successor = rightReducer;
|
---|
| 246 | rightReducer.Successor = reevaluateElitesBranch;
|
---|
| 247 | reevaluateElitesBranch.TrueBranch = uniformSubScopesProcessor2;
|
---|
| 248 | reevaluateElitesBranch.FalseBranch = null;
|
---|
| 249 | reevaluateElitesBranch.Successor = null;
|
---|
| 250 | mergingReducer.Successor = intCounter;
|
---|
| 251 | intCounter.Successor = comparator;
|
---|
| 252 | comparator.Successor = analyzer2;
|
---|
| 253 | analyzer2.Successor = termination;
|
---|
| 254 | termination.ContinueBranch = slidingWindow;
|
---|
| 255 | #endregion
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 259 | private void AfterDeserialization() {
|
---|
| 260 | // BackwardsCompatibility3.3
|
---|
| 261 | #region Backwards compatible code, remove with 3.4
|
---|
| 262 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
| 263 | Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
|
---|
| 264 | }
|
---|
| 265 | #endregion
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | public override IOperation Apply() {
|
---|
| 269 | if (CrossoverParameter.ActualValue == null)
|
---|
| 270 | return null;
|
---|
| 271 | return base.Apply();
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | }
|
---|