Changeset 5653 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations
- Timestamp:
- 03/10/11 10:44:42 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
- Property svn:ignore
-
old new 3 3 obj 4 4 HeuristicLabProblemsMetaOptimizationPlugin.cs 5 *.vs10x
-
- Property svn:ignore
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/NullValueConfiguration.cs
r5112 r5653 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 1 using System.Collections.Generic; 2 using HeuristicLab.Common; 5 3 using HeuristicLab.Core; 6 4 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 7 using HeuristicLab.Common;8 5 9 6 namespace HeuristicLab.Problems.MetaOptimization { … … 12 9 13 10 public NullValueConfiguration() { 14 this.ParameterConfigurations = new ItemList<IParameterConfiguration>();15 11 this.ActualValue = new ConstrainedValue(null, null, null, true); 16 12 this.IsOptimizable = false; … … 28 24 return "null"; 29 25 } 26 27 public override void Randomize(IRandom random) { } 28 29 public override void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { } 30 31 public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) { } 32 33 public override double CalculateSimilarity(IOptimizable optimizable) { 34 return 1.0; 35 } 36 37 public override string ParameterInfoString { 38 get { return "null"; } 39 } 40 41 public override void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) { } 42 43 public override List<IOptimizable> GetAllOptimizables() { 44 return new List<IOptimizable>(); 45 } 30 46 } 31 47 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encoding/ValueConfigurations/ValueConfiguration.cs
r5522 r5653 1 1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 2 4 using System.Linq; 3 5 using HeuristicLab.Common; 4 6 using HeuristicLab.Core; 5 7 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 6 using HeuristicLab.Data;7 using System.Drawing;8 using HeuristicLab.Encodings.RealVectorEncoding;9 using HeuristicLab.Encodings.IntegerVectorEncoding;10 8 using HeuristicLab.PluginInfrastructure; 11 using System.Collections.Generic;12 using System.Text;13 9 14 10 namespace HeuristicLab.Problems.MetaOptimization { 15 11 // TODO: ItemName/Descr, storability 16 12 [StorableClass] 17 public class ValueConfiguration : NamedItem, IValueConfiguration {13 public abstract class ValueConfiguration : NamedItem, IValueConfiguration { 18 14 public override bool CanChangeName { 19 15 get { return true; } … … 38 34 [Storable] 39 35 protected bool optimize; 40 public bool Optimize {36 public virtual bool Optimize { 41 37 get { return optimize; } 42 38 set { 43 39 if (optimize != value) { 44 40 optimize = value; 45 if (optimize) {46 ClearParameterConfigurations();47 if (this.actualValue.Value is IParameterizedNamedItem) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem);48 } else {49 ClearParameterConfigurations();50 }51 41 OnOptimizeChanged(); 52 42 OnToStringChanged(); 53 43 } 54 44 } 55 }56 57 [Storable]58 protected IItemCollection<IParameterConfiguration> parameterConfigurations = new ItemCollection<IParameterConfiguration>();59 public IItemCollection<IParameterConfiguration> ParameterConfigurations {60 get { return new ReadOnlyItemCollection<IParameterConfiguration>(this.parameterConfigurations); }61 protected set { this.parameterConfigurations = value; }62 45 } 63 46 … … 69 52 if (this.actualValue != value) { 70 53 DeregisterActualValueEvents(); 71 ClearParameterConfigurations();72 54 this.actualValue = value; 73 if (this.actualValue.Value is IParameterizedNamedItem) PopulateParameterConfigurations(this.actualValue.Value as IParameterizedNamedItem);74 55 OnValueChanged(); 75 56 OnToStringChanged(); 76 RegisterActualValueEvents();77 }78 }79 }80 81 [Storable]82 protected IRange rangeConstraint;83 public IRange RangeConstraint {84 get { return rangeConstraint; }85 private set {86 if (rangeConstraint != value) {87 DeregisterRangeConstraintEvents();88 rangeConstraint = value;89 57 RegisterActualValueEvents(); 90 58 } … … 106 74 #region Constructors and Cloning 107 75 public ValueConfiguration(IItem value, Type valueDataType) { 108 this.ParameterConfigurations = new ItemList<IParameterConfiguration>();109 76 var validTypes = ApplicationManager.Manager.GetTypes(valueDataType).OrderBy(x => x.Name).ToArray(); 110 77 this.ActualValue = new ConstrainedValue(value, valueDataType, new ItemSet<IItem> { value }, false); 111 78 this.IsOptimizable = true; 112 if (actualValue.ValueDataType == typeof(IntValue)) {113 RangeConstraint = new IntValueRange(new IntValue(0), (IntValue)value, new IntValue(1));114 } else if (actualValue.ValueDataType == typeof(DoubleValue)) {115 RangeConstraint = new DoubleValueRange(new DoubleValue(0), (DoubleValue)value, new DoubleValue(0.01));116 } else if (actualValue.ValueDataType == typeof(PercentValue)) {117 RangeConstraint = new PercentValueRange(new PercentValue(0), new PercentValue(1), new PercentValue(0.01));118 } else if (actualValue.ValueDataType == typeof(BoolValue)) {119 this.IsOptimizable = false; // there is nothing to configure for bools120 } else {121 RangeConstraint = null;122 }123 79 } 124 80 … … 128 84 protected ValueConfiguration(ValueConfiguration original, Cloner cloner) 129 85 : base(original, cloner) { 130 this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations);131 86 this.actualValue = cloner.Clone(original.ActualValue); 132 this.rangeConstraint = cloner.Clone(original.RangeConstraint);133 87 this.isOptimizable = original.isOptimizable; 134 88 this.optimize = original.optimize; 135 89 this.number = original.number; 136 90 RegisterActualValueEvents(); 137 RegisterRangeConstraintEvents();138 }139 public override IDeepCloneable Clone(Cloner cloner) {140 return new ValueConfiguration(this, cloner);141 }142 [StorableHook(HookType.AfterDeserialization)]143 private void AfterDeserialization() {144 RegisterRangeConstraintEvents();145 91 } 146 92 #endregion 147 93 148 protected virtual void PopulateParameterConfigurations(IParameterizedNamedItem parameterizedItem) {149 foreach (var childParameter in parameterizedItem.Parameters) {150 var pc = ParameterConfiguration.Create(parameterizedItem, childParameter);151 if (pc != null) this.parameterConfigurations.Add(pc);152 }153 }154 protected virtual void ClearParameterConfigurations() {155 parameterConfigurations.Clear();156 }157 158 private void RegisterRangeConstraintEvents() {159 if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged += new EventHandler(RangeConstraint_ToStringChanged);160 }161 private void DeregisterRangeConstraintEvents() {162 if (this.RangeConstraint != null) this.RangeConstraint.ToStringChanged -= new EventHandler(RangeConstraint_ToStringChanged);163 }164 94 private void RegisterActualValueEvents() { 165 95 if (this.actualValue != null) this.actualValue.ToStringChanged += new EventHandler(actualValue_ToStringChanged); … … 170 100 171 101 #region Events 172 void actualValue_ToStringChanged(object sender, EventArgs e) { 173 OnToStringChanged(); 174 } 175 void RangeConstraint_ToStringChanged(object sender, EventArgs e) { 102 private void actualValue_ToStringChanged(object sender, EventArgs e) { 176 103 OnToStringChanged(); 177 104 } … … 212 139 #endregion 213 140 214 public override string ToString() {215 if (ActualValue != null && ActualValue.Value != null) {216 string name = NumberedName;217 218 if (ActualValue.Value is IParameterizedItem) {219 if (Optimize) {220 return string.Format("{0} (Optimize)", name);221 } else {222 return string.Format("{0}", name);223 }224 } else {225 if (Optimize) {226 return string.Format("{0} (Optimize: {1})", name, RangeConstraint);227 } else {228 return string.Format("{0}: {1}", name, ActualValue.Value);229 }230 }231 } else {232 return base.ToString();233 }234 }235 236 141 public string NumberedName { 237 142 get { … … 244 149 } 245 150 246 public string ParameterInfoString { 247 get { 248 StringBuilder sb = new StringBuilder(); 249 if (this.Optimize && this.ParameterConfigurations.Count > 0) { 250 var parameterInfos = new List<string>(); 251 foreach (var pc in this.ParameterConfigurations) { 252 if (pc.Optimize) parameterInfos.Add(pc.ParameterInfoString); 253 } 254 sb.Append(string.Join(", ", parameterInfos.ToArray())); 255 } 256 return sb.ToString(); 257 } 258 } 259 260 public virtual void Parameterize(IParameterizedItem item) { 261 foreach (IParameterConfiguration pc in this.ParameterConfigurations) { 262 pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]); 263 } 264 } 265 266 public void Randomize(IRandom random) { 267 if (Optimize) { 268 if (rangeConstraint != null) { 269 this.actualValue.Value = rangeConstraint.GetRandomValue(random); 270 } else { 271 foreach (IParameterConfiguration pc in this.ParameterConfigurations) { 272 pc.Randomize(random); 273 } 274 } 275 } 276 } 277 278 public void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { 279 if (Optimize) { 280 if (rangeConstraint != null) { 281 mutate(random, this, intValueManipulator, doubleValueManipulator); 282 } else { 283 foreach (IParameterConfiguration pc in this.ParameterConfigurations) { 284 pc.Mutate(random, mutate, intValueManipulator, doubleValueManipulator); 285 } 286 } 287 } 288 } 289 290 public void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) { 291 if (Optimize) { 292 if (rangeConstraint != null) { 293 cross(random, this, other, intValueCrossover, doubleValueCrossover); 294 } else { 295 IValueConfiguration otherVc = (IValueConfiguration)other; 296 for (int i = 0; i < this.ParameterConfigurations.Count; i++) { 297 this.ParameterConfigurations.ElementAt(i).Cross(random, otherVc.ParameterConfigurations.ElementAt(i), cross, intValueCrossover, doubleValueCrossover); 298 } 299 } 300 } 301 } 302 303 public List<IOptimizable> GetAllOptimizables() { 304 var list = new List<IOptimizable>(); 305 foreach (var pc in ParameterConfigurations) { 306 if (pc.Optimize) { 307 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 308 list.AddRange(pc.GetAllOptimizables()); 309 } 310 } 311 return list; 312 } 313 314 public void CollectOptimizedParameterNames(List<string> parameterNames, string prefix) { 315 foreach (var pc in ParameterConfigurations) { 316 if (pc.Optimize) { 317 parameterNames.Add(prefix + pc.ParameterName); 318 pc.CollectOptimizedParameterNames(parameterNames, prefix + pc.ParameterName + "."); 319 } 320 } 321 } 322 323 public IEnumerable<string> GetOptimizedParameterNames() { 324 var list = new List<string>(); 325 this.CollectOptimizedParameterNames(list, ""); 326 return list; 327 } 328 329 public double CalculateSimilarity(IOptimizable optimizable) { 330 var other = (IValueConfiguration)optimizable; 331 if (rangeConstraint != null) { 332 return this.RangeConstraint.CalculateSimilarity(this.ActualValue.Value, other.ActualValue.Value); 333 } else { 334 double sum = 0; 335 int count = 0; 336 for (int i = 0; i < ParameterConfigurations.Count; i++) { 337 if (this.ParameterConfigurations.ElementAt(i).Optimize) { 338 sum += this.ParameterConfigurations.ElementAt(i).CalculateSimilarity(other.ParameterConfigurations.ElementAt(i)); 339 count++; 340 } 341 } 342 return count == 0 ? 1.0 : sum / (double)count; 343 } 344 } 151 public abstract void Randomize(IRandom random); 152 public abstract void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator); 153 public abstract void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover); 154 public abstract double CalculateSimilarity(IOptimizable optimizable); 155 public abstract string ParameterInfoString { get; } 156 public abstract void CollectOptimizedParameterNames(List<string> parameterNames, string prefix); 157 public abstract List<IOptimizable> GetAllOptimizables(); 345 158 } 346 159 }
Note: See TracChangeset
for help on using the changeset viewer.