[9334] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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;
|
---|
[9342] | 27 | using HeuristicLab.Optimization.Operators.LCS;
|
---|
[9334] | 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Selection;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Algorithms.GAssist {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// An operator which represents the main loop of a genetic algorithm.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("GAssistMainLoop", "An operator which represents the main loop of GAssist.")]
|
---|
| 37 | [StorableClass]
|
---|
| 38 | public sealed class GAssistMainLoop : AlgorithmOperator {
|
---|
| 39 | #region Parameter properties
|
---|
| 40 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 41 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 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<PercentValue> CrossoverProbabilityParameter {
|
---|
| 53 | get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
|
---|
| 54 | }
|
---|
| 55 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
| 56 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
| 57 | }
|
---|
| 58 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 59 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 60 | }
|
---|
| 61 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 62 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 63 | }
|
---|
[9392] | 64 | public ValueLookupParameter<IOperator> MDLIterationParameter {
|
---|
| 65 | get { return (ValueLookupParameter<IOperator>)Parameters["MDLIteration"]; }
|
---|
| 66 | }
|
---|
[9352] | 67 | public ValueLookupParameter<IOperator> DefaultRuleParameter {
|
---|
| 68 | get { return (ValueLookupParameter<IOperator>)Parameters["DefaultRule"]; }
|
---|
| 69 | }
|
---|
[9334] | 70 | public ValueLookupParameter<IOperator> SpecialStagesParameter {
|
---|
| 71 | get { return (ValueLookupParameter<IOperator>)Parameters["SpecialStages"]; }
|
---|
| 72 | }
|
---|
| 73 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 74 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
| 75 | }
|
---|
| 76 | public ValueLookupParameter<IntValue> ElitesParameter {
|
---|
| 77 | get { return (ValueLookupParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 78 | }
|
---|
| 79 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 80 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 81 | }
|
---|
| 82 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 83 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 84 | }
|
---|
| 85 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
| 86 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
| 87 | }
|
---|
| 88 | public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 89 | get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 90 | }
|
---|
| 91 | public ValueLookupParameter<IntValue> PopulationSizeParameter {
|
---|
| 92 | get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 93 | }
|
---|
[9342] | 94 | public ValueLookupParameter<IOperator> ReinitializationProbabilityOperatorParameter {
|
---|
| 95 | get { return (ValueLookupParameter<IOperator>)Parameters["ReinitializationProbabilityOperator"]; }
|
---|
| 96 | }
|
---|
[9334] | 97 | private ScopeParameter CurrentScopeParameter {
|
---|
| 98 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public IScope CurrentScope {
|
---|
| 102 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 103 | }
|
---|
| 104 | #endregion
|
---|
| 105 |
|
---|
| 106 | [StorableConstructor]
|
---|
| 107 | private GAssistMainLoop(bool deserializing) : base(deserializing) { }
|
---|
| 108 | private GAssistMainLoop(GAssistMainLoop original, Cloner cloner)
|
---|
| 109 | : base(original, cloner) {
|
---|
| 110 | }
|
---|
| 111 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 112 | return new GAssistMainLoop(this, cloner);
|
---|
| 113 | }
|
---|
| 114 | public GAssistMainLoop()
|
---|
| 115 | : base() {
|
---|
| 116 | Initialize();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | private void Initialize() {
|
---|
| 120 | #region Create parameters
|
---|
| 121 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 122 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 123 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 124 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 125 | Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
|
---|
| 126 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
| 127 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
| 128 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 129 | Parameters.Add(new ValueLookupParameter<IOperator>("SpecialStages", "The operator used for the special stages of GAssist."));
|
---|
| 130 | 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."));
|
---|
| 131 | Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
| 132 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 133 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 134 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
|
---|
| 135 | Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
| 136 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
|
---|
[9342] | 137 | Parameters.Add(new ValueLookupParameter<IOperator>("ReinitializationProbabilityOperator", ""));
|
---|
[9352] | 138 | Parameters.Add(new ValueLookupParameter<IOperator>("DefaultRule", ""));
|
---|
[9392] | 139 | Parameters.Add(new ValueLookupParameter<IOperator>("MDLIteration", ""));
|
---|
[9334] | 140 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
|
---|
| 141 | #endregion
|
---|
| 142 |
|
---|
| 143 | #region Create operators
|
---|
| 144 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 145 | Placeholder analyzer1 = new Placeholder();
|
---|
[9392] | 146 | Placeholder mdlIterationOperator = new Placeholder();
|
---|
| 147 | Placeholder defaultRuleOperator = new Placeholder();
|
---|
[9342] | 148 | Placeholder reinitializationProbabilityOperator = new Placeholder();
|
---|
[9334] | 149 | Placeholder selector = new Placeholder();
|
---|
| 150 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
| 151 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
| 152 | UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
|
---|
| 153 | StochasticBranch stochasticBranchCrossover = new StochasticBranch();
|
---|
| 154 | Placeholder crossover = new Placeholder();
|
---|
[9342] | 155 | RandomSelector randomSelector = new RandomSelector();
|
---|
| 156 | PreservingRightReducer preservingRightReducer = new PreservingRightReducer();
|
---|
[9334] | 157 | StochasticBranch stochasticBranchMutator = new StochasticBranch();
|
---|
| 158 | Placeholder mutator = new Placeholder();
|
---|
| 159 | Placeholder specialStages = new Placeholder();
|
---|
| 160 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 161 | UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
|
---|
| 162 | Placeholder evaluator = new Placeholder();
|
---|
| 163 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
| 164 | SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor();
|
---|
| 165 | BestSelector bestSelector = new BestSelector();
|
---|
| 166 | RightReducer rightReducer = new RightReducer();
|
---|
| 167 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 168 | IntCounter intCounter = new IntCounter();
|
---|
| 169 | Comparator comparator = new Comparator();
|
---|
| 170 | Placeholder analyzer2 = new Placeholder();
|
---|
| 171 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
| 172 |
|
---|
| 173 | resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
| 174 | resultsCollector1.ResultsParameter.ActualName = "Results";
|
---|
| 175 |
|
---|
| 176 | analyzer1.Name = "Analyzer";
|
---|
| 177 | analyzer1.OperatorParameter.ActualName = "Analyzer";
|
---|
| 178 |
|
---|
[9392] | 179 | mdlIterationOperator.Name = "MDL Iteration Operator";
|
---|
| 180 | mdlIterationOperator.OperatorParameter.ActualName = MDLIterationParameter.Name;
|
---|
| 181 |
|
---|
[9352] | 182 | defaultRuleOperator.Name = "Default Rule Operator";
|
---|
| 183 | defaultRuleOperator.OperatorParameter.ActualName = DefaultRuleParameter.Name;
|
---|
| 184 |
|
---|
[9342] | 185 | reinitializationProbabilityOperator.Name = "Reinitialization Probability operator (placeholder)";
|
---|
| 186 | reinitializationProbabilityOperator.OperatorParameter.ActualName = ReinitializationProbabilityOperatorParameter.Name;
|
---|
| 187 |
|
---|
[9334] | 188 | selector.Name = "Selector";
|
---|
| 189 | selector.OperatorParameter.ActualName = "Selector";
|
---|
| 190 |
|
---|
| 191 | childrenCreator.ParentsPerChild = new IntValue(2);
|
---|
| 192 |
|
---|
| 193 | stochasticBranchCrossover.ProbabilityParameter.ActualName = "CrossoverProbability";
|
---|
| 194 | stochasticBranchCrossover.RandomParameter.ActualName = "Random";
|
---|
| 195 |
|
---|
| 196 | crossover.Name = "Crossover";
|
---|
| 197 | crossover.OperatorParameter.ActualName = "Crossover";
|
---|
| 198 |
|
---|
[9342] | 199 | randomSelector.CopySelected.Value = true;
|
---|
| 200 | randomSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
| 201 |
|
---|
[9334] | 202 | stochasticBranchMutator.ProbabilityParameter.ActualName = "MutationProbability";
|
---|
| 203 | stochasticBranchMutator.RandomParameter.ActualName = "Random";
|
---|
| 204 |
|
---|
| 205 | mutator.Name = "Mutator";
|
---|
| 206 | mutator.OperatorParameter.ActualName = "Mutator";
|
---|
| 207 |
|
---|
| 208 | specialStages.Name = "SpecialStages";
|
---|
| 209 | specialStages.OperatorParameter.ActualName = "SpecialStages";
|
---|
| 210 |
|
---|
| 211 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 212 |
|
---|
| 213 | uniformSubScopesProcessor2.Parallel.Value = true;
|
---|
| 214 |
|
---|
| 215 | evaluator.Name = "Evaluator";
|
---|
| 216 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
| 217 |
|
---|
| 218 | subScopesCounter.Name = "Increment EvaluatedSolutions";
|
---|
| 219 | subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
| 220 |
|
---|
| 221 | bestSelector.CopySelected = new BoolValue(false);
|
---|
| 222 | bestSelector.MaximizationParameter.ActualName = "Maximization";
|
---|
| 223 | bestSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
|
---|
| 224 | bestSelector.QualityParameter.ActualName = "Quality";
|
---|
| 225 |
|
---|
| 226 | intCounter.Increment = new IntValue(1);
|
---|
| 227 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
| 228 |
|
---|
| 229 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 230 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 231 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
| 232 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
| 233 |
|
---|
| 234 | analyzer2.Name = "Analyzer";
|
---|
| 235 | analyzer2.OperatorParameter.ActualName = "Analyzer";
|
---|
| 236 |
|
---|
| 237 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
| 238 | #endregion
|
---|
| 239 |
|
---|
| 240 | #region Create operator graph
|
---|
[9392] | 241 | OperatorGraph.InitialOperator = resultsCollector1;
|
---|
[9334] | 242 | resultsCollector1.Successor = analyzer1;
|
---|
[9392] | 243 | analyzer1.Successor = mdlIterationOperator;
|
---|
| 244 | mdlIterationOperator.Successor = defaultRuleOperator;
|
---|
[9352] | 245 | defaultRuleOperator.Successor = reinitializationProbabilityOperator;
|
---|
[9342] | 246 | reinitializationProbabilityOperator.Successor = selector;
|
---|
[9334] | 247 | selector.Successor = subScopesProcessor1;
|
---|
| 248 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
| 249 | subScopesProcessor1.Operators.Add(childrenCreator);
|
---|
| 250 | subScopesProcessor1.Successor = subScopesProcessor2;
|
---|
| 251 | childrenCreator.Successor = uniformSubScopesProcessor1;
|
---|
| 252 | uniformSubScopesProcessor1.Operator = stochasticBranchCrossover;
|
---|
| 253 | uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
|
---|
| 254 | stochasticBranchCrossover.FirstBranch = crossover;
|
---|
[9342] | 255 | stochasticBranchCrossover.SecondBranch = randomSelector;
|
---|
| 256 | randomSelector.Successor = preservingRightReducer;
|
---|
[9334] | 257 | stochasticBranchCrossover.Successor = stochasticBranchMutator;
|
---|
| 258 | crossover.Successor = null;
|
---|
[9342] | 259 | preservingRightReducer.Successor = null;
|
---|
[9334] | 260 | stochasticBranchMutator.FirstBranch = mutator;
|
---|
| 261 | stochasticBranchMutator.SecondBranch = null;
|
---|
| 262 | stochasticBranchMutator.Successor = specialStages;
|
---|
| 263 | mutator.Successor = null;
|
---|
| 264 | specialStages.Successor = subScopesRemover;
|
---|
| 265 | subScopesRemover.Successor = null;
|
---|
| 266 | uniformSubScopesProcessor2.Operator = evaluator;
|
---|
| 267 | uniformSubScopesProcessor2.Successor = subScopesCounter;
|
---|
| 268 | evaluator.Successor = null;
|
---|
| 269 | subScopesCounter.Successor = null;
|
---|
| 270 | subScopesProcessor2.Operators.Add(bestSelector);
|
---|
| 271 | subScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
| 272 | subScopesProcessor2.Successor = mergingReducer;
|
---|
| 273 | bestSelector.Successor = rightReducer;
|
---|
| 274 | rightReducer.Successor = null;
|
---|
| 275 | mergingReducer.Successor = intCounter;
|
---|
| 276 | intCounter.Successor = comparator;
|
---|
| 277 | comparator.Successor = analyzer2;
|
---|
| 278 | analyzer2.Successor = conditionalBranch;
|
---|
[9392] | 279 | conditionalBranch.FalseBranch = mdlIterationOperator;
|
---|
[9334] | 280 | conditionalBranch.TrueBranch = null;
|
---|
| 281 | conditionalBranch.Successor = null;
|
---|
| 282 | #endregion
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | public override IOperation Apply() {
|
---|
| 286 | if (CrossoverParameter.ActualValue == null)
|
---|
| 287 | return null;
|
---|
| 288 | return base.Apply();
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 | }
|
---|