Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.CombinedIntegerVectorEncoding/3.3/CombinedIntegerVector.cs @ 9105

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

#1980:

  • included an adapted version of GA correctly
  • added action set subsumption
  • added deletion after GA and before covering
File size: 9.2 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.Linq;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.ConditionActionEncoding;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.CombinedIntegerVectorEncoding {
33  [StorableClass]
34  [Item("CombinedIntegerVector", "Represents a combined vector of integer values.")]
35  public class CombinedIntegerVector : IntegerVector, IClassifier {
36
37    [Storable]
38    protected int actionLength;
39    public int ActionLength { get { return actionLength; } }
40
41    /// <summary>
42    /// The bounds must contain at least one row with two columns. The first two columns specify min and max values.
43    /// The max value -1 in a row is the don't care symbol for LCS.
44    /// Each row represents the bounds for a certain dimension. If fewer rows are given, the rows are cycled
45    /// </summary>
46    [Storable]
47    protected IntMatrix bounds;
48    public IntMatrix Bounds { get { return (IntMatrix)bounds.Clone(); } }
49
50    public int ConditionLength { get { return Length - actionLength; } }
51
52    [StorableConstructor]
53    protected CombinedIntegerVector(bool deserializing) : base(deserializing) { }
54    public CombinedIntegerVector() : base() { }
55
56    public CombinedIntegerVector(int[] elements, int actionPart, IntMatrix bounds)
57      : base(elements) {
58      this.actionLength = actionPart;
59      //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
60      this.bounds = bounds;
61    }
62
63    public CombinedIntegerVector(IntegerVector combinedVector, int actionPart, IntMatrix bounds)
64      : this(combinedVector.ToArray(), actionPart, bounds) { }
65
66    public CombinedIntegerVector(IntegerVector condition, IntegerVector action, IntMatrix bounds)
67      : base(condition.Concat(action).ToArray()) {
68      actionLength = action.Length;
69      //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
70      this.bounds = bounds;
71    }
72
73    public CombinedIntegerVector(IntegerVector condition, IntMatrix conditionBounds, IntegerVector action, IntMatrix actionBounds)
74      : this(condition, action, CombineBounds(conditionBounds, actionBounds)) {
75    }
76
77    private static IntMatrix CombineBounds(IntMatrix conditionBounds, IntMatrix actionBounds) {
78      int columns = conditionBounds.Columns < actionBounds.Columns ? conditionBounds.Columns : actionBounds.Columns;
79      IntMatrix bounds = new IntMatrix(conditionBounds.Rows + actionBounds.Rows, columns);
80      for (int i = 0; i < conditionBounds.Rows; i++) {
81        for (int j = 0; j < columns; j++) {
82          bounds[i, j] = conditionBounds[i % conditionBounds.Rows, j];
83        }
84      }
85
86      for (int i = conditionBounds.Rows; i < actionBounds.Rows + conditionBounds.Rows; i++) {
87        for (int j = 0; j < columns; j++) {
88          bounds[i, j] = actionBounds[i % actionBounds.Rows, j];
89        }
90      }
91      return bounds;
92    }
93
94    public CombinedIntegerVector(IRandom random, int length, int min, int max, int actionLenght, int actionMin, int actionMax) :
95      this(new IntegerVector(length, random, min, max), new IntegerVector(actionLenght, random, actionMin, actionMax), null) {
96      bounds = new IntMatrix(length + actionLenght, 2);
97      for (int i = 0; i < length; i++) {
98        bounds[i, 0] = min;
99        bounds[i, 1] = max;
100      }
101      for (int i = length; i < length + actionLenght; i++) {
102        bounds[i, 0] = actionMin;
103        bounds[i, 1] = actionMax;
104      }
105    }
106
107    public CombinedIntegerVector(int length, int actionPart, IntMatrix bounds)
108      : base(length) {
109      this.actionLength = actionPart;
110      this.bounds = bounds;
111    }
112
113    protected CombinedIntegerVector(CombinedIntegerVector original, Cloner cloner)
114      : base(original, cloner) {
115      actionLength = original.actionLength;
116      bounds = (IntMatrix)original.bounds.Clone();
117    }
118
119    public override IDeepCloneable Clone(Cloner cloner) {
120      return new CombinedIntegerVector(this, cloner);
121    }
122
123    public IClassifier Condition {
124      get {
125        int[] condition = new int[Length - actionLength];
126        Array.Copy(this.array, condition, Length - actionLength);
127        return new CombinedIntegerVector(condition, 0, bounds);
128      }
129    }
130
131    public IClassifier Action {
132      get {
133        int[] action = new int[actionLength];
134        Array.Copy(this.array, Length - actionLength, action, 0, actionLength);
135
136        IntMatrix actionBounds = GetElementsOfBoundsForAction(bounds, Length, actionLength);
137        return new CombinedIntegerVector(action, actionLength, actionBounds);
138      }
139    }
140
141    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
142      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
143      int start = length - actionPartLength;
144      for (int i = start; i < length; i++) {
145        int pos = i % bounds.Rows;
146        for (int j = 0; j < bounds.Columns; j++) {
147          actionBounds[i - start, j] = bounds[pos, j];
148        }
149      }
150      return actionBounds;
151    }
152
153    public bool MatchCondition(IClassifier target) {
154      var targetVector = target as CombinedIntegerVector;
155      if (targetVector == null) return false;
156      if (targetVector.Length != this.Length) return false;
157
158      int curbounds;
159      for (int i = 0; i < this.Length - this.actionLength; i++) {
160        curbounds = i % bounds.Rows;
161        //if don't care symbol is matched, next indices can be checked
162        if (this[i].Equals(bounds[curbounds, 1] - 1)) {
163          continue;
164        }
165        if (!this[i].Equals(targetVector[i])) {
166          return false;
167        }
168      }
169      return true;
170    }
171
172    // no "don't care" symbols have to be considered
173    public bool MatchAction(IClassifier target) {
174      var targetVector = target as CombinedIntegerVector;
175      if (targetVector == null) return false;
176      if (targetVector.actionLength != this.actionLength) return false;
177
178      int curPos = this.Length - this.actionLength;
179      int curTargetPos = targetVector.Length - targetVector.actionLength;
180      for (int i = 0; i < this.actionLength; i++) {
181        if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
182          return false;
183        }
184      }
185      return true;
186    }
187
188    public bool IsMoreGeneral(IClassifier target) {
189      var targetVector = target as CombinedIntegerVector;
190      int curbounds;
191
192      if (this.NumberOfGeneralSymbols() <= targetVector.NumberOfGeneralSymbols()) return false;
193
194      for (int i = 0; i < ConditionLength; i++) {
195        curbounds = i % bounds.Rows;
196        if (this[i] != bounds[curbounds, 1] - 1 && this[i] != targetVector[i]) {
197          return false;
198        }
199      }
200      return true;
201    }
202
203    private int NumberOfGeneralSymbols() {
204      int numberOfGeneralSymbols = 0;
205      int curbounds;
206
207      for (int i = 0; i < ConditionLength; i++) {
208        curbounds = i % bounds.Rows;
209        numberOfGeneralSymbols += this[i] == bounds[curbounds, 1] - 1 ? 1 : 0;
210      }
211
212      return numberOfGeneralSymbols;
213    }
214
215    public override string ToString() {
216      StringBuilder strBuilder = new StringBuilder(this.Length);
217      strBuilder.Append('[');
218      int curbounds;
219      for (int i = 0; i < this.Length; i++) {
220        curbounds = i % bounds.Rows;
221        if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
222          strBuilder.Append(this[i]);
223        } else {
224          strBuilder.Append('#');
225        }
226        if (i < this.Length - 1) {
227          strBuilder.Append(';');
228        }
229      }
230      strBuilder.Append(']');
231      return strBuilder.ToString();
232    }
233
234    public override bool Equals(object obj) {
235      var cast = obj as CombinedIntegerVector;
236      if (cast != null) {
237        return this.Equals(cast);
238      }
239      return false;
240    }
241
242    public bool Equals(IClassifier other) {
243      return this.MatchAction(other) && this.MatchCondition(other);
244    }
245
246    public override int GetHashCode() {
247      int result = actionLength;
248      for (int i = 0; i < array.Length; i++) {
249        result ^= array[i];
250      }
251      return result.GetHashCode();
252    }
253  }
254}
Note: See TracBrowser for help on using the repository browser.