Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisService/HeuristicLab.Encodings.ParameterConfigurationTreeEncoding/3.3/ValueConfigurations/ParameterizedValueConfiguration.cs @ 7908

Last change on this file since 7908 was 7840, checked in by spimming, 13 years ago

#1853:

  • included files from metaopt branch
  • ignored mutation and crossover
  • updated plugin frame
File size: 7.2 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.ParameterConfigurationTreeEncoding {
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    public ParameterizedValueConfiguration() {
86      this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
87    }
88    public ParameterizedValueConfiguration(IItem value, Type valueDataType, bool discoverValidValues)
89      : base(value, valueDataType) {
90      this.discoverValidValues = discoverValidValues;
91    }
92    protected ParameterizedValueConfiguration(ParameterizedValueConfiguration original, Cloner cloner)
93      : base(original, cloner) {
94      this.discoverValidValues = original.discoverValidValues;
95      this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations);
96    }
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new ParameterizedValueConfiguration(this, cloner);
99    }
100    #endregion
101
102    protected virtual void PopulateParameterConfigurations(IItem item, bool discoverValidValues) {
103      var parameterizedItem = item as IParameterizedItem;
104      if (parameterizedItem != null) {
105        foreach (var childParameter in parameterizedItem.Parameters) {
106          IValueParameter valueParameter = childParameter as IValueParameter;
107          if (valueParameter != null) {
108            var pc = new ParameterConfiguration(valueParameter.Name, valueParameter, discoverValidValues);
109            this.parameterConfigurations.Add(pc);
110          }
111        }
112      }
113    }
114
115    protected virtual void ClearParameterConfigurations() {
116      parameterConfigurations.Clear();
117    }
118
119    public override string ParameterInfoString {
120      get {
121        StringBuilder sb = new StringBuilder();
122        if ((this.Optimize) && this.ParameterConfigurations.Count > 0) {
123          var parameterInfos = new List<string>();
124          foreach (var pc in this.ParameterConfigurations) {
125            if (pc.Optimize) parameterInfos.Add(pc.ParameterInfoString);
126          }
127          sb.Append(string.Join(", ", parameterInfos.ToArray()));
128        }
129        return sb.ToString();
130      }
131    }
132
133    public override string ToString() {
134      if (ActualValue != null && ActualValue.Value != null) {
135        if (Optimize) {
136          return string.Format("{0} (Optimize)", NumberedName);
137        } else {
138          return string.Format("{0}", NumberedName);
139        }
140      } else {
141        return base.ToString();
142      }
143    }
144
145    public override List<IOptimizable> GetAllOptimizables() {
146      var list = new List<IOptimizable>();
147      foreach (var pc in ParameterConfigurations) {
148        if (pc.Optimize) {
149          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
150          list.AddRange(pc.GetAllOptimizables());
151        }
152      }
153      return list;
154    }
155
156    public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) {
157      foreach (var pc in ParameterConfigurations) {
158        if (pc.Optimize) {
159          parameterNames.Add(prefix + pc.ParameterName);
160          pc.CollectOptimizedParameterNames(parameterNames, prefix + pc.ParameterName + ".");
161        }
162      }
163    }
164
165    public virtual IEnumerable<string> GetOptimizedParameterNames() {
166      var list = new List<string>();
167      this.CollectOptimizedParameterNames(list, string.Empty);
168      return list;
169    }
170
171    public virtual void Parameterize(IParameterizedItem item) {
172      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
173        pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]);
174      }
175    }
176
177    public override void Randomize(IRandom random) {
178      foreach (IParameterConfiguration pc in this.ParameterConfigurations) {
179        pc.Randomize(random);
180      }
181    }
182
183    public override double CalculateSimilarity(IOptimizable optimizable) {
184      var other = (ParameterizedValueConfiguration)optimizable;
185      double sum = 0;
186      int count = 0;
187      for (int i = 0; i < ParameterConfigurations.Count; i++) {
188        if (this.ParameterConfigurations.ElementAt(i).Optimize) {
189          sum += this.ParameterConfigurations.ElementAt(i).CalculateSimilarity(other.ParameterConfigurations.ElementAt(i));
190          count++;
191        }
192      }
193      return count == 0 ? 1.0 : sum / (double)count;
194    }
195  }
196}
Note: See TracBrowser for help on using the repository browser.