Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged relevant changes from the trunk into the branch (cloning,...) (#1177)

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