Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Operators/PriorityDispatchingMetaOptEvaluator.cs @ 8858

Last change on this file since 8858 was 8858, checked in by svonolfe, 11 years ago

changed name of the tardiness penalty result (#1955)

File size: 4.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Data;
11using HeuristicLab.Common;
12using System.Threading;
13using System.Threading.Tasks;
14using HeuristicLab.Encodings.PermutationEncoding;
15using HeuristicLab.Encodings.RealVectorEncoding;
16using System.Diagnostics;
17
18namespace 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}
Note: See TracBrowser for help on using the repository browser.