Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VRP/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPEvaluator.cs @ 4501

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

Improved performance of PotvinEncoding(#1177)

File size: 4.8 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("VRPEvaluator", "Represents a VRP evaluator.")]
38  [StorableClass]
39  public abstract class VRPEvaluator: VRPOperator, IVRPEvaluator {
40    public ILookupParameter<IVRPEncoding> VRPToursParameter {
41      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
42    }
43   
44    #region ISingleObjectiveEvaluator Members
45    public ILookupParameter<DoubleValue> QualityParameter {
46      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
47    }
48    #endregion
49
50    public ILookupParameter<DoubleValue> DistanceParameter {
51      get { return (ILookupParameter<DoubleValue>)Parameters["Distance"]; }
52    }
53    public ILookupParameter<DoubleValue> VehcilesUtilizedParameter {
54      get { return (ILookupParameter<DoubleValue>)Parameters["VehiclesUtilized"]; }
55    }
56   
57    [StorableConstructor]
58    protected VRPEvaluator(bool deserializing) : base(deserializing) { }
59
60    public VRPEvaluator() {
61      Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
62
63      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the VRP solution."));
64      Parameters.Add(new LookupParameter<DoubleValue>("Distance", "The distance."));
65      Parameters.Add(new LookupParameter<DoubleValue>("VehiclesUtilized", "The number of vehicles utilized."));
66    }
67
68    protected virtual VRPEvaluation CreateTourEvaluation() {
69      return new VRPEvaluation();
70    }
71
72    protected abstract void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour);
73
74    protected virtual void InitResultParameters() {
75      QualityParameter.ActualValue = new DoubleValue(0);
76      VehcilesUtilizedParameter.ActualValue = new DoubleValue(0);
77      DistanceParameter.ActualValue = new DoubleValue(0);
78    }
79
80    protected virtual void UpdateResultParameters(VRPEvaluation tourEvaluation) {
81      QualityParameter.ActualValue.Value += tourEvaluation.Quality;
82      VehcilesUtilizedParameter.ActualValue.Value += tourEvaluation.VehicleUtilization;
83      DistanceParameter.ActualValue.Value += tourEvaluation.Distance;
84    }
85
86    private VRPEvaluation EvaluateTour(IVRPProblemInstance instance, Tour tour) {
87      VRPEvaluation evaluation = CreateTourEvaluation();
88      EvaluateTour(evaluation, instance, tour);
89      return evaluation;
90    }
91
92    #region IVRPEvaluator Members
93
94    public bool Feasible(VRPEvaluation evaluation) {
95      return evaluation.Penalty < double.Epsilon;
96    }
97
98    public VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) {
99      VRPEvaluation evaluation = CreateTourEvaluation();
100
101      foreach (Tour tour in solution.GetTours()) {
102        EvaluateTour(evaluation, instance, tour);
103      }
104
105      return evaluation;
106    }
107
108    public VRPEvaluation Evaluate(IVRPProblemInstance instance, Tour tour) {
109      return EvaluateTour(instance, tour);
110    }
111
112    public override IOperation Apply() {
113      double quality = 0;
114      InitResultParameters();
115
116      foreach (Tour tour in VRPToursParameter.ActualValue.GetTours()) {
117        VRPEvaluation tourEvaluation = EvaluateTour(ProblemInstance, tour);
118        quality += tourEvaluation.Quality;
119
120        UpdateResultParameters(tourEvaluation);
121      }
122
123      QualityParameter.ActualValue = new DoubleValue(quality);
124
125      return base.Apply();
126    }
127
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.