Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork.cs @ 17215

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

#2205: worked on optimization networks

  • added lrp network 2
  • minor code changes
File size: 6.2 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 Constants
35    protected const string OrchestratorNodeName = "Orchestrator";
36    protected const string MetaSolverNodeName = "MetaSolver";
37    protected const string TspSolverNodeName = "TspSolver";
38    protected const string KspSolverNodeName = "KspSolver";
39    #endregion
40
41    #region Nodes
42    public TtpOrchestratorNode Orchestrator {
43      get { return (TtpOrchestratorNode)Nodes[OrchestratorNodeName]; }
44      protected set {
45        if (Nodes.ContainsKey(OrchestratorNodeName))
46          throw new InvalidOperationException("Orchestrator already set.");
47        Nodes.Add(value);
48      }
49    }
50
51    public OrchestratedAlgorithmNode MetaSolver {
52      get { return (OrchestratedAlgorithmNode)Nodes[MetaSolverNodeName]; }
53      protected set {
54        if (Nodes.ContainsKey(MetaSolverNodeName))
55          throw new InvalidOperationException("MetaSolver already set.");
56        Nodes.Add(value);
57        RegisterEvents();
58      }
59    }
60
61    public OrchestratedAlgorithmNode TspSolver {
62      get { return (OrchestratedAlgorithmNode)Nodes[TspSolverNodeName]; }
63      protected set {
64        if (Nodes.ContainsKey(TspSolverNodeName))
65          throw new InvalidOperationException("TspSolver already set.");
66        Nodes.Add(value);
67      }
68    }
69
70    public OrchestratedAlgorithmNode KspSolver {
71      get { return (OrchestratedAlgorithmNode)Nodes[KspSolverNodeName]; }
72      protected set {
73        if (Nodes.ContainsKey(KspSolverNodeName))
74          throw new InvalidOperationException("KspSolver already set.");
75        Nodes.Add(value);
76      }
77    }
78    #endregion
79
80    [StorableConstructor]
81    protected TtpNetwork(bool deserializing) : base(deserializing) { }
82    protected TtpNetwork(TtpNetwork original, Cloner cloner) : base(original, cloner) {
83      RegisterEvents();
84    }
85    protected TtpNetwork() : this("TtpNetwork") { }
86    protected TtpNetwork(string name) : base(name) { }
87
88    [StorableHook(HookType.AfterDeserialization)]
89    private void AfterDeserialization() {
90      RegisterEvents();
91    }
92
93    private void RegisterEvents() {
94      MetaSolver.AlgorithmChanged += MetaSolver_AlgorithmChanged;
95      RegisterAlgorithmEvents();
96    }
97
98    private void MetaSolver_AlgorithmChanged(object sender, EventArgs e) {
99      RegisterAlgorithmEvents();
100    }
101
102    private void RegisterAlgorithmEvents() {
103      var algorithm = MetaSolver.Algorithm;
104      if (algorithm == null) return;
105
106      algorithm.ExecutionStateChanged += (s, e) => OnExecutionStateChanged();
107      algorithm.ExecutionTimeChanged += (s, e) => OnExecutionTimeChanged();
108      algorithm.Prepared += (s, e) => OnPrepared();
109      algorithm.Started += (s, e) => OnStarted();
110      algorithm.Paused += (s, e) => OnPaused();
111      algorithm.Stopped += (s, e) => OnStopped();
112      algorithm.ExceptionOccurred += (s, e) => OnExceptionOccurred(e.Value);
113    }
114
115    #region IOptimizer Members
116    public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
117    public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
118    public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
119    public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
120
121    public void Prepare() { Prepare(false); }
122    public void Prepare(bool clearRuns) { Orchestrator.Prepare(clearRuns); }
123    public void Start() { Orchestrator.StartAsync(); }
124    public void Pause() { Orchestrator.Pause(); }
125    public void Stop() { Orchestrator.Stop(); }
126
127    public event EventHandler ExecutionStateChanged;
128    private void OnExecutionStateChanged() {
129      var handler = ExecutionStateChanged;
130      if (handler != null) handler(this, EventArgs.Empty);
131    }
132
133    public event EventHandler ExecutionTimeChanged;
134    private void OnExecutionTimeChanged() {
135      var handler = ExecutionTimeChanged;
136      if (handler != null) handler(this, EventArgs.Empty);
137    }
138
139    public event EventHandler Prepared;
140    private void OnPrepared() {
141      var handler = Prepared;
142      if (handler != null) handler(this, EventArgs.Empty);
143    }
144
145    public event EventHandler Started;
146    private void OnStarted() {
147      var handler = Started;
148      if (handler != null) handler(this, EventArgs.Empty);
149    }
150
151    public event EventHandler Paused;
152    private void OnPaused() {
153      var handler = Paused;
154      if (handler != null) handler(this, EventArgs.Empty);
155    }
156
157    public event EventHandler Stopped;
158    private void OnStopped() {
159      var handler = Stopped;
160      if (handler != null) handler(this, EventArgs.Empty);
161    }
162
163    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
164    private void OnExceptionOccurred(Exception exception) {
165      var handler = ExceptionOccurred;
166      if (handler != null) handler(this, new EventArgs<Exception>(exception));
167    }
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.