[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;
|
---|
[4752] | 35 | using HeuristicLab.Common;
|
---|
[4362] | 36 |
|
---|
[4363] | 37 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
[4362] | 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 | }
|
---|
[4363] | 49 | #endregion
|
---|
[4362] | 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 | }
|
---|
[5127] | 57 |
|
---|
| 58 | public ILookupParameter<DoubleValue> PenaltyParameter {
|
---|
| 59 | get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
|
---|
| 60 | }
|
---|
[4362] | 61 |
|
---|
| 62 | [StorableConstructor]
|
---|
| 63 | protected VRPEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 64 |
|
---|
| 65 | public VRPEvaluator() {
|
---|
| 66 | Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours which should be evaluated."));
|
---|
| 67 |
|
---|
| 68 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the VRP solution."));
|
---|
| 69 | Parameters.Add(new LookupParameter<DoubleValue>("Distance", "The distance."));
|
---|
| 70 | Parameters.Add(new LookupParameter<DoubleValue>("VehiclesUtilized", "The number of vehicles utilized."));
|
---|
[5127] | 71 |
|
---|
| 72 | Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The applied penalty."));
|
---|
[4362] | 73 | }
|
---|
| 74 |
|
---|
[4752] | 75 | protected VRPEvaluator(VRPEvaluator original, Cloner cloner)
|
---|
| 76 | : base(original, cloner) {
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[4362] | 79 | protected virtual VRPEvaluation CreateTourEvaluation() {
|
---|
| 80 | return new VRPEvaluation();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | protected abstract void EvaluateTour(VRPEvaluation eval, IVRPProblemInstance instance, Tour tour);
|
---|
| 84 |
|
---|
| 85 | protected virtual void InitResultParameters() {
|
---|
| 86 | QualityParameter.ActualValue = new DoubleValue(0);
|
---|
| 87 | VehcilesUtilizedParameter.ActualValue = new DoubleValue(0);
|
---|
| 88 | DistanceParameter.ActualValue = new DoubleValue(0);
|
---|
[5127] | 89 | PenaltyParameter.ActualValue = new DoubleValue(0);
|
---|
[4362] | 90 | }
|
---|
| 91 |
|
---|
[4520] | 92 | protected virtual void SetResultParameters(VRPEvaluation tourEvaluation) {
|
---|
| 93 | QualityParameter.ActualValue.Value = tourEvaluation.Quality;
|
---|
| 94 | VehcilesUtilizedParameter.ActualValue.Value = tourEvaluation.VehicleUtilization;
|
---|
| 95 | DistanceParameter.ActualValue.Value = tourEvaluation.Distance;
|
---|
[5127] | 96 | PenaltyParameter.ActualValue.Value = tourEvaluation.Penalty;
|
---|
[4362] | 97 | }
|
---|
| 98 |
|
---|
[4378] | 99 | private VRPEvaluation EvaluateTour(IVRPProblemInstance instance, Tour tour) {
|
---|
[4363] | 100 | VRPEvaluation evaluation = CreateTourEvaluation();
|
---|
| 101 | EvaluateTour(evaluation, instance, tour);
|
---|
| 102 | return evaluation;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[4362] | 105 | #region IVRPEvaluator Members
|
---|
| 106 |
|
---|
[4378] | 107 | public bool Feasible(VRPEvaluation evaluation) {
|
---|
| 108 | return evaluation.Penalty < double.Epsilon;
|
---|
[4362] | 109 | }
|
---|
| 110 |
|
---|
[6752] | 111 | protected abstract double GetTourInsertionCosts(IVRPProblemInstance instance, TourInsertionInfo tourInsertionInfo, int index, int customer, out bool feasible);
|
---|
| 112 |
|
---|
| 113 | public double GetInsertionCosts(IVRPProblemInstance instance, VRPEvaluation eval, int customer, int tour, int index, out bool feasible) {
|
---|
| 114 | bool tourFeasible;
|
---|
| 115 | double costs = GetTourInsertionCosts(
|
---|
| 116 | instance,
|
---|
| 117 | eval.InsertionInfo.GetTourInsertionInfo(tour), index,
|
---|
| 118 | customer, out tourFeasible);
|
---|
| 119 |
|
---|
| 120 | feasible = tourFeasible;
|
---|
| 121 |
|
---|
| 122 | return costs;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[4378] | 125 | public VRPEvaluation Evaluate(IVRPProblemInstance instance, IVRPEncoding solution) {
|
---|
| 126 | VRPEvaluation evaluation = CreateTourEvaluation();
|
---|
[4362] | 127 |
|
---|
| 128 | foreach (Tour tour in solution.GetTours()) {
|
---|
[4378] | 129 | EvaluateTour(evaluation, instance, tour);
|
---|
[4362] | 130 | }
|
---|
| 131 |
|
---|
[4378] | 132 | return evaluation;
|
---|
[4362] | 133 | }
|
---|
| 134 |
|
---|
[4378] | 135 | public VRPEvaluation Evaluate(IVRPProblemInstance instance, Tour tour) {
|
---|
| 136 | return EvaluateTour(instance, tour);
|
---|
[4362] | 137 | }
|
---|
| 138 |
|
---|
| 139 | public override IOperation Apply() {
|
---|
| 140 | InitResultParameters();
|
---|
| 141 |
|
---|
[4520] | 142 | VRPEvaluation evaluation = CreateTourEvaluation();
|
---|
[4362] | 143 | foreach (Tour tour in VRPToursParameter.ActualValue.GetTours()) {
|
---|
[4520] | 144 | EvaluateTour(evaluation, ProblemInstance, tour);
|
---|
[4362] | 145 | }
|
---|
[4520] | 146 | SetResultParameters(evaluation);
|
---|
[4362] | 147 |
|
---|
[4520] | 148 | QualityParameter.ActualValue = new DoubleValue(evaluation.Quality);
|
---|
[4365] | 149 |
|
---|
[4362] | 150 | return base.Apply();
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | #endregion
|
---|
| 154 | }
|
---|
| 155 | } |
---|