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