Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.VehicleRouting/3.4/ProblemInstances/VRPEvaluator.cs @ 17152

Last change on this file since 17152 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

File size: 5.6 KB
RevLine 
[4362]1#region License Information
2/* HeuristicLab
[17097]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4362]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
[8053]22using HeuristicLab.Common;
[4362]23using HeuristicLab.Core;
[8053]24using HeuristicLab.Data;
[4362]25using HeuristicLab.Parameters;
[17097]26using HEAL.Attic;
[4362]27using HeuristicLab.Problems.VehicleRouting.Encodings;
[8053]28using HeuristicLab.Problems.VehicleRouting.Interfaces;
[4362]29
[4363]30namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
[4362]31  [Item("VRPEvaluator", "Represents a VRP evaluator.")]
[17097]32  [StorableType("164315D4-BCCF-4A79-B75B-11A49C56C5E1")]
[8053]33  public abstract class VRPEvaluator : VRPOperator, IVRPEvaluator {
[4362]34    public ILookupParameter<IVRPEncoding> VRPToursParameter {
35      get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
36    }
[8053]37
[4362]38    #region ISingleObjectiveEvaluator Members
39    public ILookupParameter<DoubleValue> QualityParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
[4363]42    #endregion
[4362]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    }
[5127]50
51    public ILookupParameter<DoubleValue> PenaltyParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
53    }
[8053]54
[4362]55    [StorableConstructor]
[17097]56    protected VRPEvaluator(StorableConstructorFlag _) : base(_) { }
[4362]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."));
[5127]64
65      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The applied penalty."));
[4362]66    }
67
[4752]68    protected VRPEvaluator(VRPEvaluator original, Cloner cloner)
69      : base(original, cloner) {
70    }
71
[4362]72    protected virtual VRPEvaluation CreateTourEvaluation() {
73      return new VRPEvaluation();
74    }
75
[6838]76    protected abstract void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour, IVRPEncoding solution);
[4362]77
78    protected virtual void InitResultParameters() {
79      QualityParameter.ActualValue = new DoubleValue(0);
80      VehcilesUtilizedParameter.ActualValue = new DoubleValue(0);
81      DistanceParameter.ActualValue = new DoubleValue(0);
[5127]82      PenaltyParameter.ActualValue = new DoubleValue(0);
[4362]83    }
84
[4520]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;
[5127]89      PenaltyParameter.ActualValue.Value = tourEvaluation.Penalty;
[4362]90    }
91
92    #region IVRPEvaluator Members
93
[4378]94    public bool Feasible(VRPEvaluation evaluation) {
95      return evaluation.Penalty < double.Epsilon;
[4362]96    }
97
[6851]98    protected abstract double GetTourInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, TourInsertionInfo tourInsertionInfo, int index, int customer, out bool feasible);
[6752]99
[6851]100    public double GetInsertionCosts(IVRPProblemInstance instance, IVRPEncoding solution, VRPEvaluation eval, int customer, int tour, int index, out bool feasible) {
[6752]101      bool tourFeasible;
102      double costs = GetTourInsertionCosts(
103        instance,
[6851]104        solution,
[8053]105        eval.InsertionInfo.GetTourInsertionInfo(tour),
[7276]106        index,
[6752]107        customer, out tourFeasible);
108
109      feasible = tourFeasible;
110
111      return costs;
112    }
113
[6838]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
[8763]120    public virtual VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) {
[4378]121      VRPEvaluation evaluation = CreateTourEvaluation();
[4362]122
123      foreach (Tour tour in solution.GetTours()) {
[6838]124        EvaluateTour(evaluation, instance, tour, solution);
[4362]125      }
126
[4378]127      return evaluation;
[4362]128    }
129
[10507]130    public override IOperation InstrumentedApply() {
[4362]131      InitResultParameters();
132
[4520]133      VRPEvaluation evaluation = CreateTourEvaluation();
[6838]134      IVRPEncoding solution = VRPToursParameter.ActualValue;
135      foreach (Tour tour in solution.GetTours()) {
136        EvaluateTour(evaluation, ProblemInstance, tour, solution);
[4362]137      }
[4520]138      SetResultParameters(evaluation);
[4362]139
[4520]140      QualityParameter.ActualValue = new DoubleValue(evaluation.Quality);
[4365]141
[10507]142      return base.InstrumentedApply();
[4362]143    }
144
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.