Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on the evaluators and on the project structure of the VRP plugin - WIP (#1177)

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