Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Problems.ConditionActionClassification/3.3/ConditionActionClassificationProblem.cs @ 15529

Last change on this file since 15529 was 9467, checked in by sforsten, 12 years ago

#1980:

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