Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/SingleDepotVRP/CVRP/CVRPEvaluator.cs @ 7895

Last change on this file since 7895 was 7276, checked in by svonolfe, 13 years ago

Improved incremental evaluation (#1177)

File size: 5.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;
35using HeuristicLab.Common;
36
37namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
38  [Item("CVRPEvaluator", "Represents a single depot CVRP evaluator.")]
39  [StorableClass]
40  public class CVRPEvaluator: VRPEvaluator {
41    public ILookupParameter<DoubleValue> OverloadParameter {
42      get { return (ILookupParameter<DoubleValue>)Parameters["Overload"]; }
43    }
44
45    protected override VRPEvaluation CreateTourEvaluation() {
46      return new CVRPEvaluation();
47    }
48
49    protected override void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
50      TourInsertionInfo tourInfo = new TourInsertionInfo(solution.GetVehicleAssignment(solution.GetTourIndex(tour))); ;
51      eval.InsertionInfo.AddTourInsertionInfo(tourInfo);
52      double originalQuality = eval.Quality;
53     
54      IHomogenousCapacitatedProblemInstance cvrpInstance = instance as IHomogenousCapacitatedProblemInstance;
55      DoubleArray demand = instance.Demand;
56
57      double delivered = 0.0;
58      double overweight = 0.0;
59      double distance = 0.0;
60
61      double capacity = cvrpInstance.Capacity.Value;
62      for (int i = 0; i <= tour.Stops.Count; i++) {
63        int end = 0;
64        if (i < tour.Stops.Count)
65          end = tour.Stops[i];
66
67        delivered += demand[end];
68      }
69
70      double spareCapacity = capacity - delivered;
71
72      //simulate a tour, start and end at depot
73      for (int i = 0; i <= tour.Stops.Count; i++) {
74        int start = 0;
75        if (i > 0)
76          start = tour.Stops[i - 1];
77        int end = 0;
78        if (i < tour.Stops.Count)
79          end = tour.Stops[i];
80
81        //drive there
82        double currentDistace = instance.GetDistance(start, end, solution);
83        distance += currentDistace;
84
85        CVRPInsertionInfo stopInfo = new CVRPInsertionInfo(start, end, spareCapacity);
86        tourInfo.AddStopInsertionInfo(stopInfo);
87      }
88
89      eval.Quality += instance.FleetUsageFactor.Value;
90      eval.Quality += instance.DistanceFactor.Value * distance;
91
92      eval.Distance += distance;
93      eval.VehicleUtilization += 1;
94
95      if (delivered > capacity) {
96        overweight = delivered - capacity;
97      }
98
99      (eval as CVRPEvaluation).Overload += overweight;
100      double penalty = overweight * cvrpInstance.OverloadPenalty.Value;
101      eval.Penalty += penalty;
102      eval.Quality += penalty;
103      tourInfo.Penalty = penalty;
104      tourInfo.Quality = eval.Quality - originalQuality;
105    }
106
107    protected override double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer,
108      out bool feasible) {
109      CVRPInsertionInfo insertionInfo = tourInsertionInfo.GetStopInsertionInfo(index) as CVRPInsertionInfo;
110     
111      double costs = 0;
112      feasible = tourInsertionInfo.Penalty < double.Epsilon;
113
114      ICapacitatedProblemInstance cvrp = instance as ICapacitatedProblemInstance;
115      double overloadPenalty = cvrp.OverloadPenalty.Value;
116
117      double distance = instance.GetDistance(insertionInfo.Start, insertionInfo.End, solution);
118      double newDistance =
119        instance.GetDistance(insertionInfo.Start, customer, solution) +
120        instance.GetDistance(customer, insertionInfo.End, solution);
121      costs += instance.DistanceFactor.Value * (newDistance - distance);
122
123      double demand = instance.Demand[customer];
124      if (demand > insertionInfo.SpareCapacity) {
125        feasible = false;
126
127        if(insertionInfo.SpareCapacity >= 0)
128          costs += (demand - insertionInfo.SpareCapacity) * overloadPenalty;
129        else
130          costs += demand * overloadPenalty;
131      }
132     
133      return costs;
134    }
135
136    protected override void InitResultParameters() {
137      base.InitResultParameters();
138
139      OverloadParameter.ActualValue = new DoubleValue(0);
140    }
141
142    protected override void SetResultParameters(VRPEvaluation tourEvaluation) {
143      base.SetResultParameters(tourEvaluation);
144
145      OverloadParameter.ActualValue.Value = (tourEvaluation as CVRPEvaluation).Overload;
146    }
147   
148    [StorableConstructor]
149    protected CVRPEvaluator(bool deserializing) : base(deserializing) { }
150
151    public CVRPEvaluator() {
152      Parameters.Add(new LookupParameter<DoubleValue>("Overload", "The overload."));
153    }
154
155    public override IDeepCloneable Clone(Cloner cloner) {
156      return new CVRPEvaluator(this, cloner);
157    }
158
159    protected CVRPEvaluator(CVRPEvaluator original, Cloner cloner)
160      : base(original, cloner) {
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.