1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
9 | [StorableClass]
|
---|
10 | public class RangeValueConfiguration : ValueConfiguration {
|
---|
11 | [Storable]
|
---|
12 | protected IRange rangeConstraint;
|
---|
13 | public IRange RangeConstraint {
|
---|
14 | get { return rangeConstraint; }
|
---|
15 | private set {
|
---|
16 | if (rangeConstraint != value) {
|
---|
17 | DeregisterRangeConstraintEvents();
|
---|
18 | rangeConstraint = value;
|
---|
19 | RegisterRangeConstraintEvents();
|
---|
20 | }
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | #region Constructor and Cloning
|
---|
25 | [StorableConstructor]
|
---|
26 | protected RangeValueConfiguration(bool deserializing) : base(deserializing) { }
|
---|
27 | public RangeValueConfiguration() {
|
---|
28 |
|
---|
29 | }
|
---|
30 | protected RangeValueConfiguration(RangeValueConfiguration original, Cloner cloner)
|
---|
31 | : base(original, cloner) {
|
---|
32 | this.rangeConstraint = cloner.Clone(original.RangeConstraint);
|
---|
33 | RegisterRangeConstraintEvents();
|
---|
34 | }
|
---|
35 | public RangeValueConfiguration(IItem value, Type valueDataType)
|
---|
36 | : base(value, valueDataType) {
|
---|
37 | if (actualValue.ValueDataType == typeof(IntValue)) {
|
---|
38 | RangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value.Clone(), new IntValue(1));
|
---|
39 | } else if (actualValue.ValueDataType == typeof(DoubleValue)) {
|
---|
40 | RangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value.Clone(), new DoubleValue(0.01));
|
---|
41 | } else if (actualValue.ValueDataType == typeof(PercentValue)) {
|
---|
42 | RangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.01));
|
---|
43 | } else if (actualValue.ValueDataType == typeof(BoolValue)) {
|
---|
44 | this.IsOptimizable = false; // there is nothing to configure for bools
|
---|
45 | } else {
|
---|
46 | RangeConstraint = null;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new RangeValueConfiguration(this, cloner);
|
---|
51 | }
|
---|
52 | [StorableHook(HookType.AfterDeserialization)]
|
---|
53 | private void AfterDeserialization() {
|
---|
54 | RegisterRangeConstraintEvents();
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | private void RegisterRangeConstraintEvents() {
|
---|
59 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
|
---|
60 | }
|
---|
61 | private void DeregisterRangeConstraintEvents() {
|
---|
62 | if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged -= new EventHandler(RangeConstraint_ToStringChanged);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override string ToString() {
|
---|
66 | if (ActualValue != null && ActualValue.Value != null) {
|
---|
67 | if (Optimize) {
|
---|
68 | return string.Format("{0} (Optimize: {1})", NumberedName, RangeConstraint);
|
---|
69 | } else {
|
---|
70 | return string.Format("{0}: {1}", NumberedName, ActualValue.Value);
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | return base.ToString();
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override void Randomize(IRandom random) {
|
---|
78 | this.actualValue.Value = rangeConstraint.GetRandomValue(random);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
|
---|
82 | mutate(random, this, intValueManipulator, doubleValueManipulator);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
|
---|
86 | cross(random, this, other, intValueCrossover, doubleValueCrossover);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override double CalculateSimilarity(IOptimizable optimizable) {
|
---|
90 | var other = (IValueConfiguration)optimizable;
|
---|
91 | return this.RangeConstraint.CalculateSimilarity(this.ActualValue.Value, other.ActualValue.Value);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override string ParameterInfoString {
|
---|
95 | get { return string.Empty; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) { }
|
---|
99 |
|
---|
100 | public override List<IOptimizable> GetAllOptimizables() {
|
---|
101 | return new List<IOptimizable>();
|
---|
102 | }
|
---|
103 |
|
---|
104 | #region Events
|
---|
105 | private void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
|
---|
106 | OnToStringChanged();
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 | }
|
---|
110 | }
|
---|