Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/ValueConfigurations/RangeValueConfiguration.cs @ 16310

Last change on this file since 16310 was 8535, checked in by jkarder, 12 years ago

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
30  [StorableClass]
31  public class RangeValueConfiguration : ValueConfiguration {
32    [Storable]
33    protected IRange rangeConstraint;
34    public IRange RangeConstraint {
35      get { return rangeConstraint; }
36      private set {
37        if (rangeConstraint != value) {
38          DeregisterRangeConstraintEvents();
39          rangeConstraint = value;
40          RegisterRangeConstraintEvents();
41        }
42      }
43    }
44
45    #region Constructor and Cloning
46    [StorableConstructor]
47    protected RangeValueConfiguration(bool deserializing) : base(deserializing) { }
48    protected RangeValueConfiguration(RangeValueConfiguration original, Cloner cloner)
49      : base(original, cloner) {
50      this.rangeConstraint = cloner.Clone(original.RangeConstraint);
51      RegisterRangeConstraintEvents();
52    }
53    public RangeValueConfiguration() : base() { }
54    public RangeValueConfiguration(IItem value, Type valueDataType)
55      : base(value, valueDataType) {
56      if (actualValue.ValueDataType == typeof(IntValue)) {
57        RangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value.Clone(), new IntValue(1));
58      } else if (actualValue.ValueDataType == typeof(DoubleValue)) {
59        RangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value.Clone(), new DoubleValue(0.01));
60      } else if (actualValue.ValueDataType == typeof(PercentValue)) {
61        RangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.01));
62      } else if (actualValue.ValueDataType == typeof(BoolValue)) {
63        this.IsOptimizable = false; // there is nothing to configure for bools
64      } else {
65        RangeConstraint = null;
66      }
67    }
68    [StorableHook(HookType.AfterDeserialization)]
69    private void AfterDeserialization() {
70      RegisterRangeConstraintEvents();
71    }
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new RangeValueConfiguration(this, cloner);
74    }
75    #endregion
76
77    private void RegisterRangeConstraintEvents() {
78      if (this.RangeConstraint != null) {
79        this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
80        this.RangeConstraint.LowerBoundChanged += new EventHandler(RangeConstraintParametersChanged);
81        this.RangeConstraint.UpperBoundChanged += new EventHandler(RangeConstraintParametersChanged);
82        this.RangeConstraint.StepSizeChanged += new EventHandler(RangeConstraintParametersChanged);
83      }
84    }
85
86    private void DeregisterRangeConstraintEvents() {
87      if (this.RangeConstraint != null) {
88        this.RangeConstraint.ToStringChanged -= new EventHandler(RangeConstraint_ToStringChanged);
89        this.RangeConstraint.LowerBoundChanged -= new EventHandler(RangeConstraintParametersChanged);
90        this.RangeConstraint.UpperBoundChanged -= new EventHandler(RangeConstraintParametersChanged);
91        this.RangeConstraint.StepSizeChanged -= new EventHandler(RangeConstraintParametersChanged);
92      }
93    }
94
95    public override string ToString() {
96      if (ActualValue != null && ActualValue.Value != null) {
97        if (Optimize) {
98          return string.Format("{0} (Optimize: {1})", NumberedName, RangeConstraint);
99        } else {
100          return string.Format("{0}: {1}", NumberedName, ActualValue.Value);
101        }
102      } else {
103        return base.ToString();
104      }
105    }
106
107    public override void Randomize(IRandom random) {
108      this.actualValue.Value = rangeConstraint.GetRandomValue(random);
109    }
110
111    public override double CalculateSimilarity(IOptimizable optimizable) {
112      var other = (IValueConfiguration)optimizable;
113      return this.RangeConstraint.CalculateSimilarity(this.ActualValue.Value, other.ActualValue.Value);
114    }
115
116    public override string ParameterInfoString {
117      get { return string.Empty; }
118    }
119
120    public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) { }
121
122    public override List<IOptimizable> GetAllOptimizables() {
123      return new List<IOptimizable>();
124    }
125
126    #region Events
127    private void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
128      OnToStringChanged();
129    }
130    private void RangeConstraintParametersChanged(object sender, EventArgs e) {
131      OnCombinationsCountChanged();
132    }
133    #endregion
134  }
135}
Note: See TracBrowser for help on using the repository browser.