source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.LearningClassifierSystems/3.3/LearningClassifierSystemMainLoop.cs @ 9090

Last change on this file since 9090 was 9090, checked in by sforsten, 6 years ago

#1980: implemented covering and changed SinglePointCrossover for CombinedIntegerVectorEncoding

File size: 14.8 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.ConditionActionEncoding;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Selection;
33
34namespace HeuristicLab.Algorithms.LearningClassifierSystems {
35  /// <summary>
36  /// An operator which represents the main loop of a learning classifier system.
37  /// </summary>
38  [Item("LearningClassifierSystemMainLoop", "An operator which represents the main loop of a learning classifier system.")]
39  [StorableClass]
40  public sealed class LearningClassifierSystemMainLoop : AlgorithmOperator {
41
42    #region Parameter Properties
43    public IConstrainedValueParameter<ISelector> SelectorParameter {
44      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
45    }
46    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
47      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
48    }
49    public IConstrainedValueParameter<IManipulator> MutatorParameter {
50      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
51    }
52    #endregion
53
54    #region private properties
55    private SolutionsCreator initialSolutionsCreator;
56    private MatchConditionOperator matchConditionOperator;
57    private PredictionArrayCalculator predictionArrayCalculator;
58    private ActionSelector actionSelector;
59    private CoveringOperator covering;
60
61    private Placeholder evaluator;
62    private Placeholder actionExecuter;
63    private Placeholder classifierFetcher;
64    #endregion
65
66    [StorableConstructor]
67    private LearningClassifierSystemMainLoop(bool deserializing) : base(deserializing) { }
68    private LearningClassifierSystemMainLoop(LearningClassifierSystemMainLoop original, Cloner cloner)
69      : base(original, cloner) {
70    }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new LearningClassifierSystemMainLoop(this, cloner);
73    }
74    public LearningClassifierSystemMainLoop()
75      : base() {
76      Initialize();
77    }
78
79    private void Initialize() {
80      #region Create parameters
81      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction.", new ItemSet<ISelector>() { new ProportionalSelector() }, new ProportionalSelector()));
82      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions.", new ItemSet<ICrossover>() { new HeuristicLab.Encodings.CombinedIntegerVectorEncoding.SinglePointCrossover() }, new HeuristicLab.Encodings.CombinedIntegerVectorEncoding.SinglePointCrossover()));
83      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."));
84      UniformOnePositionManipulator test = new UniformOnePositionManipulator();
85      test.IntegerVectorParameter.ActualName = "CombinedIntegerVector";
86      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions.", new ItemSet<IManipulator>() { new UniformOnePositionManipulator() }, test));
87      #endregion
88
89      #region Create operators
90      VariableCreator variableCreator = new VariableCreator();
91      ConditionalBranch initialPopulationConditionalBranch = new ConditionalBranch();
92      initialSolutionsCreator = new SolutionsCreator();
93      IntCounter iterationCounter = new IntCounter();
94      UniformSubScopesProcessor matchCondtionSubScopesProcessor = new UniformSubScopesProcessor();
95      matchConditionOperator = new MatchConditionOperator();
96      predictionArrayCalculator = new PredictionArrayCalculator();
97      actionSelector = new ActionSelector();
98      UniformSubScopesProcessor matchActionSubScopesProcessor = new UniformSubScopesProcessor();
99      MatchActionOperator matchActionOperator = new MatchActionOperator();
100      ConditionalSelector conditionMatchSelector = new ConditionalSelector();
101      ConditionalSelector actionMatchSelector = new ConditionalSelector();
102      SubScopesProcessor matchSetSubScopesProcessor = new SubScopesProcessor();
103      CountNumberOfUniqueActions countNumberOfUniqueActions = new CountNumberOfUniqueActions();
104      covering = new CoveringOperator();
105      MergingReducer mergingReducer = new MergingReducer();
106      MergingReducer mergingReducerWithSuccessor = new MergingReducer();
107      evaluator = new Placeholder();
108      SubScopesProcessor actionSetSubScopesProcessor = new SubScopesProcessor();
109      DataReducer actionSetSizeDataReducer = new DataReducer();
110      UniformSubScopesProcessor accuracySubScopesProcessor = new UniformSubScopesProcessor();
111      CalculateAccuracy calculateAccuracy = new CalculateAccuracy();
112      SumAccuracy sumAccuracy = new SumAccuracy();
113      UniformSubScopesProcessor updateParametersSubScopesProcessor = new UniformSubScopesProcessor();
114      LCSAdaptedGeneticAlgorithm adaptedGeneticAlgorithmMainLoop = new LCSAdaptedGeneticAlgorithm();
115
116      classifierFetcher = new Placeholder();
117      actionExecuter = new Placeholder();
118
119      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("ZeroIntValue", new IntValue(0)));
120      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OneIntValue", new IntValue(1)));
121      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iteration", new IntValue(0)));
122
123      initialPopulationConditionalBranch.ConditionParameter.ActualName = "CreateInitialPopulation";
124
125      initialSolutionsCreator.NumberOfSolutionsParameter.ActualName = "N";
126
127      iterationCounter.ValueParameter.ActualName = "Iteration";
128      iterationCounter.IncrementParameter.ActualName = "OneIntValue";
129
130      matchCondtionSubScopesProcessor.Operator = matchConditionOperator;
131
132      matchConditionOperator.MatchParameter.ActualName = "CombinedIntegerVector";
133
134      conditionMatchSelector.CopySelected = new BoolValue(false);
135      conditionMatchSelector.ConditionParameter.ActualName = "MatchCondition";
136
137      countNumberOfUniqueActions.ClassifiersParameter.ActualName = "CombinedIntegerVector";
138
139      covering.ActionsInMatchSetParameter.ActualName = countNumberOfUniqueActions.UniqueActionsParameter.ActualName;
140      covering.ParallelParameter.Value.Value = true;
141      covering.RandomParameter.ActualName = "Random";
142
143      matchActionSubScopesProcessor.Operator = matchActionOperator;
144
145      matchActionOperator.MatchParameter.ActualName = "CombinedIntegerVector";
146      matchActionOperator.TargetMatchParameter.ActualName = actionSelector.SelectedActionParameter.ActualName;
147
148      predictionArrayCalculator.MatchParameter.ActualName = "CombinedIntegerVector";
149
150      actionSelector.PredictionArrayParameter.ActualName = predictionArrayCalculator.PredictionArrayParameter.Name;
151      actionSelector.RandomParameter.ActualName = "Random";
152      actionSelector.ExplorationProbabilityParameter.ActualName = "ExplorationProbability";
153
154      actionMatchSelector.CopySelected = new BoolValue(false);
155      actionMatchSelector.ConditionParameter.ActualName = "MatchAction";
156
157      SelectorParameter.Value.CopySelected = new BoolValue(true);
158      SelectorParameter.Value.NumberOfSelectedSubScopesParameter.Value = new IntValue(200);
159
160      evaluator.Name = "Evaluator";
161
162      classifierFetcher.Name = "ClassifierFetcher";
163
164      actionExecuter.Name = "ActionExecuter";
165
166      actionSetSizeDataReducer.TargetParameter.ActualName = "CurrentActionSetSize";
167      actionSetSizeDataReducer.ParameterToReduce.ActualName = "Numerosity";
168      actionSetSizeDataReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
169      actionSetSizeDataReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign);
170
171      calculateAccuracy.AlphaParameter.ActualName = "Alpha";
172      calculateAccuracy.ErrorParameter.ActualName = "Error";
173      calculateAccuracy.ErrorZeroParameter.ActualName = "ErrorZero";
174      calculateAccuracy.PowerParameter.ActualName = "v";
175
176      sumAccuracy.AccuracyParameter.ActualName = calculateAccuracy.AccuracyParameter.ActualName;
177      sumAccuracy.NumerosityParameter.ActualName = "Numerosity";
178
179      adaptedGeneticAlgorithmMainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
180      adaptedGeneticAlgorithmMainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
181      adaptedGeneticAlgorithmMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
182      adaptedGeneticAlgorithmMainLoop.RandomParameter.ActualName = "Random";
183      adaptedGeneticAlgorithmMainLoop.MaximumGenerationsParameter.ActualName = "ZeroIntValue";
184      adaptedGeneticAlgorithmMainLoop.QualityParameter.ActualName = "Fitness";
185      //needed?
186      adaptedGeneticAlgorithmMainLoop.MutationProbabilityParameter.Value = new PercentValue(10);
187      adaptedGeneticAlgorithmMainLoop.MaximizationParameter.Value = new BoolValue(true);
188      #endregion
189
190      #region Create operator graph
191      OperatorGraph.InitialOperator = variableCreator;
192
193      variableCreator.Successor = initialPopulationConditionalBranch;
194      initialPopulationConditionalBranch.TrueBranch = initialSolutionsCreator;
195      initialPopulationConditionalBranch.FalseBranch = new EmptyOperator();
196      initialPopulationConditionalBranch.Successor = classifierFetcher;
197      classifierFetcher.Successor = matchCondtionSubScopesProcessor;
198      matchCondtionSubScopesProcessor.Successor = conditionMatchSelector;
199      //variableCreator.Successor = adaptedGeneticAlgorithmMainLoop;
200      //adaptedGeneticAlgorithmMainLoop.Successor = conditionMatchSelector;
201
202      conditionMatchSelector.Successor = matchSetSubScopesProcessor;
203      matchSetSubScopesProcessor.Operators.Add(new EmptyOperator());
204      matchSetSubScopesProcessor.Operators.Add(countNumberOfUniqueActions);
205      matchSetSubScopesProcessor.Successor = mergingReducerWithSuccessor;
206      countNumberOfUniqueActions.Successor = covering;
207      covering.Successor = predictionArrayCalculator;
208      predictionArrayCalculator.Successor = actionSelector;
209      actionSelector.Successor = matchActionSubScopesProcessor;
210      matchActionSubScopesProcessor.Successor = actionMatchSelector;
211      actionMatchSelector.Successor = actionExecuter;
212      actionExecuter.Successor = actionSetSubScopesProcessor;
213      actionSetSubScopesProcessor.Operators.Add(new EmptyOperator());
214      actionSetSubScopesProcessor.Operators.Add(actionSetSizeDataReducer);
215      actionSetSizeDataReducer.Successor = accuracySubScopesProcessor;
216      accuracySubScopesProcessor.Operator = calculateAccuracy;
217      accuracySubScopesProcessor.Successor = sumAccuracy;
218      sumAccuracy.Successor = updateParametersSubScopesProcessor;
219      updateParametersSubScopesProcessor.Operator = evaluator;
220
221      actionSetSubScopesProcessor.Successor = mergingReducer;
222
223      mergingReducerWithSuccessor.Successor = iterationCounter;
224      iterationCounter.Successor = classifierFetcher;
225      //mergingReducer.Successor = adaptedGeneticAlgorithmMainLoop;
226      #endregion
227    }
228
229    private void ParameterizeStochasticOperator(IOperator op) {
230      IStochasticOperator stochasticOp = op as IStochasticOperator;
231      if (stochasticOp != null) {
232        stochasticOp.RandomParameter.ActualName = "Random";
233        stochasticOp.RandomParameter.Hidden = true;
234      }
235    }
236
237    internal void SetCurrentProblem(IConditionActionProblem problem) {
238      initialSolutionsCreator.SolutionCreatorParameter.ActualName = problem.SolutionCreatorParameter.Name;
239      initialSolutionsCreator.EvaluatorParameter.ActualName = problem.EvaluatorParameter.Name;
240
241      problem.ActionExecuter.SelectedActionParameter.ActualName = actionSelector.SelectedActionParameter.ActualName;
242
243      problem.ClassifierFetcher.IterationParameter.ActualName = "Iteration";
244
245      evaluator.OperatorParameter.ActualName = problem.EvaluatorParameter.Name;
246
247      classifierFetcher.OperatorParameter.ActualName = problem.ClassifierFetcherParameter.Name;
248
249      actionExecuter.OperatorParameter.ActualName = problem.ActionExecuterParameter.Name;
250
251      matchConditionOperator.TargetMatchParameter.ActualName = problem.ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
252
253      covering.SolutionCreatorParameter.ActualName = problem.CoveringSolutionCreatorParameter.Name;
254      covering.EvaluatorParameter.ActualName = problem.EvaluatorParameter.Name;
255      covering.MinimalNumberOfUniqueActionsParameter.ActualName = problem.ThetaMinimalNumberOfActionsParameter.Name;
256      covering.PossibleActionsParameter.ActualName = problem.PossibleActionsParameter.Name;
257
258      predictionArrayCalculator.PredictionParameter.ActualName = problem.Evaluator.PredictionParameter.ActualName;
259      predictionArrayCalculator.FitnessParameter.ActualName = problem.Evaluator.FitnessParameter.ActualName;
260    }
261    //private void ParameterizeSelectors() {
262    //  foreach (ISelector selector in SelectorParameter.ValidValues) {
263    //    selector.CopySelected = new BoolValue(true);
264    //    //set value by parameter!
265    //    selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(5);
266    //    selector.NumberOfSelectedSubScopesParameter.Hidden = true;
267    //    ParameterizeStochasticOperator(selector);
268    //  }
269    //  if (Problem != null) {
270    //    foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
271    //      selector.MaximizationParameter.Value = new BoolValue(true);
272    //      selector.MaximizationParameter.Hidden = true;
273    //      selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
274    //      selector.QualityParameter.Hidden = true;
275    //    }
276    //  }
277    //}
278  }
279}
Note: See TracBrowser for help on using the repository browser.