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;
|
---|
76 | }
|
---|
77 | public StringVariable()
|
---|
78 | : base() {
|
---|
79 | Wildcard = false;
|
---|
80 | }
|
---|
81 | public StringVariable(string variableName, IEnumerable<string> variableValues)
|
---|
82 | : base(variableName) {
|
---|
83 | if (variableValues.Count() != variableValues.Distinct().Count()) {
|
---|
84 | throw new ArgumentException("variableValues have to be distinct.");
|
---|
85 | }
|
---|
86 | featureMapping = new Dictionary<int, string>();
|
---|
87 | var distinctValuesEnumerator = variableValues.Distinct().GetEnumerator();
|
---|
88 | possibleFeatures = Enumerable.Range(0, featureMapping.Count());
|
---|
89 | var possibleFeaturesEnumerator = possibleFeatures.GetEnumerator();
|
---|
90 | while (possibleFeaturesEnumerator.MoveNext() && distinctValuesEnumerator.MoveNext()) {
|
---|
91 | featureMapping.Add(possibleFeaturesEnumerator.Current, distinctValuesEnumerator.Current);
|
---|
92 | }
|
---|
93 | Wildcard = false;
|
---|
94 | }
|
---|
95 | public StringVariable(string variableName, IEnumerable<string> variableValues, string currentValue)
|
---|
96 | : this(variableName, variableValues) {
|
---|
97 | int index = 0;
|
---|
98 | foreach (var feature in featureMapping) {
|
---|
99 | if (feature.Equals(currentValue)) {
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | index++;
|
---|
103 | }
|
---|
104 | if (index > featureMapping.Count()) { throw new ArgumentException("variableValues do not contain currentValue."); }
|
---|
105 | CurrentValue = index;
|
---|
106 | }
|
---|
107 | public StringVariable(string variableName, IDictionary<int, string> featureMapping)
|
---|
108 | : base(variableName) {
|
---|
109 | if (featureMapping.Values.Count != featureMapping.Values.Distinct().Count()) {
|
---|
110 | throw new ArgumentException("featureMapping values have to be distinct.");
|
---|
111 | }
|
---|
112 | this.possibleFeatures = featureMapping.Keys;
|
---|
113 | this.featureMapping = featureMapping;
|
---|
114 | Wildcard = false;
|
---|
115 | }
|
---|
116 | public StringVariable(string variableName, IDictionary<int, string> featureMapping, string currentValue)
|
---|
117 | : this(variableName, featureMapping) {
|
---|
118 | bool found = false;
|
---|
119 | foreach (var pair in featureMapping) {
|
---|
120 | if (pair.Value.Equals(currentValue)) {
|
---|
121 | CurrentValue = pair.Key;
|
---|
122 | found = true;
|
---|
123 | break;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | if (!found) {
|
---|
127 | throw new ArgumentException("variableValues do not contain currentValue.");
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
132 | return new StringVariable(this, cloner);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public override string ToString() {
|
---|
136 | return CurrentStringValue;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public override bool MatchInput(string target) {
|
---|
140 | return Wildcard || featureMapping[CurrentValue].Equals(target);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public override bool Match(string target) {
|
---|
144 | return MatchInput(target);
|
---|
145 | }
|
---|
146 |
|
---|
147 | public override bool MatchVariable(IVariable target) {
|
---|
148 | var targetCast = target as StringVariable;
|
---|
149 | return targetCast != null && this.variableName.Equals(targetCast.VariableName) && Match(targetCast.CurrentStringValue);
|
---|
150 | }
|
---|
151 |
|
---|
152 | public new StringVariable GetEmptyCopy() {
|
---|
153 | return new StringVariable(variableName, featureMapping);
|
---|
154 | }
|
---|
155 | public new StringVariable GetSetCopy() {
|
---|
156 | StringVariable copy = GetEmptyCopy();
|
---|
157 | copy.Wildcard = this.Wildcard;
|
---|
158 | copy.CurrentValue = this.CurrentValue;
|
---|
159 | return copy;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public override void Randomize(IRandom random) {
|
---|
163 | int index = random.Next(possibleFeatures.Count());
|
---|
164 | currentValue = possibleFeatures.ElementAt(index);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public override bool Identical(IVariable target) {
|
---|
168 | var targetCast = target as StringVariable;
|
---|
169 | if (targetCast == null) { return false; }
|
---|
170 | if (variableName != targetCast.variableName || Wildcard != targetCast.Wildcard
|
---|
171 | || currentValue != targetCast.currentValue) { return false; }
|
---|
172 |
|
---|
173 | if (featureMapping.Keys.Except(targetCast.featureMapping.Keys).Count() != 0
|
---|
174 | || targetCast.featureMapping.Keys.Except(featureMapping.Keys).Count() != 0) { return false; }
|
---|
175 |
|
---|
176 | foreach (var keyValuePair in targetCast.featureMapping) {
|
---|
177 | if (!featureMapping[keyValuePair.Key].Equals(keyValuePair.Value)) { return false; }
|
---|
178 | }
|
---|
179 |
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | public override double GetGenerality() {
|
---|
184 | return Wildcard ? 1 : 0;
|
---|
185 | }
|
---|
186 |
|
---|
187 | public override bool IsGreaterThanOrEquallyGeneral(IVariable target) {
|
---|
188 | var targetCast = target as StringVariable;
|
---|
189 | if (targetCast == null) { return false; }
|
---|
190 | return Wildcard || (currentValue == targetCast.currentValue && CurrentStringValue.Equals(targetCast.CurrentStringValue));
|
---|
191 | }
|
---|
192 |
|
---|
193 | public new StringVariable CrossParentsAtPosition(IVariable parent2, int pos) {
|
---|
194 | var parent2Cast = parent2 as StringVariable;
|
---|
195 | if (parent2Cast == null) { throw new ArgumentException("Argument is not of the correct type."); }
|
---|
196 | if (pos != 0) { throw new ArgumentOutOfRangeException(); }
|
---|
197 | return this.GetSetCopy();
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|