- Timestamp:
- 11/18/10 00:53:15 (14 years ago)
- Location:
- branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3
- Files:
-
- 9 added
- 13 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/BestQualityAnalyzer.cs
r4516 r4830 9 9 using HeuristicLab.Parameters; 10 10 using HeuristicLab.Data; 11 using HeuristicLab.Common; 11 12 12 13 namespace HeuristicLab.Problems.MetaOptimization { … … 16 17 [Item("BestQualityAnalyzer", "TODO An operator for analyzing the best solution of Traveling Salesman Problems given in path representation using city coordinates.")] 17 18 [StorableClass] 18 public sealed class BestQualityAnalyzer : SingleSuccessorOperator, IAnalyzer { 19 public sealed class BestParameterConfigurationAnalyzer : SingleSuccessorOperator, IAnalyzer { 20 // Wagner: Spezielle View für die Lösungen (ParameterConfigurations): So wie bei Runs: die zu Optimierenden Parameter(-werte) der besten solution anzeigen 19 21 20 public ScopeTreeLookupParameter<IParameter Set> ParameterSetParameter {21 get { return (ScopeTreeLookupParameter<IParameter Set>)Parameters["ParameterSet"]; }22 public ScopeTreeLookupParameter<IParameterConfiguration> ParameterVectorParameter { 23 get { return (ScopeTreeLookupParameter<IParameterConfiguration>)Parameters["ParameterConfiguration"]; } 22 24 } 23 25 public ScopeTreeLookupParameter<DoubleValue> QualityParameter { 24 26 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 25 27 } 26 public LookupParameter<IParameter Set> BestSolutionParameter {27 get { return (LookupParameter<IParameter Set>)Parameters["BestSolution"]; }28 public LookupParameter<IParameterConfiguration> BestSolutionParameter { 29 get { return (LookupParameter<IParameterConfiguration>)Parameters["BestSolution"]; } 28 30 } 29 31 public ValueLookupParameter<ResultCollection> ResultsParameter { … … 33 35 get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 34 36 } 35 public LookupParameter<IParameter Set> BestKnownSolutionParameter {36 get { return (LookupParameter<IParameter Set>)Parameters["BestKnownSolution"]; }37 public LookupParameter<IParameterConfiguration> BestKnownSolutionParameter { 38 get { return (LookupParameter<IParameterConfiguration>)Parameters["BestKnownSolution"]; } 37 39 } 38 40 39 public Best QualityAnalyzer()41 public BestParameterConfigurationAnalyzer() 40 42 : base() { 41 Parameters.Add(new ScopeTreeLookupParameter<IParameter Set>("ParameterSet", "TODO The TSP solutions given in path representation from which the best solution should be analyzed."));43 Parameters.Add(new ScopeTreeLookupParameter<IParameterConfiguration>("ParameterConfiguration", "TODO The TSP solutions given in path representation from which the best solution should be analyzed.")); 42 44 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "TODO The qualities of the TSP solutions which should be analyzed.")); 43 Parameters.Add(new LookupParameter<IParameter Set>("BestSolution", "TODO The best TSP solution."));45 Parameters.Add(new LookupParameter<IParameterConfiguration>("BestSolution", "TODO The best TSP solution.")); 44 46 Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "TODO The result collection where the best TSP solution should be stored.")); 45 47 Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "TODO The quality of the best known solution of this TSP instance.")); 46 Parameters.Add(new LookupParameter<IParameterSet>("BestKnownSolution", "TODO The best known solution of this TSP instance.")); 48 Parameters.Add(new LookupParameter<IParameterConfiguration>("BestKnownSolution", "TODO The best known solution of this TSP instance.")); 49 } 50 51 [StorableConstructor] 52 private BestParameterConfigurationAnalyzer(bool deserializing) : base(deserializing) { } 53 private BestParameterConfigurationAnalyzer(BestParameterConfigurationAnalyzer original, Cloner cloner) : base(original, cloner) { } 54 public override IDeepCloneable Clone(Cloner cloner) { 55 return new BestParameterConfigurationAnalyzer(this, cloner); 47 56 } 48 57 … … 51 60 ResultCollection results = ResultsParameter.ActualValue; 52 61 DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue; 53 ItemArray<IParameter Set> parameterSets = ParameterSetParameter.ActualValue;62 ItemArray<IParameterConfiguration> parameterVectors = ParameterVectorParameter.ActualValue; 54 63 55 64 int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; … … 57 66 if (bestKnownQuality == null || qualities[i].Value < bestKnownQuality.Value) { 58 67 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value); 59 BestKnownSolutionParameter.ActualValue = (IParameter Set)parameterSets[i].Clone();68 BestKnownSolutionParameter.ActualValue = (IParameterConfiguration)parameterVectors[i].Clone(); 60 69 } 61 70 -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Encodings/ParameterConfiguration.cs
r4525 r4830 6 6 using HeuristicLab.Common; 7 7 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 8 using HeuristicLab.Data; 9 using HeuristicLab.Parameters; 8 10 9 11 namespace HeuristicLab.Problems.MetaOptimization { 10 12 [StorableClass] 11 public abstract class ParameterConfiguration : IParameterConfiguration { 12 [Storable] 13 public IParameter Parameter { get; set; } 14 15 [Storable] 16 public string Category { get; set; } 13 public class ParameterConfiguration : DeepCloneable, IParameterConfiguration, IStorableContent { 14 protected ParameterConfiguration parentConfiguration; 15 16 [Storable] 17 public string Filename { get; set; } 18 19 [Storable] 20 protected bool optimizationEnabled; 21 public bool OptimizationEnabled { 22 get { return optimizationEnabled; } 23 set { 24 if (optimizationEnabled != value) { 25 optimizationEnabled = value; 26 OnOptimizationEnabledChanged(); 27 } 28 } 29 } 30 31 [Storable] 32 protected string parameterName; 33 public string ParameterName { 34 get { return parameterName; } 35 set { 36 if (parameterName != value) { 37 parameterName = value; 38 } 39 } 40 } 41 42 [Storable] 43 protected IItemList<IParameterConfiguration> childParameterConfigurations = new ItemList<IParameterConfiguration>(); 44 public IItemList<IParameterConfiguration> ChildParameterConfigurations { 45 get { return childParameterConfigurations; } 46 } 47 48 public virtual IParameter Parameter { 49 get { 50 return parentConfiguration.GetChildParameter(this.parameterName); 51 } 52 } 53 54 protected IItem value; 55 public IItem Value { 56 get { return value; } 57 set { 58 if (this.value != value) { 59 ClearChildParameterConfigurations(); 60 OnValueChanging(); 61 this.value = value; 62 if(this.value is IParameterizedNamedItem) AddChildParameterConfigurations(this.value as IParameterizedNamedItem); 63 OnValueChanged(); 64 } 65 } 66 } 67 68 public ParameterConfiguration(ParameterConfiguration parentConfiguration, string parameterName, IItem value) { 69 this.parentConfiguration = parentConfiguration; 70 this.ParameterName = parameterName; 71 this.Value = value; 72 } 17 73 18 74 public ParameterConfiguration() { } 19 20 75 [StorableConstructor] 21 76 protected ParameterConfiguration(bool deserializing) { } 22 23 public ParameterConfiguration(IParameter parameter, string category) { 24 this.Parameter = parameter; 25 this.Category = category; 26 Parameter.NameChanged += this.NameChanged; 27 Parameter.NameChanging += this.NameChanging; 28 Parameter.DescriptionChanged += this.DescriptionChanged; 29 Parameter.ItemImageChanged += this.ItemImageChanged; 30 parameter.ToStringChanged += this.ToStringChanged; 31 } 32 33 public bool CanChangeDescription { 34 get { return false; } 35 } 36 37 public bool CanChangeName { 38 get { return false; } 39 } 40 41 public string Description { 77 protected ParameterConfiguration(ParameterConfiguration original, Cloner cloner) 78 : base(original, cloner) { 79 this.OptimizationEnabled = original.optimizationEnabled; 80 this.ParameterName = original.parameterName; 81 this.Value = cloner.Clone(original.Value); 82 this.childParameterConfigurations = cloner.Clone(original.childParameterConfigurations); 83 } 84 public override IDeepCloneable Clone(Cloner cloner) { 85 return new ParameterConfiguration(this, cloner); 86 } 87 88 protected virtual IValueParameter GetChildParameter(string childParameterName) { 89 return (IValueParameter)((IParameterizedItem)Value).Parameters[childParameterName]; 90 } 91 92 protected virtual void AddChildParameterConfigurations(IParameterizedNamedItem parameterizedItem) { 93 foreach (var childParameter in parameterizedItem.Parameters) { 94 var pc = ParameterConfiguration.Create(this, parameterizedItem, childParameter); 95 if (pc != null) this.childParameterConfigurations.Add(pc); 96 } 97 } 98 protected virtual void ClearChildParameterConfigurations() { 99 childParameterConfigurations.Clear(); 100 } 101 102 #region INamedItem Properties 103 public virtual string Name { 104 get { return Parameter.Name; } 105 set { throw new NotSupportedException(); } 106 } 107 public virtual string Description { 42 108 get { return Parameter.Description; } 43 109 set { throw new NotSupportedException(); } 44 110 } 45 public string Name { 46 get { return Parameter.Name; } 47 set { throw new NotSupportedException(); } 48 } 49 50 public string ItemDescription { 111 public virtual bool CanChangeDescription { 112 get { return false; } 113 } 114 public virtual bool CanChangeName { 115 get { return false; } 116 } 117 public virtual string ItemDescription { 51 118 get { return Parameter.ItemDescription; } 52 119 } 53 54 public System.Drawing.Image ItemImage { 120 public virtual System.Drawing.Image ItemImage { 55 121 get { return Parameter.ItemImage; } 56 122 } 57 58 public string ItemName { 123 public virtual string ItemName { 59 124 get { return Parameter.ItemName; } 60 125 } 61 62 public Version ItemVersion { 126 public virtual Version ItemVersion { 63 127 get { return Parameter.ItemVersion; } 64 128 } 129 #endregion 65 130 66 131 #region Events 67 132 public virtual event EventHandler NameChanged; 133 protected virtual void OnNameChanged(object sender, EventArgs e) { 134 var handler = NameChanged; 135 if (handler != null) handler(sender, e); 136 } 137 68 138 public virtual event EventHandler<CancelEventArgs<string>> NameChanging; 139 protected virtual void OnNameChanging(object sender, CancelEventArgs<string> e) { 140 var handler = NameChanging; 141 if (handler != null) handler(sender, e); 142 } 143 69 144 public virtual event EventHandler DescriptionChanged; 145 protected virtual void OnDescriptionChanged(object sender, EventArgs e) { 146 var handler = DescriptionChanged; 147 if (handler != null) handler(sender, e); 148 } 149 70 150 public virtual event EventHandler ItemImageChanged; 151 protected virtual void OnItemImageChanged(object sender, EventArgs e) { 152 var handler = ItemImageChanged; 153 if (handler != null) handler(sender, e); 154 } 155 71 156 public virtual event EventHandler ToStringChanged; 72 73 protected virtual void OnToStringChanged() { 157 protected virtual void OnStringChanged(object sender, EventArgs e) { 74 158 var handler = ToStringChanged; 159 if (handler != null) handler(this, e); // important to set 'this' as sender 160 } 161 162 public virtual event EventHandler OptimizationEnabledChanged; 163 protected virtual void OnOptimizationEnabledChanged() { 164 var handler = OptimizationEnabledChanged; 75 165 if (handler != null) handler(this, EventArgs.Empty); 76 166 } 167 168 public virtual event EventHandler ValueChanging; 169 protected virtual void OnValueChanging() { 170 var handler = ValueChanging; 171 if (handler != null) handler(this, EventArgs.Empty); 172 } 173 174 public virtual event EventHandler ValueChanged; 175 protected virtual void OnValueChanged() { 176 var handler = ValueChanged; 177 if (handler != null) handler(this, EventArgs.Empty); 178 } 77 179 #endregion 78 180 79 #region Cloning80 public virtual IDeepCloneable Clone(Cloner cloner) {81 ParameterConfiguration clone = (ParameterConfiguration)Activator.CreateInstance(this.GetType(), true);82 cloner.RegisterClonedObject(this, clone);83 clone.Parameter = (IParameter)cloner.Clone(this.Parameter);84 clone.Category = this.Category;85 return clone;86 }87 88 public object Clone() {89 return Clone(new Cloner());90 }91 #endregion92 93 181 public override string ToString() { 94 return string.Format("{0}.{1}", Category, Parameter.Name); 95 } 96 97 public abstract void SetParameterWithRandomValue(IRandom random); 182 return string.Format("{0} ({1})", ParameterName, Value); 183 } 184 185 public static IParameterConfiguration Create(IParameterizedNamedItem parent) { 186 return new RootParameterConfiguration(parent); 187 } 188 189 public static IParameterConfiguration Create(ParameterConfiguration parentConfiguration, IParameterizedNamedItem parent, IParameter parameter) { 190 if (parameter is IValueParameter) { 191 IValueParameter valueParameter = parameter as IValueParameter; 192 193 if (typeof(IntValue).IsAssignableFrom(parameter.DataType)) { 194 return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value); 195 } else if (typeof(IParameterizedItem).IsAssignableFrom(parameter.DataType)) { 196 return new ParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value); 197 } else { 198 // todo 199 return new IntValueParameterConfiguration(parentConfiguration, parameter.Name, valueParameter.Value); 200 } 201 } 202 return null; 203 } 204 205 public void Parameterize(IParameterizedItem parameterizedItem) { 206 throw new NotImplementedException(); 207 } 98 208 99 209 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/MetaOptimizationEvaluator.cs
r4525 r4830 10 10 using HeuristicLab.Optimization; 11 11 using System.Threading; 12 using HeuristicLab.Common; 12 13 13 14 namespace HeuristicLab.Problems.MetaOptimization { … … 18 19 [StorableClass] 19 20 public class MetaOptimizationEvaluator : SingleSuccessorOperator, IMetaOptimizationEvaluator { 21 private const string RepetitionsParameterName = "Repetitions"; 22 20 23 private bool algorithmStopped; 21 24 … … 23 26 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 24 27 } 25 26 28 public ILookupParameter<IAlgorithm> AlgorithmParameter { 27 29 get { return (ILookupParameter<IAlgorithm>)Parameters["Algorithm"]; } 28 30 } 29 public ILookupParameter<IParameterSet> ParameterSetParameter { 30 get { return (ILookupParameter<IParameterSet>)Parameters["ParameterSet"]; } 31 public ILookupParameter<IItemList<IProblem>> ProblemsParameter { 32 get { return (ILookupParameter<IItemList<IProblem>>)Parameters["Problems"]; } 33 } 34 public ILookupParameter<IParameterConfiguration> ParameterConfigurationParameter { 35 get { return (ILookupParameter<IParameterConfiguration>)Parameters["ParameterConfiguration"]; } 36 } 37 public ValueParameter<IntValue> RepetitionsParameter { 38 get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; } 31 39 } 32 40 33 public MetaOptimizationEvaluator() 34 : base() { 35 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the TSP solution.")); 41 public IntValue Repetitions { 42 get { return RepetitionsParameter.Value; } 43 set { RepetitionsParameter.Value = value; } 44 } 45 46 public MetaOptimizationEvaluator() : base() { 47 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the ParameterVector.")); 36 48 Parameters.Add(new LookupParameter<IAlgorithm>("Algorithm", "Missing description.")); 37 Parameters.Add(new LookupParameter<IParameterSet>("ParameterSet", "Missing description.")); 49 Parameters.Add(new LookupParameter<IItemList<IProblem>>("Problems", "Missing description.")); 50 Parameters.Add(new LookupParameter<IParameterConfiguration>("ParameterConfiguration", "Missing description.")); 51 Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "Number of evaluations for one individual.", new IntValue(3))); 52 } 53 54 [StorableConstructor] 55 protected MetaOptimizationEvaluator(bool deserializing) : base(deserializing) { } 56 protected MetaOptimizationEvaluator(MetaOptimizationEvaluator original, Cloner cloner) 57 : base(original, cloner) { 58 this.algorithmStopped = original.algorithmStopped; 59 } 60 public override IDeepCloneable Clone(Cloner cloner) { 61 return new MetaOptimizationEvaluator(this, cloner); 38 62 } 39 63 40 64 public override IOperation Apply() { 41 65 AlgorithmParameter.ActualValue.Prepare(); 42 ParametrizeAlgorithm(); 66 //ParameterizeAlgorithm(); 67 ParameterConfigurationParameter.ActualValue.Parameterize(AlgorithmParameter.ActualValue); 43 68 algorithmStopped = false; 44 69 AlgorithmParameter.ActualValue.Stopped += new EventHandler(ActualValue_Stopped); 45 AlgorithmParameter.ActualValue.Start(); 46 while (!algorithmStopped) { 47 Thread.Sleep(1000); // wait for algorithm to complete; do not rely on Algorithm.ExecutionState here, because change of ExecutionState happens before Run is added (which causes problems because Algorithm might get cloned when its started already) 70 71 double qualitySum = 0; 72 73 foreach (IProblem problem in ProblemsParameter.ActualValue) { 74 AlgorithmParameter.ActualValue.Problem = problem; 75 AlgorithmParameter.ActualValue.Start(); 76 while (!algorithmStopped) { 77 Thread.Sleep(1000); // wait for algorithm to complete; do not rely on Algorithm.ExecutionState here, because change of ExecutionState happens before Run is added (which causes problems because Algorithm might get cloned when its started already) 78 } 79 AlgorithmParameter.ActualValue.Stopped -= new EventHandler(ActualValue_Stopped); 80 qualitySum += ((DoubleValue)AlgorithmParameter.ActualValue.Results["BestQuality"].Value).Value; 48 81 } 49 AlgorithmParameter.ActualValue.Stopped -= new EventHandler(ActualValue_Stopped); 50 this.QualityParameter.ActualValue = (DoubleValue)AlgorithmParameter.ActualValue.Results["BestQuality"].Value; 82 83 double qualityAvg = qualitySum / ProblemsParameter.ActualValue.Count; 84 this.QualityParameter.ActualValue = new DoubleValue(qualityAvg); 85 51 86 return base.Apply(); 52 87 } … … 56 91 } 57 92 58 private void ParametrizeAlgorithm() {59 foreach (IParameterConfiguration parameter in ParameterSetParameter.ActualValue.Parameters) {60 if (parameter.Category == "Algorithm") {61 this.AlgorithmParameter.ActualValue.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;62 } else if (parameter.Category == "Problem") {63 this.AlgorithmParameter.ActualValue.Problem.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue;64 }65 }66 }93 //private void ParameterizeAlgorithm() { 94 // foreach (IParameterConfiguration parameter in ParameterVectorParameter.ActualValue) { 95 // if (typeof(IAlgorithm).IsAssignableFrom(parameter.OperatorType)) { 96 // this.AlgorithmParameter.ActualValue.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue; 97 // } else if (typeof(IProblem).IsAssignableFrom(parameter.OperatorType)) { 98 // this.AlgorithmParameter.ActualValue.Problem.Parameters[parameter.Parameter.Name].ActualValue = parameter.Parameter.ActualValue; 99 // } 100 // } 101 //} 67 102 } 68 103 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/HeuristicLab.Problems.MetaOptimization-3.3.csproj
r4525 r4830 88 88 <ItemGroup> 89 89 <Compile Include="Analyzers\BestQualityAnalyzer.cs" /> 90 <Compile Include="Creators\RandomParameterSetCreator.cs" /> 91 <Compile Include="Encodings\BooleanParameterConfiguration.cs" /> 92 <Compile Include="Encodings\Crossovers\ParameterSetCrossover.cs" /> 93 <Compile Include="Interfaces\IParameterSetOperator.cs" /> 94 <Compile Include="NumericRangeList.cs" /> 95 <Compile Include="Encodings\EnumerableParameterConfiguration.cs" /> 96 <Compile Include="Interfaces\INumericRange.cs" /> 97 <Compile Include="Encodings\NumericParameterConfiguration.cs" /> 98 <Compile Include="Encodings\ParameterSet.cs" /> 90 <Compile Include="Interfaces\IParameterConfigurationCreator.cs" /> 91 <Compile Include="Creators\RandomParameterConfigurationCreator.cs" /> 92 <Compile Include="Encodings\Crossovers\ParameterVectorCrossover.cs" /> 93 <Compile Include="Encodings\IntValueParameterConfiguration.cs" /> 94 <Compile Include="Encodings\ValueTypeParameterConfiguration.cs"> 95 <SubType>Code</SubType> 96 </Compile> 97 <Compile Include="Encodings\RootParameterConfiguration.cs" /> 98 <Compile Include="Interfaces\IParameterConfigurationOperator.cs" /> 99 <Compile Include="Interfaces\IRange.cs" /> 99 100 <Compile Include="Evaluators\MetaOptimizationEvaluator.cs" /> 100 101 <Compile Include="Interfaces\IMetaOptimizationEvaluator.cs" /> 101 <Compile Include="Interfaces\IParameterSet.cs" />102 <Compile Include="Interfaces\IParameterSetCreator.cs" />103 102 <Compile Include="Interfaces\IParameterConfiguration.cs" /> 104 103 <Compile Include="Encodings\ParameterConfiguration.cs" /> 105 <Compile Include="Encodings\ParameterConfigurationList.cs" /> 106 <Compile Include="NumericRange.cs" /> 104 <Compile Include="Range.cs" /> 107 105 <Compile Include="Properties\AssemblyInfo.cs" /> 108 106 <Compile Include="MetaOptimizationProblem.cs" /> -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Interfaces/IParameterConfiguration.cs
r4525 r4830 7 7 namespace HeuristicLab.Problems.MetaOptimization { 8 8 public interface IParameterConfiguration : INamedItem { 9 IParameter Parameter { get; set; } 10 string Category { get; set; } 9 bool OptimizationEnabled { get; set; } 10 string ParameterName { get; set; } 11 IParameter Parameter { get; } 12 IItem Value { get; set; } 13 IItemList<IParameterConfiguration> ChildParameterConfigurations { get; } 11 14 12 void SetParameterWithRandomValue(IRandom random); 15 void Parameterize(IParameterizedItem parameterizedItem); 16 17 event EventHandler OptimizationEnabledChanged; 18 event EventHandler ValueChanging; 19 event EventHandler ValueChanged; 13 20 } 14 21 } -
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs
r4525 r4830 36 36 [Creatable("Problems")] 37 37 [StorableClass] 38 public sealed class MetaOptimizationProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent { 39 public string Filename { get; set; } 40 41 public override Image ItemImage { 42 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; } 43 } 38 public sealed class MetaOptimizationProblem : SingleObjectiveProblem<IMetaOptimizationEvaluator, IParameterConfigurationCreator> { 39 private const string AlgorithmParameterName = "Algorithm"; 40 private const string ProblemsParameterName = "Problems"; 41 private const string AlgorithmParameterConfigurationParameterName = "AlgorithmParameterConfiguration"; 42 private const string ProblemParametersConfigurationParameterName = "ProblemParametersConfiguration"; 44 43 45 44 #region Parameter Properties 46 47 public ValueParameter<IntValue> RepetitionsParameter { 48 get { return (ValueParameter<IntValue>)Parameters["Repetitions"]; } 45 public ValueParameter<IAlgorithm> AlgorithmParameter { 46 get { return (ValueParameter<IAlgorithm>)Parameters[AlgorithmParameterName]; } 49 47 } 50 51 public ValueParameter<IParameterSetCreator> SolutionCreatorParameter { 52 get { return (ValueParameter<IParameterSetCreator>)Parameters["SolutionCreator"]; } 48 public ValueParameter<IItemList<ISingleObjectiveProblem>> ProblemsParameter { 49 get { return (ValueParameter<IItemList<ISingleObjectiveProblem>>)Parameters[ProblemsParameterName]; } 53 50 } 54 IParameter IProblem.SolutionCreatorParameter {55 get { return SolutionCreatorParameter; }51 public ValueParameter<IParameterConfiguration> AlgorithmParameterConfigurationParameter { 52 get { return (ValueParameter<IParameterConfiguration>)Parameters[AlgorithmParameterConfigurationParameterName]; } 56 53 } 57 58 public ValueParameter<IMetaOptimizationEvaluator> EvaluatorParameter { 59 get { return (ValueParameter<IMetaOptimizationEvaluator>)Parameters["Evaluator"]; } 60 } 61 IParameter IProblem.EvaluatorParameter { 62 get { return EvaluatorParameter; } 63 } 64 65 public OptionalValueParameter<ParameterSet> BestKnownSolutionParameter { 66 get { return (OptionalValueParameter<ParameterSet>)Parameters["BestKnownSolution"]; } 67 } 68 69 public ValueParameter<BoolValue> MaximizationParameter { 70 get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; } 71 } 72 IParameter ISingleObjectiveProblem.MaximizationParameter { 73 get { return MaximizationParameter; } 74 } 75 76 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter { 77 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 78 } 79 IParameter ISingleObjectiveProblem.BestKnownQualityParameter { 80 get { return BestKnownQualityParameter; } 81 } 82 83 public ValueParameter<IAlgorithm> AlgorithmParameter { 84 get { return (ValueParameter<IAlgorithm>)Parameters["Algorithm"]; } 85 } 86 public ValueParameter<ParameterConfigurationList> ParametersToOptimizeParameter { 87 get { return (ValueParameter<ParameterConfigurationList>)Parameters["ParametersToOptimize"]; } 88 } 54 //public ValueParameter<IItemList<IParameterConfiguration>> ProblemParametersConfigurationParameter { 55 // get { return (ValueParameter<IItemList<IParameterConfiguration>>)Parameters[ProblemParametersConfigurationParameterName]; } 56 //} 89 57 #endregion 90 58 91 59 #region Properties 92 93 public IntValue Repetitions {94 get { return RepetitionsParameter.Value; }95 set { RepetitionsParameter.Value = value; }96 }97 98 public IEnumerable<IOperator> Operators {99 get { return operators; }100 }101 102 IEvaluator IProblem.Evaluator {103 get { return EvaluatorParameter.Value; }104 }105 public IMetaOptimizationEvaluator Evaluator {106 get { return EvaluatorParameter.Value; }107 set { EvaluatorParameter.Value = value; }108 }109 110 ISolutionCreator IProblem.SolutionCreator {111 get { return SolutionCreatorParameter.Value; }112 }113 public IParameterSetCreator SolutionCreator {114 get { return SolutionCreatorParameter.Value; }115 }116 117 ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {118 get { return this.Evaluator; }119 }120 121 public DoubleValue BestKnownQuality {122 get { return BestKnownQualityParameter.Value; }123 set { BestKnownQualityParameter.Value = value; }124 }125 public ParameterSet BestKnownSolution {126 get { return BestKnownSolutionParameter.Value; }127 set { BestKnownSolutionParameter.Value = value; }128 }129 130 60 public IAlgorithm Algorithm { 131 61 get { return AlgorithmParameter.Value; } 132 62 set { AlgorithmParameter.Value = value; } 133 63 } 134 135 public ParameterConfigurationList ParametersToOptimize { 136 get { return ParametersToOptimizeParameter.Value; } 137 set { ParametersToOptimizeParameter.Value = value; } 64 public IItemList<ISingleObjectiveProblem> Problems { 65 get { return ProblemsParameter.Value; } 66 set { ProblemsParameter.Value = value; } 138 67 } 139 140 private BestQualityAnalyzer BestQualityAnalyzer {141 get { return operators.OfType<BestQualityAnalyzer>().FirstOrDefault(); }68 public IParameterConfiguration AlgorithmParameterConfiguration { 69 get { return AlgorithmParameterConfigurationParameter.Value; } 70 set { AlgorithmParameterConfigurationParameter.Value = value; } 142 71 } 72 //public IItemList<IParameterConfiguration> ProblemParametersConfiguration { 73 // get { return ProblemParametersConfigurationParameter.Value; } 74 // set { ProblemParametersConfigurationParameter.Value = value; } 75 //} 143 76 #endregion 144 77 145 [Storable] 146 private List<IOperator> operators; 78 public MetaOptimizationProblem() 79 : base() { 80 Parameters.Add(new ValueParameter<IAlgorithm>(AlgorithmParameterName, "The algorithm which's parameters should be optimized.")); 81 Parameters.Add(new ValueParameter<IItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ItemList<IProblem>())); 82 Parameters.Add(new ValueParameter<IParameterConfiguration>(AlgorithmParameterConfigurationParameterName, "List of algorithm parameters that should be optimized.")); 83 //Parameters.Add(new ValueParameter<IItemList<IParameterConfiguration>>(ProblemParametersConfigurationParameterName, "List of problem parameters that should be optimized.", new ItemList<IParameterConfiguration>())); 84 85 Maximization = new BoolValue(false); 86 SolutionCreator = new RandomParameterConfigurationCreator(); 87 Evaluator = new MetaOptimizationEvaluator(); 88 89 InitializeOperators(); 90 RegisterParameterEvents(); 91 ParameterizeSolutionCreator(); 92 ParameterizeEvaluator(); 93 ParameterizeOperators(); 94 } 147 95 148 96 [StorableConstructor] 149 97 private MetaOptimizationProblem(bool deserializing) : base(deserializing) { } 150 public MetaOptimizationProblem() 151 : base() { 152 153 IParameterSetCreator creator = new RandomParameterSetCreator(); 154 MetaOptimizationEvaluator evaluator = new MetaOptimizationEvaluator(); 155 ParameterConfigurationList parametersToOptimize = new ParameterConfigurationList(); 156 157 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false))); 158 Parameters.Add(new ValueParameter<IntValue>("Repetitions", "Number of evaluations for one individual.", new IntValue(3))); 159 Parameters.Add(new ValueParameter<IParameterSetCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator)); 160 Parameters.Add(new ValueParameter<IMetaOptimizationEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator)); 161 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance.")); 162 Parameters.Add(new OptionalValueParameter<ParameterSet>("BestKnownSolution", "The best known solution of this TSP instance.")); 163 164 Parameters.Add(new ValueParameter<IAlgorithm>("Algorithm", "The algorithm and problem which's parameters should be optimized.")); 165 Parameters.Add(new ValueParameter<ParameterConfigurationList>("ParametersToOptimize", "List of parameters that should be optimized.", parametersToOptimize)); 166 167 ParameterizeSolutionCreator(); 168 ParameterizeEvaluator(); 169 170 InitializeOperators(); 171 AttachEventHandlers(); 98 private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) { 99 // todo 100 this.RegisterParameterEvents(); 172 101 } 173 174 #region Cloning175 102 public override IDeepCloneable Clone(Cloner cloner) { 176 MetaOptimizationProblem clone = (MetaOptimizationProblem)base.Clone(cloner); 177 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 178 // todo 179 clone.AttachEventHandlers(); 180 return clone; 103 return new MetaOptimizationProblem(this, cloner); 181 104 } 182 #endregion183 105 184 106 #region Helpers 185 107 [StorableHook(HookType.AfterDeserialization)] 186 108 private void AfterDeserializationHook() { 187 AttachEventHandlers();109 RegisterParameterEvents(); 188 110 } 189 private void AttachEventHandlers() {111 private void RegisterParameterEvents() { 190 112 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 191 113 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); … … 194 116 } 195 117 private void InitializeOperators() { 196 operators = new List<IOperator>(); 197 operators.Add(new BestQualityAnalyzer()); 198 ParameterizeAnalyzer(); 199 operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterSetOperator>().Cast<IOperator>()); 200 ParameterizeOperators(); 201 202 //UpdateMoveEvaluators(); 203 //InitializeMoveGenerators(); 118 Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>()); 119 Operators.Add(new BestParameterConfigurationAnalyzer()); 204 120 } 205 121 private void ParameterizeSolutionCreator() { 206 SolutionCreator.ParametersToOptimize = this.ParametersToOptimize;122 //SolutionCreator.ParametersToOptimize = this.ParametersToOptimize; 207 123 } 208 124 private void ParameterizeEvaluator() { 125 ((MetaOptimizationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName; 209 126 } 210 127 private void ParameterizeAnalyzer() { 211 BestQualityAnalyzer.ResultsParameter.ActualName = "Results";128 //BestQualityAnalyzer.ResultsParameter.ActualName = "Results"; 212 129 } 213 130 private void ParameterizeOperators() { 131 214 132 } 215 133 216 private void AddAlgorithmParameters() {217 foreach (IParameter parameter in Algorithm.Parameters) {218 this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Algorithm"), false);219 }220 }221 private void RemoveAlgorithmParameters() {222 foreach (IParameter parameter in Algorithm.Parameters) {223 IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);224 if (parameterConfiguration != null) {225 this.ParametersToOptimize.Remove(parameterConfiguration);226 }227 }228 }229 private void ClearAlgorithmParameters() {230 //this.ParametersToOptimize.Clear();231 }232 233 private void AddProblemParameters() {234 foreach (IParameter parameter in Algorithm.Problem.Parameters) {235 this.ParametersToOptimize.Add(new NumericParameterConfiguration(parameter, "Problem"), false);236 }237 }238 private void RemoveProblemParameters() {239 foreach (IParameter parameter in Algorithm.Problem.Parameters) {240 IParameterConfiguration parameterConfiguration = this.ParametersToOptimize.Single(p => p.Parameter == parameter);241 if (parameterConfiguration != null) {242 this.ParametersToOptimize.Remove(parameterConfiguration);243 }244 }245 }246 private void ClearProblemParameters() {247 //this.ParametersToOptimize.Clear();248 }249 134 #endregion 250 135 251 136 #region Events 252 public event EventHandler SolutionCreatorChanged;253 private void OnSolutionCreatorChanged() {254 EventHandler handler = SolutionCreatorChanged;255 if (handler != null) handler(this, EventArgs.Empty);256 }257 public event EventHandler EvaluatorChanged;258 private void OnEvaluatorChanged() {259 EventHandler handler = EvaluatorChanged;260 if (handler != null) handler(this, EventArgs.Empty);261 }262 public event EventHandler OperatorsChanged;263 private void OnOperatorsChanged() {264 EventHandler handler = OperatorsChanged;265 if (handler != null) handler(this, EventArgs.Empty);266 }267 public event EventHandler Reset;268 private void OnReset() {269 EventHandler handler = Reset;270 if (handler != null) handler(this, EventArgs.Empty);271 }272 137 273 138 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { … … 288 153 } 289 154 void BaseLevelAlgorithmParameter_ValueChanged(object sender, EventArgs e) { 290 ClearAlgorithmParameters();291 155 if (Algorithm != null) { 292 156 Algorithm.ProblemChanged += new EventHandler(BaseLevelAlgorithm_ProblemChanged); 293 A ddAlgorithmParameters(); // TODO: When to Detach?157 AlgorithmParameterConfiguration = ParameterConfiguration.Create(Algorithm); 294 158 } 295 159 BaseLevelAlgorithm_ProblemChanged(sender, e); … … 297 161 298 162 void BaseLevelAlgorithm_ProblemChanged(object sender, EventArgs e) { 299 ClearProblemParameters();300 if (Algorithm.Problem != null) {301 AddProblemParameters();302 }163 //ClearProblemParameters(); 164 //if (Algorithm.Problem != null) { 165 // AddProblemParameters(); 166 //} 303 167 } 304 168 #endregion
Note: See TracChangeset
for help on using the changeset viewer.