Changeset 5207 for branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators
- Timestamp:
- 01/04/11 02:18:27 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/ParameterConfigurationEvaluator.cs
r5184 r5207 10 10 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 11 11 using System.Collections.Generic; 12 using HeuristicLab.Algorithms.GeneticAlgorithm; 13 using System.Threading.Tasks; 14 using System.Diagnostics; 15 using System.Reflection; 12 16 13 17 namespace HeuristicLab.Problems.MetaOptimization { … … 18 22 [StorableClass] 19 23 public class ParameterConfigurationEvaluator : SingleSuccessorOperator, IParameterConfigurationEvaluator { 20 private bool algorithmStopped; 21 private bool algorithmExceptionOccured; 24 private const double PenaltyQuality = 100000.0; // todo: use something better 22 25 23 26 public ILookupParameter<DoubleValue> QualityParameter { … … 54 57 protected ParameterConfigurationEvaluator(ParameterConfigurationEvaluator original, Cloner cloner) 55 58 : base(original, cloner) { 56 this.algorithmStopped = original.algorithmStopped;57 59 } 58 60 public override IDeepCloneable Clone(Cloner cloner) { … … 61 63 62 64 public override IOperation Apply() { 63 EngineAlgorithm algorithm = AlgorithmParameter.ActualValue;65 EngineAlgorithm algorithm = (EngineAlgorithm)AlgorithmParameter.ActualValue.Clone(); 64 66 IItemList<ISingleObjectiveProblem> problems = ProblemsParameter.ActualValue; 65 67 66 68 // set parameters 67 69 ParameterConfigurationParameter.ActualValue.Parameterize(algorithm); 68 algorithm.Problem = problems.First();69 70 algorithm.StoreAlgorithmInEachRun = false; 70 71 algorithmStopped = false;72 algorithmExceptionOccured = false;73 algorithm.Stopped += new EventHandler(algorithm_Stopped);74 algorithm.Paused += new EventHandler(algorithm_Paused);75 algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);76 71 77 72 List<double> qualities = new List<double>(); … … 81 76 82 77 foreach (ISingleObjectiveProblem problem in problems) { 83 algorithm.Problem = problem;78 algorithm.Problem = (IProblem)problem.Clone(); 84 79 85 80 for (int i = 0; i < Repetitions.Value; i++) { 86 81 algorithm.Prepare(); 87 algorithm.Start();88 while (!algorithmStopped) {89 Thread.Sleep(200); // 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)90 }91 82 92 if (algorithmExceptionOccured) { 83 AlgorithmExecutor executor = new AlgorithmExecutor(algorithm); 84 executor.StartSync(); 85 86 if (algorithm.ExecutionState == ExecutionState.Paused) { 93 87 // this parametercombination was bad. set penalty for this solution 94 qualities.Add( double.MaxValue); // todo: respect Maximization88 qualities.Add(PenaltyQuality); // todo: respect Maximization; problem: this messes up average value; solution: use currently worst quality*2 95 89 executionTimes.Add(algorithm.ExecutionTime); 96 90 } else { … … 103 97 algorithm.Runs.Last().Parameters.Add("Problem", problem); 104 98 } 105 algorithmStopped = false;106 algorithmExceptionOccured = false;107 99 } 108 100 } 109 110 algorithm.Stopped -= new EventHandler(algorithm_Stopped);111 algorithm.Paused -= new EventHandler(algorithm_Paused);112 algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);113 101 algorithm.Prepare(); 114 102 … … 130 118 } 131 119 132 private void algorithm_Paused(object sender, EventArgs e) {133 algorithmStopped = true;134 }135 136 private void algorithm_Stopped(object sender, EventArgs e) {137 algorithmStopped = true;138 }139 140 void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {141 algorithmExceptionOccured = true;142 }143 144 120 public static double Variance(IEnumerable<double> source) { 145 121 double avg = source.Average(); … … 151 127 return Math.Sqrt(source.Variance()); 152 128 } 129 } 153 130 131 public class AlgorithmExecutor { 132 private EngineAlgorithm algorithm; 133 private AutoResetEvent waitHandle = new AutoResetEvent(false); 154 134 135 public AlgorithmExecutor(EngineAlgorithm algorithm) { 136 this.algorithm = algorithm; 137 } 138 139 public void StartSync() { 140 algorithm.Stopped += new EventHandler(algorithm_Stopped); 141 algorithm.Paused += new EventHandler(algorithm_Paused); 142 algorithm.Start(); 143 waitHandle.WaitOne(); 144 waitHandle.Dispose(); 145 algorithm.Stopped -= new EventHandler(algorithm_Stopped); 146 algorithm.Paused -= new EventHandler(algorithm_Paused); 147 } 148 149 void algorithm_Paused(object sender, EventArgs e) { 150 waitHandle.Set(); 151 } 152 153 void algorithm_Stopped(object sender, EventArgs e) { 154 waitHandle.Set(); 155 } 155 156 } 156 157 }
Note: See TracChangeset
for help on using the changeset viewer.