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 System.Linq;
|
---|
25 | using System.Text.RegularExpressions;
|
---|
26 | using System.Threading;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Core.Networks;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
32 | using HeuristicLab.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
35 | using HeuristicLab.Problems.FacilityLocation;
|
---|
36 | using HeuristicLab.Problems.VehicleRouting;
|
---|
37 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
38 | using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
|
---|
39 |
|
---|
40 | namespace HeuristicLab.Networks.IntegratedOptimization.LocationRouting {
|
---|
41 | [Item("LrpOrchestratorNode", "An abstract base class for orchestrators used in LRP optimization networks.")]
|
---|
42 | [StorableClass]
|
---|
43 | public abstract class LrpOrchestratorNode : OrchestratorNode {
|
---|
44 | #region Constants
|
---|
45 | protected const string InstanceParameterName = "Instance";
|
---|
46 | protected const string FlpParameterName = "FLP";
|
---|
47 | protected const string VrpParameterName = "VRP";
|
---|
48 | protected const string MetaSolverName = "MetaSolver";
|
---|
49 | protected const string FlpSolverName = "FlpSolver";
|
---|
50 | protected const string VrpSolverName = "VrpSolver";
|
---|
51 | protected const string OrchestrationPortNameSuffix = "OrchestrationPort";
|
---|
52 | protected const string EvaluationPortNameSuffix = "EvaluationPort";
|
---|
53 | #endregion
|
---|
54 |
|
---|
55 | protected CancellationTokenSource cts;
|
---|
56 |
|
---|
57 | [Storable]
|
---|
58 | protected int nrOfDepots, nrOfCustomers;
|
---|
59 | [Storable]
|
---|
60 | protected double[,] depotCoordinates = new double[0, 0], customerCoordinates = new double[0, 0];
|
---|
61 | [Storable]
|
---|
62 | protected LrpUtils.DistanceType distanceType;
|
---|
63 | [Storable]
|
---|
64 | protected double[] depotCapacities = new double[0], customerDemands = new double[0], depotCosts = new double[0];
|
---|
65 | [Storable]
|
---|
66 | protected double vehicleCapacity, vehicleCost;
|
---|
67 |
|
---|
68 | #region Parameters
|
---|
69 | public IFixedValueParameter<TextFileValue> InstanceParameter {
|
---|
70 | get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public IValueParameter<FacilityLocationProblem> FlpParameter {
|
---|
74 | get { return (IValueParameter<FacilityLocationProblem>)Parameters[FlpParameterName]; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public IValueParameter<VehicleRoutingProblem> VrpParameter {
|
---|
78 | get { return (IValueParameter<VehicleRoutingProblem>)Parameters[VrpParameterName]; }
|
---|
79 | }
|
---|
80 | #endregion
|
---|
81 |
|
---|
82 | #region Ports
|
---|
83 | public IMessagePort MetaSolverOrchestrationPort {
|
---|
84 | get { return (IMessagePort)Ports[MetaSolverName + OrchestrationPortNameSuffix]; }
|
---|
85 | protected set {
|
---|
86 | string portName = MetaSolverName + OrchestrationPortNameSuffix;
|
---|
87 | if (Ports.ContainsKey(portName))
|
---|
88 | throw new InvalidOperationException(portName + " already set.");
|
---|
89 | Ports.Add(value);
|
---|
90 | value.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IMessagePort MetaSolverEvaluationPort {
|
---|
95 | get { return (IMessagePort)Ports[MetaSolverName + EvaluationPortNameSuffix]; }
|
---|
96 | protected set {
|
---|
97 | string portName = MetaSolverName + EvaluationPortNameSuffix;
|
---|
98 | if (Ports.ContainsKey(portName))
|
---|
99 | throw new InvalidOperationException(portName + " already set.");
|
---|
100 | Ports.Add(value);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | public IMessagePort FlpSolverOrchestrationPort {
|
---|
105 | get { return (IMessagePort)Ports[FlpSolverName + OrchestrationPortNameSuffix]; }
|
---|
106 | protected set {
|
---|
107 | string portName = FlpSolverName + OrchestrationPortNameSuffix;
|
---|
108 | if (Ports.ContainsKey(portName))
|
---|
109 | throw new InvalidOperationException(portName + " already set.");
|
---|
110 | Ports.Add(value);
|
---|
111 | value.ConnectedPortChanged += FlpSolverOrchestrationPort_ConnectedPortChanged;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public IMessagePort FlpSolverEvaluationPort {
|
---|
116 | get { return (IMessagePort)Ports[FlpSolverName + EvaluationPortNameSuffix]; }
|
---|
117 | protected set {
|
---|
118 | string portName = FlpSolverName + EvaluationPortNameSuffix;
|
---|
119 | if (Ports.ContainsKey(portName))
|
---|
120 | throw new InvalidOperationException(portName + " already set.");
|
---|
121 | Ports.Add(value);
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | public IMessagePort VrpSolverOrchestrationPort {
|
---|
126 | get { return (IMessagePort)Ports[VrpSolverName + OrchestrationPortNameSuffix]; }
|
---|
127 | protected set {
|
---|
128 | string portName = VrpSolverName + OrchestrationPortNameSuffix;
|
---|
129 | if (Ports.ContainsKey(portName))
|
---|
130 | throw new InvalidOperationException(portName + " already set.");
|
---|
131 | Ports.Add(value);
|
---|
132 | value.ConnectedPortChanged += VrpSolverOrchestrationPort_ConnectedPortChanged;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public IMessagePort VrpSolverEvaluationPort {
|
---|
137 | get { return (IMessagePort)Ports[VrpSolverName + EvaluationPortNameSuffix]; }
|
---|
138 | protected set {
|
---|
139 | string portName = VrpSolverName + EvaluationPortNameSuffix;
|
---|
140 | if (Ports.ContainsKey(portName))
|
---|
141 | throw new InvalidOperationException(portName + " already set.");
|
---|
142 | Ports.Add(value);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | [StorableConstructor]
|
---|
148 | protected LrpOrchestratorNode(bool deserializing) : base(deserializing) { }
|
---|
149 | protected LrpOrchestratorNode(LrpOrchestratorNode original, Cloner cloner) : base(original, cloner) {
|
---|
150 | nrOfDepots = original.nrOfDepots;
|
---|
151 | nrOfCustomers = original.nrOfCustomers;
|
---|
152 | depotCoordinates = (double[,])original.depotCoordinates.Clone();
|
---|
153 | customerCoordinates = (double[,])original.customerCoordinates.Clone();
|
---|
154 | distanceType = original.distanceType;
|
---|
155 | depotCapacities = (double[])original.depotCapacities.Clone();
|
---|
156 | customerDemands = (double[])original.customerDemands.Clone();
|
---|
157 | depotCosts = (double[])original.depotCosts.Clone();
|
---|
158 | vehicleCapacity = original.vehicleCapacity;
|
---|
159 | vehicleCost = original.vehicleCost;
|
---|
160 |
|
---|
161 | RegisterEvents();
|
---|
162 | }
|
---|
163 | protected LrpOrchestratorNode() : this("LrpOrchestratorNode") { }
|
---|
164 | protected LrpOrchestratorNode(string name) : base(name) {
|
---|
165 | Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName));
|
---|
166 | Parameters.Add(new ValueParameter<FacilityLocationProblem>(FlpParameterName, new FacilityLocationProblem()));
|
---|
167 | Parameters.Add(new ValueParameter<VehicleRoutingProblem>(VrpParameterName, new VehicleRoutingProblem() { ProblemInstance = new CVRPProblemInstance() }));
|
---|
168 |
|
---|
169 | InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged;
|
---|
170 | }
|
---|
171 |
|
---|
172 | [StorableHook(HookType.AfterDeserialization)]
|
---|
173 | private void AfterDeserialization() {
|
---|
174 | RegisterEvents();
|
---|
175 | }
|
---|
176 |
|
---|
177 | private void RegisterEvents() {
|
---|
178 | InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged;
|
---|
179 | MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged;
|
---|
180 | FlpSolverOrchestrationPort.ConnectedPortChanged += FlpSolverOrchestrationPort_ConnectedPortChanged;
|
---|
181 | VrpSolverOrchestrationPort.ConnectedPortChanged += VrpSolverOrchestrationPort_ConnectedPortChanged;
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected virtual void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) {
|
---|
185 | string filePath = InstanceParameter.Value.Value;
|
---|
186 | LrpUtils.Import(filePath, out nrOfDepots, out nrOfCustomers,
|
---|
187 | out depotCoordinates, out customerCoordinates,
|
---|
188 | out distanceType,
|
---|
189 | out depotCapacities, out customerDemands, out depotCosts,
|
---|
190 | out vehicleCapacity, out vehicleCost);
|
---|
191 |
|
---|
192 | var flp = FlpParameter.Value;
|
---|
193 | flp.CustomerDemandsParameter.Value = new DoubleArray(customerDemands);
|
---|
194 | flp.DeliveryCostsParameter.Value = new DoubleMatrix(LrpUtils.GetFlpDeliveryCosts(depotCoordinates, customerCoordinates, distanceType));
|
---|
195 | flp.DepotCapacitiesParameter.Value = new DoubleArray(depotCapacities);
|
---|
196 | flp.Encoding.Length = nrOfCustomers;
|
---|
197 | flp.OpeningCostsParameter.Value = new DoubleArray(depotCosts);
|
---|
198 |
|
---|
199 | var vrpInstance = (CVRPProblemInstance)VrpParameter.Value.ProblemInstance;
|
---|
200 | vrpInstance.Capacity.Value = vehicleCapacity;
|
---|
201 | vrpInstance.FleetUsageFactor.Value = vehicleCost;
|
---|
202 | vrpInstance.OverloadPenalty.Value = vehicleCost * 1000.0;
|
---|
203 |
|
---|
204 | var crossover = VrpParameter.Value.OperatorsParameter.Value.OfType<MultiVRPSolutionCrossover>().Single(x => x.Name == "MultiVRPSolutionCrossover");
|
---|
205 | foreach (var c in crossover.Operators)
|
---|
206 | crossover.Operators.SetItemCheckedState(c, c.Name.StartsWith("Potvin"));
|
---|
207 | var mutator = VrpParameter.Value.OperatorsParameter.Value.OfType<MultiVRPSolutionManipulator>().Single(x => x.Name == "MultiVRPSolutionManipulator");
|
---|
208 | foreach (var m in mutator.Operators)
|
---|
209 | mutator.Operators.SetItemCheckedState(m, Regex.IsMatch(m.Name, @"Potvin(One|Two).*"));
|
---|
210 |
|
---|
211 | Prepare();
|
---|
212 | }
|
---|
213 |
|
---|
214 | protected virtual void MetaSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
215 | if (MetaSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
216 |
|
---|
217 | var node = MetaSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
218 | if (node == null) return;
|
---|
219 |
|
---|
220 | var hook = new HookOperator { Name = "Meta Eval Hook" };
|
---|
221 | hook.Parameters.Add(new LookupParameter<RealVector>("RealVector") { Hidden = true });
|
---|
222 | hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = true });
|
---|
223 | node.EvalHook = hook;
|
---|
224 |
|
---|
225 | node.OrchestrationPort.CloneParametersFromPort(MetaSolverOrchestrationPort);
|
---|
226 | node.EvaluationPort.CloneParametersFromPort(MetaSolverEvaluationPort);
|
---|
227 | node.EvaluationPort.ConnectedPort = MetaSolverEvaluationPort;
|
---|
228 | }
|
---|
229 |
|
---|
230 | protected virtual void FlpSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
231 | if (FlpSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
232 |
|
---|
233 | var node = FlpSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
234 | if (node == null) return;
|
---|
235 |
|
---|
236 | node.OrchestrationPort.CloneParametersFromPort(FlpSolverOrchestrationPort);
|
---|
237 | }
|
---|
238 |
|
---|
239 | protected virtual void VrpSolverOrchestrationPort_ConnectedPortChanged(object sender, EventArgs e) {
|
---|
240 | if (VrpSolverOrchestrationPort.ConnectedPort == null) return;
|
---|
241 |
|
---|
242 | var node = VrpSolverOrchestrationPort.ConnectedPort.Parent as OrchestratedAlgorithmNode;
|
---|
243 | if (node == null) return;
|
---|
244 |
|
---|
245 | node.OrchestrationPort.CloneParametersFromPort(VrpSolverOrchestrationPort);
|
---|
246 | }
|
---|
247 |
|
---|
248 | public override void Prepare(bool clearRuns = false) {
|
---|
249 | Results.Clear();
|
---|
250 | }
|
---|
251 |
|
---|
252 | public override void Start() {
|
---|
253 | cts = new CancellationTokenSource();
|
---|
254 |
|
---|
255 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
256 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Start);
|
---|
257 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
258 | }
|
---|
259 |
|
---|
260 | public override void Pause() {
|
---|
261 | cts.Cancel();
|
---|
262 |
|
---|
263 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
264 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Pause);
|
---|
265 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
266 |
|
---|
267 | var flpMsg = FlpSolverOrchestrationPort.PrepareMessage();
|
---|
268 | flpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
269 | FlpSolverOrchestrationPort.SendMessage(flpMsg);
|
---|
270 |
|
---|
271 | var vrpMsg = VrpSolverOrchestrationPort.PrepareMessage();
|
---|
272 | vrpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
273 | VrpSolverOrchestrationPort.SendMessage(vrpMsg);
|
---|
274 | }
|
---|
275 |
|
---|
276 | public override void Stop() {
|
---|
277 | cts.Cancel();
|
---|
278 |
|
---|
279 | var metaMsg = MetaSolverOrchestrationPort.PrepareMessage();
|
---|
280 | metaMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
281 | MetaSolverOrchestrationPort.SendMessage(metaMsg);
|
---|
282 |
|
---|
283 | var flpMsg = FlpSolverOrchestrationPort.PrepareMessage();
|
---|
284 | flpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
285 | FlpSolverOrchestrationPort.SendMessage(flpMsg);
|
---|
286 |
|
---|
287 | var vrpMsg = VrpSolverOrchestrationPort.PrepareMessage();
|
---|
288 | vrpMsg["OrchestrationMessage"] = new EnumValue<OrchestrationMessage>(OrchestrationMessage.Stop);
|
---|
289 | VrpSolverOrchestrationPort.SendMessage(vrpMsg);
|
---|
290 | }
|
---|
291 |
|
---|
292 | protected override void ProcessMessage(IMessage message, IMessagePort port, CancellationToken token) {
|
---|
293 | var messageActions = new Dictionary<IMessagePort, Action<IMessage>>();
|
---|
294 | messageActions.Add(MetaSolverOrchestrationPort, MetaSolverOrchestrationPortMessage);
|
---|
295 | messageActions.Add(MetaSolverEvaluationPort, MetaSolverEvaluationPortMessage);
|
---|
296 | messageActions.Add(FlpSolverOrchestrationPort, FlpSolverOrchestrationPortMessage);
|
---|
297 | messageActions.Add(VrpSolverOrchestrationPort, VrpSolverOrchestrationPortMessage);
|
---|
298 |
|
---|
299 | messageActions[port](message);
|
---|
300 |
|
---|
301 | base.ProcessMessage(message, port, token);
|
---|
302 | }
|
---|
303 |
|
---|
304 | #region MetaSolver Message Handling
|
---|
305 | protected virtual void MetaSolverOrchestrationPortMessage(IMessage message) { }
|
---|
306 |
|
---|
307 | protected virtual void MetaSolverEvaluationPortMessage(IMessage message) { }
|
---|
308 | #endregion
|
---|
309 |
|
---|
310 | #region FlpSolver Message Handling
|
---|
311 | protected virtual void FlpSolverOrchestrationPortMessage(IMessage message) { }
|
---|
312 |
|
---|
313 | protected virtual void FlpSolverEvaluationPortMessage(IMessage message) { }
|
---|
314 | #endregion
|
---|
315 |
|
---|
316 | #region VrpSolver Message Handling
|
---|
317 | protected virtual void VrpSolverOrchestrationPortMessage(IMessage message) { }
|
---|
318 |
|
---|
319 | private void VrpSolverEvaluationPortMessage(IMessage message) { }
|
---|
320 | #endregion
|
---|
321 | }
|
---|
322 | }
|
---|