Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ParameterizedValueConfiguration.cs @ 15740

Last change on this file since 15740 was 9827, checked in by jkarder, 11 years ago

#1099:

  • adapted MetaOpt NamedItems once more
    • gave more meaningful names to some items
    • removed unreferenced default ctors
    • changed access modifiers
File size: 7.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  [StorableClass]
11  public class ParameterizedValueConfiguration : ValueConfiguration {
12    [Storable]
13    protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
14    public virtual IItemCollection<IParameterConfiguration> ParameterConfigurations {
15      get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }
16      protected set { this.parameterConfigurations = value; }
17    }
18
19    public override bool Optimize {
20      get { return base.Optimize; }
21      set {
22        if (this.optimize != value) {
23          base.Optimize = value;
24          if (optimize) {
25            ClearParameterConfigurations();
26            PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues);
27          } else {
28            ClearParameterConfigurations();
29          }
30        }
31      }
32    }
33
34    public override ConstrainedValue ActualValue {
35      get { return base.ActualValue; }
36      set {
37        ClearParameterConfigurations();
38        base.ActualValue = value;
39        if (this.Optimize && this.actualValue.Value != null) PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues);
40      }
41    }
42
43    [Storable]
44    protected bool discoverValidValues;
45    public bool DiscoverValidValues {
46      get { return discoverValidValues; }
47      set { discoverValidValues = value; }
48    }
49
50    public override bool ValuesReadOnly {
51      set {
52        if (value != this.valuesReadOnly) {
53          this.valuesReadOnly = value;
54          foreach (var pc in this.parameterConfigurations) {
55            pc.ValuesReadOnly = value;
56          }
57        }
58      }
59    }
60
61    #region Constructors and Cloning
62    [StorableConstructor]
63    protected ParameterizedValueConfiguration(bool deserializing) : base(deserializing) { }
64    protected ParameterizedValueConfiguration()
65      : base() {
66      this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
67    }
68    public ParameterizedValueConfiguration(IItem value, Type valueDataType, bool discoverValidValues)
69      : base(value, valueDataType) {
70      this.discoverValidValues = discoverValidValues;
71    }
72    protected ParameterizedValueConfiguration(ParameterizedValueConfiguration original, Cloner cloner)
73      : base(original, cloner) {
74      this.discoverValidValues = original.discoverValidValues;
75      this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations);
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new ParameterizedValueConfiguration(this, cloner);
79    }
80    #endregion
81
82    protected virtual void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
83      var parameterizedItem = item as IParameterizedItem;
84      if (parameterizedItem != null) {
85        foreach (var childParameter in parameterizedItem.Parameters) {
86          IValueParameter valueParameter = childParameter as IValueParameter;
87          if (valueParameter != null) {
88            var pc = new ParameterConfiguration(valueParameter.Name, valueParameter, discoverValidValues);
89            this.parameterConfigurations.Add(pc);
90          }
91        }
92      }
93    }
94
95    protected virtual void ClearParameterConfigurations() {
96      parameterConfigurations.Clear();
97    }
98
99    public override string ParameterInfoString {
100      get {
101        StringBuilder sb = new StringBuilder();
102        if ((this.Optimize) && this.ParameterConfigurations.Count > 0) {
103          var parameterInfos = new List<string>();
104          foreach (var pc in this.ParameterConfigurations) {
105            if (pc.Optimize) parameterInfos.Add(pc.ParameterInfoString);
106          }
107          sb.Append(string.Join(", ", parameterInfos.ToArray()));
108        }
109        return sb.ToString();
110      }
111    }
112
113    public override string ToString() {
114      if (ActualValue != null && ActualValue.Value != null) {
115        if (Optimize) {
116          return string.Format("{0} (Optimize)", NumberedName);
117        } else {
118          return string.Format("{0}", NumberedName);
119        }
120      } else {
121        return base.ToString();
122      }
123    }
124
125    public override List<IOptimizable> GetAllOptimizables() {
126      var list = new List<IOptimizable>();
127      foreach (var pc in ParameterConfigurations) {
128        if (pc.Optimize) {
129          if (pc.ValueConfigurations.CheckedItems.Count() > 1) list.Add(pc); // only add if there are more than 1 choices. otherwise it makes no sense to optimize which VC is selected
130          list.AddRange(pc.GetAllOptimizables());
131        }
132      }
133      return list;
134    }
135
136    public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
137      foreach (var pc in ParameterConfigurations) {
138        if (pc.Optimize) {
139          parameterNames.Add(prefix + pc.ParameterName);
140          pc.CollectOptimizedParameterNames(parameterNames, prefix + pc.ParameterName + ".");
141        }
142      }
143    }
144
145    public virtual IEnumerable<string> GetOptimizedParameterNames() {
146      var list = new List<string>();
147      this.CollectOptimizedParameterNames(list, string.Empty);
148      return list;
149    }
150
151    public virtual void Parameterize(IParameterizedItem item) {
152      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
153        pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
154      }
155    }
156
157    public override void Randomize(IRandom random) {
158      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
159        pc.Randomize(random);
160      }
161    }
162
163    public override void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) {
164      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
165        pc.Mutate(random, mutate, intValueManipulator, doubleValueManipulator);
166      }
167    }
168
169    public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) {
170      var otherVc = (ParameterizedValueConfiguration)other;
171      for (int i = 0; i < this.ParameterConfigurations.Count; i++) {
172        this.ParameterConfigurations.ElementAt(i).Cross(random, otherVc.ParameterConfigurations.ElementAt(i), cross, intValueCrossover, doubleValueCrossover);
173      }
174    }
175
176    public override double CalculateSimilarity(IOptimizable optimizable) {
177      var other = (ParameterizedValueConfiguration)optimizable;
178      double sum = 0;
179      int count = 0;
180      for (int i = 0; i < ParameterConfigurations.Count; i++) {
181        if (this.ParameterConfigurations.ElementAt(i).Optimize) {
182          sum += this.ParameterConfigurations.ElementAt(i).CalculateSimilarity(other.ParameterConfigurations.ElementAt(i));
183          count++;
184        }
185      }
186      return count == 0 ? 1.0 : sum / (double)count;
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.