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 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.CombinedIntegerVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Random;
|
---|
33 |
|
---|
34 | namespace 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 | }
|
---|