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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
30 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.CombinedIntegerVectorEncoding {
|
---|
34 | [StorableClass]
|
---|
35 | [Item("CombinedIntegerVector", "Represents a combined vector of integer values.")]
|
---|
36 | public class CombinedIntegerVector : IntegerVector, IClassifier, ICondition, IAction, IInput {
|
---|
37 |
|
---|
38 | [Storable]
|
---|
39 | protected int actionLength;
|
---|
40 | public int ActionLength { get { return actionLength; } }
|
---|
41 |
|
---|
42 | /// <summary>
|
---|
43 | /// The bounds must contain at least one row with two columns. The first two columns specify min and max values.
|
---|
44 | /// The max value -1 in a row is the don't care symbol for LCS.
|
---|
45 | /// Each row represents the bounds for a certain dimension. If fewer rows are given, the rows are cycled
|
---|
46 | /// </summary>
|
---|
47 | [Storable]
|
---|
48 | protected IntMatrix bounds;
|
---|
49 | public IntMatrix Bounds { get { return (IntMatrix)bounds.Clone(); } }
|
---|
50 |
|
---|
51 | public int ConditionLength { get { return Length - actionLength; } }
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | protected CombinedIntegerVector(bool deserializing) : base(deserializing) { }
|
---|
55 | public CombinedIntegerVector() : base() { }
|
---|
56 |
|
---|
57 | public CombinedIntegerVector(int[] elements, int actionPart, IntMatrix bounds)
|
---|
58 | : base(elements) {
|
---|
59 | this.actionLength = actionPart;
|
---|
60 | //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
|
---|
61 | this.bounds = bounds;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public CombinedIntegerVector(IntegerVector combinedVector, int actionPart, IntMatrix bounds)
|
---|
65 | : this(combinedVector.ToArray(), actionPart, bounds) { }
|
---|
66 |
|
---|
67 | public CombinedIntegerVector(IntegerVector condition, IntegerVector action, IntMatrix bounds)
|
---|
68 | : base(condition.Concat(action).ToArray()) {
|
---|
69 | actionLength = action.Length;
|
---|
70 | //check if combinedVector satisfies bounds and if bounds is set correctly (at leats one row with 2 columns) is missing!
|
---|
71 | this.bounds = bounds;
|
---|
72 | }
|
---|
73 |
|
---|
74 | public CombinedIntegerVector(IntegerVector condition, IntMatrix conditionBounds, IntegerVector action, IntMatrix actionBounds)
|
---|
75 | : this(condition, action, CombineBounds(conditionBounds, actionBounds)) {
|
---|
76 | }
|
---|
77 |
|
---|
78 | private static IntMatrix CombineBounds(IntMatrix conditionBounds, IntMatrix actionBounds) {
|
---|
79 | int columns = conditionBounds.Columns < actionBounds.Columns ? conditionBounds.Columns : actionBounds.Columns;
|
---|
80 | IntMatrix bounds = new IntMatrix(conditionBounds.Rows + actionBounds.Rows, columns);
|
---|
81 | for (int i = 0; i < conditionBounds.Rows; i++) {
|
---|
82 | for (int j = 0; j < columns; j++) {
|
---|
83 | bounds[i, j] = conditionBounds[i % conditionBounds.Rows, j];
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | for (int i = conditionBounds.Rows; i < actionBounds.Rows + conditionBounds.Rows; i++) {
|
---|
88 | for (int j = 0; j < columns; j++) {
|
---|
89 | bounds[i, j] = actionBounds[i % actionBounds.Rows, j];
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return bounds;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public CombinedIntegerVector(IRandom random, int length, int min, int max, int actionLenght, int actionMin, int actionMax) :
|
---|
96 | this(new IntegerVector(length, random, min, max), new IntegerVector(actionLenght, random, actionMin, actionMax), null) {
|
---|
97 | bounds = new IntMatrix(length + actionLenght, 2);
|
---|
98 | for (int i = 0; i < length; i++) {
|
---|
99 | bounds[i, 0] = min;
|
---|
100 | bounds[i, 1] = max;
|
---|
101 | }
|
---|
102 | for (int i = length; i < length + actionLenght; i++) {
|
---|
103 | bounds[i, 0] = actionMin;
|
---|
104 | bounds[i, 1] = actionMax;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public CombinedIntegerVector(int length, int actionPart, IntMatrix bounds)
|
---|
109 | : base(length) {
|
---|
110 | this.actionLength = actionPart;
|
---|
111 | this.bounds = bounds;
|
---|
112 | }
|
---|
113 |
|
---|
114 | protected CombinedIntegerVector(CombinedIntegerVector original, Cloner cloner)
|
---|
115 | : base(original, cloner) {
|
---|
116 | actionLength = original.actionLength;
|
---|
117 | bounds = (IntMatrix)original.bounds.Clone();
|
---|
118 | }
|
---|
119 |
|
---|
120 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
121 | return new CombinedIntegerVector(this, cloner);
|
---|
122 | }
|
---|
123 |
|
---|
124 | public ICondition Condition {
|
---|
125 | get {
|
---|
126 | int[] condition = new int[Length - actionLength];
|
---|
127 | Array.Copy(this.array, condition, Length - actionLength);
|
---|
128 | return new CombinedIntegerVector(condition, 0, bounds);
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public IAction Action {
|
---|
133 | get {
|
---|
134 | int[] action = new int[actionLength];
|
---|
135 | Array.Copy(this.array, Length - actionLength, action, 0, actionLength);
|
---|
136 |
|
---|
137 | IntMatrix actionBounds = GetElementsOfBoundsForAction(bounds, Length, actionLength);
|
---|
138 | return new CombinedIntegerVector(action, actionLength, actionBounds);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | private IntMatrix GetElementsOfBoundsForAction(IntMatrix bounds, int length, int actionPartLength) {
|
---|
143 | IntMatrix actionBounds = new IntMatrix(actionPartLength, bounds.Columns);
|
---|
144 | int start = length - actionPartLength;
|
---|
145 | int rows = bounds.Rows;
|
---|
146 | for (int i = start; i < length; i++) {
|
---|
147 | int pos = i % rows;
|
---|
148 | for (int j = 0; j < bounds.Columns; j++) {
|
---|
149 | actionBounds[i - start, j] = bounds[pos, j];
|
---|
150 | }
|
---|
151 | }
|
---|
152 | return actionBounds;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public bool MatchInput(IInput target) {
|
---|
156 | var targetVector = target as CombinedIntegerVector;
|
---|
157 | if (targetVector == null) return false;
|
---|
158 | if (targetVector.Length - targetVector.actionLength != this.Length - this.actionLength) return false;
|
---|
159 |
|
---|
160 | int curbounds;
|
---|
161 | for (int i = 0; i < this.Length - this.actionLength; i++) {
|
---|
162 | curbounds = i % bounds.Rows;
|
---|
163 | //if don't care symbol is matched, next indices can be checked
|
---|
164 | if (this[i] == bounds[curbounds, 1] - 1) {
|
---|
165 | continue;
|
---|
166 | }
|
---|
167 | if (this[i] != targetVector[i]) {
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | return true;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public bool MatchAction(IClassifier target) {
|
---|
175 | return MatchAction(target.Action);
|
---|
176 | }
|
---|
177 |
|
---|
178 | // no "don't care" symbols have to be considered
|
---|
179 | public bool MatchAction(IAction target) {
|
---|
180 | var targetVector = target as CombinedIntegerVector;
|
---|
181 | if (targetVector == null) return false;
|
---|
182 | if (targetVector.actionLength != this.actionLength) return false;
|
---|
183 |
|
---|
184 | int curPos = this.Length - this.actionLength;
|
---|
185 | int curTargetPos = targetVector.Length - targetVector.actionLength;
|
---|
186 | for (int i = 0; i < this.actionLength; i++) {
|
---|
187 | if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
|
---|
188 | return false;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 |
|
---|
194 | public bool IsMoreGeneral(IClassifier target) {
|
---|
195 | return IsMoreGeneral(target as ICondition);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public bool IsMoreGeneral(ICondition target) {
|
---|
199 | var targetVector = target as CombinedIntegerVector;
|
---|
200 | int curbounds;
|
---|
201 |
|
---|
202 | if (this.NumberOfGeneralSymbols() <= targetVector.NumberOfGeneralSymbols()) return false;
|
---|
203 |
|
---|
204 | for (int i = 0; i < ConditionLength; i++) {
|
---|
205 | curbounds = i % bounds.Rows;
|
---|
206 | if (this[i] != bounds[curbounds, 1] - 1 && this[i] != targetVector[i]) {
|
---|
207 | return false;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | return true;
|
---|
211 | }
|
---|
212 |
|
---|
213 | #region ICondition Members
|
---|
214 | public bool Match(IInput target) {
|
---|
215 | return MatchInput(target);
|
---|
216 | }
|
---|
217 | #endregion
|
---|
218 |
|
---|
219 | #region IAction Members
|
---|
220 | public bool Match(IAction target) {
|
---|
221 | return MatchAction(target);
|
---|
222 | }
|
---|
223 | #endregion
|
---|
224 |
|
---|
225 | private int NumberOfGeneralSymbols() {
|
---|
226 | int numberOfGeneralSymbols = 0;
|
---|
227 | int curbounds;
|
---|
228 |
|
---|
229 | for (int i = 0; i < ConditionLength; i++) {
|
---|
230 | curbounds = i % bounds.Rows;
|
---|
231 | numberOfGeneralSymbols += this[i] == bounds[curbounds, 1] - 1 ? 1 : 0;
|
---|
232 | }
|
---|
233 |
|
---|
234 | return numberOfGeneralSymbols;
|
---|
235 | }
|
---|
236 |
|
---|
237 | public override string ToString() {
|
---|
238 | StringBuilder strBuilder = new StringBuilder(this.Length);
|
---|
239 | strBuilder.Append('[');
|
---|
240 | int curbounds;
|
---|
241 | for (int i = 0; i < this.Length; i++) {
|
---|
242 | curbounds = i % bounds.Rows;
|
---|
243 | if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
|
---|
244 | strBuilder.Append(this[i]);
|
---|
245 | } else {
|
---|
246 | strBuilder.Append('#');
|
---|
247 | }
|
---|
248 | if (i < this.Length - 1) {
|
---|
249 | strBuilder.Append(';');
|
---|
250 | }
|
---|
251 | }
|
---|
252 | strBuilder.Append(']');
|
---|
253 | return strBuilder.ToString();
|
---|
254 | }
|
---|
255 |
|
---|
256 | public bool Identical(IClassifier target) {
|
---|
257 | var cast = target as CombinedIntegerVector;
|
---|
258 | if (cast == null) { return false; }
|
---|
259 | for (int i = 0; i < array.Length; i++) {
|
---|
260 | if (!array[i].Equals(cast.array[i])) {
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 |
|
---|
267 | public override bool Equals(object obj) {
|
---|
268 | return base.Equals(obj);
|
---|
269 | }
|
---|
270 |
|
---|
271 | public override int GetHashCode() {
|
---|
272 | return base.GetHashCode();
|
---|
273 | }
|
---|
274 |
|
---|
275 | [StorableClass]
|
---|
276 | [Item("CombinedIntegerVectorComparer", "")]
|
---|
277 | public class CombinedIntegerVectorComparer : Item, IEqualityComparer<CombinedIntegerVector>, IClassifierComparer {
|
---|
278 |
|
---|
279 | [StorableConstructor]
|
---|
280 | protected CombinedIntegerVectorComparer(bool deserializing) : base(deserializing) { }
|
---|
281 | protected CombinedIntegerVectorComparer(CombinedIntegerVectorComparer original, Cloner cloner)
|
---|
282 | : base(original, cloner) {
|
---|
283 | }
|
---|
284 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
285 | return new CombinedIntegerVectorComparer(this, cloner);
|
---|
286 | }
|
---|
287 | public CombinedIntegerVectorComparer() : base() { }
|
---|
288 |
|
---|
289 | public bool Equals(CombinedIntegerVector x, CombinedIntegerVector y) {
|
---|
290 | for (int i = 0; i < x.array.Length; i++) {
|
---|
291 | if (!x.array[i].Equals(y.array[i])) {
|
---|
292 | return false;
|
---|
293 | }
|
---|
294 | }
|
---|
295 | return true;
|
---|
296 | }
|
---|
297 |
|
---|
298 | public int GetHashCode(CombinedIntegerVector obj) {
|
---|
299 | int result = obj.actionLength;
|
---|
300 | for (int i = 0; i < obj.array.Length; i++) {
|
---|
301 | result ^= obj.array[i];
|
---|
302 | }
|
---|
303 | return result.GetHashCode();
|
---|
304 | }
|
---|
305 |
|
---|
306 | public bool Equals(IAction x, IAction y) {
|
---|
307 | var xCast = x as CombinedIntegerVector;
|
---|
308 | var yCast = y as CombinedIntegerVector;
|
---|
309 | return x != null && y != null && Equals(xCast, yCast);
|
---|
310 | }
|
---|
311 |
|
---|
312 | public int GetHashCode(IAction obj) {
|
---|
313 | var objCast = obj as CombinedIntegerVector;
|
---|
314 | return objCast != null ? GetHashCode(objCast) : obj.GetHashCode();
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 | }
|
---|