Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Action/StringAction.cs @ 14536

Last change on this file since 14536 was 9605, checked in by sforsten, 12 years ago

#1980:

  • set plugin dependencies
  • added smart initialization
  • added hierarchical selection
  • fixed major and minor default rule
  • fixed several smaller bugs
  • some refactoring has been done
File size: 5.2 KB
RevLine 
[9334]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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
[9352]27using HeuristicLab.Optimization.Operators.LCS;
[9334]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.DecisionList {
31  [StorableClass]
32  [Item("StringAction", "")]
33  public class StringAction : Item, IAction<string> {
34
[9352]35    public static IEqualityComparer<IGAssistNiche> Comparer {
[9411]36      get { return new GAssistNicheComparer(); }
[9352]37    }
38
39    IEqualityComparer<IGAssistNiche> IGAssistNiche.Comparer {
40      get { return Comparer; }
41    }
42
[9334]43    [Storable]
44    protected string variableName;
45    public string VariableName {
46      get { return variableName; }
47    }
48
49    [Storable]
50    protected IList<string> possibleFeatures;
51    public IEnumerable<string> PossibleFeatures {
52      get { return possibleFeatures; }
53    }
54
55    [Storable]
56    protected int currentActionIndex;
57    public int CurrentActionIndex {
58      get { return currentActionIndex; }
59    }
60
61    public int CurrentPosition {
62      get { return CurrentActionIndex; }
63    }
64
65    public string CurrentAction {
66      get { return possibleFeatures[currentActionIndex]; }
67    }
68
69    public int Possibilities { get { return possibleFeatures.Count; } }
70
71    [StorableConstructor]
72    protected StringAction(bool deserializing) : base(deserializing) { }
73    protected StringAction(StringAction original, Cloner cloner)
74      : base(original, cloner) {
75      variableName = original.variableName;
76      possibleFeatures = new List<string>(original.possibleFeatures);
77      currentActionIndex = original.currentActionIndex;
78    }
79    public StringAction(string variableName, IList<string> variableValues)
80      : base() {
81      if (variableValues.Count() > 0 && variableValues.Count() != variableValues.Distinct().Count()) {
82        throw new ArgumentException("variableValues have to be distinct and there has to be at least one value.");
83      }
84      this.possibleFeatures = variableValues;
85      this.variableName = variableName;
86    }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new StringAction(this, cloner);
89    }
90
91    public IEnumerable<int> GetPossibleActionPositions() {
92      return Enumerable.Range(0, possibleFeatures.Count);
93    }
94
95    public IEnumerable<string> GetPossibleActions() {
96      return PossibleFeatures;
97    }
98
99    public void Randomize(IRandom random) {
100      currentActionIndex = random.Next(0, possibleFeatures.Count());
101    }
102
[9352]103    public void Randomize(IRandom random, IEnumerable<IAction> except) {
104      if (except.Count() == 0) {
105        Randomize(random);
106        return;
107      }
108      try {
109        var exceptInt = except.Cast<StringAction>().Select(x => x.CurrentActionIndex);
110        var newPossibleFeatures = Enumerable.Range(0, possibleFeatures.Count()).Except(exceptInt);
111        currentActionIndex = newPossibleFeatures.ElementAt(random.Next(0, newPossibleFeatures.Count()));
112      }
113      catch (InvalidCastException) {
114        throw new InvalidCastException("Actions have to be of type IntAction");
115      }
116    }
117
[9334]118    public bool Match(IAction action) {
119      var targetCast = action as StringAction;
120      return targetCast != null && this.variableName.Equals(targetCast.VariableName) && CurrentAction.Equals(targetCast.CurrentAction);
121    }
122
123    public void SetTo(string value) {
[9605]124      SetToPosition(possibleFeatures.IndexOf(value));
[9334]125    }
126
[9605]127    public void SetTo(IGAssistNiche action) {
128      var castAction = action as StringAction;
129      if (castAction == null) throw new ArgumentException("action has to be IntAction");
130      SetToPosition(castAction.CurrentPosition);
131    }
132
[9334]133    public void SetToPosition(int value) {
134      if (value < 0 || value > possibleFeatures.Count) { throw new ArgumentOutOfRangeException(); }
135      currentActionIndex = value;
136    }
137
138    public override string ToString() {
[9352]139      return variableName + ": " + CurrentAction;
[9334]140    }
[9352]141
142    public bool SameNiche(IGAssistNiche niche) {
143      return Match(niche as IAction);
144    }
145
146    public int GetNicheHashCode() {
147      int result = 1;
148      result = 37 * result + currentActionIndex;
149      result = 37 * result + variableName.GetHashCode();
150      foreach (var feature in possibleFeatures) {
151        result = 37 * result + feature.GetHashCode();
152      }
153      return result;
154    }
[9334]155  }
156}
Note: See TracBrowser for help on using the repository browser.