Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterConfigurationEncoding/HeuristicLab.Encodings.ParameterConfigurationEncoding/3.3/ValueConfigurations/ParameterizedValueConfiguration.cs @ 13847

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

#1853:

  • enhanced combinations count calculation
  • restructured code
  • minor code improvements
  • added license information
File size: 7.3 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 System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.ParameterConfigurationEncoding {
31  [StorableClass]
32  public class ParameterizedValueConfiguration : ValueConfiguration {
33    [Storable]
34    protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
35    public virtual IItemCollection<IParameterConfiguration> ParameterConfigurations {
36      get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }
37      protected set { this.parameterConfigurations = value; }
38    }
39
40    public override bool Optimize {
41      get { return base.Optimize; }
42      set {
43        if (this.optimize != value) {
44          base.Optimize = value;
45          if (optimize) {
46            ClearParameterConfigurations();
47            PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues);
48          } else {
49            ClearParameterConfigurations();
50          }
51        }
52      }
53    }
54
55    public override ConstrainedValue ActualValue {
56      get { return base.ActualValue; }
57      set {
58        ClearParameterConfigurations();
59        base.ActualValue = value;
60        if (this.Optimize && this.actualValue.Value != null) PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues);
61      }
62    }
63
64    [Storable]
65    protected bool discoverValidValues;
66    public bool DiscoverValidValues {
67      get { return discoverValidValues; }
68      set { discoverValidValues = value; }
69    }
70
71    public override bool ValuesReadOnly {
72      set {
73        if (value != this.valuesReadOnly) {
74          this.valuesReadOnly = value;
75          foreach (var pc in this.parameterConfigurations) {
76            pc.ValuesReadOnly = value;
77          }
78        }
79      }
80    }
81
82    #region Constructors and Cloning
83    [StorableConstructor]
84    protected ParameterizedValueConfiguration(bool deserializing) : base(deserializing) { }
85    protected ParameterizedValueConfiguration(ParameterizedValueConfiguration original, Cloner cloner)
86      : base(original, cloner) {
87      this.discoverValidValues = original.discoverValidValues;
88      this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations);
89    }
90    public ParameterizedValueConfiguration()
91      : base() {
92      this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
93    }
94    public ParameterizedValueConfiguration(IItem value, Type valueDataType, bool discoverValidValues)
95      : base(value, valueDataType) {
96      this.discoverValidValues = discoverValidValues;
97    }
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new ParameterizedValueConfiguration(this, cloner);
100    }
101    #endregion
102
103    protected virtual void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
104      var parameterizedItem = item as IParameterizedItem;
105      if (parameterizedItem != null) {
106        foreach (var childParameter in parameterizedItem.Parameters) {
107          IValueParameter valueParameter = childParameter as IValueParameter;
108          if (valueParameter != null) {
109            var pc = new ParameterConfiguration(valueParameter.Name, valueParameter, discoverValidValues);
110            pc.CombinationsCountChanged += (sender, args) => OnCombinationsCountChanged();
111            this.parameterConfigurations.Add(pc);
112          }
113        }
114      }
115    }
116
117    protected virtual void ClearParameterConfigurations() {
118      parameterConfigurations.Clear();
119    }
120
121    public override string ParameterInfoString {
122      get {
123        StringBuilder sb = new StringBuilder();
124        if ((this.Optimize) && this.ParameterConfigurations.Count > 0) {
125          var parameterInfos = new List<string>();
126          foreach (var pc in this.ParameterConfigurations) {
127            if (pc.Optimize) parameterInfos.Add(pc.ParameterInfoString);
128          }
129          sb.Append(string.Join(", ", parameterInfos.ToArray()));
130        }
131        return sb.ToString();
132      }
133    }
134
135    public override string ToString() {
136      if (ActualValue != null && ActualValue.Value != null) {
137        if (Optimize) {
138          return string.Format("{0} (Optimize)", NumberedName);
139        } else {
140          return string.Format("{0}", NumberedName);
141        }
142      } else {
143        return base.ToString();
144      }
145    }
146
147    public override List<IOptimizable> GetAllOptimizables() {
148      var list = new List<IOptimizable>();
149      foreach (var pc in ParameterConfigurations) {
150        if (pc.Optimize) {
151          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
152          list.AddRange(pc.GetAllOptimizables());
153        }
154      }
155      return list;
156    }
157
158    public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
159      foreach (var pc in ParameterConfigurations) {
160        if (pc.Optimize) {
161          parameterNames.Add(prefix + pc.ParameterName);
162          pc.CollectOptimizedParameterNames(parameterNames, prefix + pc.ParameterName + ".");
163        }
164      }
165    }
166
167    public virtual IEnumerable<string> GetOptimizedParameterNames() {
168      var list = new List<string>();
169      this.CollectOptimizedParameterNames(list, string.Empty);
170      return list;
171    }
172
173    public virtual void Parameterize(IParameterizedItem item) {
174      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
175        pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
176      }
177    }
178
179    public override void Randomize(IRandom random) {
180      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
181        pc.Randomize(random);
182      }
183    }
184
185    public override double CalculateSimilarity(IOptimizable optimizable) {
186      var other = (ParameterizedValueConfiguration)optimizable;
187      double sum = 0;
188      int count = 0;
189      for (int i = 0; i < ParameterConfigurations.Count; i++) {
190        if (this.ParameterConfigurations.ElementAt(i).Optimize) {
191          sum += this.ParameterConfigurations.ElementAt(i).CalculateSimilarity(other.ParameterConfigurations.ElementAt(i));
192          count++;
193        }
194      }
195      return count == 0 ? 1.0 : sum / (double)count;
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.