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.Algorithms.CMAEvolutionStrategy;
|
---|
25 | using HeuristicLab.Algorithms.LocalSearch;
|
---|
26 | using HeuristicLab.Algorithms.ParameterlessPopulationPyramid;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Core.Networks;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief {
|
---|
35 | [Item("TtpNetwork2", "Version 2 of a TTP optimization network.")]
|
---|
36 | [Creatable("Optimization Networks")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class TtpNetwork2 : Network, IOptimizer {
|
---|
39 | #region Nodes
|
---|
40 | public TtpOrchestratorNode2 Orchestrator {
|
---|
41 | get { return (TtpOrchestratorNode2)Nodes["Orchestrator"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public OrchestratedAlgorithmNode MetaSolver {
|
---|
45 | get { return (OrchestratedAlgorithmNode)Nodes["MetaSolver"]; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public OrchestratedAlgorithmNode TspSolver {
|
---|
49 | get { return (OrchestratedAlgorithmNode)Nodes["TspSolver"]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public OrchestratedAlgorithmNode KspSolver {
|
---|
53 | get { return (OrchestratedAlgorithmNode)Nodes["KspSolver"]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | [StorableConstructor]
|
---|
58 | private TtpNetwork2(bool deserializing) : base(deserializing) { }
|
---|
59 | private TtpNetwork2(TtpNetwork2 original, Cloner cloner) : base(original, cloner) {
|
---|
60 | RegisterEvents();
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void RegisterEvents() {
|
---|
64 | MetaSolver.Algorithm.ExecutionStateChanged += OnExecutionStateChanged;
|
---|
65 | MetaSolver.Algorithm.ExecutionTimeChanged += OnExecutionTimeChanged;
|
---|
66 | MetaSolver.Algorithm.Prepared += OnPrepared;
|
---|
67 | MetaSolver.Algorithm.Started += OnStarted;
|
---|
68 | MetaSolver.Algorithm.Paused += OnPaused;
|
---|
69 | MetaSolver.Algorithm.Stopped += OnStopped;
|
---|
70 | MetaSolver.Algorithm.ExceptionOccurred += OnExceptionOccurred;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public TtpNetwork2() : base("TtpNetwork2") {
|
---|
74 | var orchestratorNode = new TtpOrchestratorNode2("Orchestrator");
|
---|
75 | Nodes.Add(orchestratorNode);
|
---|
76 |
|
---|
77 | var metaSolverNode = new OrchestratedAlgorithmNode("MetaSolver");
|
---|
78 | var cmaes = new CMAEvolutionStrategy();
|
---|
79 | cmaes.Problem = new VariegationProblem();
|
---|
80 | cmaes.MaximumGenerations = 80;
|
---|
81 | metaSolverNode.Algorithm = cmaes;
|
---|
82 | orchestratorNode.MetaSolverOrchestrationPort.ConnectedPort = metaSolverNode.OrchestrationPort;
|
---|
83 | Nodes.Add(metaSolverNode);
|
---|
84 |
|
---|
85 | var tspSolverNode = new OrchestratedAlgorithmNode("TspSolver");
|
---|
86 | var ls = new LocalSearch();
|
---|
87 | ls.Problem = orchestratorNode.KspParameter.Value;
|
---|
88 | ls.MaximumIterations.Value = 100;
|
---|
89 | ls.SampleSize.Value = 2000;
|
---|
90 | tspSolverNode.Algorithm = ls;
|
---|
91 | orchestratorNode.TspSolverOrchestrationPort.ConnectedPort = tspSolverNode.OrchestrationPort;
|
---|
92 | Nodes.Add(tspSolverNode);
|
---|
93 |
|
---|
94 | var kspSolverNode = new OrchestratedAlgorithmNode("KspSolver");
|
---|
95 | var p3 = new ParameterlessPopulationPyramid();
|
---|
96 | p3.Problem = new LootProfitProblem();
|
---|
97 | p3.MaximumRuntime = 3;
|
---|
98 | kspSolverNode.Algorithm = p3;
|
---|
99 | orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort;
|
---|
100 | Nodes.Add(kspSolverNode);
|
---|
101 |
|
---|
102 | #region Import
|
---|
103 | DoubleMatrix tspCoordinates;
|
---|
104 | IntValue kspCapacity; IntArray kspItemWeights; IntArray kspItemValues;
|
---|
105 | IntArray ttpAvailability; DoubleValue ttpMinSpeed; DoubleValue ttpMaxSpeed; DoubleValue ttpRentingRatio;
|
---|
106 | TtpImporter.ImportTtpInstance(@"ttp-instances\berlin52-ttp\berlin52_n51_uncorr_01.ttp",
|
---|
107 | out tspCoordinates,
|
---|
108 | out kspCapacity, out kspItemValues, out kspItemWeights,
|
---|
109 | out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);
|
---|
110 |
|
---|
111 | var tsp = orchestratorNode.TspParameter.Value;
|
---|
112 | tsp.Coordinates = tspCoordinates;
|
---|
113 |
|
---|
114 | var ksp = orchestratorNode.KspParameter.Value;
|
---|
115 | ksp.KnapsackCapacity = kspCapacity;
|
---|
116 | ksp.Encoding.Length = kspItemValues.Length;
|
---|
117 | ksp.Values = kspItemValues;
|
---|
118 | ksp.Weights = kspItemWeights;
|
---|
119 |
|
---|
120 | orchestratorNode.AvailabilityParameter.Value = ttpAvailability;
|
---|
121 | orchestratorNode.MinSpeedParameter.Value = ttpMinSpeed;
|
---|
122 | orchestratorNode.MaxSpeedParameter.Value = ttpMaxSpeed;
|
---|
123 | orchestratorNode.RentingRatioParameter.Value = ttpRentingRatio;
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | RegisterEvents();
|
---|
127 | }
|
---|
128 |
|
---|
129 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
130 | return new TtpNetwork2(this, cloner);
|
---|
131 | }
|
---|
132 |
|
---|
133 | [StorableHook(HookType.AfterDeserialization)]
|
---|
134 | private void AfterDeserialization() {
|
---|
135 | RegisterEvents();
|
---|
136 | }
|
---|
137 |
|
---|
138 | #region IOptimizer Members
|
---|
139 | public RunCollection Runs { get { return MetaSolver.Algorithm.Runs; } }
|
---|
140 | public IEnumerable<IOptimizer> NestedOptimizers { get { yield break; } }
|
---|
141 | public ExecutionState ExecutionState { get { return MetaSolver.Algorithm.ExecutionState; } }
|
---|
142 | public TimeSpan ExecutionTime { get { return MetaSolver.Algorithm.ExecutionTime; } }
|
---|
143 |
|
---|
144 | public void Prepare(bool clearRuns) { Prepare(); }
|
---|
145 | public void Prepare() { Orchestrator.Prepare(); }
|
---|
146 | public void Start() {
|
---|
147 | if (MetaSolver.Algorithm.ExecutionState == ExecutionState.Prepared)
|
---|
148 | Orchestrator.Prepare();
|
---|
149 | Orchestrator.StartAsync();
|
---|
150 | }
|
---|
151 | public void Pause() { Orchestrator.Pause(); }
|
---|
152 | public void Stop() { Orchestrator.Stop(); }
|
---|
153 | #endregion
|
---|
154 |
|
---|
155 | private void OnExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); }
|
---|
156 | private void OnExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); }
|
---|
157 | private void OnPrepared(object sender, EventArgs e) { OnPrepared(); }
|
---|
158 | private void OnStarted(object sender, EventArgs e) { OnStarted(); }
|
---|
159 | private void OnPaused(object sender, EventArgs e) { OnPaused(); }
|
---|
160 | private void OnStopped(object sender, EventArgs e) { OnStopped(); }
|
---|
161 | private void OnExceptionOccurred(object sender, EventArgs<Exception> e) { OnExceptionOccurred(e.Value); }
|
---|
162 |
|
---|
163 | public event EventHandler ExecutionStateChanged;
|
---|
164 | private void OnExecutionStateChanged() {
|
---|
165 | var handler = ExecutionStateChanged;
|
---|
166 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
167 | }
|
---|
168 |
|
---|
169 | public event EventHandler ExecutionTimeChanged;
|
---|
170 | private void OnExecutionTimeChanged() {
|
---|
171 | var handler = ExecutionTimeChanged;
|
---|
172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
173 | }
|
---|
174 |
|
---|
175 | public event EventHandler Prepared;
|
---|
176 | private void OnPrepared() {
|
---|
177 | var handler = Prepared;
|
---|
178 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
179 | }
|
---|
180 |
|
---|
181 | public event EventHandler Started;
|
---|
182 | private void OnStarted() {
|
---|
183 | var handler = Started;
|
---|
184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
185 | }
|
---|
186 |
|
---|
187 | public event EventHandler Paused;
|
---|
188 | private void OnPaused() {
|
---|
189 | var handler = Paused;
|
---|
190 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
191 | }
|
---|
192 |
|
---|
193 | public event EventHandler Stopped;
|
---|
194 | private void OnStopped() {
|
---|
195 | var handler = Stopped;
|
---|
196 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
|
---|
200 | private void OnExceptionOccurred(Exception exception) {
|
---|
201 | var handler = ExceptionOccurred;
|
---|
202 | if (handler != null) handler(this, new EventArgs<Exception>(exception));
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|