[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.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.CombinedIntegerVectorEncoding;
|
---|
| 27 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.ConditionActionClassification {
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public class ConditionActionClassificationProblem : HeuristicOptimizationProblem<XCSEvaluator, UniformRandomCombinedIntegerVectorCreator>, IConditionActionProblem {
|
---|
| 36 | private const string ClassifierFetcherParameterName = "ClassifierFetcher";
|
---|
| 37 | private const string ActionExecuterParameterName = "ActionExecuter";
|
---|
[9105] | 38 | private const string ActionSetSubsumptionOperatorParameterName = "ActionSetSubsumption";
|
---|
[9089] | 39 |
|
---|
| 40 | IXCSEvaluator IConditionActionProblem.Evaluator {
|
---|
| 41 | get { return Evaluator; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | #region parameter properties
|
---|
| 45 | public IValueParameter<ConditionActionClassificationProblemData> ProblemDataParameter {
|
---|
| 46 | get { return (IValueParameter<ConditionActionClassificationProblemData>)Parameters["ProblemData"]; }
|
---|
| 47 | }
|
---|
[9090] | 48 | public IValueParameter<ICoveringSolutionCreator> CoveringSolutionCreatorParameter {
|
---|
| 49 | get { return (IValueParameter<ICoveringSolutionCreator>)Parameters["CoveringSolutionCreator"]; }
|
---|
| 50 | }
|
---|
| 51 | public IFixedValueParameter<PercentValue> ChangeSymbolProbabilityInCoveringParameter {
|
---|
| 52 | get { return (IFixedValueParameter<PercentValue>)Parameters["ChangeSymbolProbabilityInCovering"]; }
|
---|
| 53 | }
|
---|
[9089] | 54 | public IFixedValueParameter<DoubleValue> PositiveRewardParameter {
|
---|
| 55 | get { return (IFixedValueParameter<DoubleValue>)Parameters["PositiveReward"]; }
|
---|
| 56 | }
|
---|
| 57 | public IFixedValueParameter<DoubleValue> NegativeRewardParameter {
|
---|
| 58 | get { return (IFixedValueParameter<DoubleValue>)Parameters["NegativeReward"]; }
|
---|
| 59 | }
|
---|
| 60 | public IFixedValueParameter<DoubleValue> InitialPredictionParameter {
|
---|
| 61 | get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialPrediction"]; }
|
---|
| 62 | }
|
---|
| 63 | public IFixedValueParameter<DoubleValue> InitialErrorParameter {
|
---|
| 64 | get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialError"]; }
|
---|
| 65 | }
|
---|
| 66 | public IFixedValueParameter<DoubleValue> InitialFitnessParameter {
|
---|
| 67 | get { return (IFixedValueParameter<DoubleValue>)Parameters["InitialFitness"]; }
|
---|
| 68 | }
|
---|
[9090] | 69 | public IFixedValueParameter<ItemSet<IClassifier>> PossibleActionsParameter {
|
---|
| 70 | get { return (IFixedValueParameter<ItemSet<IClassifier>>)Parameters["PossibleActions"]; }
|
---|
[9089] | 71 | }
|
---|
[9110] | 72 | public IFixedValueParameter<ItemSet<CombinedIntegerVector>> PossibleActionsConcreteClassParameter {
|
---|
| 73 | get { return (IFixedValueParameter<ItemSet<CombinedIntegerVector>>)Parameters["PossibleActionsConcreteClass"]; }
|
---|
| 74 | }
|
---|
[9090] | 75 | public IFixedValueParameter<IntValue> ThetaMinimalNumberOfActionsParameter {
|
---|
[9089] | 76 | get { return (IFixedValueParameter<IntValue>)Parameters["ThetaMinimalNumberOfActions"]; }
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | #region properties
|
---|
| 81 | IParameter IConditionActionProblem.ProblemDataParameter {
|
---|
| 82 | get { return ProblemDataParameter; }
|
---|
| 83 | }
|
---|
| 84 | IConditionActionProblemData IConditionActionProblem.ProblemData {
|
---|
| 85 | get { return ProblemData; }
|
---|
| 86 | }
|
---|
| 87 | public ConditionActionClassificationProblemData ProblemData {
|
---|
| 88 | get { return ProblemDataParameter.Value; }
|
---|
| 89 | }
|
---|
[9110] | 90 | IParameter IConditionActionProblem.PossibleActionsConcreteClassParameter {
|
---|
| 91 | get { return PossibleActionsConcreteClassParameter; }
|
---|
| 92 | }
|
---|
| 93 | public ItemSet<CombinedIntegerVector> PossibleActionsConcreteClass {
|
---|
| 94 | get { return PossibleActionsConcreteClassParameter.Value; }
|
---|
| 95 | }
|
---|
[9089] | 96 | IParameter IConditionActionProblem.PossibleActionsParameter {
|
---|
| 97 | get { return PossibleActionsParameter; }
|
---|
| 98 | }
|
---|
[9110] | 99 | //IItemSet<IClassifier> IConditionActionProblem.PossibleActions {
|
---|
| 100 | // get { return PossibleActions; }
|
---|
| 101 | //}
|
---|
[9090] | 102 | public ItemSet<IClassifier> PossibleActions {
|
---|
[9089] | 103 | get { return PossibleActionsParameter.Value; }
|
---|
| 104 | }
|
---|
| 105 | public IActionExecuter ActionExecuter {
|
---|
| 106 | get { return ActionExecuterParameter.Value; }
|
---|
| 107 | }
|
---|
| 108 | public ValueParameter<IActionExecuter> ActionExecuterParameter {
|
---|
| 109 | get { return (ValueParameter<IActionExecuter>)Parameters[ActionExecuterParameterName]; }
|
---|
| 110 | }
|
---|
| 111 | IParameter IConditionActionProblem.ActionExecuterParameter { get { return ActionExecuterParameter; } }
|
---|
[9154] | 112 | public ClassifierFetcher ClassifierFetcher {
|
---|
[9089] | 113 | get { return ClassifierFetcherParameter.Value; }
|
---|
| 114 | }
|
---|
[9154] | 115 | public ValueParameter<ClassifierFetcher> ClassifierFetcherParameter {
|
---|
| 116 | get { return (ValueParameter<ClassifierFetcher>)Parameters[ClassifierFetcherParameterName]; }
|
---|
[9089] | 117 | }
|
---|
| 118 | IClassifierFetcher IConditionActionProblem.ClassifierFetcher { get { return ClassifierFetcher; } }
|
---|
| 119 | IParameter IConditionActionProblem.ClassifierFetcherParameter { get { return ClassifierFetcherParameter; } }
|
---|
| 120 |
|
---|
[9105] | 121 |
|
---|
[9110] | 122 | public ActionSetSubsumptionOperator ActionSetSubsumptionOperator {
|
---|
[9105] | 123 | get { return ActionSetSubsumptionOperatorParameter.Value; }
|
---|
| 124 | }
|
---|
[9110] | 125 | public ValueParameter<ActionSetSubsumptionOperator> ActionSetSubsumptionOperatorParameter {
|
---|
| 126 | get { return (ValueParameter<ActionSetSubsumptionOperator>)Parameters[ActionSetSubsumptionOperatorParameterName]; }
|
---|
[9105] | 127 | }
|
---|
| 128 | IActionSetSubsumption IConditionActionProblem.ActionSetSubsumptionOperator { get { return ActionSetSubsumptionOperator; } }
|
---|
| 129 | IParameter IConditionActionProblem.ActionSetSubsumptionOperatorParameter { get { return ActionSetSubsumptionOperatorParameter; } }
|
---|
| 130 |
|
---|
[9089] | 131 | private IntValue ThetaMinimalNumberOfActions {
|
---|
| 132 | get { return ThetaMinimalNumberOfActionsParameter.Value; }
|
---|
| 133 | }
|
---|
[9090] | 134 | IParameter IConditionActionProblem.ThetaMinimalNumberOfActionsParameter {
|
---|
| 135 | get { return ThetaMinimalNumberOfActionsParameter; }
|
---|
| 136 | }
|
---|
| 137 | public ICoveringSolutionCreator CoveringSolutionCreator {
|
---|
| 138 | get { return CoveringSolutionCreatorParameter.Value; }
|
---|
| 139 | }
|
---|
| 140 | IParameter IConditionActionProblem.CoveringSolutionCreatorParameter {
|
---|
| 141 | get { return CoveringSolutionCreatorParameter; }
|
---|
| 142 | }
|
---|
[9154] | 143 | private XCSSolutionAnalyzer XCSSolutionAnalyzer {
|
---|
| 144 | get { return Operators.OfType<XCSSolutionAnalyzer>().FirstOrDefault(); }
|
---|
| 145 | }
|
---|
[9089] | 146 | #endregion
|
---|
| 147 |
|
---|
| 148 | [StorableConstructor]
|
---|
| 149 | protected ConditionActionClassificationProblem(bool deserializing) : base(deserializing) { }
|
---|
| 150 | protected ConditionActionClassificationProblem(ConditionActionClassificationProblem original, Cloner cloner)
|
---|
| 151 | : base(original, cloner) {
|
---|
| 152 | }
|
---|
| 153 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 154 | return new ConditionActionClassificationProblem(this, cloner);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | public ConditionActionClassificationProblem() :
|
---|
| 158 | this(new ConditionActionClassificationProblemData(new Dataset(ConditionActionClassificationProblemData.defaultVariableNames, ConditionActionClassificationProblemData.defaultData),
|
---|
| 159 | ConditionActionClassificationProblemData.defaultVariableNames.Take(ConditionActionClassificationProblemData.defaultVariableNames.Length - 1), ConditionActionClassificationProblemData.defaultVariableNames.Last().ToEnumerable()),
|
---|
[9090] | 160 | new XCSEvaluator(), new UniformRandomCombinedIntegerVectorCreator(), new CombinedIntegerVectorCoveringCreator()) {
|
---|
[9089] | 161 | }
|
---|
| 162 |
|
---|
[9090] | 163 | public ConditionActionClassificationProblem(ConditionActionClassificationProblemData problemData, XCSEvaluator evaluator, UniformRandomCombinedIntegerVectorCreator solutionCreator, ICoveringSolutionCreator coveringSolutionCreator)
|
---|
[9089] | 164 | : base(evaluator, solutionCreator) {
|
---|
| 165 | Parameters.Add(new ValueParameter<ConditionActionClassificationProblemData>("ProblemData", "", problemData));
|
---|
| 166 | Parameters.Add(new FixedValueParameter<DoubleValue>("PositiveReward", "", new DoubleValue(1000)));
|
---|
| 167 | Parameters.Add(new FixedValueParameter<DoubleValue>("NegativeReward", "", new DoubleValue(0)));
|
---|
| 168 | Parameters.Add(new FixedValueParameter<DoubleValue>("InitialPrediction", "Initial Presiction", new DoubleValue(0)));
|
---|
| 169 | Parameters.Add(new FixedValueParameter<DoubleValue>("InitialError", "Initial Error", new DoubleValue(0)));
|
---|
| 170 | Parameters.Add(new FixedValueParameter<DoubleValue>("InitialFitness", "Initial Fitness", new DoubleValue(0)));
|
---|
| 171 |
|
---|
| 172 | Parameters.Add(new ValueParameter<IActionExecuter>(ActionExecuterParameterName, "", new ActionExecuter()));
|
---|
[9154] | 173 | Parameters.Add(new ValueParameter<ClassifierFetcher>(ClassifierFetcherParameterName, "", new ClassifierFetcher()));
|
---|
[9089] | 174 | Parameters.Add(new FixedValueParameter<ItemSet<IClassifier>>("PossibleActions"));
|
---|
[9110] | 175 | Parameters.Add(new FixedValueParameter<ItemSet<CombinedIntegerVector>>("PossibleActionsConcreteClass"));
|
---|
[9105] | 176 | 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)));
|
---|
[9089] | 177 |
|
---|
[9090] | 178 | Parameters.Add(new ValueParameter<ICoveringSolutionCreator>("CoveringSolutionCreator", "", coveringSolutionCreator));
|
---|
| 179 | Parameters.Add(new FixedValueParameter<PercentValue>("ChangeSymbolProbabilityInCovering", "", new PercentValue(0.5)));
|
---|
| 180 |
|
---|
[9110] | 181 | Parameters.Add(new ValueParameter<ActionSetSubsumptionOperator>(ActionSetSubsumptionOperatorParameterName, "", new ActionSetSubsumptionOperator()));
|
---|
[9105] | 182 |
|
---|
[9089] | 183 | Evaluator.InitialErrorParameter.ActualName = "InitialError";
|
---|
| 184 | Evaluator.InitialFitnessParameter.ActualName = "InitialFitness";
|
---|
| 185 | Evaluator.InitialPredictionParameter.ActualName = "InitialPrediction";
|
---|
| 186 |
|
---|
[9154] | 187 | SolutionCreator.ActionPartLengthParameter.ActualName = ProblemData.ActionLengthParameter.Name;
|
---|
| 188 | SolutionCreator.LengthParameter.ActualName = ProblemData.LengthParameter.Name;
|
---|
| 189 | SolutionCreator.BoundsParameter.ActualName = ProblemData.BoundsParameter.Name;
|
---|
[9089] | 190 |
|
---|
[9090] | 191 | coveringSolutionCreator.ChangeSymbolProbabilityParameter.ActualName = ChangeSymbolProbabilityInCoveringParameter.Name;
|
---|
| 192 | coveringSolutionCreator.CoverClassifierParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
|
---|
| 193 | coveringSolutionCreator.CreatedClassifierParameter.ActualName = "CombinedIntegerVector";
|
---|
| 194 |
|
---|
[9089] | 195 | ClassifierFetcher.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
|
---|
| 196 |
|
---|
| 197 | ActionExecuter.CurrentClassifierToMatchParameter.ActualName = ClassifierFetcher.CurrentClassifierToMatchParameter.ActualName;
|
---|
| 198 | ActionExecuter.NegativeRewardParameter.ActualName = NegativeRewardParameter.Name;
|
---|
| 199 | ActionExecuter.PositiveRewardParameter.ActualName = PositiveRewardParameter.Name;
|
---|
| 200 |
|
---|
[9105] | 201 | ActionSetSubsumptionOperator.ClassifiersParameter.ActualName = "CombinedIntegerVector";
|
---|
| 202 |
|
---|
[9089] | 203 | SetPossibleActions();
|
---|
| 204 | ThetaMinimalNumberOfActions.Value = PossibleActions.Count;
|
---|
| 205 |
|
---|
[9154] | 206 | InitializeOperators();
|
---|
| 207 |
|
---|
| 208 | problemData.Changed += new System.EventHandler(problemData_Changed);
|
---|
[9089] | 209 | }
|
---|
| 210 |
|
---|
[9154] | 211 | private void problemData_Changed(object sender, System.EventArgs e) {
|
---|
[9089] | 212 | SetPossibleActions();
|
---|
| 213 | }
|
---|
[9154] | 214 |
|
---|
| 215 | private void InitializeOperators() {
|
---|
| 216 | Operators.Add(new XCSSolutionAnalyzer());
|
---|
| 217 |
|
---|
| 218 | ParameterizeAnalyzers();
|
---|
[9089] | 219 | }
|
---|
[9154] | 220 |
|
---|
| 221 | private void ParameterizeAnalyzers() {
|
---|
| 222 | if (XCSSolutionAnalyzer != null) {
|
---|
| 223 | XCSSolutionAnalyzer.ClassifierParameter.ActualName = SolutionCreator.CombinedIntegerVectorParameter.ActualName;
|
---|
| 224 | XCSSolutionAnalyzer.PredictionParameter.ActualName = Evaluator.PredictionParameter.ActualName;
|
---|
| 225 | XCSSolutionAnalyzer.ErrorParameter.ActualName = Evaluator.ErrorParameter.ActualName;
|
---|
| 226 | XCSSolutionAnalyzer.FitnessParameter.ActualName = Evaluator.FitnessParameter.ActualName;
|
---|
| 227 | XCSSolutionAnalyzer.ExperienceParameter.ActualName = Evaluator.ExperienceParameter.ActualName;
|
---|
| 228 | XCSSolutionAnalyzer.AverageActionSetSizeParameter.ActualName = Evaluator.AverageActionSetSizeParameter.ActualName;
|
---|
| 229 | XCSSolutionAnalyzer.NumerosityParameter.ActualName = Evaluator.NumerosityParameter.ActualName;
|
---|
| 230 | XCSSolutionAnalyzer.TimestampParameter.ActualName = Evaluator.TimestampParameter.ActualName;
|
---|
| 231 | XCSSolutionAnalyzer.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
|
---|
| 232 | XCSSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 233 | }
|
---|
[9089] | 234 | }
|
---|
| 235 |
|
---|
| 236 | private void SetPossibleActions() {
|
---|
| 237 | //get bounds of action
|
---|
[9154] | 238 | IntMatrix actionBounds = GetElementsOfBoundsForAction(ProblemData.Bounds, ProblemData.Length.Value, ProblemData.ActionLength.Value);
|
---|
| 239 | int actionLength = ProblemData.ActionLength.Value;
|
---|
| 240 | int start = ProblemData.Length.Value - actionLength;
|
---|
[9089] | 241 | int[] elements = new int[actionLength];
|
---|
| 242 | int[] curPos = new int[actionLength];
|
---|
| 243 | bool done = false;
|
---|
| 244 | //initialize curPos
|
---|
| 245 | for (int i = 0; i < actionBounds.Rows; i++) {
|
---|
| 246 | curPos[i] = actionBounds[i, 0];
|
---|
| 247 | }
|
---|
| 248 | PossibleActions.Clear();
|
---|
[9110] | 249 | PossibleActionsConcreteClass.Clear();
|
---|
[9089] | 250 | while (!done) {
|
---|
| 251 | PossibleActions.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
|
---|
[9110] | 252 | PossibleActionsConcreteClass.Add(new CombinedIntegerVector(curPos, actionLength, actionBounds));
|
---|
[9089] | 253 | curPos = GetNextAction(curPos, actionBounds, out done);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | private int[] GetNextAction(int[] curPos, IntMatrix actionBounds, out bool done) {
|
---|
| 259 | int cur = 0;
|
---|
| 260 | while (cur < curPos.Length) {
|
---|
| 261 | curPos[cur] += actionBounds.Columns < 3 ? 1 : actionBounds[cur, 2];
|
---|
| 262 | if (curPos[cur] >= actionBounds[cur, 1]) {
|
---|
| 263 | curPos[cur] = actionBounds[cur, 0];
|
---|
| 264 | cur++;
|
---|
| 265 | } else {
|
---|
| 266 | break;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | done = cur >= curPos.Length;
|
---|
| 270 | return curPos;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
|
---|
| 274 | IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
|
---|
| 275 | int start = length - actionPartLength;
|
---|
| 276 | for (int i = start; i < length; i++) {
|
---|
| 277 | int pos = i % bounds.Rows;
|
---|
| 278 | for (int j = 0; j < bounds.Columns; j++) {
|
---|
| 279 | actionBounds[i - start, j] = bounds[pos, j];
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | return actionBounds;
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | }
|
---|