Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs @ 15215

Last change on this file since 15215 was 15215, checked in by jkarder, 7 years ago

#2258: worked on execution of basic algorithm

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Optimization {
28  [StorableClass]
29  public abstract class BasicAlgorithm : Algorithm, IStorableContent {
30    public string Filename { get; set; }
31
32    [Storable]
33    private ResultCollection results;
34    public override ResultCollection Results {
35      get { return results; }
36    }
37
38    private CancellationTokenSource cancellationTokenSource;
39    protected CancellationTokenSource CancellationTokenSource {
40      get { return cancellationTokenSource; }
41      private set { cancellationTokenSource = value; }
42    }
43
44    [StorableConstructor]
45    protected BasicAlgorithm(bool deserializing) : base(deserializing) { }
46    protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner)
47      : base(original, cloner) {
48      results = cloner.Clone(original.Results);
49    }
50    protected BasicAlgorithm()
51      : base() {
52      results = new ResultCollection();
53    }
54
55    public override void Prepare() {
56      if (Problem == null) return;
57      base.Prepare();
58      results.Clear();
59      OnPrepared();
60    }
61
62    public override void Start(CancellationToken cancellationToken) {
63      base.Start(cancellationToken);
64      CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
65
66      OnStarted();
67      try {
68        Run((object)cancellationTokenSource.Token);
69      } catch (OperationCanceledException) {
70      } catch (AggregateException ae) {
71        if (ae.InnerExceptions.Count == 1) OnExceptionOccurred(ae.InnerExceptions[0]);
72        else OnExceptionOccurred(ae);
73      } catch (Exception e) {
74        OnExceptionOccurred(e);
75      }
76
77      CancellationTokenSource.Dispose();
78      CancellationTokenSource = null;
79      OnStopped();
80    }
81
82    public override void Pause() {
83      throw new NotSupportedException("Pause is not supported in basic algorithms.");
84    }
85
86    public override void Stop() {
87      // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise stop does nothing
88      // alternatively check the IsCancellationRequested property of the cancellation token
89      base.Stop();
90      CancellationTokenSource.Cancel();
91    }
92
93
94    private DateTime lastUpdateTime;
95    private void Run(object state) {
96      CancellationToken cancellationToken = (CancellationToken)state;
97      lastUpdateTime = DateTime.UtcNow;
98      System.Timers.Timer timer = new System.Timers.Timer(250);
99      timer.AutoReset = true;
100      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
101      timer.Start();
102      try {
103        Run(cancellationToken);
104      } finally {
105        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
106        timer.Stop();
107        ExecutionTime += DateTime.UtcNow - lastUpdateTime;
108      }
109    }
110
111    protected abstract void Run(CancellationToken cancellationToken);
112
113    #region Events
114    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
115      System.Timers.Timer timer = (System.Timers.Timer)sender;
116      timer.Enabled = false;
117      DateTime now = DateTime.UtcNow;
118      ExecutionTime += now - lastUpdateTime;
119      lastUpdateTime = now;
120      timer.Enabled = true;
121    }
122    #endregion
123
124  }
125}
Note: See TracBrowser for help on using the repository browser.