Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/ActionSelection/ActionSelector.cs @ 9194

Last change on this file since 9194 was 9194, checked in by sforsten, 11 years ago

#1980:

  • added necessary interface ICondition, IAction, IInput
  • removed not used class MatchSelector and interface IMatchSelector
  • added constructors to ItemDictionary
  • added new encoding
File size: 3.5 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.ConditionActionEncoding {
32  [Item("ActionSelector", "")]
33  [StorableClass]
34  public class ActionSelector : SingleSuccessorOperator, IStochasticOperator {
35
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    public IValueLookupParameter<IAction> SelectedActionParameter {
40      get { return (IValueLookupParameter<IAction>)Parameters["SelectedAction"]; }
41    }
42    public ILookupParameter<IItemDictionary<IAction, DoubleValue>> PredictionArrayParameter {
43      get { return (ILookupParameter<IItemDictionary<IAction, DoubleValue>>)Parameters["PredictionArray"]; }
44    }
45    public ILookupParameter<PercentValue> ExplorationProbabilityParameter {
46      get { return (ILookupParameter<PercentValue>)Parameters["ExplorationProbability"]; }
47    }
48
49    [StorableConstructor]
50    protected ActionSelector(bool deserializing) : base(deserializing) { }
51    protected ActionSelector(ActionSelector original, Cloner cloner)
52      : base(original, cloner) {
53    }
54    public ActionSelector()
55      : base() {
56      Parameters.Add(new LookupParameter<IRandom>("Random"));
57      Parameters.Add(new ValueLookupParameter<IAction>("SelectedAction", "Action, which has been selected."));
58      Parameters.Add(new LookupParameter<IItemDictionary<IAction, DoubleValue>>("PredictionArray"));
59      Parameters.Add(new LookupParameter<PercentValue>("ExplorationProbability"));
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new ActionSelector(this, cloner);
64    }
65
66    public override IOperation Apply() {
67      if (RandomParameter.ActualValue.NextDouble() > ExplorationProbabilityParameter.ActualValue.Value) {
68        double maxValue = PredictionArrayParameter.ActualValue.Max(x => x.Value.Value);
69        SelectedActionParameter.ActualValue = PredictionArrayParameter.ActualValue.Where(x => x.Value.Value.IsAlmost(maxValue))
70                                                                                  .Select(x => x.Key)
71                                                                                  .First();
72      } else {
73        int actionIndex = RandomParameter.ActualValue.Next(0, PredictionArrayParameter.ActualValue.Count);
74        SelectedActionParameter.ActualValue = PredictionArrayParameter.ActualValue.ElementAt(actionIndex).Key;
75      }
76      return base.Apply();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.