[2830] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 |
|
---|
[2891] | 22 | using HeuristicLab.Analysis;
|
---|
[2830] | 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Evolutionary;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Selection;
|
---|
| 29 |
|
---|
[2884] | 30 | namespace HeuristicLab.Algorithms.SGA {
|
---|
[2830] | 31 | /// <summary>
|
---|
| 32 | /// An operator which represents a Standard Genetic Algorithm.
|
---|
| 33 | /// </summary>
|
---|
| 34 | [Item("SGAOperator", "An operator which represents a Standard Genetic Algorithm.")]
|
---|
| 35 | [Creatable("Test")]
|
---|
| 36 | public class SGAOperator : AlgorithmOperator {
|
---|
| 37 | #region Parameter properties
|
---|
| 38 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 39 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 40 | }
|
---|
| 41 | public ValueLookupParameter<BoolData> MaximizationParameter {
|
---|
| 42 | get { return (ValueLookupParameter<BoolData>)Parameters["Maximization"]; }
|
---|
| 43 | }
|
---|
| 44 | public SubScopesLookupParameter<DoubleData> QualityParameter {
|
---|
| 45 | get { return (SubScopesLookupParameter<DoubleData>)Parameters["Quality"]; }
|
---|
| 46 | }
|
---|
[2882] | 47 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
| 48 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
[2830] | 49 | }
|
---|
[2882] | 50 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
| 51 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
| 52 | }
|
---|
[2830] | 53 | public ValueLookupParameter<DoubleData> MutationProbabilityParameter {
|
---|
| 54 | get { return (ValueLookupParameter<DoubleData>)Parameters["MutationProbability"]; }
|
---|
| 55 | }
|
---|
[2882] | 56 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 57 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
[2830] | 58 | }
|
---|
[2882] | 59 | public ValueLookupParameter<IOperator> EvaluatorParameter {
|
---|
| 60 | get { return (ValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
|
---|
[2830] | 61 | }
|
---|
| 62 | public ValueLookupParameter<IntData> ElitesParameter {
|
---|
| 63 | get { return (ValueLookupParameter<IntData>)Parameters["Elites"]; }
|
---|
| 64 | }
|
---|
| 65 | public ValueLookupParameter<IntData> MaximumGenerationsParameter {
|
---|
| 66 | get { return (ValueLookupParameter<IntData>)Parameters["MaximumGenerations"]; }
|
---|
| 67 | }
|
---|
[2882] | 68 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 69 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 70 | }
|
---|
[2830] | 71 | private ScopeParameter CurrentScopeParameter {
|
---|
| 72 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public IScope CurrentScope {
|
---|
| 76 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | public SGAOperator()
|
---|
| 81 | : base() {
|
---|
| 82 | #region Create parameters
|
---|
| 83 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 84 | Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 85 | Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The value which represents the quality of a solution."));
|
---|
[2882] | 86 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 87 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
[2830] | 88 | Parameters.Add(new ValueLookupParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
|
---|
[2882] | 89 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 90 | Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions."));
|
---|
[2830] | 91 | Parameters.Add(new ValueLookupParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation."));
|
---|
| 92 | Parameters.Add(new ValueLookupParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
[2882] | 93 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
[2830] | 94 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the SGA should be applied."));
|
---|
| 95 | #endregion
|
---|
| 96 |
|
---|
| 97 | #region Create operator graph
|
---|
[2908] | 98 | VariableCreator variableCreator = new VariableCreator();
|
---|
[2924] | 99 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
[2830] | 100 | SubScopesSorter subScopesSorter1 = new SubScopesSorter();
|
---|
[2882] | 101 | Placeholder selector = new Placeholder();
|
---|
[2830] | 102 | SequentialSubScopesProcessor sequentialSubScopesProcessor1 = new SequentialSubScopesProcessor();
|
---|
| 103 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
| 104 | UniformSequentialSubScopesProcessor uniformSequentialSubScopesProcessor = new UniformSequentialSubScopesProcessor();
|
---|
| 105 | Placeholder crossover = new Placeholder();
|
---|
| 106 | StochasticBranch stochasticBranch = new StochasticBranch();
|
---|
| 107 | Placeholder mutator = new Placeholder();
|
---|
| 108 | Placeholder evaluator = new Placeholder();
|
---|
| 109 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 110 | SubScopesSorter subScopesSorter2 = new SubScopesSorter();
|
---|
| 111 | SequentialSubScopesProcessor sequentialSubScopesProcessor2 = new SequentialSubScopesProcessor();
|
---|
| 112 | LeftSelector leftSelector = new LeftSelector();
|
---|
| 113 | RightReducer rightReducer = new RightReducer();
|
---|
| 114 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 115 | IntCounter intCounter = new IntCounter();
|
---|
| 116 | Comparator comparator = new Comparator();
|
---|
[2891] | 117 | BestAverageWorstQualityCalculator bestAverageWorstQualityCalculator = new BestAverageWorstQualityCalculator();
|
---|
[2908] | 118 | DataTableValuesCollector dataTableValuesCollector = new DataTableValuesCollector();
|
---|
[2830] | 119 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
| 120 |
|
---|
[2908] | 121 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 122 |
|
---|
[2924] | 123 | variableCreator.CollectedValues.Add(new ValueParameter<IntData>("Generations", new IntData(0)));
|
---|
| 124 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleData>("Best Quality", new DoubleData(0)));
|
---|
| 125 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleData>("Average Quality", new DoubleData(0)));
|
---|
| 126 | variableCreator.CollectedValues.Add(new ValueParameter<DoubleData>("Worst Quality", new DoubleData(0)));
|
---|
[2908] | 127 | variableCreator.CollectedValues.Add(new ValueParameter<DataTable>("Qualities", new DataTable("Qualities")));
|
---|
[2924] | 128 | variableCreator.Successor = resultsCollector;
|
---|
[2908] | 129 |
|
---|
[2924] | 130 | resultsCollector.CollectedValues.Add(new LookupParameter<IntData>("Generations"));
|
---|
| 131 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Best Quality"));
|
---|
| 132 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Average Quality"));
|
---|
| 133 | resultsCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Worst Quality"));
|
---|
| 134 | resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Qualities"));
|
---|
| 135 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 136 | resultsCollector.Successor = subScopesSorter1;
|
---|
[2908] | 137 |
|
---|
[2830] | 138 | subScopesSorter1.DescendingParameter.ActualName = "Maximization";
|
---|
| 139 | subScopesSorter1.ValueParameter.ActualName = "Quality";
|
---|
[2882] | 140 | subScopesSorter1.Successor = selector;
|
---|
[2830] | 141 |
|
---|
[2882] | 142 | selector.Name = "Selector";
|
---|
| 143 | selector.OperatorParameter.ActualName = "Selector";
|
---|
| 144 | selector.Successor = sequentialSubScopesProcessor1;
|
---|
[2830] | 145 |
|
---|
| 146 | sequentialSubScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
| 147 | sequentialSubScopesProcessor1.Operators.Add(childrenCreator);
|
---|
| 148 | sequentialSubScopesProcessor1.Successor = sequentialSubScopesProcessor2;
|
---|
| 149 |
|
---|
| 150 | childrenCreator.ParentsPerChild = new IntData(2);
|
---|
| 151 | childrenCreator.Successor = uniformSequentialSubScopesProcessor;
|
---|
| 152 |
|
---|
| 153 | uniformSequentialSubScopesProcessor.Operator = crossover;
|
---|
| 154 | uniformSequentialSubScopesProcessor.Successor = subScopesSorter2;
|
---|
| 155 |
|
---|
[2882] | 156 | crossover.Name = "Crossover";
|
---|
| 157 | crossover.OperatorParameter.ActualName = "Crossover";
|
---|
[2830] | 158 | crossover.Successor = stochasticBranch;
|
---|
| 159 |
|
---|
| 160 | stochasticBranch.FirstBranch = mutator;
|
---|
| 161 | stochasticBranch.ProbabilityParameter.ActualName = "MutationProbability";
|
---|
| 162 | stochasticBranch.RandomParameter.ActualName = "Random";
|
---|
| 163 | stochasticBranch.SecondBranch = null;
|
---|
| 164 | stochasticBranch.Successor = evaluator;
|
---|
| 165 |
|
---|
[2882] | 166 | mutator.Name = "Mutator";
|
---|
| 167 | mutator.OperatorParameter.ActualName = "Mutator";
|
---|
[2830] | 168 | mutator.Successor = null;
|
---|
| 169 |
|
---|
[2882] | 170 | evaluator.Name = "Evaluator";
|
---|
| 171 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
[2830] | 172 | evaluator.Successor = subScopesRemover;
|
---|
| 173 |
|
---|
| 174 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 175 | subScopesRemover.Successor = null;
|
---|
| 176 |
|
---|
| 177 | subScopesSorter2.DescendingParameter.ActualName = "Maximization";
|
---|
| 178 | subScopesSorter2.ValueParameter.ActualName = "Quality";
|
---|
| 179 | subScopesSorter2.Successor = null;
|
---|
| 180 |
|
---|
| 181 | sequentialSubScopesProcessor2.Operators.Add(leftSelector);
|
---|
| 182 | sequentialSubScopesProcessor2.Operators.Add(new EmptyOperator());
|
---|
| 183 | sequentialSubScopesProcessor2.Successor = mergingReducer;
|
---|
| 184 |
|
---|
| 185 | leftSelector.CopySelected = new BoolData(false);
|
---|
| 186 | leftSelector.NumberOfSelectedSubScopesParameter.ActualName = "Elites";
|
---|
| 187 | leftSelector.Successor = rightReducer;
|
---|
| 188 |
|
---|
| 189 | rightReducer.Successor = null;
|
---|
| 190 |
|
---|
| 191 | mergingReducer.Successor = intCounter;
|
---|
| 192 |
|
---|
| 193 | intCounter.Increment = new IntData(1);
|
---|
| 194 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
| 195 | intCounter.Successor = comparator;
|
---|
| 196 |
|
---|
| 197 | comparator.Comparison = new ComparisonData(Comparison.GreaterOrEqual);
|
---|
| 198 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 199 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
| 200 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
[2891] | 201 | comparator.Successor = bestAverageWorstQualityCalculator;
|
---|
[2830] | 202 |
|
---|
[2924] | 203 | bestAverageWorstQualityCalculator.AverageQualityParameter.ActualName = "Average Quality";
|
---|
| 204 | bestAverageWorstQualityCalculator.BestQualityParameter.ActualName = "Best Quality";
|
---|
[2891] | 205 | bestAverageWorstQualityCalculator.MaximizationParameter.ActualName = "Maximization";
|
---|
| 206 | bestAverageWorstQualityCalculator.QualityParameter.ActualName = "Quality";
|
---|
[2924] | 207 | bestAverageWorstQualityCalculator.WorstQualityParameter.ActualName = "Worst Quality";
|
---|
[2908] | 208 | bestAverageWorstQualityCalculator.Successor = dataTableValuesCollector;
|
---|
[2891] | 209 |
|
---|
[2924] | 210 | dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Best Quality"));
|
---|
| 211 | dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Average Quality"));
|
---|
| 212 | dataTableValuesCollector.CollectedValues.Add(new LookupParameter<DoubleData>("Worst Quality"));
|
---|
[2908] | 213 | dataTableValuesCollector.DataTableParameter.ActualName = "Qualities";
|
---|
[2924] | 214 | dataTableValuesCollector.Successor = conditionalBranch;
|
---|
[2882] | 215 |
|
---|
[2830] | 216 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
| 217 | conditionalBranch.FalseBranch = subScopesSorter1;
|
---|
| 218 | conditionalBranch.TrueBranch = null;
|
---|
| 219 | conditionalBranch.Successor = null;
|
---|
| 220 | #endregion
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | }
|
---|