Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10479 was 9226, checked in by sforsten, 12 years ago

#1980:

  • made classes in Problems.ConditionActionClassification abstract
  • added Problems.VariableVectorClassification and Problems.CombinedIntegerVectorClassification
  • LCS works now with arbitrary problems, which implement ConditionActionClassificationProblem
File size: 9.6 KB
RevLine 
[8941]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;
[9089]27using HeuristicLab.Data;
28using HeuristicLab.Encodings.ConditionActionEncoding;
[8941]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.")]
[9194]35  public class CombinedIntegerVector : IntegerVector, IClassifier, ICondition, IAction, IInput {
[8941]36
37    [Storable]
[9089]38    protected int actionLength;
39    public int ActionLength { get { return actionLength; } }
[8941]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]
[9089]47    protected IntMatrix bounds;
48    public IntMatrix Bounds { get { return (IntMatrix)bounds.Clone(); } }
[8941]49
[9105]50    public int ConditionLength { get { return Length - actionLength; } }
51
[8941]52    [StorableConstructor]
53    protected CombinedIntegerVector(bool deserializing) : base(deserializing) { }
54    public CombinedIntegerVector() : base() { }
55
[9089]56    public CombinedIntegerVector(int[] elements, int actionPart, IntMatrix bounds)
[8941]57      : base(elements) {
[9089]58      this.actionLength = actionPart;
[8941]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
[9089]63    public CombinedIntegerVector(IntegerVector combinedVector, int actionPart, IntMatrix bounds)
[8941]64      : this(combinedVector.ToArray(), actionPart, bounds) { }
65
[9089]66    public CombinedIntegerVector(IntegerVector condition, IntegerVector action, IntMatrix bounds)
[8941]67      : base(condition.Concat(action).ToArray()) {
[9089]68      actionLength = action.Length;
[8941]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
[9089]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
[8941]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) {
[9089]96      bounds = new IntMatrix(length + actionLenght, 2);
[8941]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
[9089]107    public CombinedIntegerVector(int length, int actionPart, IntMatrix bounds)
[8941]108      : base(length) {
[9089]109      this.actionLength = actionPart;
[8941]110      this.bounds = bounds;
111    }
112
113    protected CombinedIntegerVector(CombinedIntegerVector original, Cloner cloner)
114      : base(original, cloner) {
[9089]115      actionLength = original.actionLength;
116      bounds = (IntMatrix)original.bounds.Clone();
[8941]117    }
[9089]118
[8941]119    public override IDeepCloneable Clone(Cloner cloner) {
120      return new CombinedIntegerVector(this, cloner);
121    }
122
[9194]123    public ICondition Condition {
[8941]124      get {
[9089]125        int[] condition = new int[Length - actionLength];
126        Array.Copy(this.array, condition, Length - actionLength);
[8941]127        return new CombinedIntegerVector(condition, 0, bounds);
128      }
129    }
130
[9194]131    public IAction Action {
[8941]132      get {
[9089]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);
[8941]138      }
139    }
140
[9089]141    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
142      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
143      int start = length - actionPartLength;
[9175]144      int rows = bounds.Rows;
[9089]145      for (int i = start; i < length; i++) {
[9175]146        int pos = i % rows;
[9089]147        for (int j = 0; j < bounds.Columns; j++) {
148          actionBounds[i - start, j] = bounds[pos, j];
149        }
150      }
151      return actionBounds;
152    }
153
[9194]154    public bool MatchInput(IInput target) {
[8941]155      var targetVector = target as CombinedIntegerVector;
156      if (targetVector == null) return false;
[9194]157      if (targetVector.Length - targetVector.actionLength != this.Length - this.actionLength) return false;
[8941]158
159      int curbounds;
[9089]160      for (int i = 0; i < this.Length - this.actionLength; i++) {
161        curbounds = i % bounds.Rows;
[8941]162        //if don't care symbol is matched, next indices can be checked
[9175]163        if (this[i] == bounds[curbounds, 1] - 1) {
[8941]164          continue;
165        }
[9175]166        if (this[i] != targetVector[i]) {
[8941]167          return false;
168        }
169      }
170      return true;
171    }
172
[9194]173    public bool MatchAction(IClassifier target) {
174      return MatchAction(target.Action);
175    }
176
[8941]177    // no "don't care" symbols have to be considered
[9194]178    public bool MatchAction(IAction target) {
[8941]179      var targetVector = target as CombinedIntegerVector;
180      if (targetVector == null) return false;
[9089]181      if (targetVector.actionLength != this.actionLength) return false;
[8941]182
[9089]183      int curPos = this.Length - this.actionLength;
184      int curTargetPos = targetVector.Length - targetVector.actionLength;
185      for (int i = 0; i < this.actionLength; i++) {
[8941]186        if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
187          return false;
188        }
189      }
190      return true;
191    }
192
[9105]193    public bool IsMoreGeneral(IClassifier target) {
[9194]194      return IsMoreGeneral(target as ICondition);
195    }
196
197    public bool IsMoreGeneral(ICondition target) {
[9105]198      var targetVector = target as CombinedIntegerVector;
199      int curbounds;
200
201      if (this.NumberOfGeneralSymbols() <= targetVector.NumberOfGeneralSymbols()) return false;
202
203      for (int i = 0; i < ConditionLength; i++) {
204        curbounds = i % bounds.Rows;
205        if (this[i] != bounds[curbounds, 1] - 1 && this[i] != targetVector[i]) {
206          return false;
207        }
208      }
209      return true;
210    }
211
[9194]212    #region ICondition Members
213    public bool Match(IInput target) {
214      return MatchInput(target);
215    }
216    #endregion
217
218    #region IAction Members
219    public bool Match(IAction target) {
220      return MatchAction(target);
221    }
222    #endregion
223
[9105]224    private int NumberOfGeneralSymbols() {
225      int numberOfGeneralSymbols = 0;
226      int curbounds;
227
228      for (int i = 0; i < ConditionLength; i++) {
229        curbounds = i % bounds.Rows;
230        numberOfGeneralSymbols += this[i] == bounds[curbounds, 1] - 1 ? 1 : 0;
231      }
232
233      return numberOfGeneralSymbols;
234    }
235
[8941]236    public override string ToString() {
237      StringBuilder strBuilder = new StringBuilder(this.Length);
238      strBuilder.Append('[');
239      int curbounds;
240      for (int i = 0; i < this.Length; i++) {
[9089]241        curbounds = i % bounds.Rows;
242        if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
[8941]243          strBuilder.Append(this[i]);
244        } else {
245          strBuilder.Append('#');
246        }
247        if (i < this.Length - 1) {
248          strBuilder.Append(';');
249        }
250      }
251      strBuilder.Append(']');
252      return strBuilder.ToString();
253    }
254
[9194]255    public bool Identical(IClassifier target) {
256      var cast = target as CombinedIntegerVector;
[9175]257      if (cast == null) { return false; }
258      for (int i = 0; i < array.Length; i++) {
259        if (!array[i].Equals(cast.array[i])) {
260          return false;
261        }
262      }
263      return true;
[8941]264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.