1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.PluginInfrastructure;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using System.Collections;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
13 | [StorableClass]
|
---|
14 | public class ParameterConfiguration : Item, IParameterConfiguration, IStorableContent {
|
---|
15 | public bool IsOptimizable {
|
---|
16 | get { return true; }
|
---|
17 | }
|
---|
18 |
|
---|
19 | [Storable]
|
---|
20 | public string Filename { get; set; }
|
---|
21 |
|
---|
22 | [Storable]
|
---|
23 | protected bool optimize;
|
---|
24 | public bool Optimize {
|
---|
25 | get { return optimize; }
|
---|
26 | set {
|
---|
27 | if (optimize != value) {
|
---|
28 | optimize = value;
|
---|
29 | if (optimize) {
|
---|
30 | PopulateValueConfigurations();
|
---|
31 | } else {
|
---|
32 | this.ValueConfigurations.Clear();
|
---|
33 | }
|
---|
34 | OnOptimizeChanged();
|
---|
35 | OnToStringChanged();
|
---|
36 | }
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | protected string parameterName;
|
---|
42 | public string ParameterName {
|
---|
43 | get { return parameterName; }
|
---|
44 | set {
|
---|
45 | if (parameterName != value) {
|
---|
46 | parameterName = value;
|
---|
47 | OnToStringChanged();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [Storable]
|
---|
53 | protected Type parameterDataType;
|
---|
54 | public Type ParameterDataType {
|
---|
55 | get { return this.parameterDataType; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [Storable]
|
---|
59 | protected IItemSet<IItem> validValues;
|
---|
60 | public IItemSet<IItem> ValidValues {
|
---|
61 | get { return validValues; }
|
---|
62 | protected set {
|
---|
63 | if (this.validValues != value) {
|
---|
64 | this.validValues = value;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [Storable]
|
---|
70 | protected Type valueDataType;
|
---|
71 | public Type ValueDataType {
|
---|
72 | get { return valueDataType; }
|
---|
73 | protected set { this.valueDataType = value; }
|
---|
74 | }
|
---|
75 |
|
---|
76 | [Storable]
|
---|
77 | protected ICheckedValueConfigurationCollection valueConfigurations;
|
---|
78 | public ICheckedValueConfigurationCollection ValueConfigurations {
|
---|
79 | get { return this.valueConfigurations; }
|
---|
80 | protected set {
|
---|
81 | if (this.valueConfigurations != value) {
|
---|
82 | if (this.valueConfigurations != null) DeregisterValueConfigurationEvents();
|
---|
83 | this.valueConfigurations = value;
|
---|
84 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | [Storable]
|
---|
90 | protected int actualValueConfigurationIndex = 0;
|
---|
91 |
|
---|
92 | [Storable]
|
---|
93 | protected ConstrainedValue actualValue;
|
---|
94 | public ConstrainedValue ActualValue {
|
---|
95 | get { return actualValue; }
|
---|
96 | set { actualValue = value; }
|
---|
97 | }
|
---|
98 |
|
---|
99 | // store parameter reference only for name/description/image/...
|
---|
100 | //[Storable]
|
---|
101 | //private IValueParameter parameter;
|
---|
102 |
|
---|
103 | #region Constructors and Cloning
|
---|
104 | public ParameterConfiguration(string parameterName, IValueParameter valueParameter) {
|
---|
105 | this.ParameterName = parameterName;
|
---|
106 | this.parameterDataType = valueParameter.GetType();
|
---|
107 | //this.parameter = valueParameter;
|
---|
108 | this.valueDataType = valueParameter.DataType;
|
---|
109 | this.validValues = GetValidValues(valueParameter);
|
---|
110 | this.ValueConfigurations = new CheckedValueConfigurationCollection(this.valueDataType);
|
---|
111 | this.ActualValue = new ConstrainedValue(valueParameter.Value, valueParameter.DataType);
|
---|
112 | if (Optimize) {
|
---|
113 | PopulateValueConfigurations();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | public ParameterConfiguration() { }
|
---|
118 | [StorableConstructor]
|
---|
119 | protected ParameterConfiguration(bool deserializing) { }
|
---|
120 | protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) : base(original, cloner) {
|
---|
121 | this.parameterName = original.ParameterName;
|
---|
122 | this.parameterDataType = original.parameterDataType;
|
---|
123 | //this.parameter = cloner.Clone(original.parameter);
|
---|
124 | this.valueDataType = original.ValueDataType;
|
---|
125 | this.validValues = cloner.Clone(original.ValidValues);
|
---|
126 | this.valueConfigurations = cloner.Clone(original.ValueConfigurations);
|
---|
127 | this.ActualValue = cloner.Clone(original.ActualValue);
|
---|
128 | this.optimize = original.optimize;
|
---|
129 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
130 | }
|
---|
131 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
132 | return new ParameterConfiguration(this, cloner);
|
---|
133 | }
|
---|
134 | [StorableHook(HookType.AfterDeserialization)]
|
---|
135 | private void AfterDeserialization() {
|
---|
136 | if (this.valueConfigurations != null) RegisterValueConfigurationEvents();
|
---|
137 | }
|
---|
138 | #endregion
|
---|
139 |
|
---|
140 | private void RegisterValueConfigurationEvents() {
|
---|
141 | this.ValueConfigurations.CheckedItemsChanged += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_CheckedItemsChanged);
|
---|
142 | this.ValueConfigurations.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsAdded);
|
---|
143 | this.ValueConfigurations.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsRemoved);
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void DeregisterValueConfigurationEvents() {
|
---|
147 | this.ValueConfigurations.CheckedItemsChanged -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_CheckedItemsChanged);
|
---|
148 | this.ValueConfigurations.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsAdded);
|
---|
149 | this.ValueConfigurations.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<IValueConfiguration>(ValueConfigurations_ItemsRemoved);
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void PopulateValueConfigurations() {
|
---|
153 | foreach (IItem validValue in this.validValues) {
|
---|
154 | IItem val;
|
---|
155 | if (ActualValue.Value != null && ActualValue.ValueDataType == validValue.GetType()) {
|
---|
156 | val = ActualValue.Value; // use existing value for that type (if available)
|
---|
157 | } else {
|
---|
158 | val = validValue;
|
---|
159 | }
|
---|
160 | this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | private IItemSet<IItem> GetValidValues(IValueParameter parameter) {
|
---|
165 | if (IsSubclassOfRawGeneric(typeof(OptionalConstrainedValueParameter<>), parameter.GetType())) {
|
---|
166 | var x = (IEnumerable)parameter.GetType().GetProperty("ValidValues").GetValue(parameter, new object[] { });
|
---|
167 | return new ItemSet<IItem>(x.Cast<IItem>());
|
---|
168 | } else {
|
---|
169 | return new ItemSet<IItem>(ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName));
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | private static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
|
---|
174 | while (toCheck != typeof(object)) {
|
---|
175 | var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
|
---|
176 | if (generic == cur) {
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 | toCheck = toCheck.BaseType;
|
---|
180 | }
|
---|
181 | return false;
|
---|
182 | }
|
---|
183 |
|
---|
184 | void ValueConfigurations_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
|
---|
185 | OnToStringChanged();
|
---|
186 | }
|
---|
187 | void ValueConfigurations_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
|
---|
188 | OnToStringChanged();
|
---|
189 | }
|
---|
190 | void ValueConfigurations_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IValueConfiguration> e) {
|
---|
191 | OnToStringChanged();
|
---|
192 | }
|
---|
193 |
|
---|
194 | #region INamedItem Properties
|
---|
195 | public virtual string Name {
|
---|
196 | get { return ParameterName; }
|
---|
197 | set { throw new NotSupportedException(); }
|
---|
198 | }
|
---|
199 | public virtual string Description {
|
---|
200 | get { return base.ItemDescription; }
|
---|
201 | set { throw new NotSupportedException(); }
|
---|
202 | }
|
---|
203 | public virtual bool CanChangeDescription {
|
---|
204 | get { return false; }
|
---|
205 | }
|
---|
206 | public virtual bool CanChangeName {
|
---|
207 | get { return false; }
|
---|
208 | }
|
---|
209 | public override string ItemDescription {
|
---|
210 | //get { return parameter != null ? parameter.Description : base.ItemDescription; }
|
---|
211 | get { return base.ItemDescription; }
|
---|
212 | }
|
---|
213 | public override System.Drawing.Image ItemImage {
|
---|
214 | //get { return parameter != null ? parameter.ItemImage : base.ItemImage; }
|
---|
215 | get { return base.ItemImage; }
|
---|
216 | }
|
---|
217 | public override string ItemName {
|
---|
218 | //get { return parameter != null ?parameter.ItemName : base.ItemName; }
|
---|
219 | get { return base.ItemName; }
|
---|
220 | }
|
---|
221 | #endregion
|
---|
222 |
|
---|
223 | #region Events
|
---|
224 | public virtual event EventHandler NameChanged;
|
---|
225 | protected virtual void OnNameChanged(object sender, EventArgs e) {
|
---|
226 | var handler = NameChanged;
|
---|
227 | if (handler != null) handler(sender, e);
|
---|
228 | }
|
---|
229 |
|
---|
230 | public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
231 | protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
|
---|
232 | var handler = NameChanging;
|
---|
233 | if (handler != null) handler(sender, e);
|
---|
234 | }
|
---|
235 |
|
---|
236 | public virtual event EventHandler DescriptionChanged;
|
---|
237 | protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
|
---|
238 | var handler = DescriptionChanged;
|
---|
239 | if (handler != null) handler(sender, e);
|
---|
240 | }
|
---|
241 |
|
---|
242 | public virtual event EventHandler IsOptimizableChanged;
|
---|
243 | private void OnIsOptimizableChanged() {
|
---|
244 | var handler = IsOptimizableChanged;
|
---|
245 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
246 | }
|
---|
247 |
|
---|
248 | public virtual event EventHandler OptimizeChanged;
|
---|
249 | protected virtual void OnOptimizeChanged() {
|
---|
250 | var handler = OptimizeChanged;
|
---|
251 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
252 | }
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | public override string ToString() {
|
---|
256 | if (Optimize) {
|
---|
257 | return string.Format("{0}: (Optimize: {1})", ParameterName, ValueConfigurations.CheckedItems.Count());
|
---|
258 | } else {
|
---|
259 | return string.Format("{0}: {1}", ParameterName, ActualValue.Value);
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | public static IParameterConfiguration Create(IParameterizedNamedItem parent, IParameter parameter) {
|
---|
264 | if (parameter is IValueParameter) {
|
---|
265 | IValueParameter valueParameter = parameter as IValueParameter;
|
---|
266 | return new ParameterConfiguration(parameter.Name, valueParameter);
|
---|
267 | }
|
---|
268 | return null;
|
---|
269 | }
|
---|
270 |
|
---|
271 | public void Parameterize(IValueParameter parameter) {
|
---|
272 | parameter.ActualValue = this.ActualValue.Value;
|
---|
273 | }
|
---|
274 |
|
---|
275 | public void Randomize(IRandom random) {
|
---|
276 | if (Optimize) {
|
---|
277 | actualValueConfigurationIndex = random.Next(ValueConfigurations.CheckedItems.Count());
|
---|
278 | this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).Randomize(random);
|
---|
279 | this.ActualValue = this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).ActualValue;
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | public void Mutate(IRandom random) {
|
---|
284 | if (Optimize) {
|
---|
285 | foreach (IValueConfiguration vc in this.ValueConfigurations.CheckedItems) {
|
---|
286 | vc.Mutate(random);
|
---|
287 | }
|
---|
288 | actualValueConfigurationIndex = random.Next(ValueConfigurations.CheckedItems.Count());
|
---|
289 | this.ActualValue = this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).ActualValue;
|
---|
290 | this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).Mutate(random);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | public void Cross(IOptimizable other, IRandom random) {
|
---|
295 | if (Optimize) {
|
---|
296 | IParameterConfiguration otherPc = (IParameterConfiguration)other;
|
---|
297 | for (int i = 0; i < this.ValueConfigurations.Count; i++) {
|
---|
298 | this.ValueConfigurations.ElementAt(i).Cross(otherPc.ValueConfigurations.ElementAt(i), random);
|
---|
299 | }
|
---|
300 | this.ActualValue = this.ValueConfigurations.CheckedItems.ElementAt(actualValueConfigurationIndex).ActualValue;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|