#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.ParameterConfigurationTreeEncoding { [StorableClass] 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(bool deserializing) : base(deserializing) { } public ParameterizedValueConfiguration() { 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 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; } } }