using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Common; using HeuristicLab.Core; using HEAL.Attic; namespace HeuristicLab.Problems.MetaOptimization { [StorableType("83FF6734-0E00-4072-A8F0-9009772E5C04")] public class ParameterizedValueConfiguration : ValueConfiguration { [Storable] protected IItemCollection parameterConfigurations = new ItemCollection(); public virtual IItemCollection ParameterConfigurations { get { return new ReadOnlyItemCollection(this.parameterConfigurations); } protected set { this.parameterConfigurations = value; } } public override bool Optimize { get { return base.Optimize; } set { if (this.optimize != value) { base.Optimize = value; if (optimize) { ClearParameterConfigurations(); PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues); } else { ClearParameterConfigurations(); } } } } public override ConstrainedValue ActualValue { get { return base.ActualValue; } set { ClearParameterConfigurations(); base.ActualValue = value; if (this.Optimize && this.actualValue.Value != null) PopulateParameterConfigurations(this.actualValue.Value, this.discoverValidValues); } } [Storable] protected bool discoverValidValues; public bool DiscoverValidValues { get { return discoverValidValues; } set { discoverValidValues = value; } } public override bool ValuesReadOnly { set { if (value != this.valuesReadOnly) { this.valuesReadOnly = value; foreach (var pc in this.parameterConfigurations) { pc.ValuesReadOnly = value; } } } } #region Constructors and Cloning [StorableConstructor] protected ParameterizedValueConfiguration(StorableConstructorFlag _) : base(_) { } protected ParameterizedValueConfiguration() : base() { this.ParameterConfigurations = new ItemList(); } public ParameterizedValueConfiguration(IItem value, Type valueDataType, bool discoverValidValues) : base(value, valueDataType) { this.discoverValidValues = discoverValidValues; } protected ParameterizedValueConfiguration(ParameterizedValueConfiguration original, Cloner cloner) : base(original, cloner) { this.discoverValidValues = original.discoverValidValues; this.ParameterConfigurations = cloner.Clone(original.parameterConfigurations); } public override IDeepCloneable Clone(Cloner cloner) { return new ParameterizedValueConfiguration(this, cloner); } #endregion protected virtual void PopulateParameterConfigurations(IItem item, bool discoverValidValues) { var parameterizedItem = item as IParameterizedItem; if (parameterizedItem != null) { foreach (var childParameter in parameterizedItem.Parameters) { IValueParameter valueParameter = childParameter as IValueParameter; if (valueParameter != null) { var pc = new ParameterConfiguration(valueParameter.Name, valueParameter, discoverValidValues); this.parameterConfigurations.Add(pc); } } } } protected virtual void ClearParameterConfigurations() { parameterConfigurations.Clear(); } public override string ParameterInfoString { get { StringBuilder sb = new StringBuilder(); if ((this.Optimize) && this.ParameterConfigurations.Count > 0) { var parameterInfos = new List(); foreach (var pc in this.ParameterConfigurations) { if (pc.Optimize) parameterInfos.Add(pc.ParameterInfoString); } sb.Append(string.Join(", ", parameterInfos.ToArray())); } return sb.ToString(); } } public override string ToString() { if (ActualValue != null && ActualValue.Value != null) { if (Optimize) { return string.Format("{0} (Optimize)", NumberedName); } else { return string.Format("{0}", NumberedName); } } else { return base.ToString(); } } public override List GetAllOptimizables() { var list = new List(); foreach (var pc in ParameterConfigurations) { if (pc.Optimize) { 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 list.AddRange(pc.GetAllOptimizables()); } } return list; } public override void CollectOptimizedParameterNames(List parameterNames, string prefix) { foreach (var pc in ParameterConfigurations) { if (pc.Optimize) { parameterNames.Add(prefix + pc.ParameterName); pc.CollectOptimizedParameterNames(parameterNames, prefix + pc.ParameterName + "."); } } } public virtual IEnumerable GetOptimizedParameterNames() { var list = new List(); this.CollectOptimizedParameterNames(list, string.Empty); return list; } public virtual void Parameterize(IParameterizedItem item) { foreach (IParameterConfiguration pc in this.ParameterConfigurations) { pc.Parameterize((IValueParameter)item.Parameters[pc.ParameterName]); } } public override void Randomize(IRandom random) { foreach (IParameterConfiguration pc in this.ParameterConfigurations) { pc.Randomize(random); } } public override void Mutate(IRandom random, MutateDelegate mutate, IIntValueManipulator intValueManipulator, IDoubleValueManipulator doubleValueManipulator) { foreach (IParameterConfiguration pc in this.ParameterConfigurations) { pc.Mutate(random, mutate, intValueManipulator, doubleValueManipulator); } } public override void Cross(IRandom random, IOptimizable other, CrossDelegate cross, IIntValueCrossover intValueCrossover, IDoubleValueCrossover doubleValueCrossover) { var otherVc = (ParameterizedValueConfiguration)other; for (int i = 0; i < this.ParameterConfigurations.Count; i++) { this.ParameterConfigurations.ElementAt(i).Cross(random, otherVc.ParameterConfigurations.ElementAt(i), cross, intValueCrossover, doubleValueCrossover); } } public override double CalculateSimilarity(IOptimizable optimizable) { var other = (ParameterizedValueConfiguration)optimizable; double sum = 0; int count = 0; for (int i = 0; i < ParameterConfigurations.Count; i++) { if (this.ParameterConfigurations.ElementAt(i).Optimize) { sum += this.ParameterConfigurations.ElementAt(i).CalculateSimilarity(other.ParameterConfigurations.ElementAt(i)); count++; } } return count == 0 ? 1.0 : sum / (double)count; } } }