Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8784 was 8783, checked in by svonolfe, 12 years ago

modified fitness function of dial a ride (#1955)

File size: 4.5 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 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}
Note: See TracBrowser for help on using the repository browser.