1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using System.Drawing;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
10 | // TODO: ItemName/Descr, storability
|
---|
11 | public class ValueConfiguration : Item, IValueConfiguration {
|
---|
12 | protected bool optimize;
|
---|
13 | public bool Optimize {
|
---|
14 | get { return optimize; }
|
---|
15 | set {
|
---|
16 | if (optimize != value) {
|
---|
17 | optimize = value;
|
---|
18 | if (optimize) {
|
---|
19 | ClearParameterConfigurations();
|
---|
20 | PopulateParameterConfigurations();
|
---|
21 | } else {
|
---|
22 | ClearParameterConfigurations();
|
---|
23 | }
|
---|
24 | OnOptimizeChanged();
|
---|
25 | OnToStringChanged();
|
---|
26 | }
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
|
---|
31 | public IItemCollection<IParameterConfiguration> ParameterConfigurations {
|
---|
32 | get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }
|
---|
33 | protected set { this.parameterConfigurations = value; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected ConstrainedValue constrainedValue;
|
---|
37 | public ConstrainedValue ConstrainedValue {
|
---|
38 | get { return constrainedValue; }
|
---|
39 | set {
|
---|
40 | if (this.constrainedValue != value) {
|
---|
41 | ClearParameterConfigurations();
|
---|
42 | this.constrainedValue = value;
|
---|
43 | PopulateParameterConfigurations();
|
---|
44 | OnValueChanged();
|
---|
45 | OnToStringChanged();
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | protected IRange rangeConstraint;
|
---|
51 | public IRange RangeConstraint {
|
---|
52 | get { return rangeConstraint; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | #region Constructors and Cloning
|
---|
56 | public ValueConfiguration(IItem value, Type valueDataType) {
|
---|
57 | this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
|
---|
58 | this.ConstrainedValue = new ConstrainedValue(value, valueDataType);
|
---|
59 | if (constrainedValue.ValueDataType == typeof(IntValue)) {
|
---|
60 | rangeConstraint = new Range<IntValue>(new IntValue(0), (IntValue)value, new IntValue(1));
|
---|
61 | } else if (constrainedValue.ValueDataType == typeof(DoubleValue)) {
|
---|
62 | rangeConstraint = new Range<DoubleValue>(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.1));
|
---|
63 | } else {
|
---|
64 | rangeConstraint = null;
|
---|
65 | }
|
---|
66 | RegisterEvents();
|
---|
67 | }
|
---|
68 |
|
---|
69 | public ValueConfiguration() { }
|
---|
70 | [StorableConstructor]
|
---|
71 | protected ValueConfiguration(bool deserializing) { }
|
---|
72 | protected ValueConfiguration(ValueConfiguration original, Cloner cloner) : base(original, cloner) {
|
---|
73 | this.ParameterConfigurations = cloner.Clone(original.ParameterConfigurations);
|
---|
74 | this.ConstrainedValue = cloner.Clone(original.ConstrainedValue);
|
---|
75 | this.rangeConstraint = cloner.Clone(original.RangeConstraint);
|
---|
76 | RegisterEvents();
|
---|
77 | }
|
---|
78 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
79 | return new ValueConfiguration(this, cloner);
|
---|
80 | }
|
---|
81 | #endregion
|
---|
82 |
|
---|
83 | protected virtual void PopulateParameterConfigurations() {
|
---|
84 | if (this.constrainedValue.Value is IParameterizedNamedItem) {
|
---|
85 | var parameterizedItem = this.constrainedValue.Value as IParameterizedNamedItem;
|
---|
86 | foreach (var childParameter in parameterizedItem.Parameters) {
|
---|
87 | var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);
|
---|
88 | if (pc != null) this.parameterConfigurations.Add(pc);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | protected virtual void ClearParameterConfigurations() {
|
---|
93 | parameterConfigurations.Clear();
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void RegisterEvents() {
|
---|
97 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
|
---|
98 | if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
|
---|
99 | }
|
---|
100 | private void DeregisterEvents() {
|
---|
101 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
|
---|
102 | if (this.ConstrainedValue != null) this.ConstrainedValue.ToStringChanged += new EventHandler(ConstrainedValue_ToStringChanged);
|
---|
103 | }
|
---|
104 |
|
---|
105 | void ConstrainedValue_ToStringChanged(object sender, EventArgs e) {
|
---|
106 | OnToStringChanged();
|
---|
107 | }
|
---|
108 | void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
|
---|
109 | OnToStringChanged();
|
---|
110 | }
|
---|
111 |
|
---|
112 | #region IItem Members
|
---|
113 | public override string ItemDescription {
|
---|
114 | get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemDescription; }
|
---|
115 | }
|
---|
116 |
|
---|
117 | public override Image ItemImage {
|
---|
118 | get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemImage : base.ItemImage; }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override string ItemName {
|
---|
122 | get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemName; }
|
---|
123 | }
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | #region Event Handlers
|
---|
127 | public virtual event EventHandler ValueChanged;
|
---|
128 | protected virtual void OnValueChanged() {
|
---|
129 | var handler = ValueChanged;
|
---|
130 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
131 | }
|
---|
132 |
|
---|
133 | public virtual event EventHandler OptimizeChanged;
|
---|
134 | protected virtual void OnOptimizeChanged() {
|
---|
135 | var handler = OptimizeChanged;
|
---|
136 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
137 | }
|
---|
138 | #endregion
|
---|
139 |
|
---|
140 | public override string ToString() {
|
---|
141 | if (ConstrainedValue != null && ConstrainedValue.Value != null) {
|
---|
142 | if(Optimize) {
|
---|
143 | return string.Format("{0}: {1}", ConstrainedValue.Value.ItemName, RangeConstraint);
|
---|
144 | } else {
|
---|
145 | return string.Format("{0}: {1}", ConstrainedValue.Value.ItemName, ConstrainedValue.Value);
|
---|
146 | }
|
---|
147 | } else {
|
---|
148 | return base.ToString();
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|