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 System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.Instances;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.ConditionActionClassification {
|
---|
34 | [StorableClass]
|
---|
35 | public abstract class ConditionActionClassificationProblem<T, U, V> : HeuristicOptimizationProblem<XCSEvaluator, T>, IConditionActionProblem,
|
---|
36 | IProblemInstanceConsumer<V>
|
---|
37 | where T : class, ISolutionCreator
|
---|
38 | where U : class, IClassifierComparer
|
---|
39 | where V : class, IConditionActionProblemData {
|
---|
40 | private const string ClassifierFetcherParameterName = "ClassifierFetcher";
|
---|
41 | private const string ActionExecuterParameterName = "ActionExecuter";
|
---|
42 | private const string ActionSetSubsumptionOperatorParameterName = "ActionSetSubsumption";
|
---|
43 |
|
---|
44 | public abstract string ChildName { get; }
|
---|
45 |
|
---|
46 | IXCSEvaluator IConditionActionProblem.Evaluator {
|
---|
47 | get { return Evaluator; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | #region parameter properties
|
---|
51 | public IValueParameter<V> ProblemDataParameter {
|
---|
52 | get { return (IValueParameter<V>)Parameters["ProblemData"]; }
|
---|
53 | }
|
---|
54 | public IValueParameter<ICoveringSolutionCreator> CoveringSolutionCreatorParameter {
|
---|
55 | get { return (IValueParameter<ICoveringSolutionCreator>)Parameters["CoveringSolutionCreator"]; }
|
---|
56 | }
|
---|
57 | public IFixedValueParameter<PercentValue> ChangeSymbolProbabilityInCoveringParameter {
|
---|
58 | get { return (IFixedValueParameter<PercentValue>)Parameters["ChangeSymbolProbabilityInCovering"]; }
|
---|
59 | }
|
---|
60 | public IFixedValueParameter<DoubleValue> PositiveRewardParameter {
|
---|
61 | get { return (IFixedValueParameter<DoubleValue>)Parameters["PositiveReward"]; }
|
---|
62 | }
|
---|
63 | public IFixedValueParameter<DoubleValue> NegativeRewardParameter {
|
---|
64 | get { return (IFixedValueParameter<DoubleValue>)Parameters["NegativeReward"]; }
|
---|
65 | }
|
---|
66 | public IFixedValueParameter<DoubleValue> InitialPredictionParameter {
|
---|
67 | get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialPrediction"]; }
|
---|
68 | }
|
---|
69 | public IFixedValueParameter<DoubleValue> InitialErrorParameter {
|
---|
70 | get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialError"]; }
|
---|
71 | }
|
---|
72 | public IFixedValueParameter<DoubleValue> InitialFitnessParameter {
|
---|
73 | get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialFitness"]; }
|
---|
74 | }
|
---|
75 | public IFixedValueParameter<ItemSet<IAction>> PossibleActionsParameter {
|
---|
76 | get { return (IFixedValueParameter<ItemSet<IAction>>)Parameters["PossibleActions"]; }
|
---|
77 | }
|
---|
78 | public IFixedValueParameter<IntValue> ThetaMinimalNumberOfActionsParameter {
|
---|
79 | get { return (IFixedValueParameter<IntValue>)Parameters["ThetaMinimalNumberOfActions"]; }
|
---|
80 | }
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | #region properties
|
---|
84 | IParameter IConditionActionProblem.ProblemDataParameter {
|
---|
85 | get { return ProblemDataParameter; }
|
---|
86 | }
|
---|
87 | IConditionActionProblemData IConditionActionProblem.ProblemData {
|
---|
88 | get { return ProblemData; }
|
---|
89 | }
|
---|
90 | public virtual V ProblemData {
|
---|
91 | get { return ProblemDataParameter.Value; }
|
---|
92 | protected set {
|
---|
93 | ProblemDataParameter.Value = value;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | IParameter IConditionActionProblem.PossibleActionsParameter {
|
---|
97 | get { return PossibleActionsParameter; }
|
---|
98 | }
|
---|
99 | public ItemSet<IAction> PossibleActions {
|
---|
100 | get { return PossibleActionsParameter.Value; }
|
---|
101 | }
|
---|
102 | public IActionExecuter ActionExecuter {
|
---|
103 | get { return ActionExecuterParameter.Value; }
|
---|
104 | }
|
---|
105 | public ValueParameter<IActionExecuter> ActionExecuterParameter {
|
---|
106 | get { return (ValueParameter<IActionExecuter>)Parameters[ActionExecuterParameterName]; }
|
---|
107 | }
|
---|
108 | IParameter IConditionActionProblem.ActionExecuterParameter { get { return ActionExecuterParameter; } }
|
---|
109 | public ClassifierFetcher ClassifierFetcher {
|
---|
110 | get { return ClassifierFetcherParameter.Value; }
|
---|
111 | }
|
---|
112 | public ValueParameter<ClassifierFetcher> ClassifierFetcherParameter {
|
---|
113 | get { return (ValueParameter<ClassifierFetcher>)Parameters[ClassifierFetcherParameterName]; }
|
---|
114 | }
|
---|
115 | IClassifierFetcher IConditionActionProblem.ClassifierFetcher { get { return ClassifierFetcher; } }
|
---|
116 | IParameter IConditionActionProblem.ClassifierFetcherParameter { get { return ClassifierFetcherParameter; } }
|
---|
117 | public ActionSetSubsumptionOperator ActionSetSubsumptionOperator {
|
---|
118 | get { return ActionSetSubsumptionOperatorParameter.Value; }
|
---|
119 | }
|
---|
120 | public ValueParameter<ActionSetSubsumptionOperator> ActionSetSubsumptionOperatorParameter {
|
---|
121 | get { return (ValueParameter<ActionSetSubsumptionOperator>)Parameters[ActionSetSubsumptionOperatorParameterName]; }
|
---|
122 | }
|
---|
123 | IActionSetSubsumption IConditionActionProblem.ActionSetSubsumptionOperator { get { return ActionSetSubsumptionOperator; } }
|
---|
124 | IParameter IConditionActionProblem.ActionSetSubsumptionOperatorParameter { get { return ActionSetSubsumptionOperatorParameter; } }
|
---|
125 |
|
---|
126 | protected IntValue ThetaMinimalNumberOfActions {
|
---|
127 | get { return ThetaMinimalNumberOfActionsParameter.Value; }
|
---|
128 | }
|
---|
129 | IParameter IConditionActionProblem.ThetaMinimalNumberOfActionsParameter {
|
---|
130 | get { return ThetaMinimalNumberOfActionsParameter; }
|
---|
131 | }
|
---|
132 | public ICoveringSolutionCreator CoveringSolutionCreator {
|
---|
133 | get { return CoveringSolutionCreatorParameter.Value; }
|
---|
134 | }
|
---|
135 | IParameter IConditionActionProblem.CoveringSolutionCreatorParameter {
|
---|
136 | get { return CoveringSolutionCreatorParameter; }
|
---|
137 | }
|
---|
138 | IParameter IConditionActionProblem.ClassifierComparerParameter {
|
---|
139 | get { return ClassifierComparerParameter; }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public abstract IFixedValueParameter<U> ClassifierComparerParameter { get; }
|
---|
143 | #endregion
|
---|
144 |
|
---|
145 | [StorableConstructor]
|
---|
146 | protected ConditionActionClassificationProblem(bool deserializing) : base(deserializing) { }
|
---|
147 | protected ConditionActionClassificationProblem(ConditionActionClassificationProblem<T, U, V> original, Cloner cloner)
|
---|
148 | : base(original, cloner) {
|
---|
149 | }
|
---|
150 |
|
---|
151 | public ConditionActionClassificationProblem(V problemData, XCSEvaluator evaluator, T solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
|
---|
152 | : base(evaluator, solutionCreator) {
|
---|
153 | Parameters.Add(new ValueParameter<V>("ProblemData", "", problemData));
|
---|
154 | Parameters.Add(new FixedValueParameter<DoubleValue>("PositiveReward", "", new DoubleValue(1000)));
|
---|
155 | Parameters.Add(new FixedValueParameter<DoubleValue>("NegativeReward", "", new DoubleValue(0)));
|
---|
156 | Parameters.Add(new FixedValueParameter<DoubleValue>("InitialPrediction", "Initial Presiction", new DoubleValue(0)));
|
---|
157 | Parameters.Add(new FixedValueParameter<DoubleValue>("InitialError", "Initial Error", new DoubleValue(0)));
|
---|
158 | Parameters.Add(new FixedValueParameter<DoubleValue>("InitialFitness", "Initial Fitness", new DoubleValue(0)));
|
---|
159 |
|
---|
160 | Parameters.Add(new ValueParameter<IActionExecuter>(ActionExecuterParameterName, "", new ActionExecuter()));
|
---|
161 | Parameters.Add(new ValueParameter<ClassifierFetcher>(ClassifierFetcherParameterName, "", new ClassifierFetcher()));
|
---|
162 | Parameters.Add(new FixedValueParameter<IntValue>("ThetaMinimalNumberOfActions", "Minimal number of actions, which have to be present in the match set, or else covering will occure.", new IntValue(1)));
|
---|
163 |
|
---|
164 | Parameters.Add(new ValueParameter<ICoveringSolutionCreator>("CoveringSolutionCreator", "", coveringSolutionCreator));
|
---|
165 | Parameters.Add(new FixedValueParameter<PercentValue>("ChangeSymbolProbabilityInCovering", "", new PercentValue(0.33)));
|
---|
166 |
|
---|
167 | Parameters.Add(new ValueParameter<ActionSetSubsumptionOperator>(ActionSetSubsumptionOperatorParameterName, "", new ActionSetSubsumptionOperator()));
|
---|
168 |
|
---|
169 | Evaluator.InitialErrorParameter.ActualName = "InitialError";
|
---|
170 | Evaluator.InitialFitnessParameter.ActualName = "InitialFitness";
|
---|
171 | Evaluator.InitialPredictionParameter.ActualName = "InitialPrediction";
|
---|
172 |
|
---|
173 | coveringSolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
|
---|
174 | coveringSolutionCreator.CoverInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
|
---|
175 | coveringSolutionCreator.CreatedClassifierParameter.ActualName = ChildName;
|
---|
176 |
|
---|
177 | ClassifierFetcher.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
|
---|
178 |
|
---|
179 | ActionExecuter.CurrentActionToMatchParameter.ActualName = ClassifierFetcher.CurrentActionToMatchParameter.ActualName;
|
---|
180 | ActionExecuter.NegativeRewardParameter.ActualName = NegativeRewardParameter.Name;
|
---|
181 | ActionExecuter.PositiveRewardParameter.ActualName = PositiveRewardParameter.Name;
|
---|
182 |
|
---|
183 | ActionSetSubsumptionOperator.ClassifiersParameter.ActualName = ChildName;
|
---|
184 |
|
---|
185 | InitializeOperators();
|
---|
186 |
|
---|
187 | //test event handlers after deserialization
|
---|
188 | RegisterEventHandlers();
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void RegisterEventHandlers() {
|
---|
192 | ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
|
---|
193 | if (ProblemDataParameter.Value != null) ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
|
---|
197 | ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
|
---|
198 | SetProblemDataSettings();
|
---|
199 | OnReset();
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void ProblemData_Changed(object sender, EventArgs e) {
|
---|
203 | SetProblemDataSettings();
|
---|
204 | }
|
---|
205 |
|
---|
206 | protected abstract void SetProblemDataSettings();
|
---|
207 |
|
---|
208 | private void InitializeOperators() {
|
---|
209 | Operators.Add(new BestTrainingXCSSolutionAnalyzer());
|
---|
210 | Operators.Add(new CurrentXCSSolutionAnalyzer());
|
---|
211 | }
|
---|
212 |
|
---|
213 | protected void ParameterizeOperators() {
|
---|
214 | foreach (XCSSolutionAnalyzer xcsAnalyzer in Operators.OfType<XCSSolutionAnalyzer>()) {
|
---|
215 | xcsAnalyzer.ClassifierParameter.ActualName = ChildName;
|
---|
216 | xcsAnalyzer.PredictionParameter.ActualName = Evaluator.PredictionParameter.ActualName;
|
---|
217 | xcsAnalyzer.ErrorParameter.ActualName = Evaluator.ErrorParameter.ActualName;
|
---|
218 | xcsAnalyzer.FitnessParameter.ActualName = Evaluator.FitnessParameter.ActualName;
|
---|
219 | xcsAnalyzer.ExperienceParameter.ActualName = Evaluator.ExperienceParameter.ActualName;
|
---|
220 | xcsAnalyzer.AverageActionSetSizeParameter.ActualName = Evaluator.AverageActionSetSizeParameter.ActualName;
|
---|
221 | xcsAnalyzer.NumerosityParameter.ActualName = Evaluator.NumerosityParameter.ActualName;
|
---|
222 | xcsAnalyzer.TimestampParameter.ActualName = Evaluator.TimestampParameter.ActualName;
|
---|
223 | xcsAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
|
---|
224 | xcsAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
225 | xcsAnalyzer.ClassifierComparerParameter.ActualName = ClassifierComparerParameter.Name;
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | protected abstract void SetPossibleActions();
|
---|
230 |
|
---|
231 | public void Load(V data) {
|
---|
232 | Name = data.Name;
|
---|
233 | Description = data.Description;
|
---|
234 | ProblemData = data;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | }
|
---|