Changeset 6024


Ignore:
Timestamp:
04/18/11 23:27:42 (8 years ago)
Author:
cneumuel
Message:

#1215

  • improved the way AlgorithmExecutor handles cancellation
Location:
branches/HeuristicLab.MetaOptimization
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.MetaOptimization.Test/Program.cs

    r6018 r6024  
    4848
    4949    private static int metaAlgorithmPopulationSize = 10;
    50     private static int metaAlgorithmMaxGenerations = 10;
     50    private static int metaAlgorithmMaxGenerations = 20;
    5151    private static int metaProblemRepetitions = 2;
    52     private static int baseAlgorithmMaxGenerations = 10;
     52    private static int baseAlgorithmMaxGenerations = 50;
    5353    private static double mutationProbability = 0.10;
    5454
     
    8080      MetaOptimizationProblem metaOptimizationProblem = new MetaOptimizationProblem();
    8181      metaOptimizationProblem.Repetitions = new IntValue(metaProblemRepetitions);
    82       GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
    83       //GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
     82      //GeneticAlgorithm metaLevelAlgorithm = GetMetaGA(metaOptimizationProblem);
     83      GeneticAlgorithm metaLevelAlgorithm = GetParallelMetaGA(metaOptimizationProblem);
    8484      //GeneticAlgorithm metaLevelAlgorithm = GetHiveParallelMetaGA(metaOptimizationProblem);
    8585
     
    353353        tasks[i] = new Task<TimeSpan>((alg) => {
    354354          Console.WriteLine("Task {0} started.", Task.CurrentId);
     355          var cancellationTokenSource = new CancellationTokenSource();
    355356
    356357          Stopwatch swx = new Stopwatch();
    357358          swx.Start();
    358359          ((EngineAlgorithm)alg).ExecutionTimeChanged += new EventHandler(Program_ExecutionTimeChanged);
    359           ((EngineAlgorithm)alg).StartSync();
     360          ((EngineAlgorithm)alg).StartSync(cancellationTokenSource.Token);
    360361          ((EngineAlgorithm)alg).ExecutionTimeChanged -= new EventHandler(Program_ExecutionTimeChanged);
    361362          swx.Stop();
     
    467468        algs.Enqueue(clonedGa);
    468469
     470        var cancellationTokenSource = new CancellationTokenSource();
    469471        //if (algs.Count > 24)
    470472        //  algs.Dequeue();
    471         clonedGa.StartSync();
     473        clonedGa.StartSync(cancellationTokenSource.Token);
    472474        sw.Stop();
    473475        latestExecutionTimes.Enqueue(sw.Elapsed);
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/AlgorithmExecutor.cs

    r6018 r6024  
    66
    77  public static class AlgorithmExtensions {
    8     public static void StartSync(this IAlgorithm algorithm) {
    9       var executor = new AlgorithmExecutor(algorithm);
    10       executor.StartSync();
    11     }
    128    public static void StartSync(this IAlgorithm algorithm, CancellationToken cancellationToken) {
    139      var executor = new AlgorithmExecutor(algorithm, cancellationToken);
     
    2319    private AutoResetEvent waitHandle = new AutoResetEvent(false);
    2420    private CancellationToken cancellationToken;
    25     private bool useCancellationToken = false;
    26 
    27     public AlgorithmExecutor(IAlgorithm algorithm) {
    28       this.algorithm = algorithm;
    29     }
    3021
    3122    public AlgorithmExecutor(IAlgorithm algorithm, CancellationToken cancellationToken) {
    3223      this.algorithm = algorithm;
    3324      this.cancellationToken = cancellationToken;
    34       this.useCancellationToken = true;
    3525    }
    3626
     
    3828      algorithm.Stopped += new EventHandler(algorithm_Stopped);
    3929      algorithm.Paused += new EventHandler(algorithm_Paused);
    40      
    41       if(useCancellationToken && algorithm is EngineAlgorithm && ((EngineAlgorithm)algorithm).Engine is SequentialEngine.SequentialEngine) {
    42         ((SequentialEngine.SequentialEngine)((EngineAlgorithm)algorithm).Engine).Start(cancellationToken);
    43       } else {
     30
     31      using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) {
    4432        algorithm.Start();
     33        waitHandle.WaitOne();
     34        waitHandle.Dispose();
    4535      }
    4636
    47       waitHandle.WaitOne();
    48       waitHandle.Dispose();
    4937      algorithm.Stopped -= new EventHandler(algorithm_Stopped);
    5038      algorithm.Paused -= new EventHandler(algorithm_Paused);
     39      if (algorithm.ExecutionState == Core.ExecutionState.Started) {
     40        algorithm.Pause();
     41      }
     42      cancellationToken.ThrowIfCancellationRequested();
    5143    }
    5244
    53     void algorithm_Paused(object sender, EventArgs e) {
     45    private void algorithm_Paused(object sender, EventArgs e) {
    5446      waitHandle.Set();
    5547    }
    5648
    57     void algorithm_Stopped(object sender, EventArgs e) {
     49    private void algorithm_Stopped(object sender, EventArgs e) {
     50      waitHandle.Set();
     51    }
     52
     53    private void cancellationToken_Canceled() {
    5854      waitHandle.Set();
    5955    }
  • branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/Evaluators/AlgorithmEvaluator.cs

    r6018 r6024  
    6464      var sw = new Stopwatch();
    6565      sw.Start();
     66      //// prepare and clear is needed, if the algorithm has been started and stopped before (this happens when meta level algorithm is paused)
     67      //algorithm.Prepare(); // <--- SHOULD NOT HAPPEN!
     68      //algorithm.Runs.Clear();
     69
    6670      algorithm.StartSync(CancellationToken);
    6771      sw.Stop();
Note: See TracChangeset for help on using the changeset viewer.