[5653] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Common;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
[16574] | 7 | using HEAL.Attic;
|
---|
[5653] | 8 |
|
---|
| 9 | namespace HeuristicLab.Problems.MetaOptimization {
|
---|
[16574] | 10 | [StorableType("83FF6734-0E00-4072-A8F0-9009772E5C04")]
|
---|
[5653] | 11 | public class ParameterizedValueConfiguration : ValueConfiguration {
|
---|
| 12 | [Storable]
|
---|
| 13 | protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();
|
---|
[5665] | 14 | public virtual IItemCollection<IParameterConfiguration> ParameterConfigurations {
|
---|
[5653] | 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 {
|
---|
[5927] | 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 | }
|
---|
[5653] | 30 | }
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | public override ConstrainedValue ActualValue {
|
---|
| 35 | get { return base.ActualValue; }
|
---|
| 36 | set {
|
---|
| 37 | ClearParameterConfigurations();
|
---|
| 38 | base.ActualValue = value;
|
---|
[5927] | 39 | if (this.Optimize && this.actualValue.Value != null) PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues);
|
---|
[5653] | 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[5665] | 43 | [Storable]
|
---|
| 44 | protected bool discoverValidValues;
|
---|
| 45 | public bool DiscoverValidValues {
|
---|
| 46 | get { return discoverValidValues; }
|
---|
| 47 | set { discoverValidValues = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[6489] | 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 |
|
---|
[5653] | 61 | #region Constructors and Cloning
|
---|
| 62 | [StorableConstructor]
|
---|
[16574] | 63 | protected ParameterizedValueConfiguration(StorableConstructorFlag _) : base(_) { }
|
---|
[9827] | 64 | protected ParameterizedValueConfiguration()
|
---|
[9823] | 65 | : base() {
|
---|
[9827] | 66 | this.ParameterConfigurations = new ItemList<IParameterConfiguration>();
|
---|
[5653] | 67 | }
|
---|
[5665] | 68 | public ParameterizedValueConfiguration(IItem value, Type valueDataType, bool discoverValidValues)
|
---|
[5653] | 69 | : base(value, valueDataType) {
|
---|
[5665] | 70 | this.discoverValidValues = discoverValidValues;
|
---|
[5653] | 71 | }
|
---|
| 72 | protected ParameterizedValueConfiguration(ParameterizedValueConfiguration original, Cloner cloner)
|
---|
| 73 | : base(original, cloner) {
|
---|
[5665] | 74 | this.discoverValidValues = original.discoverValidValues;
|
---|
[5653] | 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 |
|
---|
[5927] | 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 | }
|
---|
[5665] | 91 | }
|
---|
[5653] | 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();
|
---|
[5665] | 102 | if ((this.Optimize) && this.ParameterConfigurations.Count > 0) {
|
---|
[5653] | 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 |
|
---|
[5927] | 145 | public virtual IEnumerable<string> GetOptimizedParameterNames() {
|
---|
[5653] | 146 | var list = new List<string>();
|
---|
[5927] | 147 | this.CollectOptimizedParameterNames(list, string.Empty);
|
---|
[5653] | 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 | }
|
---|