[5112] | 1 | using System;
|
---|
[5653] | 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Drawing;
|
---|
[5112] | 4 | using HeuristicLab.Common;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
| 9 | // TODO: ItemName/Descr, storability
|
---|
| 10 | [StorableClass]
|
---|
[5653] | 11 | public abstract class ValueConfiguration : NamedItem, IValueConfiguration {
|
---|
[5359] | 12 | public override bool CanChangeName {
|
---|
| 13 | get { return true; }
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | public override bool CanChangeDescription {
|
---|
| 17 | get { return true; }
|
---|
| 18 | }
|
---|
| 19 |
|
---|
[5112] | 20 | [Storable]
|
---|
| 21 | protected bool isOptimizable;
|
---|
| 22 | public bool IsOptimizable {
|
---|
| 23 | get { return isOptimizable; }
|
---|
| 24 | set {
|
---|
| 25 | if (this.isOptimizable != value) {
|
---|
| 26 | this.isOptimizable = value;
|
---|
| 27 | OnIsOptimizableChanged();
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | [Storable]
|
---|
| 33 | protected bool optimize;
|
---|
[5653] | 34 | public virtual bool Optimize {
|
---|
[5112] | 35 | get { return optimize; }
|
---|
| 36 | set {
|
---|
| 37 | if (optimize != value) {
|
---|
| 38 | optimize = value;
|
---|
| 39 | OnOptimizeChanged();
|
---|
| 40 | OnToStringChanged();
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [Storable]
|
---|
| 46 | protected ConstrainedValue actualValue;
|
---|
| 47 | public virtual ConstrainedValue ActualValue {
|
---|
| 48 | get { return actualValue; }
|
---|
| 49 | set {
|
---|
| 50 | if (this.actualValue != value) {
|
---|
[5207] | 51 | DeregisterActualValueEvents();
|
---|
[5112] | 52 | this.actualValue = value;
|
---|
| 53 | OnValueChanged();
|
---|
| 54 | OnToStringChanged();
|
---|
[5207] | 55 | RegisterActualValueEvents();
|
---|
[5112] | 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | [Storable]
|
---|
[5361] | 61 | protected int number = 0;
|
---|
[5359] | 62 | public int Number {
|
---|
| 63 | get { return number; }
|
---|
| 64 | set {
|
---|
| 65 | if (value != number) {
|
---|
| 66 | number = value;
|
---|
| 67 | OnToStringChanged();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[5112] | 72 | #region Constructors and Cloning
|
---|
| 73 | public ValueConfiguration(IItem value, Type valueDataType) {
|
---|
[5231] | 74 | this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false);
|
---|
[5112] | 75 | this.IsOptimizable = true;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public ValueConfiguration() { }
|
---|
| 79 | [StorableConstructor]
|
---|
| 80 | protected ValueConfiguration(bool deserializing) { }
|
---|
| 81 | protected ValueConfiguration(ValueConfiguration original, Cloner cloner)
|
---|
| 82 | : base(original, cloner) {
|
---|
| 83 | this.actualValue = cloner.Clone(original.ActualValue);
|
---|
| 84 | this.isOptimizable = original.isOptimizable;
|
---|
| 85 | this.optimize = original.optimize;
|
---|
[5359] | 86 | this.number = original.number;
|
---|
[5112] | 87 | RegisterActualValueEvents();
|
---|
| 88 | }
|
---|
| 89 | #endregion
|
---|
| 90 |
|
---|
| 91 | private void RegisterActualValueEvents() {
|
---|
| 92 | if (this.actualValue != null) this.actualValue.ToStringChanged += new EventHandler(actualValue_ToStringChanged);
|
---|
| 93 | }
|
---|
| 94 | private void DeregisterActualValueEvents() {
|
---|
| 95 | if (this.actualValue != null) this.actualValue.ToStringChanged -= new EventHandler(actualValue_ToStringChanged);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | #region Events
|
---|
[5653] | 99 | private void actualValue_ToStringChanged(object sender, EventArgs e) {
|
---|
[5112] | 100 | OnToStringChanged();
|
---|
| 101 | }
|
---|
| 102 | #endregion
|
---|
[5144] | 103 |
|
---|
[5112] | 104 | #region IItem Members
|
---|
| 105 | public override string ItemDescription {
|
---|
| 106 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemDescription; }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public override Image ItemImage {
|
---|
| 110 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemImage : base.ItemImage; }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | public override string ItemName {
|
---|
| 114 | get { return (actualValue != null && actualValue.Value != null) ? actualValue.Value.ItemDescription : base.ItemName; }
|
---|
| 115 | }
|
---|
| 116 | #endregion
|
---|
[5144] | 117 |
|
---|
[5112] | 118 | #region Event Handlers
|
---|
| 119 | public virtual event EventHandler ValueChanged;
|
---|
| 120 | protected virtual void OnValueChanged() {
|
---|
| 121 | var handler = ValueChanged;
|
---|
| 122 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | public virtual event EventHandler IsOptimizableChanged;
|
---|
| 126 | private void OnIsOptimizableChanged() {
|
---|
| 127 | var handler = IsOptimizableChanged;
|
---|
| 128 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public virtual event EventHandler OptimizeChanged;
|
---|
| 132 | protected virtual void OnOptimizeChanged() {
|
---|
| 133 | var handler = OptimizeChanged;
|
---|
| 134 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 135 | }
|
---|
| 136 | #endregion
|
---|
| 137 |
|
---|
[5359] | 138 | public string NumberedName {
|
---|
| 139 | get {
|
---|
| 140 | if (this.number == 0) {
|
---|
[5361] | 141 | return (ActualValue != null && ActualValue.Value != null) ? ActualValue.Value.ItemName : base.ToString();
|
---|
[5359] | 142 | } else {
|
---|
| 143 | return string.Format("{0} {1}", ActualValue.Value.ItemName, number);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[5653] | 148 | public abstract void Randomize(IRandom random);
|
---|
| 149 | public abstract void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator);
|
---|
| 150 | public abstract void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover);
|
---|
| 151 | public abstract double CalculateSimilarity(IOptimizable optimizable);
|
---|
| 152 | public abstract string ParameterInfoString { get; }
|
---|
| 153 | public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix);
|
---|
| 154 | public abstract List<IOptimizable> GetAllOptimizables();
|
---|
[5112] | 155 | }
|
---|
| 156 | }
|
---|