[4362] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[8053] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[7934] | 25 | using HeuristicLab.Common;
|
---|
[4362] | 26 | using HeuristicLab.Core;
|
---|
[7934] | 27 | using HeuristicLab.Data;
|
---|
[4362] | 28 | using HeuristicLab.Parameters;
|
---|
[7934] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5867] | 30 | using HeuristicLab.Problems.VehicleRouting.Encodings.General;
|
---|
[7934] | 31 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
[4362] | 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.VehicleRouting.ProblemInstances {
|
---|
| 34 | [Item("VRPProblemInstance", "Represents a VRP instance.")]
|
---|
| 35 | [StorableClass]
|
---|
[6711] | 36 | public abstract class VRPProblemInstance : ParameterizedNamedItem, IVRPProblemInstance, IStatefulItem {
|
---|
[5867] | 37 | IVRPEvaluator moveEvaluator;
|
---|
| 38 |
|
---|
[9363] | 39 | private object locker = new object();
|
---|
| 40 |
|
---|
[5867] | 41 | public IVRPEvaluator MoveEvaluator {
|
---|
| 42 | get {
|
---|
[9363] | 43 | lock (locker) {
|
---|
| 44 | if (evaluator == null)
|
---|
| 45 | return null;
|
---|
| 46 | else {
|
---|
| 47 | if (moveEvaluator == null) {
|
---|
| 48 | moveEvaluator = evaluator.Clone() as IVRPEvaluator;
|
---|
[5867] | 49 |
|
---|
[9363] | 50 | foreach (IParameter parameter in moveEvaluator.Parameters) {
|
---|
| 51 | if (parameter is ILookupParameter
|
---|
| 52 | && parameter != moveEvaluator.ProblemInstanceParameter
|
---|
| 53 | && parameter != moveEvaluator.VRPToursParameter) {
|
---|
| 54 | (parameter as ILookupParameter).ActualName =
|
---|
| 55 | VRPMoveEvaluator.MovePrefix +
|
---|
| 56 | (parameter as ILookupParameter).ActualName;
|
---|
| 57 | }
|
---|
[5867] | 58 | }
|
---|
| 59 | }
|
---|
[9363] | 60 |
|
---|
| 61 | return moveEvaluator;
|
---|
[5867] | 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[4374] | 67 | protected abstract IEnumerable<IOperator> GetOperators();
|
---|
| 68 | protected abstract IEnumerable<IOperator> GetAnalyzers();
|
---|
| 69 |
|
---|
| 70 | public IEnumerable<IOperator> Operators {
|
---|
| 71 | get {
|
---|
| 72 | return GetOperators().Union(GetAnalyzers());
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
[7934] | 75 |
|
---|
[4362] | 76 | protected ValueParameter<DoubleMatrix> CoordinatesParameter {
|
---|
| 77 | get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
|
---|
| 78 | }
|
---|
[4380] | 79 | protected OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
|
---|
| 80 | get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
|
---|
| 81 | }
|
---|
[4362] | 82 | protected ValueParameter<BoolValue> UseDistanceMatrixParameter {
|
---|
| 83 | get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
|
---|
| 84 | }
|
---|
| 85 | protected ValueParameter<IntValue> VehiclesParameter {
|
---|
| 86 | get { return (ValueParameter<IntValue>)Parameters["Vehicles"]; }
|
---|
| 87 | }
|
---|
| 88 | protected ValueParameter<DoubleArray> DemandParameter {
|
---|
| 89 | get { return (ValueParameter<DoubleArray>)Parameters["Demand"]; }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | protected IValueParameter<DoubleValue> FleetUsageFactorParameter {
|
---|
| 93 | get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; }
|
---|
| 94 | }
|
---|
| 95 | protected IValueParameter<DoubleValue> DistanceFactorParameter {
|
---|
| 96 | get { return (IValueParameter<DoubleValue>)Parameters["EvalDistanceFactor"]; }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public DoubleMatrix Coordinates {
|
---|
| 100 | get { return CoordinatesParameter.Value; }
|
---|
| 101 | set { CoordinatesParameter.Value = value; }
|
---|
| 102 | }
|
---|
[4380] | 103 |
|
---|
| 104 | public DoubleMatrix DistanceMatrix {
|
---|
| 105 | get { return DistanceMatrixParameter.Value; }
|
---|
| 106 | set { DistanceMatrixParameter.Value = value; }
|
---|
| 107 | }
|
---|
[4362] | 108 | public BoolValue UseDistanceMatrix {
|
---|
| 109 | get { return UseDistanceMatrixParameter.Value; }
|
---|
| 110 | set { UseDistanceMatrixParameter.Value = value; }
|
---|
| 111 | }
|
---|
| 112 | public IntValue Vehicles {
|
---|
| 113 | get { return VehiclesParameter.Value; }
|
---|
| 114 | set { VehiclesParameter.Value = value; }
|
---|
| 115 | }
|
---|
| 116 | public DoubleArray Demand {
|
---|
| 117 | get { return DemandParameter.Value; }
|
---|
| 118 | set { DemandParameter.Value = value; }
|
---|
| 119 | }
|
---|
[4365] | 120 | public virtual IntValue Cities {
|
---|
[4362] | 121 | get { return new IntValue(Demand.Length); }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | public DoubleValue FleetUsageFactor {
|
---|
| 125 | get { return FleetUsageFactorParameter.Value; }
|
---|
| 126 | set { FleetUsageFactorParameter.Value = value; }
|
---|
| 127 | }
|
---|
| 128 | public DoubleValue DistanceFactor {
|
---|
| 129 | get { return DistanceFactorParameter.Value; }
|
---|
| 130 | set { DistanceFactorParameter.Value = value; }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[6885] | 133 | protected virtual double CalculateDistance(int start, int end) {
|
---|
[4362] | 134 | double distance = 0.0;
|
---|
| 135 |
|
---|
| 136 | distance =
|
---|
| 137 | Math.Sqrt(
|
---|
| 138 | Math.Pow(Coordinates[start, 0] - Coordinates[end, 0], 2) +
|
---|
| 139 | Math.Pow(Coordinates[start, 1] - Coordinates[end, 1], 2));
|
---|
| 140 |
|
---|
| 141 | return distance;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | private DoubleMatrix CreateDistanceMatrix() {
|
---|
| 145 | DoubleMatrix distanceMatrix = new DoubleMatrix(Coordinates.Rows, Coordinates.Rows);
|
---|
| 146 |
|
---|
| 147 | for (int i = 0; i < distanceMatrix.Rows; i++) {
|
---|
[7543] | 148 | for (int j = 0; j < distanceMatrix.Columns; j++) {
|
---|
[4362] | 149 | double distance = CalculateDistance(i, j);
|
---|
| 150 |
|
---|
| 151 | distanceMatrix[i, j] = distance;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return distanceMatrix;
|
---|
| 156 | }
|
---|
[6607] | 157 |
|
---|
[6851] | 158 | public virtual double[] GetCoordinates(int city) {
|
---|
| 159 | double[] coordinates = new double[Coordinates.Columns];
|
---|
| 160 |
|
---|
| 161 | for (int i = 0; i < Coordinates.Columns; i++) {
|
---|
| 162 | coordinates[i] = Coordinates[city, i];
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | return coordinates;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | public virtual double GetDemand(int city) {
|
---|
| 169 | return Demand[city];
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[6607] | 172 | //cache for performance improvement
|
---|
| 173 | private DoubleMatrix distanceMatrix = null;
|
---|
| 174 | private IVRPEvaluator evaluator = null;
|
---|
| 175 |
|
---|
[7852] | 176 | public IVRPEvaluator SolutionEvaluator {
|
---|
[7934] | 177 | get {
|
---|
[7852] | 178 | return evaluator;
|
---|
| 179 | }
|
---|
[8006] | 180 |
|
---|
| 181 | set {
|
---|
[9363] | 182 | lock (locker) {
|
---|
| 183 | moveEvaluator = null;
|
---|
| 184 | evaluator = value;
|
---|
| 185 | EvalBestKnownSolution();
|
---|
| 186 | }
|
---|
[8006] | 187 | }
|
---|
[7852] | 188 | }
|
---|
| 189 |
|
---|
[6851] | 190 | public virtual double GetDistance(int start, int end, IVRPEncoding solution) {
|
---|
[4380] | 191 | if (distanceMatrix == null && UseDistanceMatrix.Value) {
|
---|
[9363] | 192 | if (DistanceMatrix == null) DistanceMatrix = CreateDistanceMatrix();
|
---|
| 193 | distanceMatrix = DistanceMatrix;
|
---|
[4380] | 194 | }
|
---|
| 195 |
|
---|
[9363] | 196 | if (distanceMatrix != null) return distanceMatrix[start, end];
|
---|
| 197 | return CalculateDistance(start, end);
|
---|
[4362] | 198 | }
|
---|
| 199 |
|
---|
[6854] | 200 | public virtual double GetInsertionDistance(int start, int customer, int end, IVRPEncoding solution,
|
---|
| 201 | out double startDistance, out double endDistance) {
|
---|
[6851] | 202 | double distance = GetDistance(start, end, solution);
|
---|
| 203 |
|
---|
[6854] | 204 | startDistance = GetDistance(start, customer, solution);
|
---|
| 205 | endDistance = GetDistance(customer, end, solution);
|
---|
[7934] | 206 |
|
---|
[6854] | 207 | double newDistance = startDistance + endDistance;
|
---|
| 208 |
|
---|
[6851] | 209 | return newDistance - distance;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[4362] | 212 | public bool Feasible(IVRPEncoding solution) {
|
---|
[6607] | 213 | return evaluator.Feasible(
|
---|
| 214 | evaluator.Evaluate(
|
---|
[4378] | 215 | this, solution));
|
---|
[4362] | 216 | }
|
---|
| 217 |
|
---|
[6838] | 218 | public bool TourFeasible(Tour tour, IVRPEncoding solution) {
|
---|
[6607] | 219 | return evaluator.Feasible(
|
---|
[6838] | 220 | evaluator.EvaluateTour(
|
---|
| 221 | this, tour, solution));
|
---|
[4362] | 222 | }
|
---|
| 223 |
|
---|
[6607] | 224 | public VRPEvaluation Evaluate(IVRPEncoding solution) {
|
---|
| 225 | return evaluator.Evaluate(this, solution);
|
---|
[4362] | 226 | }
|
---|
| 227 |
|
---|
[6838] | 228 | public VRPEvaluation EvaluateTour(Tour tour, IVRPEncoding solution) {
|
---|
| 229 | return evaluator.EvaluateTour(this, tour, solution);
|
---|
[4362] | 230 | }
|
---|
| 231 |
|
---|
[6607] | 232 | public bool Feasible(VRPEvaluation eval) {
|
---|
| 233 | return evaluator.Feasible(eval);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[6851] | 236 | public double GetInsertionCosts(VRPEvaluation eval, IVRPEncoding solution, int customer, int tour, int index, out bool feasible) {
|
---|
| 237 | return evaluator.GetInsertionCosts(this, solution, eval, customer, tour, index, out feasible);
|
---|
[6752] | 238 | }
|
---|
| 239 |
|
---|
[7852] | 240 |
|
---|
| 241 | public event EventHandler EvaluationChanged;
|
---|
| 242 |
|
---|
[4860] | 243 | protected void EvalBestKnownSolution() {
|
---|
[7852] | 244 | EventHandler tmp = EvaluationChanged;
|
---|
| 245 | if (tmp != null)
|
---|
| 246 | tmp(this, null);
|
---|
[4860] | 247 | }
|
---|
| 248 |
|
---|
[4362] | 249 | protected abstract IVRPEvaluator Evaluator { get; }
|
---|
| 250 | protected abstract IVRPCreator Creator { get; }
|
---|
[7934] | 251 |
|
---|
[4362] | 252 | [StorableConstructor]
|
---|
| 253 | protected VRPProblemInstance(bool deserializing) : base(deserializing) { }
|
---|
| 254 |
|
---|
| 255 | public VRPProblemInstance()
|
---|
| 256 | : base() {
|
---|
| 257 | Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix()));
|
---|
[4380] | 258 | Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
[4362] | 259 | Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
|
---|
| 260 | Parameters.Add(new ValueParameter<IntValue>("Vehicles", "The number of vehicles.", new IntValue(0)));
|
---|
| 261 | Parameters.Add(new ValueParameter<DoubleArray>("Demand", "The demand of each customer.", new DoubleArray()));
|
---|
| 262 |
|
---|
[5127] | 263 | Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(0)));
|
---|
[4362] | 264 | Parameters.Add(new ValueParameter<DoubleValue>("EvalDistanceFactor", "The distance factor considered in the evaluation.", new DoubleValue(1)));
|
---|
| 265 |
|
---|
[7852] | 266 | evaluator = Evaluator;
|
---|
[4860] | 267 | AttachEventHandlers();
|
---|
[4362] | 268 | }
|
---|
[4752] | 269 |
|
---|
| 270 | protected VRPProblemInstance(VRPProblemInstance original, Cloner cloner)
|
---|
| 271 | : base(original, cloner) {
|
---|
[7934] | 272 | evaluator = Evaluator;
|
---|
[7852] | 273 | AttachEventHandlers();
|
---|
[4752] | 274 | }
|
---|
[4860] | 275 |
|
---|
| 276 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[7934] | 277 | private void AfterDeserialization() {
|
---|
[7852] | 278 | evaluator = Evaluator;
|
---|
[4860] | 279 | AttachEventHandlers();
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | private void AttachEventHandlers() {
|
---|
| 283 | DistanceFactorParameter.ValueChanged += new EventHandler(DistanceFactorParameter_ValueChanged);
|
---|
| 284 | DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactor_ValueChanged);
|
---|
| 285 | FleetUsageFactorParameter.ValueChanged += new EventHandler(FleetUsageFactorParameter_ValueChanged);
|
---|
| 286 | FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactor_ValueChanged);
|
---|
| 287 | DistanceMatrixParameter.ValueChanged += new EventHandler(DistanceMatrixParameter_ValueChanged);
|
---|
| 288 | if (DistanceMatrix != null) {
|
---|
| 289 | DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
|
---|
| 290 | DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
|
---|
| 291 | }
|
---|
| 292 | UseDistanceMatrixParameter.ValueChanged += new EventHandler(UseDistanceMatrixParameter_ValueChanged);
|
---|
| 293 | UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[6711] | 296 | public virtual void InitializeState() {
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | public virtual void ClearState() {
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[4860] | 302 | #region Event handlers
|
---|
| 303 | void DistanceFactorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 304 | DistanceFactorParameter.Value.ValueChanged += new EventHandler(DistanceFactor_ValueChanged);
|
---|
| 305 | EvalBestKnownSolution();
|
---|
| 306 | }
|
---|
| 307 | void DistanceFactor_ValueChanged(object sender, EventArgs e) {
|
---|
| 308 | EvalBestKnownSolution();
|
---|
| 309 | }
|
---|
| 310 | void FleetUsageFactorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 311 | FleetUsageFactorParameter.Value.ValueChanged += new EventHandler(FleetUsageFactor_ValueChanged);
|
---|
| 312 | EvalBestKnownSolution();
|
---|
| 313 | }
|
---|
| 314 | void FleetUsageFactor_ValueChanged(object sender, EventArgs e) {
|
---|
| 315 | EvalBestKnownSolution();
|
---|
| 316 | }
|
---|
| 317 | void DistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 318 | if (DistanceMatrix != null) {
|
---|
| 319 | DistanceMatrix.ItemChanged += new EventHandler<EventArgs<int, int>>(DistanceMatrix_ItemChanged);
|
---|
| 320 | DistanceMatrix.Reset += new EventHandler(DistanceMatrix_Reset);
|
---|
| 321 | }
|
---|
[6607] | 322 | distanceMatrix = DistanceMatrix;
|
---|
[4860] | 323 | EvalBestKnownSolution();
|
---|
| 324 | }
|
---|
| 325 | void DistanceMatrix_Reset(object sender, EventArgs e) {
|
---|
| 326 | EvalBestKnownSolution();
|
---|
| 327 | }
|
---|
| 328 | void DistanceMatrix_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
[6607] | 329 | distanceMatrix = DistanceMatrix;
|
---|
[4860] | 330 | EvalBestKnownSolution();
|
---|
| 331 | }
|
---|
| 332 | void UseDistanceMatrixParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 333 | UseDistanceMatrix.ValueChanged += new EventHandler(UseDistanceMatrix_ValueChanged);
|
---|
[6607] | 334 | if (!UseDistanceMatrix.Value)
|
---|
| 335 | distanceMatrix = null;
|
---|
[4860] | 336 | EvalBestKnownSolution();
|
---|
| 337 | }
|
---|
| 338 | void UseDistanceMatrix_ValueChanged(object sender, EventArgs e) {
|
---|
[6607] | 339 | if (!UseDistanceMatrix.Value)
|
---|
| 340 | distanceMatrix = null;
|
---|
[4860] | 341 | EvalBestKnownSolution();
|
---|
| 342 | }
|
---|
| 343 | #endregion
|
---|
[4362] | 344 | }
|
---|
| 345 | } |
---|