Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8941 was 8941, checked in by sforsten, 11 years ago

#1980:
A general implementation of most main components of LCS is done with an own encoding.
At the moment you can just watch the LCS generating a number of solutions and selecting the match and action set in the debug engine.

File size: 10.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.Algorithms.GeneticAlgorithm;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.CombinedIntegerVectorEncoding;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Random;
34using HeuristicLab.Selection;
35
36namespace HeuristicLab.Algorithms.LearningClassifierSystems {
37  /// <summary>
38  /// An operator which represents the main loop of a learning classifier system.
39  /// </summary>
40  [Item("LearningClassifierSystemMainLoop", "An operator which represents the main loop of a learning classifier system.")]
41  [StorableClass]
42  public class LearningClassifierSystemMainLoop : AlgorithmOperator {
43
44    #region Parameter Properties
45    public IConstrainedValueParameter<ISelector> SelectorParameter {
46      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
47    }
48    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
49      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
50    }
51    public IConstrainedValueParameter<IManipulator> MutatorParameter {
52      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
53    }
54    #endregion
55
56    #region Properties
57    public ISelector Selector {
58      get { return SelectorParameter.Value; }
59      set { SelectorParameter.Value = value; }
60    }
61    public ICrossover Crossover {
62      get { return CrossoverParameter.Value; }
63      set { CrossoverParameter.Value = value; }
64    }
65    public IManipulator Mutator {
66      get { return MutatorParameter.Value; }
67      set { MutatorParameter.Value = value; }
68    }
69    #endregion
70
71    [StorableConstructor]
72    private LearningClassifierSystemMainLoop(bool deserializing) : base(deserializing) { }
73    protected LearningClassifierSystemMainLoop(LearningClassifierSystemMainLoop original, Cloner cloner)
74      : base(original, cloner) {
75    }
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new LearningClassifierSystemMainLoop(this, cloner);
78    }
79    public LearningClassifierSystemMainLoop()
80      : base() {
81      Initialize();
82    }
83
84    private void Initialize() {
85      #region Create parameters
86      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction.", new ItemSet<ISelector>() { new ProportionalSelector() }, new ProportionalSelector()));
87      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions.", new ItemSet<ICrossover>() { new SinglePointCrossover() }, new SinglePointCrossover()));
88      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions.", new ItemSet<IManipulator>() { new UniformOnePositionManipulator() }, new UniformOnePositionManipulator()));
89      //for test purposes
90      int[] numbers = new int[3] { 1, 1, 1 };
91      int[,] elements = new int[,] { { 0, 3 }, { 0, 3 }, { 0, 2 } };
92      Parameters.Add(new ValueLookupParameter<CombinedIntegerVector>("TestTarget", "Target for test", new CombinedIntegerVector(numbers, 1, elements)));
93
94      #endregion
95
96      #region Create operators
97      VariableCreator variableCreator = new VariableCreator();
98      MatchSelector conditionMatchSelector = new MatchSelector();
99      MatchSelector actionMatchSelector = new MatchSelector();
100      SubScopesProcessor coveringSubScopesProcessor = new SubScopesProcessor();
101      SubScopesRemover subScopesRemover = new SubScopesRemover();
102      Comparator comparator = new Comparator();
103      ConditionalBranch conditionalBranch = new ConditionalBranch();
104      MergingReducer mergingReducer = new MergingReducer();
105      SolutionsCreator coveringMechanism = new SolutionsCreator();
106      SubScopesProcessor actionSelectionSubScopesProcessor = new SubScopesProcessor();
107      MaxValueActionSelector maxValueActionSelection = new MaxValueActionSelector();
108      GeneticAlgorithmMainLoop geneticAlgorithmMainLoop = new GeneticAlgorithmMainLoop();
109
110      UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
111      UniformRandomizer uniformRandomizer = new UniformRandomizer();
112
113      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("ZeroIntValue", new IntValue(0)));
114      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("OneIntValue", new IntValue(1)));
115
116      conditionMatchSelector.CopySelected = new BoolValue(false);
117      conditionMatchSelector.MatchCondition = new BoolValue(true);
118      //this has to be set differently
119      conditionMatchSelector.MatchParameter.ActualName = "CombinedIntegerVector";
120      conditionMatchSelector.TargetMatchParameter.ActualName = "TestTarget";
121
122      comparator.Comparison = new Comparison(ComparisonType.LessOrEqual);
123      comparator.LeftSideParameter.ActualName = conditionMatchSelector.NumberOfSelectedSubScopesParameter.ActualName;
124      comparator.ResultParameter.ActualName = "Covering";
125      //more than zero solutions have to be selected
126      comparator.RightSideParameter.ActualName = "ZeroIntValue";
127
128      conditionalBranch.ConditionParameter.ActualName = "Covering";
129
130      subScopesRemover.SubScopeIndexParameter.ActualName = "OneIntValue";
131      subScopesRemover.RemoveAllSubScopes = false;
132
133      coveringMechanism.NumberOfSolutionsParameter.ActualName = "OneIntValue";
134      coveringMechanism.SolutionCreatorParameter.ActualName = "SolutionCreator";
135
136      maxValueActionSelection.ValueParameter.ActualName = "Fitness";
137      maxValueActionSelection.MatchParameter.ActualName = "CombinedIntegerVector";
138
139      actionMatchSelector.CopySelected = new BoolValue(false);
140      actionMatchSelector.MatchCondition = new BoolValue(false);
141      //this has to be set differently
142      actionMatchSelector.MatchParameter.ActualName = "CombinedIntegerVector";
143      actionMatchSelector.TargetMatchParameter.ActualName = maxValueActionSelection.SelectedActionParameter.ActualName;
144
145      SelectorParameter.Value.CopySelected = new BoolValue(true);
146      SelectorParameter.Value.NumberOfSelectedSubScopesParameter.Value = new IntValue(200);
147
148      geneticAlgorithmMainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
149      geneticAlgorithmMainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
150      geneticAlgorithmMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
151      geneticAlgorithmMainLoop.RandomParameter.ActualName = "Random";
152      geneticAlgorithmMainLoop.ElitesParameter.ActualName = "OneIntValue";
153      geneticAlgorithmMainLoop.MaximumGenerationsParameter.ActualName = "ZeroIntValue";
154      geneticAlgorithmMainLoop.QualityParameter.ActualName = "Fitness";
155      //needed?
156      geneticAlgorithmMainLoop.MutationProbabilityParameter.Value = new PercentValue(10);
157      geneticAlgorithmMainLoop.MaximizationParameter.Value = new BoolValue(true);
158
159      uniformSubScopeProcessor.Parallel = new BoolValue(true);
160
161      uniformRandomizer.RandomParameter.ActualName = "Random";
162      uniformRandomizer.MinParameter.Value = new DoubleValue(0);
163      uniformRandomizer.MaxParameter.Value = new DoubleValue(100);
164      uniformRandomizer.ValueParameter.ActualName = "Fitness";
165
166      //ParameterizeSelectors();
167      #endregion
168
169      #region Create operator graph
170      OperatorGraph.InitialOperator = variableCreator;
171      variableCreator.Successor = conditionMatchSelector;
172      //variableCreator.Successor = geneticAlgorithmMainLoop;
173      //geneticAlgorithmMainLoop.Successor = conditionMatchSelector;
174      conditionMatchSelector.Successor = comparator;
175      comparator.Successor = conditionalBranch;
176      conditionalBranch.TrueBranch = coveringSubScopesProcessor;
177      conditionalBranch.FalseBranch = actionSelectionSubScopesProcessor;
178      conditionalBranch.Successor = subScopesRemover;
179      coveringMechanism.Successor = uniformSubScopeProcessor;
180      uniformSubScopeProcessor.Operator = uniformRandomizer;
181      coveringSubScopesProcessor.Operators.Add(new EmptyOperator());
182      coveringSubScopesProcessor.Operators.Add(coveringMechanism);
183      coveringSubScopesProcessor.Successor = actionSelectionSubScopesProcessor;
184      actionSelectionSubScopesProcessor.Operators.Add(new EmptyOperator());
185      actionSelectionSubScopesProcessor.Operators.Add(maxValueActionSelection);
186      maxValueActionSelection.Successor = actionMatchSelector;
187      subScopesRemover.Successor = mergingReducer;
188      mergingReducer.Successor = conditionMatchSelector;
189      //mergingReducer.Successor = geneticAlgorithmMainLoop;
190      #endregion
191    }
192
193    private void ParameterizeStochasticOperator(IOperator op) {
194      IStochasticOperator stochasticOp = op as IStochasticOperator;
195      if (stochasticOp != null) {
196        stochasticOp.RandomParameter.ActualName = "Random";
197        stochasticOp.RandomParameter.Hidden = true;
198      }
199    }
200    //private void ParameterizeSelectors() {
201    //  foreach (ISelector selector in SelectorParameter.ValidValues) {
202    //    selector.CopySelected = new BoolValue(true);
203    //    //set value by parameter!
204    //    selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(5);
205    //    selector.NumberOfSelectedSubScopesParameter.Hidden = true;
206    //    ParameterizeStochasticOperator(selector);
207    //  }
208    //  if (Problem != null) {
209    //    foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
210    //      selector.MaximizationParameter.Value = new BoolValue(true);
211    //      selector.MaximizationParameter.Hidden = true;
212    //      selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
213    //      selector.QualityParameter.Hidden = true;
214    //    }
215    //  }
216    //}
217  }
218}
Note: See TracBrowser for help on using the repository browser.