Changeset 4981 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings
- Timestamp:
- 11/28/10 21:42:49 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings
- Files:
-
- 4 added
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfigurations/ParameterConfiguration.cs
r4839 r4981 14 14 15 15 [Storable] 16 protected bool optimiz ationEnabled;17 public bool Optimiz ationEnabled{18 get { return optimiz ationEnabled; }16 protected bool optimize; 17 public bool Optimize { 18 get { return optimize; } 19 19 set { 20 if (optimiz ationEnabled!= value) {21 optimiz ationEnabled= value;22 if (optimiz ationEnabled) {20 if (optimize != value) { 21 optimize = value; 22 if (optimize) { 23 23 PopulateValueConfigurations(); 24 24 } else { 25 25 this.ValueConfigurations.Clear(); 26 26 } 27 OnOptimiz ationEnabledChanged();27 OnOptimizeChanged(); 28 28 } 29 29 } … … 62 62 } 63 63 64 protected IChecked ItemList<IValueConfiguration>valueConfigurations;65 public IChecked ItemList<IValueConfiguration>ValueConfigurations {64 protected ICheckedValueConfigurationCollection valueConfigurations; 65 public ICheckedValueConfigurationCollection ValueConfigurations { 66 66 get { return this.valueConfigurations; } 67 67 protected set { this.valueConfigurations = value; } … … 75 75 } 76 76 77 public ConstrainedValue ConstrainedValue { 78 get { return actualValueConfiguration.ConstrainedValue; } 79 set { actualValueConfiguration.ConstrainedValue = value; } 80 } 81 77 82 // store parameter reference only for name/description/image/... 78 83 private IValueParameter parameter; 79 84 85 #region Constructors and Cloning 80 86 public ParameterConfiguration(string parameterName, IValueParameter valueParameter) { 81 87 this.ParameterName = parameterName; … … 84 90 this.valueDataType = valueParameter.DataType; 85 91 this.validValues = GetValidValues(); 86 this.ValueConfigurations = new Checked ItemList<IValueConfiguration>();92 this.ValueConfigurations = new CheckedValueConfigurationCollection(this.valueDataType); 87 93 this.ActualValueConfiguration = new ValueConfiguration(valueParameter.Value, valueParameter.DataType); 88 if (Optimiz ationEnabled) {94 if (Optimize) { 89 95 PopulateValueConfigurations(); 90 96 } … … 94 100 [StorableConstructor] 95 101 protected ParameterConfiguration(bool deserializing) { } 96 protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) : base(original, cloner) { 102 protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) 103 : base(original, cloner) { 97 104 this.ParameterName = original.ParameterName; 98 105 this.parameterDataType = original.parameterDataType; … … 102 109 this.ValueConfigurations = cloner.Clone(original.ValueConfigurations); 103 110 this.ActualValueConfiguration = cloner.Clone(original.ActualValueConfiguration); 104 this.Optimiz ationEnabled = original.optimizationEnabled;111 this.Optimize = original.optimize; 105 112 } 106 113 public override IDeepCloneable Clone(Cloner cloner) { 107 114 return new ParameterConfiguration(this, cloner); 108 115 } 116 #endregion 109 117 110 118 private void PopulateValueConfigurations() { … … 118 126 } 119 127 120 void ValidValues_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<IItem>> e) {121 var oldItems = e.OldItems.Select(x => x.Value);122 var newItems = e.Items.Select(x => x.Value);123 var intersection = oldItems.Intersect(newItems);124 var removedItems = oldItems.Where(x => !intersection.Contains(x));125 var addedItems = newItems.Where(x => !intersection.Contains(x));126 127 foreach (var item in removedItems) {128 RemoveValueConfiguration(item);129 }130 foreach (var item in addedItems) {131 this.ValueConfigurations.Add(new ValueConfiguration(item, item.GetType()));132 }133 }134 135 private void RemoveValueConfiguration(IItem item) {136 IValueConfiguration vc = this.ValueConfigurations.Single(x => x.ValueDataType == item.GetType());137 this.ValueConfigurations.Remove(vc);138 }128 //void ValidValues_CheckedItemsChanged(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<IItem>> e) { 129 // var oldItems = e.OldItems.Select(x => x.Value); 130 // var newItems = e.Items.Select(x => x.Value); 131 // var intersection = oldItems.Intersect(newItems); 132 // var removedItems = oldItems.Where(x => !intersection.Contains(x)); 133 // var addedItems = newItems.Where(x => !intersection.Contains(x)); 134 135 // foreach (var item in removedItems) { 136 // RemoveValueConfiguration(item); 137 // } 138 // foreach (var item in addedItems) { 139 // this.ValueConfigurations.Add(new ValueConfiguration(item, item.GetType())); 140 // } 141 //} 142 143 //private void RemoveValueConfiguration(IItem item) { 144 // IValueConfiguration vc = this.ValueConfigurations.Single(x => x.ValueDataType == item.GetType()); 145 // this.ValueConfigurations.Remove(vc); 146 //} 139 147 140 148 #region INamedItem Properties … … 198 206 } 199 207 200 public virtual event EventHandler Optimiz ationEnabledChanged;201 protected virtual void OnOptimiz ationEnabledChanged() {202 var handler = Optimiz ationEnabledChanged;208 public virtual event EventHandler OptimizeChanged; 209 protected virtual void OnOptimizeChanged() { 210 var handler = OptimizeChanged; 203 211 if (handler != null) handler(this, EventArgs.Empty); 204 212 } … … 206 214 207 215 public override string ToString() { 208 return string.Format("{0} ", ParameterName);216 return string.Format("{0}: {1}", ParameterName, ConstrainedValue.Value); 209 217 } 210 218 … … 217 225 return null; 218 226 } 219 220 227 } 221 228 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ValueConfigurations/ValueConfiguration.cs
r4839 r4981 1 1 using System; 2 using System.Linq; 2 3 using HeuristicLab.Common; 3 4 using HeuristicLab.Core; 4 5 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 6 using HeuristicLab.Data; 7 using System.Drawing; 5 8 6 9 namespace HeuristicLab.Problems.MetaOptimization { 7 [StorableClass] 8 public class ValueConfiguration : DeepCloneable, IValueConfiguration { 9 [Storable] 10 protected IItemList<IParameterConfiguration> parameterConfigurations = new ItemList<IParameterConfiguration>(); 11 public IItemList<IParameterConfiguration> ParameterConfigurations { 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 { 12 32 get { return this.parameterConfigurations; } 13 33 protected set { this.parameterConfigurations = value; } 14 34 } 15 35 16 protected IItem value;17 public IItemValue {18 get { return value; }36 protected ConstrainedValue constrainedValue; 37 public ConstrainedValue ConstrainedValue { 38 get { return constrainedValue; } 19 39 set { 20 if (this. value != value) {21 Clear ChildParameterConfigurations();22 this. value = value;23 if (this.value is IParameterizedNamedItem) AddChildParameterConfigurations(this.value as IParameterizedNamedItem);40 if (this.constrainedValue != value) { 41 ClearParameterConfigurations(); 42 this.constrainedValue = value; 43 PopulateParameterConfigurations(); 24 44 OnValueChanged(); 45 OnToStringChanged(); 25 46 } 26 47 } 27 48 } 28 49 29 protected Type valueDataType; 30 public Type ValueDataType { 31 get { return this.valueDataType; } 32 protected set { this.valueDataType = value; } 50 protected IRange rangeConstraint; 51 public IRange RangeConstraint { 52 get { return rangeConstraint; } 33 53 } 34 54 55 #region Constructors and Cloning 35 56 public ValueConfiguration(IItem value, Type valueDataType) { 36 this.Value = value; 37 this.ValueDataType = 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(); 38 67 } 39 68 … … 43 72 protected ValueConfiguration(ValueConfiguration original, Cloner cloner) : base(original, cloner) { 44 73 this.ParameterConfigurations = cloner.Clone(original.ParameterConfigurations); 45 this. Value = cloner.Clone(original.Value);46 this. ValueDataType = original.ValueDataType;47 74 this.ConstrainedValue = cloner.Clone(original.ConstrainedValue); 75 this.rangeConstraint = cloner.Clone(original.RangeConstraint); 76 RegisterEvents(); 48 77 } 49 78 public override IDeepCloneable Clone(Cloner cloner) { 50 79 return new ValueConfiguration(this, cloner); 51 80 } 81 #endregion 52 82 53 protected virtual void AddChildParameterConfigurations(IParameterizedNamedItem parameterizedItem) { 54 foreach (var childParameter in parameterizedItem.Parameters) { 55 var pc = ParameterConfiguration.Create(parameterizedItem, childParameter); 56 if (pc != null) this.ParameterConfigurations.Add(pc); 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 } 57 90 } 58 91 } 59 protected virtual void Clear ChildParameterConfigurations() {92 protected virtual void ClearParameterConfigurations() { 60 93 ParameterConfigurations.Clear(); 61 94 } 62 63 #region IItem Members 64 public string ItemDescription { 65 get { return value.ItemDescription; } 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); 66 103 } 67 104 68 public System.Drawing.Image ItemImage { 69 get { return value.ItemImage; } 105 void ConstrainedValue_ToStringChanged(object sender, EventArgs e) { 106 OnToStringChanged(); 107 } 108 void RangeConstraint_ToStringChanged(object sender, EventArgs e) { 109 OnToStringChanged(); 70 110 } 71 111 72 public string ItemName { 73 get { return value.ItemDescription; } 112 #region IItem Members 113 public override string ItemDescription { 114 get { return (constrainedValue != null && constrainedValue.Value != null) ? constrainedValue.Value.ItemDescription : base.ItemDescription; } 74 115 } 75 116 76 public Version ItemVersion { 77 get { return value.ItemVersion; } 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; } 78 123 } 79 124 #endregion 80 125 81 #region Events 82 public virtual event EventHandler ItemImageChanged; 83 protected virtual void OnItemImageChanged(object sender, EventArgs e) { 84 var handler = ItemImageChanged; 85 if (handler != null) handler(sender, e); 86 } 87 88 public virtual event EventHandler ToStringChanged; 89 protected virtual void OnStringChanged(object sender, EventArgs e) { 90 var handler = ToStringChanged; 91 if (handler != null) handler(this, e); // important to set 'this' as sender 92 } 93 126 #region Event Handlers 94 127 public virtual event EventHandler ValueChanged; 95 128 protected virtual void OnValueChanged() { … … 97 130 if (handler != null) handler(this, EventArgs.Empty); 98 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 } 99 138 #endregion 100 139 101 140 public override string ToString() { 102 return value.ItemName; 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 103 151 } 104 152 }
Note: See TracChangeset
for help on using the changeset viewer.