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