Free cookie consent management tool by TermsFeed Policy Generator

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

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

Worked on VRP operators (WIP) (#1177)

File size: 5.0 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    protected 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(IVRPProblemInstance instance, IVRPEncoding solution) {
95      bool feasible = true;
96
97      foreach (Tour tour in solution.GetTours()) {
98        if (!Feasible(instance, tour)) {
99          feasible = false;
100          break;
101        }
102      }
103
104      return feasible;
105    }
106
107    public bool Feasible(IVRPProblemInstance instance, Tour tour) {
108      return EvaluateTour(instance, tour).Penalty == 0;
109    }
110
111    public double Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) {
112      double quality = 0;
113
114      foreach (Tour tour in solution.GetTours()) {
115        quality += Evaluate(instance, tour);
116      }
117
118      return quality;
119    }
120
121    public double Evaluate(IVRPProblemInstance instance, Tour tour) {
122      return EvaluateTour(instance, tour).Quality;
123    }
124
125    public override IOperation Apply() {
126      double quality = 0;
127      InitResultParameters();
128
129      foreach (Tour tour in VRPToursParameter.ActualValue.GetTours()) {
130        VRPEvaluation tourEvaluation = EvaluateTour(ProblemInstance, tour);
131        quality += tourEvaluation.Quality;
132
133        UpdateResultParameters(tourEvaluation);
134      }
135
136      QualityParameter.ActualValue = new DoubleValue(quality);
137
138      return base.Apply();
139    }
140
141    #endregion
142  }
143}
Note: See TracBrowser for help on using the repository browser.