1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Parameters;
|
---|
10 | using HeuristicLab.Data;
|
---|
11 | using HeuristicLab.Common;
|
---|
12 | using System.Threading;
|
---|
13 | using System.Threading.Tasks;
|
---|
14 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
15 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
16 | using System.Diagnostics;
|
---|
17 |
|
---|
18 | namespace HeuristicLab.PDPSimulation.Operators {
|
---|
19 | [Item("PriorityDispatchingMetaOptEvaluator", "Metaoptimization of the priority dispatching parameters for the PDP simulation.")]
|
---|
20 | [StorableClass]
|
---|
21 | public abstract class PriorityDispatchingMetaOptEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluator {
|
---|
22 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
23 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public IValueParameter<ItemList<PickupDeliverySimulation>> SimulationParameter {
|
---|
27 | get { return (IValueParameter<ItemList<PickupDeliverySimulation>>)Parameters["Simulation"]; }
|
---|
28 | }
|
---|
29 |
|
---|
30 | public IValueParameter<IntValue> RepetitionsParameter {
|
---|
31 | get { return (IValueParameter<IntValue>)Parameters["Repetitions"]; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public PriorityDispatchingMetaOptEvaluator() {
|
---|
35 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Result of the evaluation of a solution."));
|
---|
36 | Parameters.Add(new ValueParameter<ItemList<PickupDeliverySimulation>>("Simulation", "The PDP simulation"));
|
---|
37 | Parameters.Add(new ValueParameter<IntValue>("Repetitions", "The number of repetitions", new IntValue(1)));
|
---|
38 | }
|
---|
39 |
|
---|
40 | [StorableConstructor]
|
---|
41 | protected PriorityDispatchingMetaOptEvaluator(bool deserializing) : base(deserializing) { }
|
---|
42 | protected PriorityDispatchingMetaOptEvaluator(PriorityDispatchingMetaOptEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
43 |
|
---|
44 | protected abstract void Parameterize(PriorityDispatching dispatching);
|
---|
45 |
|
---|
46 | public override IOperation Apply() {
|
---|
47 | double quality = 0;
|
---|
48 |
|
---|
49 | List<PickupDeliverySimulation> simulations = new List<PickupDeliverySimulation>();
|
---|
50 | foreach (var originalSimulation in SimulationParameter.Value) {
|
---|
51 | PickupDeliverySimulation simulation = originalSimulation.Clone() as PickupDeliverySimulation;
|
---|
52 | PriorityDispatching dispatching = simulation.OptimizationParameter.Value as PriorityDispatching;
|
---|
53 | Parameterize(dispatching);
|
---|
54 |
|
---|
55 | simulations.Add(simulation);
|
---|
56 | }
|
---|
57 |
|
---|
58 | int repetitions = 0;
|
---|
59 | object locker = new object();
|
---|
60 | var options = new ParallelOptions();
|
---|
61 | //options.MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount - 1, 1);
|
---|
62 | Parallel.ForEach<PickupDeliverySimulation>(simulations, options, simulation => {
|
---|
63 | int index = simulations.IndexOf(simulation);
|
---|
64 | int executions = 0;
|
---|
65 |
|
---|
66 | using (EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset)) {
|
---|
67 | EventHandler handler = new EventHandler(delegate(object o, EventArgs e) {
|
---|
68 | waitHandle.Set();
|
---|
69 | });
|
---|
70 |
|
---|
71 | while (executions < RepetitionsParameter.Value.Value) {
|
---|
72 | waitHandle.Reset();
|
---|
73 |
|
---|
74 | simulation.Prepared += handler;
|
---|
75 | simulation.Prepare(true);
|
---|
76 | waitHandle.WaitOne();
|
---|
77 | simulation.Prepared -= handler;
|
---|
78 |
|
---|
79 | waitHandle.Reset();
|
---|
80 |
|
---|
81 | simulation.Stopped += handler;
|
---|
82 | simulation.Start();
|
---|
83 | waitHandle.WaitOne();
|
---|
84 | simulation.Stopped -= handler;
|
---|
85 |
|
---|
86 | if (!simulation.Results.ContainsKey("Finished")) {
|
---|
87 | throw new Exception("Simulation did not finish");
|
---|
88 | }
|
---|
89 | executions++;
|
---|
90 |
|
---|
91 | double leadTime = (simulation.Results["LeadTime"].Value as DoubleValue).Value;
|
---|
92 | double tardiness = (simulation.Results["TotalTardinessPenalty"].Value as DoubleValue).Value;
|
---|
93 |
|
---|
94 | lock (locker) {
|
---|
95 | double runQuality = leadTime + tardiness;
|
---|
96 |
|
---|
97 | quality += runQuality;
|
---|
98 | repetitions++;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 | });
|
---|
103 |
|
---|
104 | QualityParameter.ActualValue = new DoubleValue(quality / (double)repetitions);
|
---|
105 |
|
---|
106 | return base.Apply();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|