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.Linq;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
29 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace 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 | 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 MatchCondition(IClassifier target) {
|
---|
155 | var targetVector = target as CombinedIntegerVector;
|
---|
156 | if (targetVector == null) return false;
|
---|
157 | if (targetVector.Length != this.Length) 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 | // no "don't care" symbols have to be considered
|
---|
174 | public bool MatchAction(IClassifier target) {
|
---|
175 | var targetVector = target as CombinedIntegerVector;
|
---|
176 | if (targetVector == null) return false;
|
---|
177 | if (targetVector.actionLength != this.actionLength) return false;
|
---|
178 |
|
---|
179 | int curPos = this.Length - this.actionLength;
|
---|
180 | int curTargetPos = targetVector.Length - targetVector.actionLength;
|
---|
181 | for (int i = 0; i < this.actionLength; i++) {
|
---|
182 | if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
|
---|
183 | return false;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 |
|
---|
189 | public bool IsMoreGeneral(IClassifier target) {
|
---|
190 | var targetVector = target as CombinedIntegerVector;
|
---|
191 | int curbounds;
|
---|
192 |
|
---|
193 | if (this.NumberOfGeneralSymbols() <= targetVector.NumberOfGeneralSymbols()) return false;
|
---|
194 |
|
---|
195 | for (int i = 0; i < ConditionLength; i++) {
|
---|
196 | curbounds = i % bounds.Rows;
|
---|
197 | if (this[i] != bounds[curbounds, 1] - 1 && this[i] != targetVector[i]) {
|
---|
198 | return false;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | return true;
|
---|
202 | }
|
---|
203 |
|
---|
204 | private int NumberOfGeneralSymbols() {
|
---|
205 | int numberOfGeneralSymbols = 0;
|
---|
206 | int curbounds;
|
---|
207 |
|
---|
208 | for (int i = 0; i < ConditionLength; i++) {
|
---|
209 | curbounds = i % bounds.Rows;
|
---|
210 | numberOfGeneralSymbols += this[i] == bounds[curbounds, 1] - 1 ? 1 : 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 | return numberOfGeneralSymbols;
|
---|
214 | }
|
---|
215 |
|
---|
216 | public override string ToString() {
|
---|
217 | StringBuilder strBuilder = new StringBuilder(this.Length);
|
---|
218 | strBuilder.Append('[');
|
---|
219 | int curbounds;
|
---|
220 | for (int i = 0; i < this.Length; i++) {
|
---|
221 | curbounds = i % bounds.Rows;
|
---|
222 | if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
|
---|
223 | strBuilder.Append(this[i]);
|
---|
224 | } else {
|
---|
225 | strBuilder.Append('#');
|
---|
226 | }
|
---|
227 | if (i < this.Length - 1) {
|
---|
228 | strBuilder.Append(';');
|
---|
229 | }
|
---|
230 | }
|
---|
231 | strBuilder.Append(']');
|
---|
232 | return strBuilder.ToString();
|
---|
233 | }
|
---|
234 |
|
---|
235 | public override bool Equals(object obj) {
|
---|
236 | var cast = obj as CombinedIntegerVector;
|
---|
237 | if (cast != null) {
|
---|
238 | return this.Equals(cast);
|
---|
239 | }
|
---|
240 | return false;
|
---|
241 | }
|
---|
242 |
|
---|
243 | public bool Equals(IClassifier other) {
|
---|
244 | var cast = other as CombinedIntegerVector;
|
---|
245 | if (cast == null) { return false; }
|
---|
246 | for (int i = 0; i < array.Length; i++) {
|
---|
247 | if (!array[i].Equals(cast.array[i])) {
|
---|
248 | return false;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | return true;
|
---|
252 | }
|
---|
253 |
|
---|
254 | public override int GetHashCode() {
|
---|
255 | int result = actionLength;
|
---|
256 | for (int i = 0; i < array.Length; i++) {
|
---|
257 | result ^= array[i];
|
---|
258 | }
|
---|
259 | return result.GetHashCode();
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|