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 | ItemList<PickupDeliverySimulation> simulations = SimulationParameter.Value;
|
---|
50 | int repetitions = 0;
|
---|
51 |
|
---|
52 | object locker = new object();
|
---|
53 | var options = new ParallelOptions();
|
---|
54 | //options.MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount - 1, 1);
|
---|
55 | Parallel.ForEach<PickupDeliverySimulation>(simulations, options, originalSimulation => {
|
---|
56 | int index = simulations.IndexOf(originalSimulation);
|
---|
57 |
|
---|
58 | PickupDeliverySimulation simulation = originalSimulation.Clone() as PickupDeliverySimulation;
|
---|
59 | PriorityDispatching dispatching = simulation.OptimizationParameter.Value as PriorityDispatching;
|
---|
60 | Parameterize(dispatching);
|
---|
61 |
|
---|
62 | EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
|
---|
63 | EventHandler handler = new EventHandler(delegate(object o, EventArgs e) {
|
---|
64 | waitHandle.Set();
|
---|
65 | });
|
---|
66 |
|
---|
67 | int executions = 0;
|
---|
68 | while (executions < RepetitionsParameter.Value.Value) {
|
---|
69 | waitHandle.Reset();
|
---|
70 |
|
---|
71 | simulation.Prepared += handler;
|
---|
72 | simulation.Prepare(true);
|
---|
73 | waitHandle.WaitOne();
|
---|
74 | simulation.Prepared -= handler;
|
---|
75 |
|
---|
76 | waitHandle.Reset();
|
---|
77 |
|
---|
78 | simulation.Stopped += handler;
|
---|
79 | simulation.Start();
|
---|
80 | waitHandle.WaitOne();
|
---|
81 | simulation.Stopped -= handler;
|
---|
82 |
|
---|
83 | Debug.Assert(simulation.Results.ContainsKey("Finished"));
|
---|
84 | double leadTime = (simulation.Results["LeadTime"].Value as DoubleValue).Value;
|
---|
85 | double tardiness = (simulation.Results["TardinessPenalty"].Value as DoubleValue).Value;
|
---|
86 |
|
---|
87 | lock (locker) {
|
---|
88 | double runQuality = leadTime + tardiness;
|
---|
89 |
|
---|
90 | quality += runQuality;
|
---|
91 | repetitions++;
|
---|
92 | }
|
---|
93 |
|
---|
94 | executions++;
|
---|
95 | }
|
---|
96 | });
|
---|
97 |
|
---|
98 | QualityParameter.ActualValue = new DoubleValue(quality / (double)repetitions);
|
---|
99 |
|
---|
100 | return base.Apply();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|