Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16954 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
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, ICondition, IAction, IInput {
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 ICondition 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 IAction 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      int rows = bounds.Rows;
145      for (int i = start; i < length; i++) {
146        int pos = i % rows;
147        for (int j = 0; j < bounds.Columns; j++) {
148          actionBounds[i - start, j] = bounds[pos, j];
149        }
150      }
151      return actionBounds;
152    }
153
154    public bool MatchInput(IInput target) {
155      var targetVector = target as CombinedIntegerVector;
156      if (targetVector == null) return false;
157      if (targetVector.Length - targetVector.actionLength != this.Length - this.actionLength) return false;
158
159      int curbounds;
160      for (int i = 0; i < this.Length - this.actionLength; i++) {
161        curbounds = i % bounds.Rows;
162        //if don't care symbol is matched, next indices can be checked
163        if (this[i] == bounds[curbounds, 1] - 1) {
164          continue;
165        }
166        if (this[i] != targetVector[i]) {
167          return false;
168        }
169      }
170      return true;
171    }
172
173    public bool MatchAction(IClassifier target) {
174      return MatchAction(target.Action);
175    }
176
177    // no "don't care" symbols have to be considered
178    public bool MatchAction(IAction target) {
179      var targetVector = target as CombinedIntegerVector;
180      if (targetVector == null) return false;
181      if (targetVector.actionLength != this.actionLength) return false;
182
183      int curPos = this.Length - this.actionLength;
184      int curTargetPos = targetVector.Length - targetVector.actionLength;
185      for (int i = 0; i < this.actionLength; i++) {
186        if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
187          return false;
188        }
189      }
190      return true;
191    }
192
193    public bool IsMoreGeneral(IClassifier target) {
194      return IsMoreGeneral(target as ICondition);
195    }
196
197    public bool IsMoreGeneral(ICondition target) {
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
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
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
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++) {
241        curbounds = i % bounds.Rows;
242        if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
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
255    public bool Identical(IClassifier target) {
256      var cast = target as CombinedIntegerVector;
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;
264    }
265  }
266}
Note: See TracBrowser for help on using the repository browser.