Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added analyzers and views (#1177)

File size: 4.4 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      base.EvaluateTour(eval, instance, tour);
54
55      ITimeWindowedProblemInstance vrptw = instance as ITimeWindowedProblemInstance;
56
57      double time = 0.0;
58      double waitingTime = 0.0;
59      double serviceTime = 0.0;
60      double tardiness = 0.0;
61
62      //simulate a tour, start and end at depot
63      for (int i = 0; i <= tour.Stops.Count; i++) {
64        int start = 0;
65        if (i > 0)
66          start = tour.Stops[i - 1];
67        int end = 0;
68        if (i < tour.Stops.Count)
69          end = tour.Stops[i];
70
71        //drive there
72        double currentDistace = vrptw.GetDistance(start, end);
73        time += currentDistace;
74
75        //check if it was serviced on time
76        if (time > vrptw.DueTime[end])
77          tardiness += time - vrptw.DueTime[end];
78
79        //wait
80        double currentWaitingTime = 0.0;
81        if (time < vrptw.ReadyTime[end])
82          currentWaitingTime = vrptw.ReadyTime[end] - time;
83        waitingTime += currentWaitingTime;
84        time += currentWaitingTime;
85
86        //service
87        double currentServiceTime = vrptw.ServiceTime[end];
88        serviceTime += currentServiceTime;
89        time += currentServiceTime;
90      }
91
92      (eval as CVRPTWEvaluation).Tardiness = tardiness;
93      (eval as CVRPTWEvaluation).TravelTime = time;
94
95      double penalty = tardiness * vrptw.TardinessPenalty.Value;
96      eval.Penalty += penalty;
97      eval.Quality += penalty;
98      eval.Quality += time * vrptw.TimeFactor.Value;
99    }
100
101    protected override void InitResultParameters() {
102      base.InitResultParameters();
103
104      TardinessParameter.ActualValue = new DoubleValue(0);
105      TravelTimeParameter.ActualValue = new DoubleValue(0);
106    }
107
108    protected override void UpdateResultParameters(VRPEvaluation tourEvaluation) {
109      base.UpdateResultParameters(tourEvaluation);
110
111      TardinessParameter.ActualValue.Value += (tourEvaluation as CVRPTWEvaluation).Tardiness;
112      TravelTimeParameter.ActualValue.Value += (tourEvaluation as CVRPTWEvaluation).TravelTime;
113    }
114   
115    [StorableConstructor]
116    protected CVRPTWEvaluator(bool deserializing) : base(deserializing) { }
117
118    public CVRPTWEvaluator() {
119      Parameters.Add(new LookupParameter<DoubleValue>("Tardiness", "The tardiness."));
120      Parameters.Add(new LookupParameter<DoubleValue>("TravelTime", "The travel time."));
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.