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