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