#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 HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.ConditionActionEncoding { [Item("XCSEvaluator", "Evaluator tries to match condition and action of IMatching.")] [StorableClass] public class XCSEvaluator : SingleSuccessorOperator, IXCSEvaluator { #region parameter properties public IValueLookupParameter FitnessParameter { get { return (IValueLookupParameter)Parameters["Fitness"]; } } public IValueLookupParameter CurrentPayoffParameter { get { return (IValueLookupParameter)Parameters["CurrentPayoff"]; } } public IValueLookupParameter PredictionParameter { get { return (IValueLookupParameter)Parameters["Prediction"]; } } public IValueLookupParameter ErrorParameter { get { return (IValueLookupParameter)Parameters["Error"]; } } public IValueLookupParameter ExperienceParameter { get { return (IValueLookupParameter)Parameters["Experience"]; } } public IValueLookupParameter TimestampParameter { get { return (IValueLookupParameter)Parameters["Timestamp"]; } } public IValueLookupParameter AverageActionSetSizeParameter { get { return (IValueLookupParameter)Parameters["AverageActionSetSize"]; } } public IValueLookupParameter NumerosityParameter { get { return (IValueLookupParameter)Parameters["Numerosity"]; } } public IValueLookupParameter InsertInPopulationParameter { get { return (IValueLookupParameter)Parameters["InsertInPopulation"]; } } public ILookupParameter InitialPredictionParameter { get { return (ILookupParameter)Parameters["InitialPrediction"]; } } public ILookupParameter InitialErrorParameter { get { return (ILookupParameter)Parameters["InitialError"]; } } public ILookupParameter InitialFitnessParameter { get { return (ILookupParameter)Parameters["InitialFitness"]; } } public ILookupParameter BetaParameter { get { return (ILookupParameter)Parameters["Beta"]; } } public ILookupParameter AlphaParameter { get { return (ILookupParameter)Parameters["Alpha"]; } } public ILookupParameter PowerParameter { get { return (ILookupParameter)Parameters["v"]; } } public ILookupParameter ActualTimeParameter { get { return (ILookupParameter)Parameters["ActualTime"]; } } public ILookupParameter CurrentActionSetSizeParameter { get { return (ILookupParameter)Parameters["CurrentActionSetSize"]; } } public ILookupParameter ErrorZeroParameter { get { return (ILookupParameter)Parameters["ErrorZero"]; } } public ILookupParameter AccuracyParameter { get { return (ILookupParameter)Parameters["Accuracy"]; } } public ILookupParameter AccuracySumParameter { get { return (ILookupParameter)Parameters["AccuracySum"]; } } #endregion #region properties private int Experience { get { return ExperienceParameter.ActualValue.Value; } set { ExperienceParameter.ActualValue.Value = value; } } private double Beta { get { return BetaParameter.ActualValue.Value; } set { BetaParameter.ActualValue.Value = value; } } private double Prediction { get { return PredictionParameter.ActualValue.Value; } set { PredictionParameter.ActualValue.Value = value; } } private double CurrentPayoff { get { return CurrentPayoffParameter.ActualValue.Value; } set { CurrentPayoffParameter.ActualValue.Value = value; } } private double Error { get { return ErrorParameter.ActualValue.Value; } set { ErrorParameter.ActualValue.Value = value; } } private double AverageActionSetSize { get { return AverageActionSetSizeParameter.ActualValue.Value; } set { AverageActionSetSizeParameter.ActualValue.Value = value; } } private int CurrentActionSetSize { get { return CurrentActionSetSizeParameter.ActualValue.Value; } set { CurrentActionSetSizeParameter.ActualValue.Value = value; } } private double Fitness { get { return FitnessParameter.ActualValue.Value; } set { FitnessParameter.ActualValue.Value = value; } } private double Accuracy { get { return AccuracyParameter.ActualValue.Value; } } private double AccuracySum { get { return AccuracySumParameter.ActualValue.Value; } } private int Numerosity { get { return NumerosityParameter.ActualValue.Value; } } #endregion [StorableConstructor] protected XCSEvaluator(bool deserializing) : base(deserializing) { } public override IDeepCloneable Clone(Cloner cloner) { return new XCSEvaluator(this, cloner); } protected XCSEvaluator(XCSEvaluator original, Cloner cloner) : base(original, cloner) { } public XCSEvaluator() : base() { Parameters.Add(new ValueLookupParameter("Fitness", "")); Parameters.Add(new ValueLookupParameter("CurrentPayoff", "")); Parameters.Add(new ValueLookupParameter("Prediction", "")); Parameters.Add(new ValueLookupParameter("Error", "")); Parameters.Add(new ValueLookupParameter("Experience", "")); Parameters.Add(new ValueLookupParameter("Timestamp", "")); Parameters.Add(new ValueLookupParameter("AverageActionSetSize", "")); Parameters.Add(new LookupParameter("Accuracy", "")); Parameters.Add(new LookupParameter("AccuracySum", "")); Parameters.Add(new ValueLookupParameter("Numerosity", "")); Parameters.Add(new ValueLookupParameter("InsertInPopulation", "")); Parameters.Add(new LookupParameter("InitialPrediction")); Parameters.Add(new LookupParameter("InitialError")); Parameters.Add(new LookupParameter("InitialFitness")); Parameters.Add(new LookupParameter("ActualTime")); Parameters.Add(new LookupParameter("Beta")); Parameters.Add(new LookupParameter("Alpha")); Parameters.Add(new LookupParameter("v")); Parameters.Add(new LookupParameter("ErrorZero")); Parameters.Add(new LookupParameter("CurrentActionSetSize")); } public override IOperation Apply() { if (PredictionParameter.ActualValue == null) { //the classifier has just been created PredictionParameter.ActualValue = new DoubleValue(InitialPredictionParameter.ActualValue.Value); ErrorParameter.ActualValue = new DoubleValue(InitialErrorParameter.ActualValue.Value); FitnessParameter.ActualValue = new DoubleValue(InitialFitnessParameter.ActualValue.Value); ExperienceParameter.ActualValue = new IntValue(0); TimestampParameter.ActualValue = new IntValue(ActualTimeParameter.ActualValue.Value); AverageActionSetSizeParameter.ActualValue = new DoubleValue(1); NumerosityParameter.ActualValue = new IntValue(1); InsertInPopulationParameter.ActualValue = new BoolValue(false); } else { Experience += 1; if (Experience < (1 / Beta)) { Prediction += (CurrentPayoff - Prediction) / Experience; Error += (Math.Abs(CurrentPayoff - Prediction) - Error) / Experience; AverageActionSetSize += (CurrentActionSetSize - AverageActionSetSize) / Experience; } else { Prediction += Beta * (CurrentPayoff - Prediction); Error += Beta * (Math.Abs(CurrentPayoff - Prediction) - Error); AverageActionSetSize += Beta * (CurrentActionSetSize - AverageActionSetSize); } Fitness += Beta * ((Accuracy * Numerosity) / AccuracySum - Fitness); } return base.Apply(); } } }