Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8812


Ignore:
Timestamp:
10/16/12 13:07:54 (12 years ago)
Author:
svonolfe
Message:

Improved threading of evaluator (#1955)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Operators/PriorityDispatchingMetaOptEvaluator.cs

    r8808 r8812  
    4747      double quality = 0;
    4848
    49       ItemList<PickupDeliverySimulation> simulations = SimulationParameter.Value;
     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
    5058      int repetitions = 0;
    51 
    5259      object locker = new object();
    5360      var options = new ParallelOptions();
    5461      //options.MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount - 1, 1);
    55       Parallel.ForEach<PickupDeliverySimulation>(simulations, options, originalSimulation => {
    56         int index = simulations.IndexOf(originalSimulation);
     62      Parallel.ForEach<PickupDeliverySimulation>(simulations, options, simulation => {
     63        int index = simulations.IndexOf(simulation);
     64        int executions = 0;
    5765
    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();                       
     66        using (EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset)) {
     67          EventHandler handler = new EventHandler(delegate(object o, EventArgs e) {
     68            waitHandle.Set();
    6569          });
    6670
    67         int executions = 0;
    68         while (executions < RepetitionsParameter.Value.Value) {
    69           waitHandle.Reset();
     71          while (executions < RepetitionsParameter.Value.Value) {
     72            waitHandle.Reset();
    7073
    71           simulation.Prepared += handler;
    72           simulation.Prepare(true);
    73           waitHandle.WaitOne();
    74           simulation.Prepared -= handler;
     74            simulation.Prepared += handler;
     75            simulation.Prepare(true);
     76            waitHandle.WaitOne();
     77            simulation.Prepared -= handler;
    7578
    76           waitHandle.Reset();
     79            waitHandle.Reset();
    7780
    78           simulation.Stopped += handler;
    79           simulation.Start();
    80           waitHandle.WaitOne();
    81           simulation.Stopped -= handler;
     81            simulation.Stopped += handler;
     82            simulation.Start();
     83            waitHandle.WaitOne();
     84            simulation.Stopped -= handler;
    8285
    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            if (!simulation.Results.ContainsKey("Finished")) {
     87              throw new Exception("Simulation did not finish");
     88            }
     89            executions++;
    8690
    87           lock (locker) {
    88             double runQuality = leadTime + tardiness;
     91            double leadTime = (simulation.Results["LeadTime"].Value as DoubleValue).Value;
     92            double tardiness = (simulation.Results["TardinessPenalty"].Value as DoubleValue).Value;
    8993
    90             quality += runQuality;
    91             repetitions++;
     94            lock (locker) {
     95              double runQuality = leadTime + tardiness;
     96
     97              quality += runQuality;
     98              repetitions++;
     99            }
    92100          }
    93 
    94           executions++;
    95101        }
    96102      });
Note: See TracChangeset for help on using the changeset viewer.