Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8941 was 8941, checked in by sforsten, 12 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: 7.0 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 System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.CombinedIntegerVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Algorithms.LearningClassifierSystems {
35  /// <summary>
36  /// A learning classifier system.
37  /// </summary>
38  [Item("Learning Classifier System", "A genetic algorithm.")]
39  [Creatable("Algorithms")]
40  [StorableClass]
41  public class LearningClassifierSystem : HeuristicOptimizationEngineAlgorithm, IStorableContent {
42    public string Filename { get; set; }
43
44    #region Problem Properties
45    public override Type ProblemType {
46      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
47    }
48    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
49      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
50      set { base.Problem = value; }
51    }
52    #endregion
53
54    #region Parameter Properties
55    private ValueParameter<IntValue> SeedParameter {
56      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
57    }
58    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
59      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
60    }
61    private ValueParameter<IntValue> PopulationSizeParameter {
62      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
63    }
64    //for test purpose
65    public ValueParameter<ICombinedIntegerVectorCreator> SolutionCreatorParameter {
66      get { return (ValueParameter<ICombinedIntegerVectorCreator>)Parameters["SolutionCreator"]; }
67    }
68    public ValueParameter<IntValue> Length {
69      get { return (ValueParameter<IntValue>)Parameters["Length"]; }
70    }
71    public ValueParameter<IntValue> ActionPartLength {
72      get { return (ValueParameter<IntValue>)Parameters["ActionPartLength"]; }
73    }
74    public ValueParameter<IntMatrix> Bounds {
75      get { return (ValueParameter<IntMatrix>)Parameters["Bounds"]; }
76    }
77    #endregion
78
79    #region Properties
80    public IntValue Seed {
81      get { return SeedParameter.Value; }
82      set { SeedParameter.Value = value; }
83    }
84    public BoolValue SetSeedRandomly {
85      get { return SetSeedRandomlyParameter.Value; }
86      set { SetSeedRandomlyParameter.Value = value; }
87    }
88    public IntValue PopulationSize {
89      get { return PopulationSizeParameter.Value; }
90      set { PopulationSizeParameter.Value = value; }
91    }
92
93    #endregion
94
95    public LearningClassifierSystem()
96      : base() {
97      #region Create parameters
98      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
99      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
100      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
101      //for test purpose
102      Parameters.Add(new ValueParameter<ICombinedIntegerVectorCreator>("SolutionCreator", "The operator to create a solution.", new UniformRandomCombinedIntegerVectorCreator()));
103      Parameters.Add(new ValueParameter<IntValue>("Length", "The operator to create a solution.", new IntValue(3)));
104      Parameters.Add(new ValueParameter<IntValue>("ActionPartLength", "The operator to create a solution.", new IntValue(1)));
105      int[,] elements = new int[,] { { 0, 3 }, { 0, 3 }, { 0, 2 } };
106      Parameters.Add(new ValueParameter<IntMatrix>("Bounds", "The operator to create a solution.", new IntMatrix(elements)));
107      #endregion
108
109      #region Create operators
110      RandomCreator randomCreator = new RandomCreator();
111      SolutionsCreator solutionsCreator = new SolutionsCreator();
112      UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
113      UniformRandomizer uniformRandomizer = new UniformRandomizer();
114      //ResultsCollector resultsCollector = new ResultsCollector();
115      LearningClassifierSystemMainLoop mainLoop = new LearningClassifierSystemMainLoop();
116
117      randomCreator.RandomParameter.ActualName = "Random";
118      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
119      randomCreator.SeedParameter.Value = null;
120      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
121      randomCreator.SetSeedRandomlyParameter.Value = null;
122
123      SolutionCreatorParameter.Value.ActionPartLengthParameter.ActualName = ActionPartLength.Name;
124      SolutionCreatorParameter.Value.LengthParameter.ActualName = Length.Name;
125      SolutionCreatorParameter.Value.BoundsParameter.ActualName = Bounds.Name;
126
127      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
128      solutionsCreator.SolutionCreatorParameter.ActualName = SolutionCreatorParameter.Name;
129
130      uniformSubScopeProcessor.Parallel = new BoolValue(true);
131
132      uniformRandomizer.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
133      uniformRandomizer.MinParameter.Value = new DoubleValue(0);
134      uniformRandomizer.MaxParameter.Value = new DoubleValue(100);
135      uniformRandomizer.ValueParameter.ActualName = "Fitness";
136
137      #endregion
138
139      #region Create operator graph
140      OperatorGraph.InitialOperator = randomCreator;
141      randomCreator.Successor = solutionsCreator;
142      solutionsCreator.Successor = uniformSubScopeProcessor;
143      uniformSubScopeProcessor.Operator = uniformRandomizer;
144      uniformSubScopeProcessor.Successor = mainLoop;
145      #endregion
146    }
147    protected LearningClassifierSystem(LearningClassifierSystem original, Cloner cloner)
148      : base(original, cloner) {
149    }
150    public override IDeepCloneable Clone(Cloner cloner) {
151      return new LearningClassifierSystem(this, cloner);
152    }
153    [StorableConstructor]
154    private LearningClassifierSystem(bool deserializing) : base(deserializing) { }
155  }
156}
Note: See TracBrowser for help on using the repository browser.