Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/29/12 11:31:44 (12 years ago)
Author:
abeham
Message:

#1985:

  • Moved arithmetic progression dialog to MainForm (created a folder dialogs)
  • Added a button to generate such a progression for snapshot times
  • Changed TimeLimitRun to react on ExecutionTimeChanged instead of maintaining its own timers
  • Changed to allow manual snapshoting only during Paused state
  • Perform a pause + snapshot when terminating the algorithm instead of stop + snapshot (allows the algorithm in the final snapshot to continue running if the option was selected)
Location:
branches/RuntimeOptimizer/HeuristicLab.Optimization/3.3/MetaOptimizers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/RuntimeOptimizer/HeuristicLab.Optimization/3.3/MetaOptimizers/BatchRun.cs

    r8955 r8975  
    175175      repetitions = 10;
    176176      repetitionsCounter = 0;
    177       Runs = new RunCollection { AlgorithmName = Name };
     177      Runs = new RunCollection { OptimizerName = Name };
    178178    }
    179179    public BatchRun(string name)
     
    185185      repetitions = 10;
    186186      repetitionsCounter = 0;
    187       Runs = new RunCollection { AlgorithmName = Name };
     187      Runs = new RunCollection { OptimizerName = Name };
    188188    }
    189189    public BatchRun(string name, string description)
     
    194194      repetitions = 10;
    195195      repetitionsCounter = 0;
    196       Runs = new RunCollection { AlgorithmName = Name };
     196      Runs = new RunCollection { OptimizerName = Name };
    197197    }
    198198    [StorableConstructor]
     
    237237        batchRunAction = BatchRunAction.Prepare;
    238238        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    239         try { Optimizer.Prepare(clearRuns); }
    240         catch (InvalidOperationException) { }
     239        try { Optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
    241240      } else {
    242241        ExecutionState = ExecutionState.Stopped;
     
    250249      if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
    251250      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    252       try { Optimizer.Start(); }
    253       catch (InvalidOperationException) { }
     251      try { Optimizer.Start(); } catch (InvalidOperationException) { }
    254252    }
    255253    public void Pause() {
     
    260258      if (Optimizer.ExecutionState != ExecutionState.Started) return;
    261259      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    262       try { Optimizer.Pause(); }
    263       catch (InvalidOperationException) { }
     260      try { Optimizer.Pause(); } catch (InvalidOperationException) { }
    264261    }
    265262    public void Stop() {
     
    273270      }
    274271      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    275       try { Optimizer.Stop(); }
    276       catch (InvalidOperationException) { }
     272      try { Optimizer.Stop(); } catch (InvalidOperationException) { }
    277273    }
    278274
     
    280276    protected override void OnNameChanged() {
    281277      base.OnNameChanged();
    282       runs.AlgorithmName = Name;
     278      runs.OptimizerName = Name;
    283279    }
    284280
  • branches/RuntimeOptimizer/HeuristicLab.Optimization/3.3/MetaOptimizers/Experiment.cs

    r8955 r8975  
    117117      executionTime = TimeSpan.Zero;
    118118      optimizers = new OptimizerList();
    119       Runs = new RunCollection { AlgorithmName = Name };
     119      Runs = new RunCollection { OptimizerName = Name };
    120120      Initialize();
    121121    }
     
    126126      executionTime = TimeSpan.Zero;
    127127      optimizers = new OptimizerList();
    128       Runs = new RunCollection { AlgorithmName = Name };
     128      Runs = new RunCollection { OptimizerName = Name };
    129129      Initialize();
    130130    }
     
    134134      executionTime = TimeSpan.Zero;
    135135      optimizers = new OptimizerList();
    136       Runs = new RunCollection { AlgorithmName = Name };
     136      Runs = new RunCollection { OptimizerName = Name };
    137137      Initialize();
    138138    }
     
    180180      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) {
    181181        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    182         try { optimizer.Prepare(clearRuns); }
    183         catch (InvalidOperationException) { }
     182        try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
    184183      }
    185184    }
     
    194193      if (optimizer != null) {
    195194        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    196         try { optimizer.Start(); }
    197         catch (InvalidOperationException) { }
     195        try { optimizer.Start(); } catch (InvalidOperationException) { }
    198196      }
    199197    }
     
    207205      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
    208206        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    209         try { optimizer.Pause(); }
    210         catch (InvalidOperationException) { }
     207        try { optimizer.Pause(); } catch (InvalidOperationException) { }
    211208      }
    212209    }
     
    221218        foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
    222219          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
    223           try { optimizer.Stop(); }
    224           catch (InvalidOperationException) { }
     220          try { optimizer.Stop(); } catch (InvalidOperationException) { }
    225221        }
    226222      } else {
     
    232228    protected override void OnNameChanged() {
    233229      base.OnNameChanged();
    234       Runs.AlgorithmName = Name;
     230      Runs.OptimizerName = Name;
    235231    }
    236232
  • branches/RuntimeOptimizer/HeuristicLab.Optimization/3.3/MetaOptimizers/TimeLimitRun.cs

    r8961 r8975  
    2525using System.Drawing;
    2626using System.Linq;
    27 using System.Threading;
    2827using System.Threading.Tasks;
    29 using System.Timers;
    3028using HeuristicLab.Collections;
    3129using HeuristicLab.Common;
     
    3331using HeuristicLab.Core;
    3432using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    35 using Timer = System.Timers.Timer;
    3633
    3734namespace HeuristicLab.Optimization {
     
    5653    #endregion
    5754
    58     private Timer snapTimer, runTimer;
    59     private readonly ManualResetEvent algorithmStarted = new ManualResetEvent(false);
    60     private readonly ManualResetEvent algorithmPaused = new ManualResetEvent(false);
    61     private readonly ManualResetEvent algorithmStopped = new ManualResetEvent(false);
    62     [Storable]
    63     private bool snapshotTimesDirty;
     55    private bool pausedForSnapshot = false;
     56    private bool pausedForTermination = false;
    6457
    6558    [Storable]
     
    7063        if (maximumExecutionTime == value) return;
    7164        maximumExecutionTime = value;
    72         runTimer.Interval = (maximumExecutionTime - ExecutionTime).TotalMilliseconds;
    7365        OnPropertyChanged("MaximumExecutionTime");
    7466      }
     
    8476        if (snapshotTimes == value) return;
    8577        snapshotTimes = value;
    86         snapshotTimesDirty = true;
     78        snapshotTimes.Sort();
     79        FindNextSnapshotTimeIndex(ExecutionTime);
    8780        OnPropertyChanged("SnapshotTimes");
    8881      }
     
    131124        if (algorithm != null) {
    132125          RegisterAlgorithmEvents();
    133           if (algorithm.ExecutionState == ExecutionState.Started) algorithmStarted.Set();
    134           else algorithmStarted.Reset();
    135           if (algorithm.ExecutionState == ExecutionState.Paused) algorithmPaused.Set();
    136           else algorithmPaused.Reset();
    137           if (algorithm.ExecutionState == ExecutionState.Stopped) algorithmStopped.Set();
    138           else algorithmStopped.Reset();
    139126        }
    140127        OnPropertyChanged("Algorithm");
     
    173160      snapshotTimesIndex = original.snapshotTimesIndex;
    174161      snapshots = cloner.Clone(original.snapshots);
    175       snapshotTimesDirty = original.snapshotTimesDirty;
    176162      storeAlgorithmInEachSnapshot = original.storeAlgorithmInEachSnapshot;
    177163      algorithm = cloner.Clone(original.algorithm);
    178164      runs = cloner.Clone(original.runs);
    179 
    180       snapTimer = new Timer() {
    181         AutoReset = original.snapTimer.AutoReset,
    182         Interval = original.snapTimer.Interval
    183       };
    184       runTimer = new Timer() {
    185         AutoReset = original.runTimer.AutoReset,
    186         Interval = original.runTimer.Interval
    187       };
    188       if (original.algorithm != null && original.algorithm.ExecutionState == ExecutionState.Started)
    189         algorithmStarted.Set();
    190       if (original.algorithm != null && original.algorithm.ExecutionState == ExecutionState.Paused)
    191         algorithmPaused.Set();
    192       if (original.algorithm != null && original.algorithm.ExecutionState == ExecutionState.Stopped)
    193         algorithmStopped.Set();
    194165
    195166      Initialize();
     
    199170      name = ItemName;
    200171      description = ItemDescription;
    201       maximumExecutionTime = TimeSpan.FromMinutes(1);
     172      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    202173      snapshotTimes = new ObservableList<TimeSpan>(new[] {
    203174          TimeSpan.FromSeconds(5),
    204175          TimeSpan.FromSeconds(10),
    205           TimeSpan.FromSeconds(30) });
     176          TimeSpan.FromSeconds(15) });
    206177      snapshotTimesIndex = 0;
    207       snapTimer = new Timer() {
    208         AutoReset = false,
    209         Enabled = false,
    210         Interval = 100
    211       };
    212       runTimer = new Timer() {
    213         AutoReset = false,
    214         Enabled = false,
    215         Interval = MaximumExecutionTime.TotalMilliseconds
    216       };
    217178      snapshots = new RunCollection();
    218       Runs = new RunCollection { AlgorithmName = Name };
     179      Runs = new RunCollection { OptimizerName = Name };
    219180      Initialize();
    220181    }
     
    222183      : base(name) {
    223184      description = ItemDescription;
    224       maximumExecutionTime = TimeSpan.FromMinutes(1);
     185      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    225186      snapshotTimes = new ObservableList<TimeSpan>(new[] {
    226187          TimeSpan.FromSeconds(5),
    227188          TimeSpan.FromSeconds(10),
    228           TimeSpan.FromSeconds(30) });
     189          TimeSpan.FromSeconds(15) });
    229190      snapshotTimesIndex = 0;
    230       snapTimer = new Timer() {
    231         AutoReset = false,
    232         Enabled = false,
    233         Interval = 100
    234       };
    235       snapshots = new RunCollection();
    236       runTimer = new Timer() {
    237         AutoReset = false,
    238         Enabled = false,
    239         Interval = MaximumExecutionTime.TotalMilliseconds
    240       };
    241       Runs = new RunCollection { AlgorithmName = Name };
     191      Runs = new RunCollection { OptimizerName = Name };
    242192      Initialize();
    243193    }
    244194    public TimeLimitRun(string name, string description)
    245195      : base(name, description) {
    246       maximumExecutionTime = TimeSpan.FromMinutes(1);
     196      maximumExecutionTime = TimeSpan.FromMinutes(.5);
    247197      snapshotTimes = new ObservableList<TimeSpan>(new[] {
    248198          TimeSpan.FromSeconds(5),
    249199          TimeSpan.FromSeconds(10),
    250           TimeSpan.FromSeconds(30) });
     200          TimeSpan.FromSeconds(15) });
    251201      snapshotTimesIndex = 0;
    252       snapTimer = new Timer() {
    253         AutoReset = false,
    254         Enabled = false,
    255         Interval = 100
    256       };
    257       snapshots = new RunCollection();
    258       runTimer = new Timer() {
    259         AutoReset = false,
    260         Enabled = false,
    261         Interval = MaximumExecutionTime.TotalMilliseconds
    262       };
    263       Runs = new RunCollection { AlgorithmName = Name };
     202      Runs = new RunCollection { OptimizerName = Name };
    264203      Initialize();
    265204    }
     
    272211    [StorableHook(HookType.AfterDeserialization)]
    273212    private void AfterDeserialization() {
    274       // the timers will not be serialized
    275       snapTimer = new Timer() {
    276         AutoReset = false,
    277         Enabled = false,
    278         Interval = 100
    279       };
    280       runTimer = new Timer() {
    281         AutoReset = false,
    282         Enabled = false,
    283         Interval = double.MaxValue
    284       };
    285       if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Started)
    286         algorithmStarted.Set();
    287       else algorithmStarted.Reset();
    288       if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Paused)
    289         algorithmPaused.Set();
    290       else algorithmPaused.Reset();
    291       if (Algorithm != null && Algorithm.ExecutionState == ExecutionState.Stopped)
    292         algorithmStopped.Set();
    293       else algorithmStopped.Reset();
    294213      Initialize();
    295214    }
     
    302221      snapshotTimes.ItemsReplaced += snapshotTimes_Changed;
    303222      snapshotTimes.CollectionReset += snapshotTimes_Changed;
    304       snapTimer.Elapsed += snapshotTimer_Elapsed;
    305       runTimer.Elapsed += runTimer_Elapsed;
    306     }
    307 
    308     private void snapshotTimes_Changed(object sender, EventArgs e) {
    309       snapshotTimesDirty = true;
    310     }
    311 
    312     private readonly object snapshotLock = new object();
    313     private void snapshotTimer_Elapsed(object sender, ElapsedEventArgs e) {
    314       snapTimer.Stop();
    315       lock (snapshotLock) {
    316         try {
    317           if (Algorithm == null) return;
    318           if (ExecutionState != ExecutionState.Started) return;
    319 
    320           var execTime = ExecutionTime;
    321           if (snapshotTimesIndex < SnapshotTimes.Count &&
    322               SnapshotTimes[snapshotTimesIndex] <= execTime) {
    323             DoSnapshot();
    324             snapshotTimesIndex++;
    325             try {
    326               Algorithm.Start();
    327               algorithmStarted.WaitOne();
    328             } catch (InvalidOperationException) { }
    329           }
    330         } catch { }
    331         snapTimer.Start();
    332       }
    333     }
    334 
    335     private void runTimer_Elapsed(object sender, ElapsedEventArgs e) {
    336       snapTimer.Stop();
    337       lock (snapshotLock) {
    338         try {
    339           Algorithm.Stop();
    340           algorithmStopped.WaitOne();
    341         } catch (InvalidOperationException) { }
    342       }
    343     }
     223    }
     224
     225    private void snapshotTimes_Changed(object sender, CollectionItemsChangedEventArgs<IndexedItem<TimeSpan>> e) {
     226      if (e.Items.Any()) snapshotTimes.Sort();
     227      FindNextSnapshotTimeIndex(ExecutionTime);
     228    }
     229
     230    public void Snapshot() {
     231      if (Algorithm == null || Algorithm.ExecutionState != ExecutionState.Paused) throw new InvalidOperationException("Snapshot not allowed in execution states other than Paused");
     232      Task.Factory.StartNew(MakeSnapshot);
     233    }
     234
     235    public void Prepare() {
     236      Prepare(false);
     237    }
     238    public void Prepare(bool clearRuns) {
     239      Algorithm.Prepare(clearRuns);
     240    }
     241    public void Start() {
     242      Algorithm.Start();
     243    }
     244    public void Pause() {
     245      Algorithm.Pause();
     246    }
     247    public void Stop() {
     248      Algorithm.Stop();
     249    }
     250
     251    #region Events
     252    protected override void OnNameChanged() {
     253      base.OnNameChanged();
     254      runs.OptimizerName = Name;
     255    }
     256
     257    public event PropertyChangedEventHandler PropertyChanged;
     258    private void OnPropertyChanged(string property) {
     259      var handler = PropertyChanged;
     260      if (handler != null) handler(this, new PropertyChangedEventArgs(property));
     261    }
     262
     263    #region IExecutable Events
     264    public event EventHandler ExecutionStateChanged;
     265    private void OnExecutionStateChanged() {
     266      var handler = ExecutionStateChanged;
     267      if (handler != null) handler(this, EventArgs.Empty);
     268    }
     269    public event EventHandler ExecutionTimeChanged;
     270    private void OnExecutionTimeChanged() {
     271      var handler = ExecutionTimeChanged;
     272      if (handler != null) handler(this, EventArgs.Empty);
     273    }
     274    public event EventHandler Prepared;
     275    private void OnPrepared() {
     276      var handler = Prepared;
     277      if (handler != null) handler(this, EventArgs.Empty);
     278    }
     279    public event EventHandler Started;
     280    private void OnStarted() {
     281      var handler = Started;
     282      if (handler != null) handler(this, EventArgs.Empty);
     283    }
     284    public event EventHandler Paused;
     285    private void OnPaused() {
     286      var handler = Paused;
     287      if (handler != null) handler(this, EventArgs.Empty);
     288    }
     289    public event EventHandler Stopped;
     290    private void OnStopped() {
     291      var handler = Stopped;
     292      if (handler != null) handler(this, EventArgs.Empty);
     293    }
     294    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
     295    private void OnExceptionOccurred(Exception exception) {
     296      var handler = ExceptionOccurred;
     297      if (handler != null) handler(this, new EventArgs<Exception>(exception));
     298    }
     299    #endregion
     300
     301    #region Algorithm Events
     302    private void RegisterAlgorithmEvents() {
     303      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
     304      algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged;
     305      algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;
     306      algorithm.Paused += Algorithm_Paused;
     307      algorithm.Prepared += Algorithm_Prepared;
     308      algorithm.Started += Algorithm_Started;
     309      algorithm.Stopped += Algorithm_Stopped;
     310    }
     311    private void DeregisterAlgorithmEvents() {
     312      algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
     313      algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged;
     314      algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;
     315      algorithm.Paused -= Algorithm_Paused;
     316      algorithm.Prepared -= Algorithm_Prepared;
     317      algorithm.Started -= Algorithm_Started;
     318      algorithm.Stopped -= Algorithm_Stopped;
     319    }
     320    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
     321      OnExceptionOccurred(e.Value);
     322    }
     323    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
     324      if (snapshotTimesIndex < SnapshotTimes.Count && ExecutionTime >= SnapshotTimes[snapshotTimesIndex]
     325        && !pausedForSnapshot) {
     326        pausedForSnapshot = true;
     327        Algorithm.Pause();
     328      }
     329      if (ExecutionTime >= MaximumExecutionTime && !pausedForTermination) {
     330        pausedForTermination = true;
     331        if (!pausedForSnapshot) Algorithm.Pause();
     332      }
     333      OnExecutionTimeChanged();
     334    }
     335    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
     336      OnExecutionStateChanged();
     337    }
     338    private void Algorithm_Paused(object sender, EventArgs e) {
     339      var action = pausedForTermination ? ExecutionState.Stopped : (pausedForSnapshot ? ExecutionState.Started : ExecutionState.Paused);
     340      if (pausedForSnapshot || pausedForTermination) {
     341        pausedForSnapshot = pausedForTermination = false;
     342        MakeSnapshot();
     343        snapshotTimesIndex++;
     344      }
     345      OnPaused();
     346      if (action == ExecutionState.Started) Algorithm.Start();
     347      else if (action == ExecutionState.Stopped) Algorithm.Stop();
     348    }
     349    private void Algorithm_Prepared(object sender, EventArgs e) {
     350      snapshotTimesIndex = 0;
     351      snapshots.Clear();
     352      OnPrepared();
     353    }
     354    private void Algorithm_Started(object sender, EventArgs e) {
     355      OnStarted();
     356    }
     357    private void Algorithm_Stopped(object sender, EventArgs e) {
     358      var cloner = new Cloner();
     359      var algRun = cloner.Clone(Algorithm.Runs.Last());
     360      var clonedSnapshots = cloner.Clone(snapshots);
     361      try {
     362        algRun.Results.Add("Snapshots", clonedSnapshots);
     363      } catch (ArgumentException ex) { // Snapshots already exists
     364        // TODO strategy?
     365      }
     366      Runs.Add(algRun);
     367      Algorithm.Runs.Clear();
     368      OnStopped();
     369    }
     370    #endregion
     371    #endregion
    344372
    345373    private void FindNextSnapshotTimeIndex(TimeSpan reference) {
     
    349377      };
    350378      snapshotTimesIndex = index;
    351     }
    352 
    353     private void DoSnapshot() {
    354       lock (snapshotLock) {
    355         var wasPaused = Algorithm.ExecutionState == ExecutionState.Paused;
    356         var error = false;
    357         try {
    358           Algorithm.Pause();
    359           algorithmPaused.WaitOne();
    360         } catch (InvalidOperationException) {
    361           error = true;
    362         }
    363         if (error && ExecutionState != ExecutionState.Paused) return; // Algorithm is either stopped or prepared
    364 
    365         MakeSnapshot();
    366         if (!wasPaused) {
    367           try {
    368             Algorithm.Start();
    369             algorithmStarted.WaitOne();
    370           } catch (InvalidOperationException) { }
    371         }
    372       }
    373379    }
    374380
     
    397403      snapshots.Add(run);
    398404    }
    399 
    400     public void Snapshot() {
    401       Task.Factory.StartNew(DoSnapshot);
    402     }
    403 
    404     public void Prepare() {
    405       Prepare(false);
    406     }
    407     public void Prepare(bool clearRuns) {
    408       Algorithm.Prepare(clearRuns);
    409     }
    410     public void Start() {
    411       Algorithm.Start();
    412     }
    413     public void Pause() {
    414       Algorithm.Pause();
    415     }
    416     public void Stop() {
    417       Algorithm.Stop();
    418     }
    419 
    420     #region Events
    421     protected override void OnNameChanged() {
    422       base.OnNameChanged();
    423       runs.AlgorithmName = Name;
    424     }
    425 
    426     public event PropertyChangedEventHandler PropertyChanged;
    427     private void OnPropertyChanged(string property) {
    428       var handler = PropertyChanged;
    429       if (handler != null) handler(this, new PropertyChangedEventArgs(property));
    430     }
    431 
    432     #region IExecutable Events
    433     public event EventHandler ExecutionStateChanged;
    434     private void OnExecutionStateChanged() {
    435       var handler = ExecutionStateChanged;
    436       if (handler != null) handler(this, EventArgs.Empty);
    437     }
    438     public event EventHandler ExecutionTimeChanged;
    439     private void OnExecutionTimeChanged() {
    440       var handler = ExecutionTimeChanged;
    441       if (handler != null) handler(this, EventArgs.Empty);
    442     }
    443     public event EventHandler Prepared;
    444     private void OnPrepared() {
    445       var handler = Prepared;
    446       if (handler != null) handler(this, EventArgs.Empty);
    447     }
    448     public event EventHandler Started;
    449     private void OnStarted() {
    450       var handler = Started;
    451       if (handler != null) handler(this, EventArgs.Empty);
    452     }
    453     public event EventHandler Paused;
    454     private void OnPaused() {
    455       var handler = Paused;
    456       if (handler != null) handler(this, EventArgs.Empty);
    457     }
    458     public event EventHandler Stopped;
    459     private void OnStopped() {
    460       var handler = Stopped;
    461       if (handler != null) handler(this, EventArgs.Empty);
    462     }
    463     public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
    464     private void OnExceptionOccurred(Exception exception) {
    465       var handler = ExceptionOccurred;
    466       if (handler != null) handler(this, new EventArgs<Exception>(exception));
    467     }
    468     #endregion
    469 
    470     #region Algorithm Events
    471     private void RegisterAlgorithmEvents() {
    472       algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
    473       algorithm.ExecutionTimeChanged += Algorithm_ExecutionTimeChanged;
    474       algorithm.ExecutionStateChanged += Algorithm_ExecutionStateChanged;
    475       algorithm.Paused += Algorithm_Paused;
    476       algorithm.Prepared += Algorithm_Prepared;
    477       algorithm.Started += Algorithm_Started;
    478       algorithm.Stopped += Algorithm_Stopped;
    479     }
    480     private void DeregisterAlgorithmEvents() {
    481       algorithm.ExceptionOccurred -= Algorithm_ExceptionOccurred;
    482       algorithm.ExecutionTimeChanged -= Algorithm_ExecutionTimeChanged;
    483       algorithm.ExecutionStateChanged -= Algorithm_ExecutionStateChanged;
    484       algorithm.Paused -= Algorithm_Paused;
    485       algorithm.Prepared -= Algorithm_Prepared;
    486       algorithm.Started -= Algorithm_Started;
    487       algorithm.Stopped -= Algorithm_Stopped;
    488     }
    489     private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    490       OnExceptionOccurred(e.Value);
    491     }
    492     private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
    493       OnExecutionTimeChanged();
    494     }
    495     private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
    496       OnExecutionStateChanged();
    497     }
    498     private void Algorithm_Paused(object sender, EventArgs e) {
    499       algorithmStarted.Reset();
    500       algorithmPaused.Set();
    501       algorithmStopped.Reset();
    502       OnPaused();
    503     }
    504     private void Algorithm_Prepared(object sender, EventArgs e) {
    505       algorithmStarted.Reset();
    506       algorithmPaused.Reset();
    507       algorithmStopped.Reset();
    508       snapTimer.Stop();
    509       runTimer.Stop();
    510       snapshotTimesIndex = 0;
    511       snapshots.Clear();
    512       OnPrepared();
    513     }
    514     private void Algorithm_Started(object sender, EventArgs e) {
    515       algorithmStarted.Set();
    516       algorithmPaused.Reset();
    517       algorithmStopped.Reset();
    518       var execTime = ExecutionTime;
    519       if (snapshotTimesDirty) {
    520         SnapshotTimes.Sort();
    521         snapshotTimesDirty = false; // sort will mark it dirty again
    522         FindNextSnapshotTimeIndex(execTime);
    523       }
    524       runTimer.Interval = (MaximumExecutionTime - execTime).TotalMilliseconds;
    525       snapTimer.Start();
    526       runTimer.Start();
    527       OnStarted();
    528     }
    529     private void Algorithm_Stopped(object sender, EventArgs e) {
    530       algorithmStarted.Reset();
    531       algorithmPaused.Reset();
    532       algorithmStopped.Set();
    533       lock (snapshotLock) {
    534         snapTimer.Stop();
    535         runTimer.Stop();
    536         MakeSnapshot();
    537       }
    538       var cloner = new Cloner();
    539       var algRun = cloner.Clone(Algorithm.Runs.Last());
    540       var clonedSnapshots = cloner.Clone(snapshots);
    541       try {
    542         algRun.Results.Add("Snapshots", clonedSnapshots);
    543       } catch (ArgumentException ex) {
    544         // TODO strategy?
    545       }
    546       Runs.Add(algRun);
    547       Algorithm.Runs.Clear();
    548       OnStopped();
    549     }
    550     #endregion
    551     #endregion
    552405  }
    553406}
Note: See TracChangeset for help on using the changeset viewer.