Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/ValueConfigurations/RangeValueConfiguration.cs @ 10216

Last change on this file since 10216 was 7911, checked in by spimming, 13 years ago

#1853:

  • allow to set range constraints
  • provide double value factor range
File size: 4.6 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.ParameterConfigurationTreeEncoding {
30  [StorableClass]
31  public class RangeValueConfiguration : ValueConfiguration {
32    [Storable]
33    protected IRange rangeConstraint;
34    public IRange RangeConstraint {
35      get { return rangeConstraint; }
36      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    public RangeValueConfiguration() {
49
50    }
51    protected RangeValueConfiguration(RangeValueConfiguration original, Cloner cloner)
52      : base(original, cloner) {
53      this.rangeConstraint = cloner.Clone(original.RangeConstraint);
54      RegisterRangeConstraintEvents();
55    }
56    public RangeValueConfiguration(IItem value, Type valueDataType)
57      : base(value, valueDataType) {
58      if (actualValue.ValueDataType == typeof(IntValue)) {
59        RangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value.Clone(), new IntValue(1));
60      } else if (actualValue.ValueDataType == typeof(DoubleValue)) {
61        RangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value.Clone(), new DoubleValue(0.01));
62      } else if (actualValue.ValueDataType == typeof(PercentValue)) {
63        RangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.01));
64      } else if (actualValue.ValueDataType == typeof(BoolValue)) {
65        this.IsOptimizable = false; // there is nothing to configure for bools
66      } else {
67        RangeConstraint = null;
68      }
69    }
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new RangeValueConfiguration(this, cloner);
72    }
73    [StorableHook(HookType.AfterDeserialization)]
74    private void AfterDeserialization() {
75      RegisterRangeConstraintEvents();
76    }
77    #endregion
78
79    private void RegisterRangeConstraintEvents() {
80      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);
81    }
82    private void DeregisterRangeConstraintEvents() {
83      if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged -= new EventHandler(RangeConstraint_ToStringChanged);
84    }
85
86    public override string ToString() {
87      if (ActualValue != null && ActualValue.Value != null) {
88        if (Optimize) {
89          return string.Format("{0} (Optimize: {1})", NumberedName, RangeConstraint);
90        } else {
91          return string.Format("{0}: {1}", NumberedName, ActualValue.Value);
92        }
93      } else {
94        return base.ToString();
95      }
96    }
97
98    public override void Randomize(IRandom random) {
99      this.actualValue.Value = rangeConstraint.GetRandomValue(random);
100    }
101
102    public override double CalculateSimilarity(IOptimizable optimizable) {
103      var other = (IValueConfiguration)optimizable;
104      return this.RangeConstraint.CalculateSimilarity(this.ActualValue.Value, other.ActualValue.Value);
105    }
106
107    public override string ParameterInfoString {
108      get { return string.Empty; }
109    }
110
111    public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) { }
112
113    public override List<IOptimizable> GetAllOptimizables() {
114      return new List<IOptimizable>();
115    }
116
117    #region Events
118    private void RangeConstraint_ToStringChanged(object sender, EventArgs e) {
119      OnToStringChanged();
120    }
121    #endregion
122  }
123}
Note: See TracBrowser for help on using the repository browser.