Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork.cs @ 14613

Last change on this file since 14613 was 14613, checked in by jkarder, 8 years ago

#2205: worked on optimization networks

  • fixed event handler registration
File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Core.Networks;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
31  [Item("TtpNetwork", "An abstract base class for TTP optimization networks.")]
32  [StorableClass]
33  public abstract class TtpNetwork : Network, IOptimizer {
34    #region Nodes
35    public TtpOrchestratorNode1 Orchestrator {
36      get { return (TtpOrchestratorNode1)Nodes["Orchestrator"]; }
37    }
38
39    public OrchestratedAlgorithmNode MetaSolver {
40      get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; }
41    }
42
43    public OrchestratedAlgorithmNode TspSolver {
44      get { return (OrchestratedAlgorithmNode)Nodes["TspSolver"]; }
45    }
46
47    public OrchestratedAlgorithmNode KspSolver {
48      get { return (OrchestratedAlgorithmNode)Nodes["KspSolver"]; }
49    }
50    #endregion
51
52    [StorableConstructor]
53    protected TtpNetwork(bool deserializing) : base(deserializing) { }
54    protected TtpNetwork(TtpNetwork original, Cloner cloner) : base(original, cloner) {
55      RegisterEvents();
56    }
57    protected TtpNetwork() : this("TtpNetwork") { }
58    protected TtpNetwork(string name) : base(name) {
59      Nodes.Add(new TtpOrchestratorNode1("Orchestrator"));
60      Nodes.Add(new OrchestratedAlgorithmNode("MetaSolver"));
61      Nodes.Add(new OrchestratedAlgorithmNode("TspSolver"));
62      Nodes.Add(new OrchestratedAlgorithmNode("KspSolver"));
63
64      RegisterEvents();
65    }
66
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      RegisterEvents();
70    }
71
72    protected virtual void RegisterEvents() {
73      MetaSolver.AlgorithmChanged += MetaSolver_AlgorithmChanged;
74      RegisterAlgorithmEvents();
75    }
76
77    protected virtual void RegisterAlgorithmEvents() {
78      var algorithm = MetaSolver.Algorithm;
79      if (algorithm == null) return;
80
81      algorithm.ExecutionStateChanged += OnExecutionStateChanged;
82      algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
83      algorithm.Prepared += OnPrepared;
84      algorithm.Started += OnStarted;
85      algorithm.Paused += OnPaused;
86      algorithm.Stopped += OnStopped;
87      algorithm.ExceptionOccurred += OnExceptionOccurred;
88    }
89
90    protected virtual void MetaSolver_AlgorithmChanged(object sender, EventArgs e) {
91      RegisterAlgorithmEvents();
92    }
93
94    #region IOptimizer Members
95    public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
96    public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
97    public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
98    public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
99
100    public void Prepare() { Prepare(false); }
101    public void Prepare(bool clearRuns) { Orchestrator.Prepare(clearRuns); }
102    public void Start() { Orchestrator.StartAsync(); }
103    public void Pause() { Orchestrator.Pause(); }
104    public void Stop() { Orchestrator.Stop(); }
105    #endregion
106
107    private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
108    private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
109    private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
110    private void OnStarted(object sender, EventArgs e) { OnStarted(); }
111    private void OnPaused(object sender, EventArgs e) { OnPaused(); }
112    private void OnStopped(object sender, EventArgs e) { OnStopped(); }
113    private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
114
115    public event EventHandler ExecutionStateChanged;
116    private void OnExecutionStateChanged() {
117      var handler = ExecutionStateChanged;
118      if (handler != null) handler(this, EventArgs.Empty);
119    }
120
121    public event EventHandler ExecutionTimeChanged;
122    private void OnExecutionTimeChanged() {
123      var handler = ExecutionTimeChanged;
124      if (handler != null) handler(this, EventArgs.Empty);
125    }
126
127    public event EventHandler Prepared;
128    private void OnPrepared() {
129      var handler = Prepared;
130      if (handler != null) handler(this, EventArgs.Empty);
131    }
132
133    public event EventHandler Started;
134    private void OnStarted() {
135      var handler = Started;
136      if (handler != null) handler(this, EventArgs.Empty);
137    }
138
139    public event EventHandler Paused;
140    private void OnPaused() {
141      var handler = Paused;
142      if (handler != null) handler(this, EventArgs.Empty);
143    }
144
145    public event EventHandler Stopped;
146    private void OnStopped() {
147      var handler = Stopped;
148      if (handler != null) handler(this, EventArgs.Empty);
149    }
150
151    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
152    private void OnExceptionOccurred(Exception exception) {
153      var handler = ExceptionOccurred;
154      if (handler != null) handler(this, new EventArgs<Exception>(exception));
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.