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 class PriorityDispatchingMetaOptEvaluator : SingleSuccessorOperator, ISingleObjectiveEvaluator {
|
---|
22 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
23 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public IValueLookupParameter<DoubleArray> VectorParameter {
|
---|
27 | get { return (IValueLookupParameter<DoubleArray>)Parameters["Vector"]; }
|
---|
28 | }
|
---|
29 |
|
---|
30 | public IValueParameter<ItemList<PickupDeliverySimulation>> SimulationParameter {
|
---|
31 | get { return (IValueParameter<ItemList<PickupDeliverySimulation>>)Parameters["Simulation"]; }
|
---|
32 | }
|
---|
33 |
|
---|
34 | public IValueParameter<IntValue> RepetitionsParameter {
|
---|
35 | get { return (IValueParameter<IntValue>)Parameters["Repetitions"]; }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public PriorityDispatchingMetaOptEvaluator() {
|
---|
39 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Result of the evaluation of a solution."));
|
---|
40 | Parameters.Add(new ValueLookupParameter<DoubleArray>("Vector", "The priority dispatching parameters."));
|
---|
41 | Parameters.Add(new ValueParameter<ItemList<PickupDeliverySimulation>>("Simulation", "The PDP simulation"));
|
---|
42 | Parameters.Add(new ValueParameter<IntValue>("Repetitions", "The number of repetitions", new IntValue(1)));
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected PriorityDispatchingMetaOptEvaluator(bool deserializing) : base(deserializing) { }
|
---|
47 | protected PriorityDispatchingMetaOptEvaluator(PriorityDispatchingMetaOptEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
48 |
|
---|
49 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
50 | return new PriorityDispatchingMetaOptEvaluator(this, cloner);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override IOperation Apply() {
|
---|
54 | double quality = 0;
|
---|
55 |
|
---|
56 | ItemList<PickupDeliverySimulation> simulations = SimulationParameter.Value;
|
---|
57 | int repetitions = 0;
|
---|
58 |
|
---|
59 | DoubleArray vector = VectorParameter.ActualValue;
|
---|
60 |
|
---|
61 | object locker = new object();
|
---|
62 | var options = new ParallelOptions();
|
---|
63 | //options.MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount - 1, 1);
|
---|
64 | Parallel.ForEach<PickupDeliverySimulation>(simulations, options, originalSimulation => {
|
---|
65 | int index = simulations.IndexOf(originalSimulation);
|
---|
66 |
|
---|
67 | PickupDeliverySimulation simulation = originalSimulation.Clone() as PickupDeliverySimulation;
|
---|
68 | PriorityDispatching dispatching = simulation.OptimizationParameter.Value as PriorityDispatching;
|
---|
69 | dispatching.WeightsParameter.Value = new RealVector(vector.ToArray());
|
---|
70 |
|
---|
71 | EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
|
---|
72 | EventHandler handler = new EventHandler(delegate(object o, EventArgs e) {
|
---|
73 | waitHandle.Set();
|
---|
74 | });
|
---|
75 |
|
---|
76 | int executions = 0;
|
---|
77 | while (executions < RepetitionsParameter.Value.Value) {
|
---|
78 | waitHandle.Reset();
|
---|
79 |
|
---|
80 | simulation.Prepared += handler;
|
---|
81 | simulation.Prepare(true);
|
---|
82 | waitHandle.WaitOne();
|
---|
83 | simulation.Prepared -= handler;
|
---|
84 |
|
---|
85 | waitHandle.Reset();
|
---|
86 |
|
---|
87 | simulation.Stopped += handler;
|
---|
88 | simulation.Start();
|
---|
89 | waitHandle.WaitOne();
|
---|
90 | simulation.Stopped -= handler;
|
---|
91 |
|
---|
92 | Debug.Assert(simulation.Results.ContainsKey("Finished"));
|
---|
93 | double leadTime = (simulation.Results["LeadTime"].Value as DoubleValue).Value;
|
---|
94 | double tardiness = (simulation.Results["TardinessPenalty"].Value as DoubleValue).Value;
|
---|
95 |
|
---|
96 | lock (locker) {
|
---|
97 | double runQuality = leadTime + tardiness;
|
---|
98 |
|
---|
99 | quality += runQuality;
|
---|
100 | repetitions++;
|
---|
101 | }
|
---|
102 |
|
---|
103 | executions++;
|
---|
104 | }
|
---|
105 | });
|
---|
106 |
|
---|
107 | QualityParameter.ActualValue = new DoubleValue(quality / (double)repetitions);
|
---|
108 |
|
---|
109 | return base.Apply();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|