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