[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 |
|
---|
| 50 | [StorableConstructor]
|
---|
| 51 | protected CombinedIntegerVector(bool deserializing) : base(deserializing) { }
|
---|
| 52 | public CombinedIntegerVector() : base() { }
|
---|
| 53 |
|
---|
[9089] | 54 | public CombinedIntegerVector(int[] elements, int actionPart, IntMatrix bounds)
|
---|
[8941] | 55 | : base(elements) {
|
---|
[9089] | 56 | this.actionLength = actionPart;
|
---|
[8941] | 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 |
|
---|
[9089] | 61 | public CombinedIntegerVector(IntegerVector combinedVector, int actionPart, IntMatrix bounds)
|
---|
[8941] | 62 | : this(combinedVector.ToArray(), actionPart, bounds) { }
|
---|
| 63 |
|
---|
[9089] | 64 | public CombinedIntegerVector(IntegerVector condition, IntegerVector action, IntMatrix bounds)
|
---|
[8941] | 65 | : base(condition.Concat(action).ToArray()) {
|
---|
[9089] | 66 | actionLength = action.Length;
|
---|
[8941] | 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 |
|
---|
[9089] | 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 |
|
---|
[8941] | 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) {
|
---|
[9089] | 94 | bounds = new IntMatrix(length + actionLenght, 2);
|
---|
[8941] | 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 |
|
---|
[9089] | 105 | public CombinedIntegerVector(int length, int actionPart, IntMatrix bounds)
|
---|
[8941] | 106 | : base(length) {
|
---|
[9089] | 107 | this.actionLength = actionPart;
|
---|
[8941] | 108 | this.bounds = bounds;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | protected CombinedIntegerVector(CombinedIntegerVector original, Cloner cloner)
|
---|
| 112 | : base(original, cloner) {
|
---|
[9089] | 113 | actionLength = original.actionLength;
|
---|
| 114 | bounds = (IntMatrix)original.bounds.Clone();
|
---|
[8941] | 115 | }
|
---|
[9089] | 116 |
|
---|
[8941] | 117 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 118 | return new CombinedIntegerVector(this, cloner);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[9089] | 121 | public IClassifier Condition {
|
---|
[8941] | 122 | get {
|
---|
[9089] | 123 | int[] condition = new int[Length - actionLength];
|
---|
| 124 | Array.Copy(this.array, condition, Length - actionLength);
|
---|
[8941] | 125 | return new CombinedIntegerVector(condition, 0, bounds);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[9089] | 129 | public IClassifier Action {
|
---|
[8941] | 130 | get {
|
---|
[9089] | 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);
|
---|
[8941] | 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[9089] | 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) {
|
---|
[8941] | 152 | var targetVector = target as CombinedIntegerVector;
|
---|
| 153 | if (targetVector == null) return false;
|
---|
| 154 | if (targetVector.Length != this.Length) return false;
|
---|
| 155 |
|
---|
| 156 | int curbounds;
|
---|
[9089] | 157 | for (int i = 0; i < this.Length - this.actionLength; i++) {
|
---|
| 158 | curbounds = i % bounds.Rows;
|
---|
[8941] | 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
|
---|
[9089] | 171 | public bool MatchAction(IClassifier target) {
|
---|
[8941] | 172 | var targetVector = target as CombinedIntegerVector;
|
---|
| 173 | if (targetVector == null) return false;
|
---|
[9089] | 174 | if (targetVector.actionLength != this.actionLength) return false;
|
---|
[8941] | 175 |
|
---|
[9089] | 176 | int curPos = this.Length - this.actionLength;
|
---|
| 177 | int curTargetPos = targetVector.Length - targetVector.actionLength;
|
---|
| 178 | for (int i = 0; i < this.actionLength; i++) {
|
---|
[8941] | 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++) {
|
---|
[9089] | 191 | curbounds = i % bounds.Rows;
|
---|
| 192 | if (i >= this.Length - this.actionLength || this[i] < bounds[curbounds, 1] - 1) {
|
---|
[8941] | 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 |
|
---|
[9089] | 213 | public bool Equals(IClassifier other) {
|
---|
[8941] | 214 | return this.MatchAction(other) && this.MatchCondition(other);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | public override int GetHashCode() {
|
---|
[9089] | 218 | int result = actionLength;
|
---|
[8941] | 219 | for (int i = 0; i < array.Length; i++) {
|
---|
| 220 | result ^= array[i];
|
---|
| 221 | }
|
---|
| 222 | return result.GetHashCode();
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 | }
|
---|