- Timestamp:
- 05/14/14 14:46:00 (11 years ago)
- Location:
- branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Datastructures
- Files:
-
- 7 added
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Datastructures/ParameterVector.cs
r10796 r10850 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 using HeuristicLab.Collections; 25 using HeuristicLab.Common; 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 23 using System.Linq; 28 24 using HeuristicLab.Encodings.BinaryVectorEncoding; 29 25 using HeuristicLab.Encodings.IntegerVectorEncoding; 30 26 using HeuristicLab.Encodings.PermutationEncoding; 31 27 using HeuristicLab.Encodings.RealVectorEncoding; 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;33 28 34 namespace HeuristicLab.Encodings.ParameterVectorEncoding { 35 [Item("ParameterVector", "Encodes a range of parameters of different types.")] 36 [StorableClass] 37 public class ParameterVector : Item { 38 private bool isFeasible; 39 [Storable] 40 public bool IsFeasible { 41 get { return isFeasible; } 42 set { 43 if (isFeasible == value) return; 44 isFeasible = value; 45 OnIsFeasibleChanged(); 46 } 29 namespace HeuristicLab.Problems.Programmable { 30 public class ParameterVector { 31 protected Dictionary<string, BinaryVector> BinaryParameters; 32 protected Dictionary<string, IntegerVector> IntegerParameters; 33 protected Dictionary<string, RealVector> RealParameters; 34 protected Dictionary<string, Permutation> PermutationParameters; 35 36 public ParameterVector(IEnumerable<KeyValuePair<string, BinaryVector>> binaryVectors = null, 37 IEnumerable<KeyValuePair<string, IntegerVector>> integerVectors = null, 38 IEnumerable<KeyValuePair<string, RealVector>> realVectors = null, 39 IEnumerable<KeyValuePair<string, Permutation>> permutations = null) { 40 if (binaryVectors != null) BinaryParameters = binaryVectors.ToDictionary(x => x.Key, x => x.Value); 41 if (integerVectors != null) IntegerParameters = integerVectors.ToDictionary(x => x.Key, x => x.Value); 42 if (realVectors != null) RealParameters = realVectors.ToDictionary(x => x.Key, x => x.Value); 43 if (permutations != null) PermutationParameters = permutations.ToDictionary(x => x.Key, x => x.Value); 47 44 } 48 45 49 [Storable] 50 public BinaryVector BooleanParameters { get; set; } 51 [Storable] 52 public IntegerVector IntegerParameters { get; set; } 53 [Storable] 54 public RealVector RealParameters { get; set; } 55 [Storable] 56 public Permutation PermutationParameter { get; set; } 57 58 [Storable] 59 protected BidirectionalDictionary<string, int> BoolMap; 60 [Storable] 61 protected BidirectionalDictionary<string, int> IntMap; 62 [Storable] 63 protected BidirectionalDictionary<string, int> RealMap; 64 65 [Storable] 66 public IntMatrix IntegerBounds { get; set; } 67 [Storable] 68 public DoubleMatrix RealBounds { get; set; } 69 70 [StorableConstructor] 71 protected ParameterVector(bool deserializing) : base(deserializing) { } 72 protected ParameterVector(ParameterVector original, Cloner cloner) 73 : base(original, cloner) { 74 IsFeasible = original.IsFeasible; 75 if (original.BooleanParameters != null) { 76 BooleanParameters = cloner.Clone(original.BooleanParameters); 77 BoolMap = new BidirectionalDictionary<string, int>(original.BoolMap); 78 } 79 if (original.IntegerParameters != null) { 80 IntegerParameters = cloner.Clone(original.IntegerParameters); 81 IntMap = new BidirectionalDictionary<string, int>(original.IntMap); 82 IntegerBounds = cloner.Clone(original.IntegerBounds); 83 } 84 if (original.RealParameters != null) { 85 RealParameters = cloner.Clone(original.RealParameters); 86 RealMap = new BidirectionalDictionary<string, int>(original.RealMap); 87 RealBounds = cloner.Clone(original.RealBounds); 88 } 89 PermutationParameter = cloner.Clone(original.PermutationParameter); 90 } 91 public ParameterVector() { 92 46 public BinaryVector Binary(string name) { 47 return BinaryParameters[name]; 93 48 } 94 49 95 public override IDeepCloneable Clone(Cloner cloner) {96 return new ParameterVector(this, cloner);50 public bool Binary(string name, int index) { 51 return BinaryParameters[name][index]; 97 52 } 98 53 99 public bool Boolean(string name){100 return BooleanParameters[BoolMap.GetByFirst(name)];54 public IEnumerable<string> BinaryNames { 55 get { return BinaryParameters.Keys; } 101 56 } 102 57 103 public bool Boolean(int index) {104 return BooleanParameters[index];58 public IntegerVector Integer(string name) { 59 return IntegerParameters[name]; 105 60 } 106 61 107 public IEnumerable<string> BooleanParameterNames { 108 get { 109 for (int i = 0; i < BooleanParameters.Length; i++) 110 yield return BoolMap.GetBySecond(i); 111 } 62 public int Integer(string name, int index) { 63 return IntegerParameters[name][index]; 112 64 } 113 65 114 public int Integer(string name){115 return IntegerParameters[IntMap.GetByFirst(name)];66 public IEnumerable<string> IntegerNames { 67 get { return IntegerParameters.Keys; } 116 68 } 117 69 118 public int Integer(int index) {119 return IntegerParameters[index];70 public RealVector Real(string name) { 71 return RealParameters[name]; 120 72 } 121 73 122 public IEnumerable<string> IntegerParameterNames { 123 get { 124 for (int i = 0; i < IntegerParameters.Length; i++) 125 yield return IntMap.GetBySecond(i); 126 } 74 public double Real(string name, int index) { 75 return RealParameters[name][index]; 127 76 } 128 77 129 public double Real(string name){130 return RealParameters[RealMap.GetByFirst(name)];78 public IEnumerable<string> RealNames { 79 get { return RealParameters.Keys; } 131 80 } 132 81 133 public double Real(int index) {134 return RealParameters[index];82 public Permutation Permutation(string name) { 83 return PermutationParameters[name]; 135 84 } 136 85 137 public IEnumerable<string> RealParameterNames { 138 get { 139 for (int i = 0; i < RealParameters.Length; i++) 140 yield return RealMap.GetBySecond(i); 141 } 86 public IEnumerable<string> PermutationNames { 87 get { return PermutationParameters.Keys; } 142 88 } 143 144 public Permutation Permutation() {145 return PermutationParameter;146 }147 148 public event EventHandler IsFeasibleChanged;149 protected virtual void OnIsFeasibleChanged() {150 var handler = IsFeasibleChanged;151 if (handler != null) handler(this, EventArgs.Empty);152 }153 154 #region Internal Methods for the Builder155 internal void SetBooleanParameters(HashSet<string> bools = null) {156 if (bools == null) {157 BooleanParameters = null;158 BoolMap = null;159 } else {160 BooleanParameters = new BinaryVector(bools.Count);161 BoolMap = new BidirectionalDictionary<string, int>();162 foreach (var p in bools) {163 BoolMap.Add(p, BoolMap.Count);164 }165 }166 }167 168 internal void SetIntegerParameters(Dictionary<string, Tuple<int, int, int?>> ints = null) {169 if (ints == null) {170 IntegerParameters = null;171 IntMap = null;172 IntegerBounds = null;173 } else {174 IntegerParameters = new IntegerVector(ints.Count);175 IntMap = new BidirectionalDictionary<string, int>();176 IntegerBounds = new IntMatrix(ints.Count, 3);177 var i = 0;178 foreach (var p in ints) {179 IntegerBounds[i, 0] = p.Value.Item1;180 IntegerBounds[i, 1] = p.Value.Item2;181 IntegerBounds[i, 2] = p.Value.Item3 ?? 1;182 IntegerParameters[i] = p.Value.Item1; // default to min183 IntMap.Add(p.Key, i++);184 }185 }186 }187 188 internal void SetRealParameters(Dictionary<string, Tuple<double, double>> reals = null) {189 if (reals == null) {190 RealParameters = null;191 RealMap = null;192 RealBounds = null;193 } else {194 RealParameters = new RealVector(reals.Count);195 RealMap = new BidirectionalDictionary<string, int>();196 RealBounds = new DoubleMatrix(reals.Count, 2);197 var i = 0;198 foreach (var p in reals) {199 RealBounds[i, 0] = p.Value.Item1;200 RealBounds[i, 1] = p.Value.Item2;201 RealParameters[i] = p.Value.Item1; // default to min202 RealMap.Add(p.Key, i++);203 }204 }205 }206 207 internal void SetPermutationParameters(Tuple<PermutationTypes, int> perms = null) {208 if (perms == null) PermutationParameter = null;209 else PermutationParameter = new Permutation(perms.Item1, perms.Item2);210 }211 #endregion212 89 } 213 90 }
Note: See TracChangeset
for help on using the changeset viewer.