Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: worked on optimization networks

  • added abstract base classes for ttp networks/orchestrators
  • removed ttp networks/orchestrators from HeuristicLab.Networks.IntegratedOptimization
  • runs can now be cleared when preparing OrchestratedAlgorithmNodes
File size: 5.8 KB
RevLine 
[14610]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() : base("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    }
75
76    protected virtual void MetaSolver_AlgorithmChanged(object sender, EventArgs e) {
77      var algorithm = MetaSolver.Algorithm;
78      if (algorithm == null) return;
79
80      algorithm.ExecutionStateChanged += OnExecutionStateChanged;
81      algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
82      algorithm.Prepared += OnPrepared;
83      algorithm.Started += OnStarted;
84      algorithm.Paused += OnPaused;
85      algorithm.Stopped += OnStopped;
86      algorithm.ExceptionOccurred += OnExceptionOccurred;
87    }
88
89    #region IOptimizer Members
90    public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
91    public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
92    public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
93    public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
94
95    public void Prepare() { Prepare(false); }
96    public void Prepare(bool clearRuns) { Orchestrator.Prepare(clearRuns); }
97    public void Start() { Orchestrator.StartAsync(); }
98    public void Pause() { Orchestrator.Pause(); }
99    public void Stop() { Orchestrator.Stop(); }
100    #endregion
101
102    private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
103    private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
104    private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
105    private void OnStarted(object sender, EventArgs e) { OnStarted(); }
106    private void OnPaused(object sender, EventArgs e) { OnPaused(); }
107    private void OnStopped(object sender, EventArgs e) { OnStopped(); }
108    private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
109
110    public event EventHandler ExecutionStateChanged;
111    private void OnExecutionStateChanged() {
112      var handler = ExecutionStateChanged;
113      if (handler != null) handler(this, EventArgs.Empty);
114    }
115
116    public event EventHandler ExecutionTimeChanged;
117    private void OnExecutionTimeChanged() {
118      var handler = ExecutionTimeChanged;
119      if (handler != null) handler(this, EventArgs.Empty);
120    }
121
122    public event EventHandler Prepared;
123    private void OnPrepared() {
124      var handler = Prepared;
125      if (handler != null) handler(this, EventArgs.Empty);
126    }
127
128    public event EventHandler Started;
129    private void OnStarted() {
130      var handler = Started;
131      if (handler != null) handler(this, EventArgs.Empty);
132    }
133
134    public event EventHandler Paused;
135    private void OnPaused() {
136      var handler = Paused;
137      if (handler != null) handler(this, EventArgs.Empty);
138    }
139
140    public event EventHandler Stopped;
141    private void OnStopped() {
142      var handler = Stopped;
143      if (handler != null) handler(this, EventArgs.Empty);
144    }
145
146    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
147    private void OnExceptionOccurred(Exception exception) {
148      var handler = ExceptionOccurred;
149      if (handler != null) handler(this, new EventArgs<Exception>(exception));
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.