Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPEvaluator.cs @ 8922

Last change on this file since 8922 was 8763, checked in by svonolfe, 12 years ago

made solution evaluation method virtual (#1953)

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.VehicleRouting.Encodings;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
31  [Item("VRPEvaluator", "Represents a VRP evaluator.")]
32  [StorableClass]
33  public abstract class VRPEvaluator : VRPOperator, IVRPEvaluator {
34    public ILookupParameter<IVRPEncoding> VRPToursParameter {
35      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
36    }
37
38    #region ISingleObjectiveEvaluator Members
39    public ILookupParameter<DoubleValue> QualityParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
42    #endregion
43
44    public ILookupParameter<DoubleValue> DistanceParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["Distance"]; }
46    }
47    public ILookupParameter<DoubleValue> VehcilesUtilizedParameter {
48      get { return (ILookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
49    }
50
51    public ILookupParameter<DoubleValue> PenaltyParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
53    }
54
55    [StorableConstructor]
56    protected VRPEvaluator(bool deserializing) : base(deserializing) { }
57
58    public VRPEvaluator() {
59      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
60
61      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the VRP solution."));
62      Parameters.Add(new LookupParameter<DoubleValue>("Distance", "The distance."));
63      Parameters.Add(new LookupParameter<DoubleValue>("VehiclesUtilized", "The number of vehicles utilized."));
64
65      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The applied penalty."));
66    }
67
68    protected VRPEvaluator(VRPEvaluator original, Cloner cloner)
69      : base(original, cloner) {
70    }
71
72    protected virtual VRPEvaluation CreateTourEvaluation() {
73      return new VRPEvaluation();
74    }
75
76    protected abstract void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution);
77
78    protected virtual void InitResultParameters() {
79      QualityParameter.ActualValue = new DoubleValue(0);
80      VehcilesUtilizedParameter.ActualValue = new DoubleValue(0);
81      DistanceParameter.ActualValue = new DoubleValue(0);
82      PenaltyParameter.ActualValue = new DoubleValue(0);
83    }
84
85    protected virtual void SetResultParameters(VRPEvaluation tourEvaluation) {
86      QualityParameter.ActualValue.Value = tourEvaluation.Quality;
87      VehcilesUtilizedParameter.ActualValue.Value = tourEvaluation.VehicleUtilization;
88      DistanceParameter.ActualValue.Value = tourEvaluation.Distance;
89      PenaltyParameter.ActualValue.Value = tourEvaluation.Penalty;
90    }
91
92    #region IVRPEvaluator Members
93
94    public bool Feasible(VRPEvaluation evaluation) {
95      return evaluation.Penalty < double.Epsilon;
96    }
97
98    protected abstract double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer, out bool feasible);
99
100    public double GetInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, VRPEvaluation eval, int customer, int tour, int index, out bool feasible) {
101      bool tourFeasible;
102      double costs = GetTourInsertionCosts(
103        instance,
104        solution,
105        eval.InsertionInfo.GetTourInsertionInfo(tour),
106        index,
107        customer, out tourFeasible);
108
109      feasible = tourFeasible;
110
111      return costs;
112    }
113
114    public VRPEvaluation EvaluateTour(IVRPProblemInstance instance, Tour tour, IVRPEncoding solution) {
115      VRPEvaluation evaluation = CreateTourEvaluation();
116      EvaluateTour(evaluation, instance, tour, solution);
117      return evaluation;
118    }
119
120    public virtual VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) {
121      VRPEvaluation evaluation = CreateTourEvaluation();
122
123      foreach (Tour tour in solution.GetTours()) {
124        EvaluateTour(evaluation, instance, tour, solution);
125      }
126
127      return evaluation;
128    }
129
130    public override IOperation Apply() {
131      InitResultParameters();
132
133      VRPEvaluation evaluation = CreateTourEvaluation();
134      IVRPEncoding solution = VRPToursParameter.ActualValue;
135      foreach (Tour tour in solution.GetTours()) {
136        EvaluateTour(evaluation, ProblemInstance, tour, solution);
137      }
138      SetResultParameters(evaluation);
139
140      QualityParameter.ActualValue = new DoubleValue(evaluation.Quality);
141
142      return base.Apply();
143    }
144
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.