Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPTW/CVRPTWEvaluator.cs @ 4377

Last change on this file since 4377 was 4377, checked in by svonolfe, 14 years ago

Fixed performance issue in Evaluator (#1177)

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Problems.VehicleRouting.Interfaces;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Core;
29using HeuristicLab.Parameters;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34using HeuristicLab.Problems.VehicleRouting.Encodings;
35
36namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
37  [Item("CVRPTWEvaluator", "Represents a single depot CVRPTW evaluator.")]
38  [StorableClass]
39  public class CVRPTWEvaluator: CVRPEvaluator {
40    public ILookupParameter<DoubleValue> TardinessParameter {
41      get { return (ILookupParameter<DoubleValue>)Parameters["Tardiness"]; }
42    }
43
44    public ILookupParameter<DoubleValue> TravelTimeParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["TravelTime"]; }
46    }
47
48    protected override VRPEvaluation CreateTourEvaluation() {
49      return new CVRPTWEvaluation();
50    }
51
52    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour) {
53      IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
54      DoubleArray demand = instance.Demand;
55
56      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
57      DoubleArray dueTime = vrptw.DueTime;
58      DoubleArray readyTime = vrptw.ReadyTime;
59      DoubleArray serviceTimes = vrptw.ServiceTime;
60
61      double time = 0.0;
62      double waitingTime = 0.0;
63      double serviceTime = 0.0;
64      double tardiness = 0.0;
65      double delivered = 0.0;
66      double overweight = 0.0;
67      double distance = 0.0;
68
69      //simulate a tour, start and end at depot
70      for (int i = 0; i <= tour.Stops.Count; i++) {
71        int start = 0;
72        if (i > 0)
73          start = tour.Stops[i - 1];
74        int end = 0;
75        if (i < tour.Stops.Count)
76          end = tour.Stops[i];
77
78        //drive there
79        double currentDistace = vrptw.GetDistance(start, end);
80        time += currentDistace;
81        distance += currentDistace;
82
83        //check if it was serviced on time
84        if (time > dueTime[end])
85          tardiness += time - dueTime[end];
86
87        //wait
88        double currentWaitingTime = 0.0;
89        if (time < readyTime[end])
90          currentWaitingTime = readyTime[end] - time;
91        waitingTime += currentWaitingTime;
92        time += currentWaitingTime;
93
94        //service
95        double currentServiceTime = serviceTimes[end];
96        serviceTime += currentServiceTime;
97        time += currentServiceTime;
98        delivered += demand[end];
99      }
100
101      eval.Quality += instance.FleetUsageFactor.Value;
102      eval.Quality += instance.DistanceFactor.Value * distance;
103      eval.Distance = distance;
104      eval.VehicleUtilization = 1;
105
106      double capacity = cvrpInstance.Capacity.Value;
107      if (delivered > capacity) {
108        overweight = delivered - capacity;
109      }
110
111      (eval as CVRPEvaluation).Overload = overweight;
112      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
113      eval.Penalty += penalty;
114      eval.Quality += penalty;
115
116      (eval as CVRPTWEvaluation).Tardiness = tardiness;
117      (eval as CVRPTWEvaluation).TravelTime = time;
118
119      penalty = tardiness * vrptw.TardinessPenalty.Value;
120      eval.Penalty += penalty;
121      eval.Quality += penalty;
122      eval.Quality += time * vrptw.TimeFactor.Value;
123    }
124
125    protected override void InitResultParameters() {
126      base.InitResultParameters();
127
128      TardinessParameter.ActualValue = new DoubleValue(0);
129      TravelTimeParameter.ActualValue = new DoubleValue(0);
130    }
131
132    protected override void UpdateResultParameters(VRPEvaluation tourEvaluation) {
133      base.UpdateResultParameters(tourEvaluation);
134
135      TardinessParameter.ActualValue.Value += (tourEvaluation as CVRPTWEvaluation).Tardiness;
136      TravelTimeParameter.ActualValue.Value += (tourEvaluation as CVRPTWEvaluation).TravelTime;
137    }
138   
139    [StorableConstructor]
140    protected CVRPTWEvaluator(bool deserializing) : base(deserializing) { }
141
142    public CVRPTWEvaluator() {
143      Parameters.Add(new LookupParameter<DoubleValue>("Tardiness", "The tardiness."));
144      Parameters.Add(new LookupParameter<DoubleValue>("TravelTime", "The travel time."));
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.