[9194] | 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 HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
| 30 | [StorableClass]
|
---|
| 31 | [Item(Name = "StringVariable", Description = "")]
|
---|
| 32 | public class StringVariable : Variable<string>, IActionVariable {
|
---|
| 33 |
|
---|
| 34 | public override int VirtualLength {
|
---|
| 35 | get { return 1; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | [Storable]
|
---|
| 39 | public bool Wildcard { get; set; }
|
---|
| 40 |
|
---|
| 41 | [Storable]
|
---|
| 42 | protected IEnumerable<int> possibleFeatures;
|
---|
| 43 | public IEnumerable<int> PossibleFeatures {
|
---|
| 44 | get { return possibleFeatures; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | [Storable]
|
---|
| 48 | protected IDictionary<int, string> featureMapping;
|
---|
| 49 | public IDictionary<int, string> FeatureMapping {
|
---|
| 50 | get { return featureMapping; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | [Storable]
|
---|
| 54 | protected int currentValue;
|
---|
| 55 | public int CurrentValue {
|
---|
| 56 | get { return currentValue; }
|
---|
| 57 | set {
|
---|
| 58 | if (!possibleFeatures.Contains(value)) {
|
---|
| 59 | throw new ArgumentException("Value is not a possible feature of this variable.");
|
---|
| 60 | }
|
---|
| 61 | currentValue = value;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public string CurrentStringValue {
|
---|
| 66 | get { return featureMapping[currentValue]; }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | [StorableConstructor]
|
---|
| 70 | protected StringVariable(bool deserializing) : base(deserializing) { }
|
---|
| 71 | protected StringVariable(StringVariable original, Cloner cloner)
|
---|
| 72 | : base(original, cloner) {
|
---|
| 73 | this.possibleFeatures = new List<int>(original.possibleFeatures);
|
---|
| 74 | this.featureMapping = new Dictionary<int, string>(original.featureMapping);
|
---|
| 75 | this.Wildcard = original.Wildcard;
|
---|
[9226] | 76 | this.currentValue = original.currentValue;
|
---|
[9194] | 77 | }
|
---|
| 78 | public StringVariable()
|
---|
| 79 | : base() {
|
---|
| 80 | Wildcard = false;
|
---|
| 81 | }
|
---|
| 82 | public StringVariable(string variableName, IEnumerable<string> variableValues)
|
---|
| 83 | : base(variableName) {
|
---|
| 84 | if (variableValues.Count() != variableValues.Distinct().Count()) {
|
---|
| 85 | throw new ArgumentException("variableValues have to be distinct.");
|
---|
| 86 | }
|
---|
| 87 | featureMapping = new Dictionary<int, string>();
|
---|
| 88 | var distinctValuesEnumerator = variableValues.Distinct().GetEnumerator();
|
---|
[9475] | 89 | possibleFeatures = Enumerable.Range(0, variableValues.Count()).ToList();
|
---|
[9194] | 90 | var possibleFeaturesEnumerator = possibleFeatures.GetEnumerator();
|
---|
| 91 | while (possibleFeaturesEnumerator.MoveNext() && distinctValuesEnumerator.MoveNext()) {
|
---|
| 92 | featureMapping.Add(possibleFeaturesEnumerator.Current, distinctValuesEnumerator.Current);
|
---|
| 93 | }
|
---|
| 94 | Wildcard = false;
|
---|
| 95 | }
|
---|
| 96 | public StringVariable(string variableName, IEnumerable<string> variableValues, string currentValue)
|
---|
| 97 | : this(variableName, variableValues) {
|
---|
| 98 | int index = 0;
|
---|
| 99 | foreach (var feature in featureMapping) {
|
---|
| 100 | if (feature.Equals(currentValue)) {
|
---|
| 101 | break;
|
---|
| 102 | }
|
---|
| 103 | index++;
|
---|
| 104 | }
|
---|
| 105 | if (index > featureMapping.Count()) { throw new ArgumentException("variableValues do not contain currentValue."); }
|
---|
| 106 | CurrentValue = index;
|
---|
| 107 | }
|
---|
| 108 | public StringVariable(string variableName, IDictionary<int, string> featureMapping)
|
---|
| 109 | : base(variableName) {
|
---|
| 110 | if (featureMapping.Values.Count != featureMapping.Values.Distinct().Count()) {
|
---|
| 111 | throw new ArgumentException("featureMapping values have to be distinct.");
|
---|
| 112 | }
|
---|
[9475] | 113 | this.possibleFeatures = featureMapping.Keys.ToList();
|
---|
[9194] | 114 | this.featureMapping = featureMapping;
|
---|
| 115 | Wildcard = false;
|
---|
| 116 | }
|
---|
| 117 | public StringVariable(string variableName, IDictionary<int, string> featureMapping, string currentValue)
|
---|
| 118 | : this(variableName, featureMapping) {
|
---|
| 119 | bool found = false;
|
---|
| 120 | foreach (var pair in featureMapping) {
|
---|
| 121 | if (pair.Value.Equals(currentValue)) {
|
---|
| 122 | CurrentValue = pair.Key;
|
---|
| 123 | found = true;
|
---|
| 124 | break;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | if (!found) {
|
---|
| 128 | throw new ArgumentException("variableValues do not contain currentValue.");
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 133 | return new StringVariable(this, cloner);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | public override string ToString() {
|
---|
[9204] | 137 | return Wildcard ? "#" : CurrentStringValue;
|
---|
[9194] | 138 | }
|
---|
| 139 |
|
---|
| 140 | public override bool MatchInput(string target) {
|
---|
| 141 | return Wildcard || featureMapping[CurrentValue].Equals(target);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | public override bool Match(string target) {
|
---|
| 145 | return MatchInput(target);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | public override bool MatchVariable(IVariable target) {
|
---|
| 149 | var targetCast = target as StringVariable;
|
---|
| 150 | return targetCast != null && this.variableName.Equals(targetCast.VariableName) && Match(targetCast.CurrentStringValue);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[9226] | 153 | public override IVariable GetEmptyCopy() {
|
---|
[9194] | 154 | return new StringVariable(variableName, featureMapping);
|
---|
| 155 | }
|
---|
[9226] | 156 | public override IVariable GetSetCopy() {
|
---|
| 157 | StringVariable copy = (StringVariable)GetEmptyCopy();
|
---|
[9194] | 158 | copy.Wildcard = this.Wildcard;
|
---|
| 159 | copy.CurrentValue = this.CurrentValue;
|
---|
| 160 | return copy;
|
---|
| 161 | }
|
---|
[9226] | 162 | IActionVariable IActionVariable.GetEmptyCopy() {
|
---|
| 163 | return (StringVariable)GetEmptyCopy();
|
---|
| 164 | }
|
---|
| 165 | IActionVariable IActionVariable.GetSetCopy() {
|
---|
| 166 | return (StringVariable)GetSetCopy();
|
---|
| 167 | }
|
---|
[9194] | 168 |
|
---|
[9467] | 169 | public void RandomizeAction(IRandom random) {
|
---|
[9194] | 170 | int index = random.Next(possibleFeatures.Count());
|
---|
| 171 | currentValue = possibleFeatures.ElementAt(index);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[9467] | 174 | public override void Randomize(IRandom random, double changeSymbolProbability) {
|
---|
| 175 | Wildcard = random.NextDouble() < changeSymbolProbability;
|
---|
| 176 | int index = random.Next(possibleFeatures.Count());
|
---|
| 177 | currentValue = possibleFeatures.ElementAt(index);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[9194] | 180 | public override bool Identical(IVariable target) {
|
---|
| 181 | var targetCast = target as StringVariable;
|
---|
| 182 | if (targetCast == null) { return false; }
|
---|
| 183 | if (variableName != targetCast.variableName || Wildcard != targetCast.Wildcard
|
---|
| 184 | || currentValue != targetCast.currentValue) { return false; }
|
---|
| 185 |
|
---|
[9392] | 186 | var thisEnumerator = featureMapping.GetEnumerator();
|
---|
| 187 | var castEnumerator = targetCast.featureMapping.GetEnumerator();
|
---|
| 188 | while (thisEnumerator.MoveNext() && castEnumerator.MoveNext()) {
|
---|
| 189 | if (thisEnumerator.Current.Key != castEnumerator.Current.Key ||
|
---|
| 190 | thisEnumerator.Current.Value != castEnumerator.Current.Value)
|
---|
| 191 | return false;
|
---|
[9194] | 192 | }
|
---|
| 193 |
|
---|
[9392] | 194 | return !thisEnumerator.MoveNext() && !castEnumerator.MoveNext();
|
---|
[9194] | 195 | }
|
---|
| 196 |
|
---|
| 197 | public override double GetGenerality() {
|
---|
| 198 | return Wildcard ? 1 : 0;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | public override bool IsGreaterThanOrEquallyGeneral(IVariable target) {
|
---|
| 202 | var targetCast = target as StringVariable;
|
---|
| 203 | if (targetCast == null) { return false; }
|
---|
| 204 | return Wildcard || (currentValue == targetCast.currentValue && CurrentStringValue.Equals(targetCast.CurrentStringValue));
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[9242] | 207 | public override IVariable CrossParentsAtPosition(IVariable parent2, int pos) {
|
---|
[9194] | 208 | var parent2Cast = parent2 as StringVariable;
|
---|
| 209 | if (parent2Cast == null) { throw new ArgumentException("Argument is not of the correct type."); }
|
---|
| 210 | if (pos != 0) { throw new ArgumentOutOfRangeException(); }
|
---|
[9242] | 211 | return (StringVariable)parent2.GetSetCopy();
|
---|
[9194] | 212 | }
|
---|
[9204] | 213 |
|
---|
[9475] | 214 | public override void Manipulate(IRandom random, string stringValue, int pos,
|
---|
| 215 | double spreadPercentage) {
|
---|
| 216 | Manipulate(random, stringValue, pos);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[9204] | 219 | public override void Manipulate(IRandom random, string stringValue, int pos) {
|
---|
| 220 | if (pos != 0) { throw new ArgumentOutOfRangeException(); }
|
---|
| 221 | Wildcard = !Wildcard;
|
---|
| 222 | if (!Wildcard) {
|
---|
[9226] | 223 | currentValue = featureMapping.First(x => x.Value.Equals(stringValue)).Key;
|
---|
[9204] | 224 | }
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | public override void Cover(IRandom random, string stringValue, double changeSymbolProbability) {
|
---|
| 228 | Wildcard = random.NextDouble() < changeSymbolProbability;
|
---|
| 229 | if (!Wildcard) {
|
---|
[9226] | 230 | currentValue = featureMapping.First(x => x.Value.Equals(stringValue)).Key;
|
---|
[9204] | 231 | }
|
---|
| 232 | }
|
---|
[9226] | 233 |
|
---|
| 234 | public override int GetHashCode() {
|
---|
| 235 | int result = 1;
|
---|
| 236 | int wildcardInt = Wildcard ? 1 : 0;
|
---|
| 237 | result = 37 * result + wildcardInt;
|
---|
| 238 | result = 37 * result + currentValue;
|
---|
| 239 | foreach (var feature in featureMapping) {
|
---|
| 240 | result = 37 * result + feature.Key;
|
---|
| 241 | result = 37 * result + feature.Value.GetHashCode();
|
---|
| 242 | }
|
---|
| 243 | return result;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | #region IActionVariable Members
|
---|
| 247 | public void SetTo(string value) {
|
---|
| 248 | currentValue = featureMapping.First(x => x.Value.Equals(value)).Key;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | public IEnumerable<string> GetAllPossibleActions() {
|
---|
| 252 | return featureMapping.Values;
|
---|
| 253 | }
|
---|
| 254 | #endregion
|
---|
[9194] | 255 | }
|
---|
| 256 | }
|
---|