[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[9089] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.ConditionActionEncoding;
|
---|
[8941] | 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.")]
|
---|
[9089] | 35 | public class CombinedIntegerVector : IntegerVector, IClassifier {
|
---|
[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 |
|
---|
[9089] | 123 | public IClassifier 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 |
|
---|
[9089] | 131 | public IClassifier 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;
|
---|
| 144 | for (int i = start; i < length; i++) {
|
---|
| 145 | int pos = i % bounds.Rows;
|
---|
| 146 | for (int j = 0; j < bounds.Columns; j++) {
|
---|
| 147 | actionBounds[i - start, j] = bounds[pos, j];
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | return actionBounds;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | public bool MatchCondition(IClassifier target) {
|
---|
[8941] | 154 | var targetVector = target as CombinedIntegerVector;
|
---|
| 155 | if (targetVector == null) return false;
|
---|
| 156 | if (targetVector.Length != this.Length) return false;
|
---|
| 157 |
|
---|
| 158 | int curbounds;
|
---|
[9089] | 159 | for (int i = 0; i < this.Length - this.actionLength; i++) {
|
---|
| 160 | curbounds = i % bounds.Rows;
|
---|
[8941] | 161 | //if don't care symbol is matched, next indices can be checked
|
---|
| 162 | if (this[i].Equals(bounds[curbounds, 1] - 1)) {
|
---|
| 163 | continue;
|
---|
| 164 | }
|
---|
| 165 | if (!this[i].Equals(targetVector[i])) {
|
---|
| 166 | return false;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | return true;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | // no "don't care" symbols have to be considered
|
---|
[9089] | 173 | public bool MatchAction(IClassifier target) {
|
---|
[8941] | 174 | var targetVector = target as CombinedIntegerVector;
|
---|
| 175 | if (targetVector == null) return false;
|
---|
[9089] | 176 | if (targetVector.actionLength != this.actionLength) return false;
|
---|
[8941] | 177 |
|
---|
[9089] | 178 | int curPos = this.Length - this.actionLength;
|
---|
| 179 | int curTargetPos = targetVector.Length - targetVector.actionLength;
|
---|
| 180 | for (int i = 0; i < this.actionLength; i++) {
|
---|
[8941] | 181 | if (!this[curPos + i].Equals(targetVector[curTargetPos + i])) {
|
---|
| 182 | return false;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | return true;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[9105] | 188 | public bool IsMoreGeneral(IClassifier target) {
|
---|
| 189 | var targetVector = target as CombinedIntegerVector;
|
---|
| 190 | int curbounds;
|
---|
| 191 |
|
---|
| 192 | if (this.NumberOfGeneralSymbols() <= targetVector.NumberOfGeneralSymbols()) return false;
|
---|
| 193 |
|
---|
| 194 | for (int i = 0; i < ConditionLength; i++) {
|
---|
| 195 | curbounds = i % bounds.Rows;
|
---|
| 196 | if (this[i] != bounds[curbounds, 1] - 1 && this[i] != targetVector[i]) {
|
---|
| 197 | return false;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | return true;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | private int NumberOfGeneralSymbols() {
|
---|
| 204 | int numberOfGeneralSymbols = 0;
|
---|
| 205 | int curbounds;
|
---|
| 206 |
|
---|
| 207 | for (int i = 0; i < ConditionLength; i++) {
|
---|
| 208 | curbounds = i % bounds.Rows;
|
---|
| 209 | numberOfGeneralSymbols += this[i] == bounds[curbounds, 1] - 1 ? 1 : 0;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | return numberOfGeneralSymbols;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[8941] | 215 | public override string ToString() {
|
---|
| 216 | StringBuilder strBuilder = new StringBuilder(this.Length);
|
---|
| 217 | strBuilder.Append('[');
|
---|
| 218 | int curbounds;
|
---|
| 219 | for (int i = 0; i < this.Length; i++) {
|
---|
[9089] | 220 | curbounds = i % bounds.Rows;
|
---|
| 221 | if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
|
---|
[8941] | 222 | strBuilder.Append(this[i]);
|
---|
| 223 | } else {
|
---|
| 224 | strBuilder.Append('#');
|
---|
| 225 | }
|
---|
| 226 | if (i < this.Length - 1) {
|
---|
| 227 | strBuilder.Append(';');
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 | strBuilder.Append(']');
|
---|
| 231 | return strBuilder.ToString();
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | public override bool Equals(object obj) {
|
---|
| 235 | var cast = obj as CombinedIntegerVector;
|
---|
| 236 | if (cast != null) {
|
---|
| 237 | return this.Equals(cast);
|
---|
| 238 | }
|
---|
| 239 | return false;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[9089] | 242 | public bool Equals(IClassifier other) {
|
---|
[8941] | 243 | return this.MatchAction(other) && this.MatchCondition(other);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | public override int GetHashCode() {
|
---|
[9089] | 247 | int result = actionLength;
|
---|
[8941] | 248 | for (int i = 0; i < array.Length; i++) {
|
---|
| 249 | result ^= array[i];
|
---|
| 250 | }
|
---|
| 251 | return result.GetHashCode();
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|