[9089] | 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 |
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
[9110] | 26 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
[9089] | 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization.Operators;
|
---|
[9342] | 29 | using HeuristicLab.Optimization.Operators.LCS;
|
---|
[9089] | 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 | using HeuristicLab.Selection;
|
---|
| 33 | namespace HeuristicLab.Algorithms.LearningClassifierSystems {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// An operator which represents the main loop of a genetic algorithm.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("LCSAdaptedGeneticAlgorithm", "An operator which represents the main loop of a genetic algorithm, which has been adapdet for learning classifier systems.")]
|
---|
| 38 | [StorableClass]
|
---|
| 39 | public sealed class LCSAdaptedGeneticAlgorithm : AlgorithmOperator {
|
---|
[9110] | 40 | private const string TEMPID = "TempID";
|
---|
| 41 | private const string SUBSUMEDBY = "SubsumedBy";
|
---|
| 42 | private const string SUBSUMED = "Subsumed";
|
---|
| 43 |
|
---|
[9089] | 44 | #region Parameter properties
|
---|
| 45 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
| 46 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 47 | }
|
---|
| 48 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
| 49 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 50 | }
|
---|
| 51 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 52 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 53 | }
|
---|
| 54 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
| 55 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
| 56 | }
|
---|
[9167] | 57 | public ValueLookupParameter<IOperator> AfterCopyingParentsParameter {
|
---|
| 58 | get { return (ValueLookupParameter<IOperator>)Parameters["AfterCopyingParents"]; }
|
---|
| 59 | }
|
---|
[9089] | 60 | public ValueLookupParameter<IOperator> CrossoverParameter {
|
---|
| 61 | get { return (ValueLookupParameter<IOperator>)Parameters["Crossover"]; }
|
---|
| 62 | }
|
---|
[9105] | 63 | public ValueLookupParameter<IOperator> AfterCrossoverParameter {
|
---|
| 64 | get { return (ValueLookupParameter<IOperator>)Parameters["AfterCrossover"]; }
|
---|
| 65 | }
|
---|
[9089] | 66 | public ValueLookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 67 | get { return (ValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 68 | }
|
---|
[9160] | 69 | public ValueLookupParameter<PercentValue> CrossoverProbabilityParameter {
|
---|
| 70 | get { return (ValueLookupParameter<PercentValue>)Parameters["CrossoverProbability"]; }
|
---|
| 71 | }
|
---|
[9089] | 72 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
| 73 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
| 74 | }
|
---|
| 75 | public ValueLookupParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 76 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 77 | }
|
---|
| 78 | public ValueLookupParameter<VariableCollection> ResultsParameter {
|
---|
| 79 | get { return (ValueLookupParameter<VariableCollection>)Parameters["Results"]; }
|
---|
| 80 | }
|
---|
| 81 | public ValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 82 | get { return (ValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 83 | }
|
---|
| 84 | public ValueLookupParameter<IntValue> PopulationSizeParameter {
|
---|
| 85 | get { return (ValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 86 | }
|
---|
[9110] | 87 | public ValueLookupParameter<BoolValue> DoGASubsumptionParameter {
|
---|
| 88 | get { return (ValueLookupParameter<BoolValue>)Parameters["DoGASubsumption"]; }
|
---|
| 89 | }
|
---|
[9089] | 90 | private ScopeParameter CurrentScopeParameter {
|
---|
| 91 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public IScope CurrentScope {
|
---|
| 95 | get { return CurrentScopeParameter.ActualValue; }
|
---|
| 96 | }
|
---|
| 97 | #endregion
|
---|
| 98 |
|
---|
[9226] | 99 | private CheckGASubsumptionOperator checkGASubsumptionOperator;
|
---|
| 100 |
|
---|
[9089] | 101 | [StorableConstructor]
|
---|
| 102 | private LCSAdaptedGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 103 | private LCSAdaptedGeneticAlgorithm(LCSAdaptedGeneticAlgorithm original, Cloner cloner)
|
---|
| 104 | : base(original, cloner) {
|
---|
| 105 | }
|
---|
| 106 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 107 | return new LCSAdaptedGeneticAlgorithm(this, cloner);
|
---|
| 108 | }
|
---|
| 109 | public LCSAdaptedGeneticAlgorithm()
|
---|
| 110 | : base() {
|
---|
| 111 | Initialize();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private void Initialize() {
|
---|
| 115 | #region Create parameters
|
---|
| 116 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
| 117 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
| 118 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
| 119 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
|
---|
[9167] | 120 | Parameters.Add(new ValueLookupParameter<IOperator>("AfterCopyingParents", "The operator executed after copying a parent instead of using crossover."));
|
---|
[9089] | 121 | Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
|
---|
[9105] | 122 | Parameters.Add(new ValueLookupParameter<IOperator>("AfterCrossover", "The operator executed after crossing the solutions."));
|
---|
[9160] | 123 | Parameters.Add(new ValueLookupParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on a solution."));
|
---|
| 124 | Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that druing the mutation operator a mutation takes place."));
|
---|
[9089] | 125 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 126 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed."));
|
---|
| 127 | Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
|
---|
| 128 | Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
|
---|
| 129 | Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
|
---|
[9110] | 130 | Parameters.Add(new ValueLookupParameter<BoolValue>("DoGASubsumption", "Sets if GA subsumption is executed."));
|
---|
[9089] | 131 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents a population of solutions on which the genetic algorithm should be applied."));
|
---|
| 132 | #endregion
|
---|
| 133 |
|
---|
| 134 | #region Create operators
|
---|
| 135 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 136 | ResultsCollector resultsCollector1 = new ResultsCollector();
|
---|
| 137 | Placeholder selector = new Placeholder();
|
---|
| 138 | SubScopesProcessor subScopesProcessor1 = new SubScopesProcessor();
|
---|
| 139 | ChildrenCreator childrenCreator = new ChildrenCreator();
|
---|
| 140 | UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
|
---|
[9160] | 141 | StochasticBranch crossoverStochasticBranch = new StochasticBranch();
|
---|
| 142 | RandomSelector randomSelector = new RandomSelector();
|
---|
| 143 | PreservingRightReducer preservingRightReducer = new PreservingRightReducer();
|
---|
[9167] | 144 | Placeholder afterCopyingParents = new Placeholder();
|
---|
[9089] | 145 | Placeholder crossover = new Placeholder();
|
---|
[9105] | 146 | Placeholder afterCrossover = new Placeholder();
|
---|
[9089] | 147 | Placeholder mutator = new Placeholder();
|
---|
| 148 | SubScopesRemover subScopesRemover = new SubScopesRemover();
|
---|
| 149 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
| 150 | MergingReducer mergingReducer = new MergingReducer();
|
---|
| 151 | IntCounter intCounter = new IntCounter();
|
---|
| 152 | Comparator comparator = new Comparator();
|
---|
| 153 | ConditionalBranch conditionalBranch = new ConditionalBranch();
|
---|
| 154 |
|
---|
[9110] | 155 | TempSubScopeIDAssigner tempIdAssigner = new TempSubScopeIDAssigner();
|
---|
| 156 | ConditionalBranch doGASubsumptionBranch1 = new ConditionalBranch();
|
---|
| 157 | UniformSubScopesProcessor setSubsumptionFalseSubScopesProcessor = new UniformSubScopesProcessor();
|
---|
| 158 | Assigner setSubsumpByAssigner = new Assigner();
|
---|
| 159 | Assigner setSubsumptionFalseAssigner = new Assigner();
|
---|
[9226] | 160 | checkGASubsumptionOperator = new CheckGASubsumptionOperator();
|
---|
[9110] | 161 | ConditionalBranch doGASubsumptionBranch2 = new ConditionalBranch();
|
---|
| 162 | ExecuteGASubsumptionOperator executeGAsubsumptionOperator = new ExecuteGASubsumptionOperator();
|
---|
| 163 | ConditionalSelector subsumptionSelector = new ConditionalSelector();
|
---|
| 164 | LeftReducer subsumptionLeftReducer = new LeftReducer();
|
---|
| 165 |
|
---|
[9089] | 166 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GeneticAlgorithm expects this to be called Generations
|
---|
| 167 |
|
---|
[9154] | 168 | //resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
[9089] | 169 | resultsCollector1.ResultsParameter.ActualName = "Results";
|
---|
| 170 |
|
---|
[9110] | 171 | tempIdAssigner.LeftSideParameter.ActualName = TEMPID;
|
---|
| 172 |
|
---|
| 173 | setSubsumpByAssigner.LeftSideParameter.ActualName = SUBSUMEDBY;
|
---|
| 174 | setSubsumpByAssigner.RightSideParameter.Value = new IntValue(-1);
|
---|
| 175 |
|
---|
| 176 | setSubsumptionFalseAssigner.LeftSideParameter.ActualName = SUBSUMED;
|
---|
| 177 | setSubsumptionFalseAssigner.RightSideParameter.Value = new BoolValue(false);
|
---|
| 178 |
|
---|
[9089] | 179 | selector.Name = "Selector";
|
---|
| 180 | selector.OperatorParameter.ActualName = "Selector";
|
---|
| 181 |
|
---|
| 182 | childrenCreator.ParentsPerChild = new IntValue(2);
|
---|
| 183 |
|
---|
[9160] | 184 | crossoverStochasticBranch.ProbabilityParameter.ActualName = CrossoverProbabilityParameter.ActualName;
|
---|
| 185 | crossoverStochasticBranch.RandomParameter.ActualName = "Random";
|
---|
| 186 |
|
---|
| 187 | randomSelector.CopySelected.Value = true;
|
---|
| 188 | randomSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
| 189 |
|
---|
[9167] | 190 | afterCopyingParents.Name = "AfterCopyingParents";
|
---|
| 191 | afterCopyingParents.OperatorParameter.ActualName = "AfterCopyingParents";
|
---|
| 192 |
|
---|
[9089] | 193 | crossover.Name = "Crossover";
|
---|
| 194 | crossover.OperatorParameter.ActualName = "Crossover";
|
---|
| 195 |
|
---|
[9105] | 196 | afterCrossover.Name = "AfterCrossover";
|
---|
| 197 | afterCrossover.OperatorParameter.ActualName = "AfterCrossover";
|
---|
[9089] | 198 |
|
---|
| 199 | mutator.Name = "Mutator";
|
---|
| 200 | mutator.OperatorParameter.ActualName = "Mutator";
|
---|
| 201 |
|
---|
[9110] | 202 | doGASubsumptionBranch1.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
|
---|
| 203 |
|
---|
| 204 | checkGASubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
|
---|
| 205 | checkGASubsumptionOperator.ExperiencesParameter.ActualName = "Experience";
|
---|
| 206 | checkGASubsumptionOperator.ErrorsParameter.ActualName = "Error";
|
---|
| 207 | checkGASubsumptionOperator.TempIDParameter.ActualName = TEMPID;
|
---|
| 208 | checkGASubsumptionOperator.ErrorZeroParameter.ActualName = "ErrorZero";
|
---|
| 209 | checkGASubsumptionOperator.ThetaSubsumptionParameter.ActualName = "ThetaSubsumption";
|
---|
| 210 | checkGASubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
|
---|
| 211 | checkGASubsumptionOperator.SubsumedParameter.ActualName = SUBSUMED;
|
---|
| 212 |
|
---|
[9089] | 213 | subScopesRemover.RemoveAllSubScopes = true;
|
---|
| 214 |
|
---|
[9110] | 215 | doGASubsumptionBranch2.ConditionParameter.ActualName = DoGASubsumptionParameter.ActualName;
|
---|
| 216 |
|
---|
| 217 | executeGAsubsumptionOperator.NumerositiesParameter.ActualName = "Numerosity";
|
---|
| 218 | executeGAsubsumptionOperator.TempIDParameter.ActualName = TEMPID;
|
---|
| 219 | executeGAsubsumptionOperator.SubsumedByParameter.ActualName = SUBSUMEDBY;
|
---|
| 220 |
|
---|
| 221 | subsumptionSelector.ConditionParameter.ActualName = SUBSUMED;
|
---|
| 222 | subsumptionSelector.CopySelected = new BoolValue(false);
|
---|
| 223 |
|
---|
[9089] | 224 | subScopesCounter.Name = "Increment EvaluatedSolutions";
|
---|
| 225 | subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
|
---|
| 226 |
|
---|
| 227 | intCounter.Increment = new IntValue(1);
|
---|
| 228 | intCounter.ValueParameter.ActualName = "Generations";
|
---|
| 229 |
|
---|
| 230 | comparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
| 231 | comparator.LeftSideParameter.ActualName = "Generations";
|
---|
| 232 | comparator.ResultParameter.ActualName = "Terminate";
|
---|
| 233 | comparator.RightSideParameter.ActualName = "MaximumGenerations";
|
---|
| 234 |
|
---|
| 235 | conditionalBranch.ConditionParameter.ActualName = "Terminate";
|
---|
| 236 | #endregion
|
---|
| 237 |
|
---|
| 238 | #region Create operator graph
|
---|
| 239 | OperatorGraph.InitialOperator = variableCreator;
|
---|
| 240 | variableCreator.Successor = resultsCollector1;
|
---|
[9154] | 241 | resultsCollector1.Successor = tempIdAssigner;
|
---|
[9110] | 242 | tempIdAssigner.Successor = setSubsumptionFalseSubScopesProcessor;
|
---|
| 243 | setSubsumptionFalseSubScopesProcessor.Operator = setSubsumpByAssigner;
|
---|
| 244 | setSubsumpByAssigner.Successor = setSubsumptionFalseAssigner;
|
---|
| 245 | setSubsumptionFalseAssigner.Successor = null;
|
---|
| 246 | setSubsumptionFalseSubScopesProcessor.Successor = selector;
|
---|
[9089] | 247 | selector.Successor = subScopesProcessor1;
|
---|
| 248 | subScopesProcessor1.Operators.Add(new EmptyOperator());
|
---|
| 249 | subScopesProcessor1.Operators.Add(childrenCreator);
|
---|
[9105] | 250 | subScopesProcessor1.Successor = mergingReducer;
|
---|
[9089] | 251 | childrenCreator.Successor = uniformSubScopesProcessor1;
|
---|
[9160] | 252 | uniformSubScopesProcessor1.Operator = crossoverStochasticBranch;
|
---|
[9089] | 253 | uniformSubScopesProcessor1.Successor = subScopesCounter;
|
---|
[9160] | 254 | crossoverStochasticBranch.FirstBranch = crossover;
|
---|
| 255 | crossoverStochasticBranch.SecondBranch = randomSelector;
|
---|
| 256 | randomSelector.Successor = preservingRightReducer;
|
---|
[9167] | 257 | preservingRightReducer.Successor = afterCopyingParents;
|
---|
[9160] | 258 | crossoverStochasticBranch.Successor = mutator;
|
---|
[9105] | 259 | crossover.Successor = afterCrossover;
|
---|
[9110] | 260 | mutator.Successor = doGASubsumptionBranch1;
|
---|
| 261 | doGASubsumptionBranch1.TrueBranch = checkGASubsumptionOperator;
|
---|
| 262 | doGASubsumptionBranch1.FalseBranch = new EmptyOperator();
|
---|
| 263 | doGASubsumptionBranch1.Successor = subScopesRemover;
|
---|
[9089] | 264 | subScopesRemover.Successor = null;
|
---|
| 265 | subScopesCounter.Successor = null;
|
---|
[9110] | 266 | mergingReducer.Successor = doGASubsumptionBranch2;
|
---|
| 267 | doGASubsumptionBranch2.TrueBranch = executeGAsubsumptionOperator;
|
---|
| 268 | doGASubsumptionBranch2.FalseBranch = new EmptyOperator();
|
---|
| 269 | executeGAsubsumptionOperator.Successor = subsumptionSelector;
|
---|
| 270 | subsumptionSelector.Successor = subsumptionLeftReducer;
|
---|
| 271 | subsumptionLeftReducer.Successor = null;
|
---|
| 272 | doGASubsumptionBranch2.Successor = intCounter;
|
---|
[9089] | 273 | intCounter.Successor = comparator;
|
---|
[9154] | 274 | comparator.Successor = conditionalBranch;
|
---|
[9089] | 275 | conditionalBranch.FalseBranch = selector;
|
---|
| 276 | conditionalBranch.TrueBranch = null;
|
---|
| 277 | conditionalBranch.Successor = null;
|
---|
| 278 | #endregion
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | public override IOperation Apply() {
|
---|
| 282 | if (CrossoverParameter.ActualValue == null)
|
---|
| 283 | return null;
|
---|
| 284 | return base.Apply();
|
---|
| 285 | }
|
---|
[9226] | 286 |
|
---|
| 287 | public void SetChildName(string childName) {
|
---|
| 288 | checkGASubsumptionOperator.ChildClassifiersParameter.ActualName = childName;
|
---|
| 289 | checkGASubsumptionOperator.ParentsClassifiersParameter.ActualName = childName;
|
---|
| 290 | }
|
---|
[9089] | 291 | }
|
---|
| 292 | }
|
---|