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 |
|
---|
9 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
10 | [StorableClass]
|
---|
11 | public class ParameterConfiguration : DeepCloneable, IParameterConfiguration, IStorableContent {
|
---|
12 | [Storable]
|
---|
13 | public string Filename { get; set; }
|
---|
14 |
|
---|
15 | [Storable]
|
---|
16 | protected bool optimize;
|
---|
17 | public bool Optimize {
|
---|
18 | get { return optimize; }
|
---|
19 | set {
|
---|
20 | if (optimize != value) {
|
---|
21 | optimize = value;
|
---|
22 | if (optimize) {
|
---|
23 | PopulateValueConfigurations();
|
---|
24 | } else {
|
---|
25 | this.ValueConfigurations.Clear();
|
---|
26 | }
|
---|
27 | OnOptimizeChanged();
|
---|
28 | }
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | [Storable]
|
---|
33 | protected string parameterName;
|
---|
34 | public string ParameterName {
|
---|
35 | get { return parameterName; }
|
---|
36 | set {
|
---|
37 | if (parameterName != value) {
|
---|
38 | parameterName = value;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected Type parameterDataType;
|
---|
44 | public Type ParameterDataType {
|
---|
45 | get { return this.parameterDataType; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected IEnumerable<IItem> validValues;
|
---|
49 | public IEnumerable<IItem> ValidValues {
|
---|
50 | get { return validValues; }
|
---|
51 | protected set {
|
---|
52 | if (this.validValues != value) {
|
---|
53 | this.validValues = value;
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected Type valueDataType;
|
---|
59 | public Type ValueDataType {
|
---|
60 | get { return valueDataType; }
|
---|
61 | protected set { this.valueDataType = value; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected ICheckedValueConfigurationCollection valueConfigurations;
|
---|
65 | public ICheckedValueConfigurationCollection ValueConfigurations {
|
---|
66 | get { return this.valueConfigurations; }
|
---|
67 | protected set { this.valueConfigurations = value; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | // todo: chooses one of the parameter configurations as actual value
|
---|
71 | protected IValueConfiguration actualValueConfiguration;
|
---|
72 | public IValueConfiguration ActualValueConfiguration {
|
---|
73 | get { return this.actualValueConfiguration; }
|
---|
74 | set { this.actualValueConfiguration = value; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public ConstrainedValue ConstrainedValue {
|
---|
78 | get { return actualValueConfiguration.ConstrainedValue; }
|
---|
79 | set { actualValueConfiguration.ConstrainedValue = value; }
|
---|
80 | }
|
---|
81 |
|
---|
82 | // store parameter reference only for name/description/image/...
|
---|
83 | private IValueParameter parameter;
|
---|
84 |
|
---|
85 | #region Constructors and Cloning
|
---|
86 | public ParameterConfiguration(string parameterName, IValueParameter valueParameter) {
|
---|
87 | this.ParameterName = parameterName;
|
---|
88 | this.parameterDataType = valueParameter.GetType();
|
---|
89 | this.parameter = valueParameter;
|
---|
90 | this.valueDataType = valueParameter.DataType;
|
---|
91 | this.validValues = GetValidValues();
|
---|
92 | this.ValueConfigurations = new CheckedValueConfigurationCollection(this.valueDataType);
|
---|
93 | this.ActualValueConfiguration = new ValueConfiguration(valueParameter.Value, valueParameter.DataType);
|
---|
94 | if (Optimize) {
|
---|
95 | PopulateValueConfigurations();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | public ParameterConfiguration() { }
|
---|
100 | [StorableConstructor]
|
---|
101 | protected ParameterConfiguration(bool deserializing) { }
|
---|
102 | protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner)
|
---|
103 | : base(original, cloner) {
|
---|
104 | this.ParameterName = original.ParameterName;
|
---|
105 | this.parameterDataType = original.parameterDataType;
|
---|
106 | this.parameter = cloner.Clone(original.parameter);
|
---|
107 | this.ValueDataType = original.ValueDataType;
|
---|
108 | this.ValidValues = original.ValidValues.Select(x => cloner.Clone(x));
|
---|
109 | this.ValueConfigurations = cloner.Clone(original.ValueConfigurations);
|
---|
110 | this.ActualValueConfiguration = cloner.Clone(original.ActualValueConfiguration);
|
---|
111 | this.Optimize = original.optimize;
|
---|
112 | }
|
---|
113 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
114 | return new ParameterConfiguration(this, cloner);
|
---|
115 | }
|
---|
116 | #endregion
|
---|
117 |
|
---|
118 | private void PopulateValueConfigurations() {
|
---|
119 | foreach (IItem validValue in this.validValues) {
|
---|
120 | IItem val;
|
---|
121 | if (actualValueConfiguration.ConstrainedValue.Value != null && actualValueConfiguration.ConstrainedValue.ValueDataType == validValue.GetType()) {
|
---|
122 | val = actualValueConfiguration.ConstrainedValue.Value; // use existing value for that type (if available)
|
---|
123 | } else {
|
---|
124 | val = validValue;
|
---|
125 | }
|
---|
126 | this.ValueConfigurations.Add(new ValueConfiguration(val, val.GetType()), true);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private IEnumerable<IItem> GetValidValues() {
|
---|
131 | return ApplicationManager.Manager.GetInstances(valueDataType).Select(x => (IItem)x).OrderBy(x => x.ItemName).ToArray();
|
---|
132 | }
|
---|
133 |
|
---|
134 | //void ValidValues_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<IItem>> e) {
|
---|
135 | // var oldItems = e.OldItems.Select(x => x.Value);
|
---|
136 | // var newItems = e.Items.Select(x => x.Value);
|
---|
137 | // var intersection = oldItems.Intersect(newItems);
|
---|
138 | // var removedItems = oldItems.Where(x => !intersection.Contains(x));
|
---|
139 | // var addedItems = newItems.Where(x => !intersection.Contains(x));
|
---|
140 |
|
---|
141 | // foreach (var item in removedItems) {
|
---|
142 | // RemoveValueConfiguration(item);
|
---|
143 | // }
|
---|
144 | // foreach (var item in addedItems) {
|
---|
145 | // this.ValueConfigurations.Add(new ValueConfiguration(item, item.GetType()));
|
---|
146 | // }
|
---|
147 | //}
|
---|
148 |
|
---|
149 | //private void RemoveValueConfiguration(IItem item) {
|
---|
150 | // IValueConfiguration vc = this.ValueConfigurations.Single(x => x.ValueDataType == item.GetType());
|
---|
151 | // this.ValueConfigurations.Remove(vc);
|
---|
152 | //}
|
---|
153 |
|
---|
154 | #region INamedItem Properties
|
---|
155 | public virtual string Name {
|
---|
156 | get { return ParameterName; }
|
---|
157 | set { throw new NotSupportedException(); }
|
---|
158 | }
|
---|
159 | public virtual string Description {
|
---|
160 | get { return parameter.Description; }
|
---|
161 | set { throw new NotSupportedException(); }
|
---|
162 | }
|
---|
163 | public virtual bool CanChangeDescription {
|
---|
164 | get { return false; }
|
---|
165 | }
|
---|
166 | public virtual bool CanChangeName {
|
---|
167 | get { return false; }
|
---|
168 | }
|
---|
169 | public virtual string ItemDescription {
|
---|
170 | get { return parameter.Description; }
|
---|
171 | }
|
---|
172 | public virtual System.Drawing.Image ItemImage {
|
---|
173 | get { return parameter.ItemImage; }
|
---|
174 | }
|
---|
175 | public virtual string ItemName {
|
---|
176 | get { return parameter.ItemName; }
|
---|
177 | }
|
---|
178 | public virtual Version ItemVersion {
|
---|
179 | get { return parameter.ItemVersion; }
|
---|
180 | }
|
---|
181 | #endregion
|
---|
182 |
|
---|
183 | #region Events
|
---|
184 | public virtual event EventHandler NameChanged;
|
---|
185 | protected virtual void OnNameChanged(object sender, EventArgs e) {
|
---|
186 | var handler = NameChanged;
|
---|
187 | if (handler != null) handler(sender, e);
|
---|
188 | }
|
---|
189 |
|
---|
190 | public virtual event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
191 | protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) {
|
---|
192 | var handler = NameChanging;
|
---|
193 | if (handler != null) handler(sender, e);
|
---|
194 | }
|
---|
195 |
|
---|
196 | public virtual event EventHandler DescriptionChanged;
|
---|
197 | protected virtual void OnDescriptionChanged(object sender, EventArgs e) {
|
---|
198 | var handler = DescriptionChanged;
|
---|
199 | if (handler != null) handler(sender, e);
|
---|
200 | }
|
---|
201 |
|
---|
202 | public virtual event EventHandler ItemImageChanged;
|
---|
203 | protected virtual void OnItemImageChanged(object sender, EventArgs e) {
|
---|
204 | var handler = ItemImageChanged;
|
---|
205 | if (handler != null) handler(sender, e);
|
---|
206 | }
|
---|
207 |
|
---|
208 | public virtual event EventHandler ToStringChanged;
|
---|
209 | protected virtual void OnStringChanged(object sender, EventArgs e) {
|
---|
210 | var handler = ToStringChanged;
|
---|
211 | if (handler != null) handler(this, e); // important to set 'this' as sender
|
---|
212 | }
|
---|
213 |
|
---|
214 | public virtual event EventHandler OptimizeChanged;
|
---|
215 | protected virtual void OnOptimizeChanged() {
|
---|
216 | var handler = OptimizeChanged;
|
---|
217 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
218 | }
|
---|
219 | #endregion
|
---|
220 |
|
---|
221 | public override string ToString() {
|
---|
222 | return string.Format("{0}: {1}", ParameterName, ConstrainedValue.Value);
|
---|
223 | }
|
---|
224 |
|
---|
225 | public static IParameterConfiguration Create(IParameterizedNamedItem parent, IParameter parameter) {
|
---|
226 | if (parameter is IValueParameter) {
|
---|
227 | IValueParameter valueParameter = parameter as IValueParameter;
|
---|
228 |
|
---|
229 | return new ParameterConfiguration(parameter.Name, valueParameter);
|
---|
230 | }
|
---|
231 | return null;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|