Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs @ 9573

Last change on this file since 9573 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System;
23using System.Threading;
24using System.Threading.Tasks;
25using HeuristicLab.Common;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  [StorableClass]
32  public abstract class FixedDataAnalysisAlgorithm<T> : Algorithm,
33    IDataAnalysisAlgorithm<T>,
34    IStorableContent
35    where T : class, IDataAnalysisProblem {
36    public string Filename { get; set; }
37
38    #region Properties
39    public override Type ProblemType {
40      get { return typeof(T); }
41    }
42    public new T Problem {
43      get { return (T)base.Problem; }
44      set { base.Problem = value; }
45    }
46    [Storable]
47    private ResultCollection results;
48    public override ResultCollection Results {
49      get { return results; }
50    }
51    #endregion
52
53    private DateTime lastUpdateTime;
54
55    [StorableConstructor]
56    protected FixedDataAnalysisAlgorithm(bool deserializing) : base(deserializing) { }
57    protected FixedDataAnalysisAlgorithm(FixedDataAnalysisAlgorithm<T> original, Cloner cloner)
58      : base(original, cloner) {
59      results = cloner.Clone(original.Results);
60    }
61    public FixedDataAnalysisAlgorithm()
62      : base() {
63      results = new ResultCollection();
64    }
65
66    public override void Prepare() {
67      if (Problem != null) base.Prepare();
68      results.Clear();
69      OnPrepared();
70    }
71
72    public override void Start() {
73      base.Start();
74      var cancellationTokenSource = new CancellationTokenSource();
75
76      OnStarted();
77      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
78      task.ContinueWith(t => {
79        try {
80          t.Wait();
81        }
82        catch (AggregateException ex) {
83          try {
84            ex.Flatten().Handle(x => x is OperationCanceledException);
85          }
86          catch (AggregateException remaining) {
87            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
88            else OnExceptionOccurred(remaining);
89          }
90        }
91        cancellationTokenSource.Dispose();
92        cancellationTokenSource = null;
93        OnStopped();
94      });
95    }
96    private void Run(object state) {
97      CancellationToken cancellationToken = (CancellationToken)state;
98      lastUpdateTime = DateTime.Now;
99      System.Timers.Timer timer = new System.Timers.Timer(250);
100      timer.AutoReset = true;
101      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
102      timer.Start();
103      try {
104        Run();
105      }
106      finally {
107        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
108        timer.Stop();
109        ExecutionTime += DateTime.Now - lastUpdateTime;
110      }
111
112      cancellationToken.ThrowIfCancellationRequested();
113    }
114    protected abstract void Run();
115    #region Events
116    protected override void OnProblemChanged() {
117      Problem.Reset += new EventHandler(Problem_Reset);
118      base.OnProblemChanged();
119    }
120    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
121      System.Timers.Timer timer = (System.Timers.Timer)sender;
122      timer.Enabled = false;
123      DateTime now = DateTime.Now;
124      ExecutionTime += now - lastUpdateTime;
125      lastUpdateTime = now;
126      timer.Enabled = true;
127    }
128    #endregion
129
130  }
131}
Note: See TracBrowser for help on using the repository browser.