Changeset 4830 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators
- Timestamp:
- 11/18/10 00:53:15 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.