Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added ConditionActionClassificationProblem
  • added ConditionActionEncoding
  • added Manipulators, Crossovers and an LCSAdaptedGeneticAlgorithm
  • changed LearningClassifierSystemMainLoop
File size: 8.4 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    [StorableConstructor]
51    protected CombinedIntegerVector(bool deserializing) : base(deserializing) { }
52    public CombinedIntegerVector() : base() { }
53
54    public CombinedIntegerVector(int[] elements, int actionPart, IntMatrix bounds)
55      : base(elements) {
56      this.actionLength = actionPart;
57      //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
58      this.bounds = bounds;
59    }
60
61    public CombinedIntegerVector(IntegerVector combinedVector, int actionPart, IntMatrix bounds)
62      : this(combinedVector.ToArray(), actionPart, bounds) { }
63
64    public CombinedIntegerVector(IntegerVector condition, IntegerVector action, IntMatrix bounds)
65      : base(condition.Concat(action).ToArray()) {
66      actionLength = action.Length;
67      //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
68      this.bounds = bounds;
69    }
70
71    public CombinedIntegerVector(IntegerVector condition, IntMatrix conditionBounds, IntegerVector action, IntMatrix actionBounds)
72      : this(condition, action, CombineBounds(conditionBounds, actionBounds)) {
73    }
74
75    private static IntMatrix CombineBounds(IntMatrix conditionBounds, IntMatrix actionBounds) {
76      int columns = conditionBounds.Columns < actionBounds.Columns ? conditionBounds.Columns : actionBounds.Columns;
77      IntMatrix bounds = new IntMatrix(conditionBounds.Rows + actionBounds.Rows, columns);
78      for (int i = 0; i < conditionBounds.Rows; i++) {
79        for (int j = 0; j < columns; j++) {
80          bounds[i, j] = conditionBounds[i % conditionBounds.Rows, j];
81        }
82      }
83
84      for (int i = conditionBounds.Rows; i < actionBounds.Rows + conditionBounds.Rows; i++) {
85        for (int j = 0; j < columns; j++) {
86          bounds[i, j] = actionBounds[i % actionBounds.Rows, j];
87        }
88      }
89      return bounds;
90    }
91
92    public CombinedIntegerVector(IRandom random, int length, int min, int max, int actionLenght, int actionMin, int actionMax) :
93      this(new IntegerVector(length, random, min, max), new IntegerVector(actionLenght, random, actionMin, actionMax), null) {
94      bounds = new IntMatrix(length + actionLenght, 2);
95      for (int i = 0; i < length; i++) {
96        bounds[i, 0] = min;
97        bounds[i, 1] = max;
98      }
99      for (int i = length; i < length + actionLenght; i++) {
100        bounds[i, 0] = actionMin;
101        bounds[i, 1] = actionMax;
102      }
103    }
104
105    public CombinedIntegerVector(int length, int actionPart, IntMatrix bounds)
106      : base(length) {
107      this.actionLength = actionPart;
108      this.bounds = bounds;
109    }
110
111    protected CombinedIntegerVector(CombinedIntegerVector original, Cloner cloner)
112      : base(original, cloner) {
113      actionLength = original.actionLength;
114      bounds = (IntMatrix)original.bounds.Clone();
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new CombinedIntegerVector(this, cloner);
119    }
120
121    public IClassifier Condition {
122      get {
123        int[] condition = new int[Length - actionLength];
124        Array.Copy(this.array, condition, Length - actionLength);
125        return new CombinedIntegerVector(condition, 0, bounds);
126      }
127    }
128
129    public IClassifier Action {
130      get {
131        int[] action = new int[actionLength];
132        Array.Copy(this.array, Length - actionLength, action, 0, actionLength);
133
134        IntMatrix actionBounds = GetElementsOfBoundsForAction(bounds, Length, actionLength);
135        return new CombinedIntegerVector(action, actionLength, actionBounds);
136      }
137    }
138
139    private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
140      IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
141      int start = length - actionPartLength;
142      for (int i = start; i < length; i++) {
143        int pos = i % bounds.Rows;
144        for (int j = 0; j < bounds.Columns; j++) {
145          actionBounds[i - start, j] = bounds[pos, j];
146        }
147      }
148      return actionBounds;
149    }
150
151    public bool MatchCondition(IClassifier target) {
152      var targetVector = target as CombinedIntegerVector;
153      if (targetVector == null) return false;
154      if (targetVector.Length != this.Length) return false;
155
156      int curbounds;
157      for (int i = 0; i < this.Length - this.actionLength; i++) {
158        curbounds = i % bounds.Rows;
159        //if don't care symbol is matched, next indices can be checked
160        if (this[i].Equals(bounds[curbounds, 1] - 1)) {
161          continue;
162        }
163        if (!this[i].Equals(targetVector[i])) {
164          return false;
165        }
166      }
167      return true;
168    }
169
170    // no "don't care" symbols have to be considered
171    public bool MatchAction(IClassifier target) {
172      var targetVector = target as CombinedIntegerVector;
173      if (targetVector == null) return false;
174      if (targetVector.actionLength != this.actionLength) return false;
175
176      int curPos = this.Length - this.actionLength;
177      int curTargetPos = targetVector.Length - targetVector.actionLength;
178      for (int i = 0; i < this.actionLength; i++) {
179        if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
180          return false;
181        }
182      }
183      return true;
184    }
185
186    public override string ToString() {
187      StringBuilder strBuilder = new StringBuilder(this.Length);
188      strBuilder.Append('[');
189      int curbounds;
190      for (int i = 0; i < this.Length; i++) {
191        curbounds = i % bounds.Rows;
192        if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
193          strBuilder.Append(this[i]);
194        } else {
195          strBuilder.Append('#');
196        }
197        if (i < this.Length - 1) {
198          strBuilder.Append(';');
199        }
200      }
201      strBuilder.Append(']');
202      return strBuilder.ToString();
203    }
204
205    public override bool Equals(object obj) {
206      var cast = obj as CombinedIntegerVector;
207      if (cast != null) {
208        return this.Equals(cast);
209      }
210      return false;
211    }
212
213    public bool Equals(IClassifier other) {
214      return this.MatchAction(other) && this.MatchCondition(other);
215    }
216
217    public override int GetHashCode() {
218      int result = actionLength;
219      for (int i = 0; i < array.Length; i++) {
220        result ^= array[i];
221      }
222      return result.GetHashCode();
223    }
224  }
225}
Note: See TracBrowser for help on using the repository browser.