[9089] | 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.Operators;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Encodings.ConditionActionEncoding {
|
---|
| 31 | [Item("XCSEvaluator", "Evaluator tries to match condition and action of IMatching.")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public class XCSEvaluator : SingleSuccessorOperator, IXCSEvaluator {
|
---|
| 34 | #region parameter properties
|
---|
| 35 | public IValueLookupParameter<DoubleValue> FitnessParameter {
|
---|
| 36 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Fitness"]; }
|
---|
| 37 | }
|
---|
| 38 | public IValueLookupParameter<DoubleValue> CurrentPayoffParameter {
|
---|
| 39 | get { return (IValueLookupParameter<DoubleValue>)Parameters["CurrentPayoff"]; }
|
---|
| 40 | }
|
---|
| 41 | public IValueLookupParameter<DoubleValue> PredictionParameter {
|
---|
| 42 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Prediction"]; }
|
---|
| 43 | }
|
---|
| 44 | public IValueLookupParameter<DoubleValue> ErrorParameter {
|
---|
| 45 | get { return (IValueLookupParameter<DoubleValue>)Parameters["Error"]; }
|
---|
| 46 | }
|
---|
| 47 | public IValueLookupParameter<IntValue> ExperienceParameter {
|
---|
| 48 | get { return (IValueLookupParameter<IntValue>)Parameters["Experience"]; }
|
---|
| 49 | }
|
---|
| 50 | public IValueLookupParameter<IntValue> TimestampParameter {
|
---|
| 51 | get { return (IValueLookupParameter<IntValue>)Parameters["Timestamp"]; }
|
---|
| 52 | }
|
---|
| 53 | public IValueLookupParameter<DoubleValue> AverageActionSetSizeParameter {
|
---|
| 54 | get { return (IValueLookupParameter<DoubleValue>)Parameters["AverageActionSetSize"]; }
|
---|
| 55 | }
|
---|
| 56 | public IValueLookupParameter<IntValue> NumerosityParameter {
|
---|
| 57 | get { return (IValueLookupParameter<IntValue>)Parameters["Numerosity"]; }
|
---|
| 58 | }
|
---|
[9110] | 59 | public IValueLookupParameter<BoolValue> InsertInPopulationParameter {
|
---|
| 60 | get { return (IValueLookupParameter<BoolValue>)Parameters["InsertInPopulation"]; }
|
---|
| 61 | }
|
---|
[9089] | 62 | public ILookupParameter<DoubleValue> InitialPredictionParameter {
|
---|
| 63 | get { return (ILookupParameter<DoubleValue>)Parameters["InitialPrediction"]; }
|
---|
| 64 | }
|
---|
| 65 | public ILookupParameter<DoubleValue> InitialErrorParameter {
|
---|
| 66 | get { return (ILookupParameter<DoubleValue>)Parameters["InitialError"]; }
|
---|
| 67 | }
|
---|
| 68 | public ILookupParameter<DoubleValue> InitialFitnessParameter {
|
---|
| 69 | get { return (ILookupParameter<DoubleValue>)Parameters["InitialFitness"]; }
|
---|
| 70 | }
|
---|
| 71 | public ILookupParameter<PercentValue> BetaParameter {
|
---|
| 72 | get { return (ILookupParameter<PercentValue>)Parameters["Beta"]; }
|
---|
| 73 | }
|
---|
| 74 | public ILookupParameter<PercentValue> AlphaParameter {
|
---|
| 75 | get { return (ILookupParameter<PercentValue>)Parameters["Alpha"]; }
|
---|
| 76 | }
|
---|
| 77 | public ILookupParameter<DoubleValue> PowerParameter {
|
---|
| 78 | get { return (ILookupParameter<DoubleValue>)Parameters["v"]; }
|
---|
| 79 | }
|
---|
| 80 | public ILookupParameter<IntValue> ActualTimeParameter {
|
---|
| 81 | get { return (ILookupParameter<IntValue>)Parameters["ActualTime"]; }
|
---|
| 82 | }
|
---|
| 83 | public ILookupParameter<IntValue> CurrentActionSetSizeParameter {
|
---|
| 84 | get { return (ILookupParameter<IntValue>)Parameters["CurrentActionSetSize"]; }
|
---|
| 85 | }
|
---|
| 86 | public ILookupParameter<DoubleValue> ErrorZeroParameter {
|
---|
| 87 | get { return (ILookupParameter<DoubleValue>)Parameters["ErrorZero"]; }
|
---|
| 88 | }
|
---|
| 89 | public ILookupParameter<DoubleValue> AccuracyParameter {
|
---|
| 90 | get { return (ILookupParameter<DoubleValue>)Parameters["Accuracy"]; }
|
---|
| 91 | }
|
---|
| 92 | public ILookupParameter<DoubleValue> AccuracySumParameter {
|
---|
| 93 | get { return (ILookupParameter<DoubleValue>)Parameters["AccuracySum"]; }
|
---|
| 94 | }
|
---|
| 95 | #endregion
|
---|
| 96 |
|
---|
| 97 | #region properties
|
---|
| 98 | private int Experience {
|
---|
| 99 | get { return ExperienceParameter.ActualValue.Value; }
|
---|
| 100 | set { ExperienceParameter.ActualValue.Value = value; }
|
---|
| 101 | }
|
---|
| 102 | private double Beta {
|
---|
| 103 | get { return BetaParameter.ActualValue.Value; }
|
---|
| 104 | set { BetaParameter.ActualValue.Value = value; }
|
---|
| 105 | }
|
---|
| 106 | private double Prediction {
|
---|
| 107 | get { return PredictionParameter.ActualValue.Value; }
|
---|
| 108 | set { PredictionParameter.ActualValue.Value = value; }
|
---|
| 109 | }
|
---|
| 110 | private double CurrentPayoff {
|
---|
| 111 | get { return CurrentPayoffParameter.ActualValue.Value; }
|
---|
| 112 | set { CurrentPayoffParameter.ActualValue.Value = value; }
|
---|
| 113 | }
|
---|
| 114 | private double Error {
|
---|
| 115 | get { return ErrorParameter.ActualValue.Value; }
|
---|
| 116 | set { ErrorParameter.ActualValue.Value = value; }
|
---|
| 117 | }
|
---|
| 118 | private double AverageActionSetSize {
|
---|
| 119 | get { return AverageActionSetSizeParameter.ActualValue.Value; }
|
---|
| 120 | set { AverageActionSetSizeParameter.ActualValue.Value = value; }
|
---|
| 121 | }
|
---|
| 122 | private int CurrentActionSetSize {
|
---|
| 123 | get { return CurrentActionSetSizeParameter.ActualValue.Value; }
|
---|
| 124 | set { CurrentActionSetSizeParameter.ActualValue.Value = value; }
|
---|
| 125 | }
|
---|
| 126 | private double Fitness {
|
---|
| 127 | get { return FitnessParameter.ActualValue.Value; }
|
---|
| 128 | set { FitnessParameter.ActualValue.Value = value; }
|
---|
| 129 | }
|
---|
| 130 | private double Accuracy {
|
---|
| 131 | get { return AccuracyParameter.ActualValue.Value; }
|
---|
| 132 | }
|
---|
| 133 | private double AccuracySum {
|
---|
| 134 | get { return AccuracySumParameter.ActualValue.Value; }
|
---|
| 135 | }
|
---|
| 136 | private int Numerosity {
|
---|
| 137 | get { return NumerosityParameter.ActualValue.Value; }
|
---|
| 138 | }
|
---|
| 139 | #endregion
|
---|
| 140 |
|
---|
| 141 | [StorableConstructor]
|
---|
| 142 | protected XCSEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 143 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 144 | return new XCSEvaluator(this, cloner);
|
---|
| 145 | }
|
---|
| 146 | protected XCSEvaluator(XCSEvaluator original, Cloner cloner)
|
---|
| 147 | : base(original, cloner) {
|
---|
| 148 | }
|
---|
| 149 | public XCSEvaluator()
|
---|
| 150 | : base() {
|
---|
| 151 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Fitness", ""));
|
---|
| 152 | Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentPayoff", ""));
|
---|
| 153 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Prediction", ""));
|
---|
| 154 | Parameters.Add(new ValueLookupParameter<DoubleValue>("Error", ""));
|
---|
| 155 | Parameters.Add(new ValueLookupParameter<IntValue>("Experience", ""));
|
---|
| 156 | Parameters.Add(new ValueLookupParameter<IntValue>("Timestamp", ""));
|
---|
| 157 | Parameters.Add(new ValueLookupParameter<DoubleValue>("AverageActionSetSize", ""));
|
---|
| 158 | Parameters.Add(new LookupParameter<DoubleValue>("Accuracy", ""));
|
---|
| 159 | Parameters.Add(new LookupParameter<DoubleValue>("AccuracySum", ""));
|
---|
| 160 | Parameters.Add(new ValueLookupParameter<IntValue>("Numerosity", ""));
|
---|
[9110] | 161 | Parameters.Add(new ValueLookupParameter<BoolValue>("InsertInPopulation", ""));
|
---|
[9089] | 162 |
|
---|
| 163 | Parameters.Add(new LookupParameter<DoubleValue>("InitialPrediction"));
|
---|
| 164 | Parameters.Add(new LookupParameter<DoubleValue>("InitialError"));
|
---|
| 165 | Parameters.Add(new LookupParameter<DoubleValue>("InitialFitness"));
|
---|
| 166 | Parameters.Add(new LookupParameter<IntValue>("ActualTime"));
|
---|
| 167 | Parameters.Add(new LookupParameter<PercentValue>("Beta"));
|
---|
| 168 | Parameters.Add(new LookupParameter<PercentValue>("Alpha"));
|
---|
| 169 | Parameters.Add(new LookupParameter<DoubleValue>("v"));
|
---|
| 170 | Parameters.Add(new LookupParameter<DoubleValue>("ErrorZero"));
|
---|
| 171 | Parameters.Add(new LookupParameter<IntValue>("CurrentActionSetSize"));
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | public override IOperation Apply() {
|
---|
| 175 | if (PredictionParameter.ActualValue == null) {
|
---|
| 176 | //the classifier has just been created
|
---|
| 177 | PredictionParameter.ActualValue = new DoubleValue(InitialPredictionParameter.ActualValue.Value);
|
---|
| 178 | ErrorParameter.ActualValue = new DoubleValue(InitialErrorParameter.ActualValue.Value);
|
---|
| 179 | FitnessParameter.ActualValue = new DoubleValue(InitialFitnessParameter.ActualValue.Value);
|
---|
| 180 | ExperienceParameter.ActualValue = new IntValue(0);
|
---|
| 181 | TimestampParameter.ActualValue = new IntValue(ActualTimeParameter.ActualValue.Value);
|
---|
| 182 | AverageActionSetSizeParameter.ActualValue = new DoubleValue(1);
|
---|
| 183 | NumerosityParameter.ActualValue = new IntValue(1);
|
---|
[9110] | 184 | InsertInPopulationParameter.ActualValue = new BoolValue(false);
|
---|
[9089] | 185 | } else {
|
---|
| 186 | Experience += 1;
|
---|
| 187 | if (Experience < (1 / Beta)) {
|
---|
| 188 | Prediction += (CurrentPayoff - Prediction) / Experience;
|
---|
| 189 | Error += (Math.Abs(CurrentPayoff - Prediction) - Error) / Experience;
|
---|
| 190 | AverageActionSetSize += (CurrentActionSetSize - AverageActionSetSize) / Experience;
|
---|
| 191 | } else {
|
---|
| 192 | Prediction += Beta * (CurrentPayoff - Prediction);
|
---|
| 193 | Error += Beta * (Math.Abs(CurrentPayoff - Prediction) - Error);
|
---|
| 194 | AverageActionSetSize += Beta * (CurrentActionSetSize - AverageActionSetSize);
|
---|
| 195 | }
|
---|
| 196 | Fitness += Beta * ((Accuracy * Numerosity) / AccuracySum - Fitness);
|
---|
| 197 | }
|
---|
| 198 | return base.Apply();
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
| 201 | }
|
---|