[4839] | 1 | using System;
|
---|
[4981] | 2 | using System.Linq;
|
---|
[4839] | 3 | using HeuristicLab.Common;
|
---|
| 4 | using HeuristicLab.Core;
|
---|
| 5 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4981] | 6 | using HeuristicLab.Data;
|
---|
| 7 | using System.Drawing;
|
---|
[4839] | 8 |
|
---|
| 9 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
[4981] | 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 {
|
---|
[4982] | 32 | get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }
|
---|
[4839] | 33 | protected set { this.parameterConfigurations = value; }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[4981] | 36 | protected ConstrainedValue constrainedValue;
|
---|
| 37 | public ConstrainedValue ConstrainedValue {
|
---|
| 38 | get { return constrainedValue; }
|
---|
[4839] | 39 | set {
|
---|
[4981] | 40 | if (this.constrainedValue != value) {
|
---|
| 41 | ClearParameterConfigurations();
|
---|
| 42 | this.constrainedValue = value;
|
---|
| 43 | PopulateParameterConfigurations();
|
---|
[4839] | 44 | OnValueChanged();
|
---|
[4981] | 45 | OnToStringChanged();
|
---|
[4839] | 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[4981] | 50 | protected IRange rangeConstraint;
|
---|
| 51 | public IRange RangeConstraint {
|
---|
| 52 | get { return rangeConstraint; }
|
---|
[4839] | 53 | }
|
---|
[4981] | 54 |
|
---|
| 55 | #region Constructors and Cloning
|
---|
[4839] | 56 | public ValueConfiguration(IItem value, Type valueDataType) {
|
---|
[4982] | 57 | this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
|
---|
[4981] | 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();
|
---|
[4839] | 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);
|
---|
[4981] | 74 | this.ConstrainedValue = cloner.Clone(original.ConstrainedValue);
|
---|
| 75 | this.rangeConstraint = cloner.Clone(original.RangeConstraint);
|
---|
| 76 | RegisterEvents();
|
---|
[4839] | 77 | }
|
---|
| 78 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 79 | return new ValueConfiguration(this, cloner);
|
---|
| 80 | }
|
---|
[4981] | 81 | #endregion
|
---|
[4839] | 82 |
|
---|
[4981] | 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);
|
---|
[4982] | 88 | if (pc != null) this.parameterConfigurations.Add(pc);
|
---|
[4981] | 89 | }
|
---|
[4839] | 90 | }
|
---|
| 91 | }
|
---|
[4981] | 92 | protected virtual void ClearParameterConfigurations() {
|
---|
[4982] | 93 | parameterConfigurations.Clear();
|
---|
[4839] | 94 | }
|
---|
| 95 |
|
---|
[4981] | 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);
|
---|
[4839] | 99 | }
|
---|
[4981] | 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 | }
|
---|
[4839] | 104 |
|
---|
[4981] | 105 | void ConstrainedValue_ToStringChanged(object sender, EventArgs e) {
|
---|
| 106 | OnToStringChanged();
|
---|
[4839] | 107 | }
|
---|
[4981] | 108 | void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
|
---|
| 109 | OnToStringChanged();
|
---|
| 110 | }
|
---|
[4839] | 111 |
|
---|
[4981] | 112 | #region IItem Members
|
---|
| 113 | public override string ItemDescription {
|
---|
| 114 | get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemDescription; }
|
---|
[4839] | 115 | }
|
---|
| 116 |
|
---|
[4981] | 117 | public override Image ItemImage {
|
---|
| 118 | get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemImage : base.ItemImage; }
|
---|
[4839] | 119 | }
|
---|
| 120 |
|
---|
[4981] | 121 | public override string ItemName {
|
---|
| 122 | get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemName; }
|
---|
[4839] | 123 | }
|
---|
[4981] | 124 | #endregion
|
---|
[4839] | 125 |
|
---|
[4981] | 126 | #region Event Handlers
|
---|
[4839] | 127 | public virtual event EventHandler ValueChanged;
|
---|
| 128 | protected virtual void OnValueChanged() {
|
---|
| 129 | var handler = ValueChanged;
|
---|
| 130 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 131 | }
|
---|
[4981] | 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 | }
|
---|
[4839] | 138 | #endregion
|
---|
| 139 |
|
---|
| 140 | public override string ToString() {
|
---|
[4981] | 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 |
|
---|
[4839] | 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|