[4362] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Data;
|
---|
| 31 | using HeuristicLab.Optimization;
|
---|
| 32 | using HeuristicLab.PluginInfrastructure;
|
---|
[4363] | 33 | using HeuristicLab.Problems.VehicleRouting.Variants;
|
---|
[4362] | 34 | using HeuristicLab.Problems.VehicleRouting.Encodings;
|
---|
| 35 |
|
---|
[4363] | 36 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
[4362] | 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 | }
|
---|
[4363] | 48 | #endregion
|
---|
[4362] | 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 |
|
---|
[4378] | 86 | private VRPEvaluation EvaluateTour(IVRPProblemInstance instance, Tour tour) {
|
---|
[4363] | 87 | VRPEvaluation evaluation = CreateTourEvaluation();
|
---|
| 88 | EvaluateTour(evaluation, instance, tour);
|
---|
| 89 | return evaluation;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[4362] | 92 | #region IVRPEvaluator Members
|
---|
| 93 |
|
---|
[4378] | 94 | public bool Feasible(VRPEvaluation evaluation) {
|
---|
| 95 | return evaluation.Penalty < double.Epsilon;
|
---|
[4362] | 96 | }
|
---|
| 97 |
|
---|
[4378] | 98 | public VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) {
|
---|
| 99 | VRPEvaluation evaluation = CreateTourEvaluation();
|
---|
[4362] | 100 |
|
---|
| 101 | foreach (Tour tour in solution.GetTours()) {
|
---|
[4378] | 102 | EvaluateTour(evaluation, instance, tour);
|
---|
[4362] | 103 | }
|
---|
| 104 |
|
---|
[4378] | 105 | return evaluation;
|
---|
[4362] | 106 | }
|
---|
| 107 |
|
---|
[4378] | 108 | public VRPEvaluation Evaluate(IVRPProblemInstance instance, Tour tour) {
|
---|
| 109 | return EvaluateTour(instance, tour);
|
---|
[4362] | 110 | }
|
---|
| 111 |
|
---|
| 112 | public override IOperation Apply() {
|
---|
[4365] | 113 | double quality = 0;
|
---|
[4362] | 114 | InitResultParameters();
|
---|
| 115 |
|
---|
| 116 | foreach (Tour tour in VRPToursParameter.ActualValue.GetTours()) {
|
---|
| 117 | VRPEvaluation tourEvaluation = EvaluateTour(ProblemInstance, tour);
|
---|
[4365] | 118 | quality += tourEvaluation.Quality;
|
---|
[4362] | 119 |
|
---|
| 120 | UpdateResultParameters(tourEvaluation);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[4365] | 123 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
| 124 |
|
---|
[4362] | 125 | return base.Apply();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | #endregion
|
---|
| 129 | }
|
---|
| 130 | } |
---|