Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Action/IntAction.cs @ 10007

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