#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.ConditionActionEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
using HeuristicLab.Problems.DataAnalysis;
using HeuristicLab.Problems.Instances;
namespace HeuristicLab.Problems.ConditionActionClassification {
[StorableClass]
public abstract class ConditionActionClassificationProblem : HeuristicOptimizationProblem, IConditionActionProblem,
IProblemInstanceConsumer
where T : class, ISolutionCreator
where U : class, IClassifierComparer
where V : class, IConditionActionProblemData {
private const string ClassifierFetcherParameterName = "ClassifierFetcher";
private const string ActionExecuterParameterName = "ActionExecuter";
private const string ActionSetSubsumptionOperatorParameterName = "ActionSetSubsumption";
private const string MaximizationParameterName = "Maximization";
public abstract string ChildName { get; }
public IFixedValueParameter MaximizationParameter {
get { return (IFixedValueParameter)Parameters[MaximizationParameterName]; }
}
IParameter IConditionActionProblem.MaximizationParameter {
get { return MaximizationParameter; }
}
IXCSEvaluator IConditionActionProblem.Evaluator {
get { return Evaluator; }
}
#region parameter properties
public IValueParameter ProblemDataParameter {
get { return (IValueParameter)Parameters["ProblemData"]; }
}
public IValueParameter CoveringSolutionCreatorParameter {
get { return (IValueParameter)Parameters["CoveringSolutionCreator"]; }
}
public IFixedValueParameter ChangeSymbolProbabilityInCoveringParameter {
get { return (IFixedValueParameter)Parameters["ChangeSymbolProbabilityInCovering"]; }
}
public IFixedValueParameter PositiveRewardParameter {
get { return (IFixedValueParameter)Parameters["PositiveReward"]; }
}
public IFixedValueParameter NegativeRewardParameter {
get { return (IFixedValueParameter)Parameters["NegativeReward"]; }
}
public IFixedValueParameter InitialPredictionParameter {
get { return (IFixedValueParameter)Parameters["InitialPrediction"]; }
}
public IFixedValueParameter InitialErrorParameter {
get { return (IFixedValueParameter)Parameters["InitialError"]; }
}
public IFixedValueParameter InitialFitnessParameter {
get { return (IFixedValueParameter)Parameters["InitialFitness"]; }
}
public IFixedValueParameter> PossibleActionsParameter {
get { return (IFixedValueParameter>)Parameters["PossibleActions"]; }
}
public IFixedValueParameter ThetaMinimalNumberOfActionsParameter {
get { return (IFixedValueParameter)Parameters["ThetaMinimalNumberOfActions"]; }
}
#endregion
#region properties
IConditionActionProblemData IConditionActionProblem.ProblemData {
get { return ProblemData; }
}
public virtual V ProblemData {
get { return ProblemDataParameter.Value; }
protected set {
ProblemDataParameter.Value = value;
}
}
IParameter IConditionActionProblem.PossibleActionsParameter {
get { return PossibleActionsParameter; }
}
public ItemSet PossibleActions {
get { return PossibleActionsParameter.Value; }
}
public IActionExecuter ActionExecuter {
get { return ActionExecuterParameter.Value; }
}
public ValueParameter ActionExecuterParameter {
get { return (ValueParameter)Parameters[ActionExecuterParameterName]; }
}
IParameter IConditionActionProblem.ActionExecuterParameter { get { return ActionExecuterParameter; } }
public ClassifierFetcher ClassifierFetcher {
get { return ClassifierFetcherParameter.Value; }
}
public ValueParameter ClassifierFetcherParameter {
get { return (ValueParameter)Parameters[ClassifierFetcherParameterName]; }
}
IClassifierFetcher IConditionActionProblem.ClassifierFetcher { get { return ClassifierFetcher; } }
IParameter IConditionActionProblem.ClassifierFetcherParameter { get { return ClassifierFetcherParameter; } }
public ActionSetSubsumptionOperator ActionSetSubsumptionOperator {
get { return ActionSetSubsumptionOperatorParameter.Value; }
}
public ValueParameter ActionSetSubsumptionOperatorParameter {
get { return (ValueParameter)Parameters[ActionSetSubsumptionOperatorParameterName]; }
}
IActionSetSubsumption IConditionActionProblem.ActionSetSubsumptionOperator { get { return ActionSetSubsumptionOperator; } }
IParameter IConditionActionProblem.ActionSetSubsumptionOperatorParameter { get { return ActionSetSubsumptionOperatorParameter; } }
protected IntValue ThetaMinimalNumberOfActions {
get { return ThetaMinimalNumberOfActionsParameter.Value; }
}
IParameter IConditionActionProblem.ThetaMinimalNumberOfActionsParameter {
get { return ThetaMinimalNumberOfActionsParameter; }
}
public ICoveringSolutionCreator CoveringSolutionCreator {
get { return CoveringSolutionCreatorParameter.Value; }
}
IParameter IConditionActionProblem.CoveringSolutionCreatorParameter {
get { return CoveringSolutionCreatorParameter; }
}
IParameter IConditionActionProblem.ClassifierComparerParameter {
get { return ClassifierComparerParameter; }
}
public abstract IFixedValueParameter ClassifierComparerParameter { get; }
#endregion
[StorableConstructor]
protected ConditionActionClassificationProblem(bool deserializing) : base(deserializing) { }
protected ConditionActionClassificationProblem(ConditionActionClassificationProblem original, Cloner cloner)
: base(original, cloner) {
}
public ConditionActionClassificationProblem(V problemData, XCSEvaluator evaluator, T solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
: base(evaluator, solutionCreator) {
Parameters.Add(new FixedValueParameter(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue((true))));
Parameters.Add(new ValueParameter("ProblemData", "", problemData));
Parameters.Add(new FixedValueParameter("PositiveReward", "", new DoubleValue(1000)));
Parameters.Add(new FixedValueParameter("NegativeReward", "", new DoubleValue(0)));
Parameters.Add(new FixedValueParameter("InitialPrediction", "Initial Presiction", new DoubleValue(0)));
Parameters.Add(new FixedValueParameter("InitialError", "Initial Error", new DoubleValue(0)));
Parameters.Add(new FixedValueParameter("InitialFitness", "Initial Fitness", new DoubleValue(0)));
Parameters.Add(new ValueParameter(ActionExecuterParameterName, "", new ActionExecuter()));
Parameters.Add(new ValueParameter(ClassifierFetcherParameterName, "", new ClassifierFetcher()));
Parameters.Add(new FixedValueParameter("ThetaMinimalNumberOfActions", "Minimal number of actions, which have to be present in the match set, or else covering will occure.", new IntValue(1)));
Parameters.Add(new ValueParameter("CoveringSolutionCreator", "", coveringSolutionCreator));
Parameters.Add(new FixedValueParameter("ChangeSymbolProbabilityInCovering", "", new PercentValue(0.33)));
Parameters.Add(new ValueParameter(ActionSetSubsumptionOperatorParameterName, "", new ActionSetSubsumptionOperator()));
Evaluator.InitialErrorParameter.ActualName = "InitialError";
Evaluator.InitialFitnessParameter.ActualName = "InitialFitness";
Evaluator.InitialPredictionParameter.ActualName = "InitialPrediction";
coveringSolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
coveringSolutionCreator.CoverInputParameter.ActualName = ClassifierFetcher.CurrentInputToMatchParameter.ActualName;
coveringSolutionCreator.CreatedClassifierParameter.ActualName = ChildName;
ClassifierFetcher.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
ActionExecuter.CurrentActionToMatchParameter.ActualName = ClassifierFetcher.CurrentActionToMatchParameter.ActualName;
ActionExecuter.NegativeRewardParameter.ActualName = NegativeRewardParameter.Name;
ActionExecuter.PositiveRewardParameter.ActualName = PositiveRewardParameter.Name;
ActionSetSubsumptionOperator.ClassifiersParameter.ActualName = ChildName;
InitializeOperators();
//test event handlers after deserialization
RegisterEventHandlers();
}
private void RegisterEventHandlers() {
ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
if (ProblemDataParameter.Value != null) ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
}
private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
ProblemDataParameter.Value.Changed += new EventHandler(ProblemData_Changed);
SetProblemDataSettings();
OnReset();
}
private void ProblemData_Changed(object sender, EventArgs e) {
SetProblemDataSettings();
}
protected abstract void SetProblemDataSettings();
private void InitializeOperators() {
Operators.Add(new BestTrainingXCSSolutionAnalyzer());
Operators.Add(new CurrentXCSSolutionAnalyzer());
Operators.AddRange(ApplicationManager.Manager.GetInstances().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
}
protected virtual void ParameterizeOperators() {
foreach (XCSSolutionAnalyzer xcsAnalyzer in Operators.OfType()) {
xcsAnalyzer.ClassifierParameter.ActualName = ChildName;
xcsAnalyzer.PredictionParameter.ActualName = Evaluator.PredictionParameter.ActualName;
xcsAnalyzer.ErrorParameter.ActualName = Evaluator.ErrorParameter.ActualName;
xcsAnalyzer.FitnessParameter.ActualName = Evaluator.FitnessParameter.ActualName;
xcsAnalyzer.ExperienceParameter.ActualName = Evaluator.ExperienceParameter.ActualName;
xcsAnalyzer.AverageActionSetSizeParameter.ActualName = Evaluator.AverageActionSetSizeParameter.ActualName;
xcsAnalyzer.NumerosityParameter.ActualName = Evaluator.NumerosityParameter.ActualName;
xcsAnalyzer.TimestampParameter.ActualName = Evaluator.TimestampParameter.ActualName;
xcsAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
xcsAnalyzer.ResultsParameter.ActualName = "Results";
xcsAnalyzer.ClassifierComparerParameter.ActualName = ClassifierComparerParameter.Name;
}
}
protected abstract void SetPossibleActions();
public void Load(V data) {
Name = data.Name;
Description = data.Description;
ProblemData = data;
}
#region IDataAnalysisProblem Members
IParameter DataAnalysis.IDataAnalysisProblem.ProblemDataParameter {
get { return ProblemDataParameter; }
}
IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
get { return ProblemData; }
}
public event EventHandler ProblemDataChanged;
#endregion
}
}