1 | using System;
|
---|
2 | using System.Collections.Concurrent;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Threading;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Core.Networks;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
11 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
12 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
13 | using HeuristicLab.Operators;
|
---|
14 | using HeuristicLab.Optimization;
|
---|
15 | using HeuristicLab.Parameters;
|
---|
16 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
17 | using HeuristicLab.Problems.Knapsack;
|
---|
18 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
19 |
|
---|
20 | namespace HeuristicLab.Networks.IntegratedOptimization {
|
---|
21 | [Item("TtpOrchestratorNode5", "An abstract base class for an orchestrator node for the TTP.")]
|
---|
22 | [StorableClass]
|
---|
23 | public sealed class TtpOrchestratorNode5 : OrchestratorNode {
|
---|
24 | #region Constants
|
---|
25 | private const string TspParameterName = "TSP";
|
---|
26 | private const string KspParameterName = "KSP";
|
---|
27 | private const string AvailabilityParameterName = "Availability";
|
---|
28 | private const string MinSpeedParameterName = "MinSpeed";
|
---|
29 | private const string MaxSpeedParameterName = "MaxSpeed";
|
---|
30 | private const string RentingRatioParameterName = "RentingRatio";
|
---|
31 | private const string IterationsParameterName = "Iterations";
|
---|
32 | private const string MetaSolverName = "MetaSolver";
|
---|
33 | private const string TspSolverName = "TspSolver";
|
---|
34 | private const string KspSolverName = "KspSolver";
|
---|
35 | private const string TspResultsResultName = "TspResults";
|
---|
36 | private const string KspResultsResultName = "KspResults";
|
---|
37 | private const string TtpResultsResultName = "TtpResults";
|
---|
38 | #endregion
|
---|
39 |
|
---|
40 | #region Thread Results
|
---|
41 | private object locker = new object();
|
---|
42 | private ConcurrentDictionary<int, ResultCollection> tspThreadResults = new ConcurrentDictionary<int, ResultCollection>();
|
---|
43 | private ConcurrentDictionary<int, ResultCollection> kspThreadResults = new ConcurrentDictionary<int, ResultCollection>();
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | #region Parameters
|
---|
47 | public IValueParameter<IntValue> IterationsParameter {
|
---|
48 | get { return (IValueParameter<IntValue>)Parameters[IterationsParameterName]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IValueParameter<TravelingSalesmanProblem> TspParameter {
|
---|
52 | get { return (IValueParameter<TravelingSalesmanProblem>)Parameters[TspParameterName]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IValueParameter<BinaryKnapsackProblem> KspParameter {
|
---|
56 | get { return (IValueParameter<BinaryKnapsackProblem>)Parameters[KspParameterName]; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public IValueParameter<IntArray> AvailabilityParameter {
|
---|
60 | get { return (IValueParameter<IntArray>)Parameters[AvailabilityParameterName]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IValueParameter<DoubleValue> MinSpeedParameter {
|
---|
64 | get { return (IValueParameter<DoubleValue>)Parameters[MinSpeedParameterName]; }
|
---|
65 | }
|
---|
66 |
|
---|
67 | public IValueParameter<DoubleValue> MaxSpeedParameter {
|
---|
68 | get { return (IValueParameter<DoubleValue>)Parameters[MaxSpeedParameterName]; }
|
---|
69 | }
|
---|
70 |
|
---|
71 | public IValueParameter<DoubleValue> RentingRatioParameter {
|
---|
72 | get { return (IValueParameter<DoubleValue>)Parameters[RentingRatioParameterName]; }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region Ports
|
---|
77 | public IConfigurationPort MetaSolverOrchestrationPort {
|
---|
78 | get { return (IConfigurationPort)Ports[MetaSolverName + OrchestrationPortNameSuffix]; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public IConfigurationPort MetaSolverEvaluationPort {
|
---|
82 | get { return (IConfigurationPort)Ports[MetaSolverName + EvaluationPortNameSuffix]; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | public IConfigurationPort TspSolverOrchestrationPort {
|
---|
86 | get { return (IConfigurationPort)Ports[TspSolverName + OrchestrationPortNameSuffix]; }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public IConfigurationPort TspSolverEvaluationPort {
|
---|
90 | get { return (IConfigurationPort)Ports[TspSolverName + EvaluationPortNameSuffix]; }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public IConfigurationPort KspSolverOrchestrationPort {
|
---|
94 | get { return (IConfigurationPort)Ports[KspSolverName + OrchestrationPortNameSuffix]; }
|
---|
95 | }
|
---|
96 |
|
---|
97 | public IConfigurationPort KspSolverEvaluationPort {
|
---|
98 | get { return (IConfigurationPort)Ports[KspSolverName + EvaluationPortNameSuffix]; }
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | #region Results
|
---|
103 | public ItemList<ResultCollection> TspResults {
|
---|
104 | get { return (ItemList<ResultCollection>)Results[TspResultsResultName].Value; }
|
---|
105 | }
|
---|
106 |
|
---|
107 | public ItemList<ResultCollection> KspResults {
|
---|
108 | get { return (ItemList<ResultCollection>)Results[KspResultsResultName].Value; }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public ItemList<ResultCollection> TtpResults {
|
---|
112 | get { return (ItemList<ResultCollection>)Results[TtpResultsResultName].Value; }
|
---|
113 | }
|
---|
114 | #endregion
|
---|
115 |
|
---|
116 | [StorableConstructor]
|
---|
117 | private TtpOrchestratorNode5(bool deserializing) : base(deserializing) { }
|
---|
118 | private TtpOrchestratorNode5(TtpOrchestratorNode5 original, Cloner cloner) : base(original, cloner) { }
|
---|
119 | public TtpOrchestratorNode5() : this("TtpOrchestratorNode5") { }
|
---|
120 | public TtpOrchestratorNode5(string name) : base(name) {
|
---|
121 | #region Configure Parameters
|
---|
122 | Parameters.Add(new ValueParameter<IntValue>(IterationsParameterName, new IntValue(20)));
|
---|
123 | Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem()));
|
---|
124 | Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem()));
|
---|
125 | Parameters.Add(new ValueParameter<IntArray>(AvailabilityParameterName));
|
---|
126 | Parameters.Add(new ValueParameter<DoubleValue>(MinSpeedParameterName, new DoubleValue(0.1)));
|
---|
127 | Parameters.Add(new ValueParameter<DoubleValue>(MaxSpeedParameterName, new DoubleValue(1.0)));
|
---|
128 | Parameters.Add(new ValueParameter<DoubleValue>(RentingRatioParameterName, new DoubleValue(0.5)));
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region Configure Ports
|
---|
132 | AddOrchestrationPort<VariegationProblem>(MetaSolverName);
|
---|
133 | AddEvaluationPort<RealVector>(MetaSolverName, "RealVector", "Quality");
|
---|
134 | AddOrchestrationPort<TourProfitProblem>(TspSolverName);
|
---|
135 | AddEvaluationPort<Permutation>(TspSolverName, "TSPTour", "TSPTourLength");
|
---|
136 | AddOrchestrationPort<BinaryKnapsackProblem>(KspSolverName);
|
---|
137 | AddEvaluationPort<BinaryVector>(KspSolverName, "KnapsackSolution", "Quality");
|
---|
138 |
|
---|
139 | MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
|
---|
140 | TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged;
|
---|
141 | KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged;
|
---|
142 | #endregion
|
---|
143 | }
|
---|
144 |
|
---|
145 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
146 | return new TtpOrchestratorNode5(this, cloner);
|
---|
147 | }
|
---|
148 |
|
---|
149 | public override void Prepare() {
|
---|
150 | tspThreadResults.Clear();
|
---|
151 | kspThreadResults.Clear();
|
---|
152 | Results.Clear();
|
---|
153 | Results.Add(new Result(TspResultsResultName, new ItemList<ResultCollection>()));
|
---|
154 | Results.Add(new Result(KspResultsResultName, new ItemList<ResultCollection>()));
|
---|
155 | Results.Add(new Result(TtpResultsResultName, new ItemList<ResultCollection>()));
|
---|
156 |
|
---|
157 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
158 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.QualityAdaption);
|
---|
159 | var problem = new VariegationProblem();
|
---|
160 | problem.Encoding.Length = KspParameter.Value.Length;
|
---|
161 | problem.Encoding.Bounds = new DoubleMatrix(new[,] { { -1.0, 1.0 } });
|
---|
162 | metaMsg["Problem"] = problem;
|
---|
163 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
164 | }
|
---|
165 |
|
---|
166 | public override void Start() {
|
---|
167 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
168 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
|
---|
169 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public override void Pause() {
|
---|
173 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
174 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
|
---|
175 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
176 | }
|
---|
177 |
|
---|
178 | public override void Stop() {
|
---|
179 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
180 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
181 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected override void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) {
|
---|
185 | var messageActions = new Dictionary<IMessagePort, Action<IMessage>>();
|
---|
186 | messageActions.Add(MetaSolverOrchestrationPort, MetaSolverOrchestrationPortMessage);
|
---|
187 | messageActions.Add(MetaSolverEvaluationPort, MetaSolverEvaluationPortMessage);
|
---|
188 | messageActions.Add(TspSolverOrchestrationPort, TspSolverOrchestrationPortMessage);
|
---|
189 | messageActions.Add(TspSolverEvaluationPort, TspSolverEvaluationPortMessage);
|
---|
190 | messageActions.Add(KspSolverOrchestrationPort, KspSolverOrchestrationPortMessage);
|
---|
191 | messageActions.Add(KspSolverEvaluationPort, KspSolverEvaluationPortMessage);
|
---|
192 |
|
---|
193 | messageActions[port](message);
|
---|
194 |
|
---|
195 | base.ProcessMessage(message, port, token);
|
---|
196 | }
|
---|
197 |
|
---|
198 | #region MetaSolver Message Handling
|
---|
199 | private void MetaSolverOrchestrationPortMessage(IMessage message) { }
|
---|
200 |
|
---|
201 | private void MetaSolverEvaluationPortMessage(IMessage message) {
|
---|
202 | var factors = (RealVector)message["RealVector"];
|
---|
203 |
|
---|
204 | var ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone();
|
---|
205 | for (int i = 0; i < factors.Length; i++)
|
---|
206 | ksp.Values[i] = (int)Math.Ceiling(ksp.Values[i] * factors[i]);
|
---|
207 |
|
---|
208 | var kspMsg = KspSolverOrchestrationPort.PrepareMessage();
|
---|
209 | kspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
|
---|
210 | kspMsg["Problem"] = ksp;
|
---|
211 | KspSolverOrchestrationPort.SendMessage(kspMsg);
|
---|
212 |
|
---|
213 | var kspResults = kspThreadResults[Thread.CurrentThread.ManagedThreadId];
|
---|
214 | var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value;
|
---|
215 | var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone();
|
---|
216 | var kspPenalty = new DoubleValue(0.0);
|
---|
217 | var kspWeights = (IntArray)KspParameter.Value.Weights.Clone();
|
---|
218 | var kspValues = (IntArray)KspParameter.Value.Values.Clone();
|
---|
219 | var bestKspQuality = KnapsackEvaluator.Apply(bestKspSolution, kspCapacity, kspPenalty, kspWeights, kspValues).Quality;
|
---|
220 | var loot = new KnapsackSolution(bestKspSolution, bestKspQuality, kspCapacity, kspWeights, kspValues);
|
---|
221 | kspResults.Add(new Result("Best KSP Solution", loot));
|
---|
222 |
|
---|
223 | var tspMsg = TspSolverOrchestrationPort.PrepareMessage();
|
---|
224 | tspMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Prepare | OrchestrationMessage.Start);
|
---|
225 | var tpp = new TourProfitProblem {
|
---|
226 | Tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(),
|
---|
227 | Ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(),
|
---|
228 | FixedKspSolution = bestKspSolution,
|
---|
229 | Availability = AvailabilityParameter.Value.ToArray(),
|
---|
230 | RentingRatio = RentingRatioParameter.Value.Value,
|
---|
231 | MinSpeed = MinSpeedParameter.Value.Value,
|
---|
232 | MaxSpeed = MaxSpeedParameter.Value.Value
|
---|
233 | };
|
---|
234 | tpp.Encoding.Length = TspParameter.Value.Coordinates.Rows;
|
---|
235 | tspMsg["Problem"] = tpp;
|
---|
236 | TspSolverOrchestrationPort.SendMessage(tspMsg);
|
---|
237 |
|
---|
238 | var tspResults = tspThreadResults[Thread.CurrentThread.ManagedThreadId];
|
---|
239 | var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value;
|
---|
240 | var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone();
|
---|
241 | var tour = new PathTSPTour(coordinates, bestTspSolution, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution)));
|
---|
242 |
|
---|
243 | #region Analyze
|
---|
244 | double objectiveValue = TtpUtils.EvaluateTtp(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),
|
---|
245 | AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value);
|
---|
246 | ((DoubleValue)message["Quality"]).Value = objectiveValue;
|
---|
247 |
|
---|
248 | //var ttpResults = new ResultCollection();
|
---|
249 | //ttpResults.Add(new Result("Quality", new DoubleValue(objectiveValue)));
|
---|
250 | //ttpResults.Add(new Result("Tour", tour));
|
---|
251 | //ttpResults.Add(new Result("Loot", loot));
|
---|
252 |
|
---|
253 | lock (locker) {
|
---|
254 | //TtpResults.Add(ttpResults);
|
---|
255 | //TspResults.Add(tspResults);
|
---|
256 | //KspResults.Add(kspResults);
|
---|
257 |
|
---|
258 | IResult bestQuality;
|
---|
259 | if (!Results.TryGetValue("Best TTP Quality", out bestQuality)) {
|
---|
260 | Results.Add(new Result("Best TTP Quality", new DoubleValue(objectiveValue)));
|
---|
261 | Results.Add(new Result("Best Tour", tour));
|
---|
262 | Results.Add(new Result("Best Loot", loot));
|
---|
263 | } else if (((DoubleValue)bestQuality.Value).Value < objectiveValue) {
|
---|
264 | ((DoubleValue)bestQuality.Value).Value = objectiveValue;
|
---|
265 | Results["Best Tour"].Value = tour;
|
---|
266 | Results["Best Loot"].Value = loot;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | #endregion
|
---|
270 | }
|
---|
271 | #endregion
|
---|
272 |
|
---|
273 | #region TspSolver Message Handling
|
---|
274 | private void TspSolverOrchestrationPortMessage(IMessage message) {
|
---|
275 | var results = (ResultCollection)message["Results"];
|
---|
276 | if (results.ContainsKey("Best TSP Solution")) {
|
---|
277 | tspThreadResults[Thread.CurrentThread.ManagedThreadId] = results;
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | private void TspSolverEvaluationPortMessage(IMessage message) { }
|
---|
282 | #endregion
|
---|
283 |
|
---|
284 | #region KspSolver Message Handling
|
---|
285 | private void KspSolverOrchestrationPortMessage(IMessage message) {
|
---|
286 | var results = (ResultCollection)message["Results"];
|
---|
287 | if (results.ContainsKey("Best Solution")) {
|
---|
288 | kspThreadResults[Thread.CurrentThread.ManagedThreadId] = results;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | private void KspSolverEvaluationPortMessage(IMessage message) { }
|
---|
293 | #endregion
|
---|
294 |
|
---|
295 | #region Event Handlers
|
---|
296 | private void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
297 | if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
298 |
|
---|
299 | var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
300 | if (node == null) return;
|
---|
301 |
|
---|
302 | var hook = new HookOperator { Name = "Meta Eval Hook" };
|
---|
303 | hook.Parameters.Add(new LookupParameter<RealVector>("RealVector") { Hidden = true });
|
---|
304 | hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
|
---|
305 | node.EvalHook = hook;
|
---|
306 |
|
---|
307 | node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
|
---|
308 | node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
|
---|
309 | node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
|
---|
310 | }
|
---|
311 |
|
---|
312 | private void TspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
313 | if (TspSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
314 |
|
---|
315 | var node = TspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
316 | if (node == null) return;
|
---|
317 |
|
---|
318 | var hook = new HookOperator { Name = "TSP Eval Hook" };
|
---|
319 | hook.Parameters.Add(new LookupParameter<Permutation>("TSPTour") { Hidden = true });
|
---|
320 | hook.Parameters.Add(new LookupParameter<DoubleValue>("TSPTourLength") { Hidden = true });
|
---|
321 | node.EvalHook = hook;
|
---|
322 |
|
---|
323 | node.OrchestrationPort.CloneParametersFromPort(TspSolverOrchestrationPort);
|
---|
324 | node.EvaluationPort.CloneParametersFromPort(TspSolverEvaluationPort);
|
---|
325 | node.EvaluationPort.ConnectedPort = TspSolverEvaluationPort;
|
---|
326 | }
|
---|
327 |
|
---|
328 | private void KspSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
329 | if (KspSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
330 |
|
---|
331 | var node = KspSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
332 | if (node == null) return;
|
---|
333 |
|
---|
334 | var hook = new HookOperator { Name = "KSP Eval Hook" };
|
---|
335 | hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = true });
|
---|
336 | hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
|
---|
337 | node.EvalHook = hook;
|
---|
338 |
|
---|
339 | node.OrchestrationPort.CloneParametersFromPort(KspSolverOrchestrationPort);
|
---|
340 | node.EvaluationPort.CloneParametersFromPort(KspSolverEvaluationPort);
|
---|
341 | node.EvaluationPort.ConnectedPort = KspSolverEvaluationPort;
|
---|
342 | }
|
---|
343 | #endregion
|
---|
344 | }
|
---|
345 | }
|
---|