1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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 HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
29 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
31 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
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 | }
|
---|
47 | }
|
---|
48 |
|
---|
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 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
96 | return new ParameterVector(this, cloner);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public bool Boolean(string name) {
|
---|
100 | return BooleanParameters[BoolMap.GetByFirst(name)];
|
---|
101 | }
|
---|
102 |
|
---|
103 | public bool Boolean(int index) {
|
---|
104 | return BooleanParameters[index];
|
---|
105 | }
|
---|
106 |
|
---|
107 | public IEnumerable<string> BooleanParameterNames {
|
---|
108 | get {
|
---|
109 | for (int i = 0; i < BooleanParameters.Length; i++)
|
---|
110 | yield return BoolMap.GetBySecond(i);
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public int Integer(string name) {
|
---|
115 | return IntegerParameters[IntMap.GetByFirst(name)];
|
---|
116 | }
|
---|
117 |
|
---|
118 | public int Integer(int index) {
|
---|
119 | return IntegerParameters[index];
|
---|
120 | }
|
---|
121 |
|
---|
122 | public IEnumerable<string> IntegerParameterNames {
|
---|
123 | get {
|
---|
124 | for (int i = 0; i < IntegerParameters.Length; i++)
|
---|
125 | yield return IntMap.GetBySecond(i);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public double Real(string name) {
|
---|
130 | return RealParameters[RealMap.GetByFirst(name)];
|
---|
131 | }
|
---|
132 |
|
---|
133 | public double Real(int index) {
|
---|
134 | return RealParameters[index];
|
---|
135 | }
|
---|
136 |
|
---|
137 | public IEnumerable<string> RealParameterNames {
|
---|
138 | get {
|
---|
139 | for (int i = 0; i < RealParameters.Length; i++)
|
---|
140 | yield return RealMap.GetBySecond(i);
|
---|
141 | }
|
---|
142 | }
|
---|
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 Builder
|
---|
155 | 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 min
|
---|
183 | 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 min
|
---|
202 | 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 | #endregion
|
---|
212 | }
|
---|
213 | }
|
---|