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