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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Core.Networks;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
|
---|
31 | [Item("LrpNetwork", "An abstract base class for LRP optimization networks.")]
|
---|
32 | [StorableClass]
|
---|
33 | public abstract class LrpNetwork : Network {
|
---|
34 | #region
|
---|
35 | protected const string OrchestratorNodeName = "Orchestrator";
|
---|
36 | protected const string MetaSolverNodeName = "MetaSolver";
|
---|
37 | protected const string FlpSolverNodeName = "FlpSolver";
|
---|
38 | protected const string VrpSolverNodeName = "VrpSolver";
|
---|
39 | #endregion
|
---|
40 |
|
---|
41 | #region Nodes
|
---|
42 | public LrpOrchestratorNode Orchestrator {
|
---|
43 | get { return (LrpOrchestratorNode)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 FlpSolver {
|
---|
62 | get { return (OrchestratedAlgorithmNode)Nodes[FlpSolverNodeName]; }
|
---|
63 | protected set {
|
---|
64 | if (Nodes.ContainsKey(FlpSolverNodeName))
|
---|
65 | throw new InvalidOperationException("FlpSolver already set.");
|
---|
66 | Nodes.Add(value);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public OrchestratedAlgorithmNode VrpSolver {
|
---|
71 | get { return (OrchestratedAlgorithmNode)Nodes[VrpSolverNodeName]; }
|
---|
72 | protected set {
|
---|
73 | if (Nodes.ContainsKey(VrpSolverNodeName))
|
---|
74 | throw new InvalidOperationException("VrpSolver already set.");
|
---|
75 | Nodes.Add(value);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | #endregion
|
---|
79 |
|
---|
80 | [StorableConstructor]
|
---|
81 | protected LrpNetwork(bool deserializing) : base(deserializing) { }
|
---|
82 | protected LrpNetwork(LrpNetwork original, Cloner cloner) : base(original, cloner) {
|
---|
83 | RegisterEvents();
|
---|
84 | }
|
---|
85 | protected LrpNetwork() : this("LrpNetwork") { }
|
---|
86 | protected LrpNetwork(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 | }
|
---|