[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 System.Threading.Tasks;
|
---|
| 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);
|
---|
| 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 |
|
---|
| 73 | public override void Start() {
|
---|
| 74 | base.Start();
|
---|
| 75 | CancellationTokenSource = new CancellationTokenSource();
|
---|
[14517] | 76 | pausePending = false;
|
---|
[11790] | 77 | OnStarted();
|
---|
[14523] | 78 |
|
---|
| 79 | Task task = Task.Factory.StartNew(Run, CancellationTokenSource.Token, CancellationTokenSource.Token);
|
---|
[11790] | 80 | task.ContinueWith(t => {
|
---|
| 81 | try {
|
---|
| 82 | t.Wait();
|
---|
[14523] | 83 | }
|
---|
| 84 | catch (AggregateException ex) {
|
---|
[11790] | 85 | try {
|
---|
| 86 | ex.Flatten().Handle(x => x is OperationCanceledException);
|
---|
[14523] | 87 | }
|
---|
| 88 | catch (AggregateException remaining) {
|
---|
[11790] | 89 | if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
|
---|
| 90 | else OnExceptionOccurred(remaining);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 | CancellationTokenSource.Dispose();
|
---|
| 94 | CancellationTokenSource = null;
|
---|
[14517] | 95 | if (pausePending) OnPaused();
|
---|
| 96 | else OnStopped();
|
---|
[11790] | 97 | });
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public override void Pause() {
|
---|
[14523] | 101 | // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise pause does nothing
|
---|
| 102 | // alternatively check the IsCancellationRequested property of the cancellation token
|
---|
| 103 | if (!SupportsPause)
|
---|
[14517] | 104 | throw new NotSupportedException("Pause is not supported by this algorithm.");
|
---|
| 105 |
|
---|
| 106 | base.Pause();
|
---|
| 107 | pausePending = true;
|
---|
[14523] | 108 | CancellationTokenSource.Cancel();
|
---|
[11790] | 109 | }
|
---|
| 110 |
|
---|
| 111 | public override void Stop() {
|
---|
[11878] | 112 | // CancellationToken.ThrowIfCancellationRequested() must be called from within the Run method, otherwise stop does nothing
|
---|
| 113 | // alternatively check the IsCancellationRequested property of the cancellation token
|
---|
[11790] | 114 | base.Stop();
|
---|
[14517] | 115 | if (ExecutionState == ExecutionState.Paused) OnStopped();
|
---|
| 116 | else 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);
|
---|
[14523] | 131 | }
|
---|
| 132 | finally {
|
---|
[11790] | 133 | timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
|
---|
| 134 | timer.Stop();
|
---|
| 135 | ExecutionTime += DateTime.UtcNow - lastUpdateTime;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[14517] | 139 | protected virtual void Initialize(CancellationToken cancellationToken) { }
|
---|
[11790] | 140 | protected abstract void Run(CancellationToken cancellationToken);
|
---|
| 141 |
|
---|
| 142 | #region Events
|
---|
| 143 | private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
|
---|
| 144 | System.Timers.Timer timer = (System.Timers.Timer)sender;
|
---|
| 145 | timer.Enabled = false;
|
---|
| 146 | DateTime now = DateTime.UtcNow;
|
---|
| 147 | ExecutionTime += now - lastUpdateTime;
|
---|
| 148 | lastUpdateTime = now;
|
---|
| 149 | timer.Enabled = true;
|
---|
| 150 | }
|
---|
| 151 | #endregion
|
---|
| 152 |
|
---|
| 153 | }
|
---|
| 154 | }
|
---|