[11790] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[16565] | 26 | using HEAL.Attic;
|
---|
[11790] | 27 |
|
---|
| 28 | namespace HeuristicLab.Optimization {
|
---|
[16565] | 29 | [StorableType("EFBEE5EB-B15B-4FBB-A210-C4E36898B89D")]
|
---|
[11790] | 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; }
|
---|
[16910] | 38 | public virtual bool SupportsStop {
|
---|
| 39 | get { return true; }
|
---|
| 40 | }
|
---|
[14523] | 41 |
|
---|
[11790] | 42 | [Storable]
|
---|
[14523] | 43 | private bool initialized;
|
---|
| 44 | [Storable]
|
---|
| 45 | private readonly ResultCollection results;
|
---|
[11790] | 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]
|
---|
[16565] | 57 | protected BasicAlgorithm(StorableConstructorFlag _) : base(_) { }
|
---|
[11790] | 58 | protected BasicAlgorithm(BasicAlgorithm original, Cloner cloner)
|
---|
| 59 | : base(original, cloner) {
|
---|
| 60 | results = cloner.Clone(original.Results);
|
---|
[15302] | 61 | initialized = original.initialized;
|
---|
[11790] | 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();
|
---|
[14517] | 72 | initialized = false;
|
---|
[11790] | 73 | OnPrepared();
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[15287] | 76 | public override void Start(CancellationToken cancellationToken) {
|
---|
| 77 | base.Start(cancellationToken);
|
---|
| 78 | CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
---|
[14517] | 79 | pausePending = false;
|
---|
[11790] | 80 | OnStarted();
|
---|
[14523] | 81 |
|
---|
[15287] | 82 | try {
|
---|
| 83 | Run((object)cancellationTokenSource.Token);
|
---|
[15408] | 84 | } catch (OperationCanceledException) {
|
---|
| 85 | } catch (AggregateException ae) {
|
---|
[15367] | 86 | ae.FlattenAndHandle(new[] { typeof(OperationCanceledException) }, e => OnExceptionOccurred(e));
|
---|
[15408] | 87 | } catch (Exception e) {
|
---|
[15287] | 88 | OnExceptionOccurred(e);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | CancellationTokenSource.Dispose();
|
---|
| 92 | CancellationTokenSource = null;
|
---|
| 93 | if (pausePending) OnPaused();
|
---|
| 94 | else OnStopped();
|
---|
[11790] | 95 | }
|
---|
| 96 |
|
---|
| 97 | public override void Pause() {
|
---|
[14523] | 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)
|
---|
[14517] | 101 | throw new NotSupportedException("Pause is not supported by this algorithm.");
|
---|
| 102 |
|
---|
| 103 | base.Pause();
|
---|
| 104 | pausePending = true;
|
---|
[15409] | 105 | if (CancellationTokenSource != null) CancellationTokenSource.Cancel();
|
---|
[11790] | 106 | }
|
---|
| 107 |
|
---|
| 108 | public override void Stop() {
|
---|
[11878] | 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
|
---|
[16910] | 111 | if (!SupportsStop)
|
---|
| 112 | throw new NotSupportedException("Stop is not supported by this algorithm.");
|
---|
| 113 |
|
---|
[11790] | 114 | base.Stop();
|
---|
[14517] | 115 | if (ExecutionState == ExecutionState.Paused) OnStopped();
|
---|
[15409] | 116 | else if (CancellationTokenSource != null) CancellationTokenSource.Cancel();
|
---|
[11790] | 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 {
|
---|
[14517] | 127 | if (!initialized)
|
---|
| 128 | Initialize(cancellationToken);
|
---|
| 129 | initialized = true;
|
---|
[11790] | 130 | Run(cancellationToken);
|
---|
[15408] | 131 | } finally {
|
---|
[11790] | 132 | timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
| 133 | timer.Stop();
|
---|
| 134 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[14517] | 138 | protected virtual void Initialize(CancellationToken cancellationToken) { }
|
---|
[11790] | 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 | }
|
---|