1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HEAL.Attic;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization {
|
---|
30 | [StorableType("EFBEE5EB-B15B-4FBB-A210-C4E36898B89D")]
|
---|
31 | public abstract class BasicAlgorithm : Algorithm, IStorableContent {
|
---|
32 |
|
---|
33 | private bool pausePending;
|
---|
34 | private DateTime lastUpdateTime;
|
---|
35 |
|
---|
36 | public string Filename { get; set; }
|
---|
37 |
|
---|
38 | public abstract bool SupportsPause { get; }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private bool initialized;
|
---|
42 | [Storable]
|
---|
43 | private readonly ResultCollection results;
|
---|
44 | public override ResultCollection Results {
|
---|
45 | get { return results; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private CancellationTokenSource cancellationTokenSource;
|
---|
49 | protected CancellationTokenSource CancellationTokenSource {
|
---|
50 | get { return cancellationTokenSource; }
|
---|
51 | private set { cancellationTokenSource = value; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | [StorableConstructor]
|
---|
55 | protected BasicAlgorithm(StorableConstructorFlag _) : base(_) { }
|
---|
56 | protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner)
|
---|
57 | : base(original, cloner) {
|
---|
58 | results = cloner.Clone(original.Results);
|
---|
59 | initialized = original.initialized;
|
---|
60 | }
|
---|
61 | protected BasicAlgorithm()
|
---|
62 | : base() {
|
---|
63 | results = new ResultCollection();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override void Prepare() {
|
---|
67 | if (Problem == null) return;
|
---|
68 | base.Prepare();
|
---|
69 | results.Clear();
|
---|
70 | initialized = false;
|
---|
71 | OnPrepared();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override void Start(CancellationToken cancellationToken) {
|
---|
75 | base.Start(cancellationToken);
|
---|
76 | CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
---|
77 | pausePending = false;
|
---|
78 | OnStarted();
|
---|
79 |
|
---|
80 | try {
|
---|
81 | Run((object)cancellationTokenSource.Token);
|
---|
82 | } catch (OperationCanceledException) {
|
---|
83 | } catch (AggregateException ae) {
|
---|
84 | ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e));
|
---|
85 | } catch (Exception e) {
|
---|
86 | OnExceptionOccurred(e);
|
---|
87 | }
|
---|
88 |
|
---|
89 | CancellationTokenSource.Dispose();
|
---|
90 | CancellationTokenSource = null;
|
---|
91 | if (pausePending) OnPaused();
|
---|
92 | else OnStopped();
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override void Pause() {
|
---|
96 | // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise pause does nothing
|
---|
97 | // alternatively check the IsCancellationRequested property of the cancellation token
|
---|
98 | if (!SupportsPause)
|
---|
99 | throw new NotSupportedException("Pause is not supported by this algorithm.");
|
---|
100 |
|
---|
101 | base.Pause();
|
---|
102 | pausePending = true;
|
---|
103 | if (CancellationTokenSource != null) CancellationTokenSource.Cancel();
|
---|
104 | }
|
---|
105 |
|
---|
106 | public override void Stop() {
|
---|
107 | // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise stop does nothing
|
---|
108 | // alternatively check the IsCancellationRequested property of the cancellation token
|
---|
109 | base.Stop();
|
---|
110 | if (ExecutionState == ExecutionState.Paused) OnStopped();
|
---|
111 | else if (CancellationTokenSource != null) CancellationTokenSource.Cancel();
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void Run(object state) {
|
---|
115 | CancellationToken cancellationToken = (CancellationToken)state;
|
---|
116 | lastUpdateTime = DateTime.UtcNow;
|
---|
117 | System.Timers.Timer timer = new System.Timers.Timer(250);
|
---|
118 | timer.AutoReset = true;
|
---|
119 | timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
120 | timer.Start();
|
---|
121 | try {
|
---|
122 | if (!initialized)
|
---|
123 | Initialize(cancellationToken);
|
---|
124 | initialized = true;
|
---|
125 | Run(cancellationToken);
|
---|
126 | }
|
---|
127 | finally {
|
---|
128 | timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
129 | timer.Stop();
|
---|
130 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected virtual void Initialize(CancellationToken cancellationToken) { }
|
---|
135 | protected abstract void Run(CancellationToken cancellationToken);
|
---|
136 |
|
---|
137 | #region Events
|
---|
138 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
139 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
140 | timer.Enabled = false;
|
---|
141 | DateTime now = DateTime.UtcNow;
|
---|
142 | ExecutionTime += now - lastUpdateTime;
|
---|
143 | lastUpdateTime = now;
|
---|
144 | timer.Enabled = true;
|
---|
145 | }
|
---|
146 | #endregion
|
---|
147 |
|
---|
148 | }
|
---|
149 | }
|
---|