[6448] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6448] | 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.Optimization;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Data;
|
---|
| 30 | using HeuristicLab.Common;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Operators;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
|
---|
| 35 | [Item("AlbaLambdaInterchangeLocalImprovementOperator", "Takes a solution and finds the local optimum with respect to the lambda interchange neighborhood by decending along the steepest gradient.")]
|
---|
| 36 | [StorableClass]
|
---|
| 37 | public class AlbaLambdaInterchangeLocalImprovementOperator : SingleSuccessorOperator, IStochasticOperator, ILocalImprovementOperator {
|
---|
| 38 | public Type ProblemType {
|
---|
| 39 | get { return typeof(VehicleRoutingProblem); }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | [Storable]
|
---|
| 43 | private VehicleRoutingProblem problem;
|
---|
| 44 | public IProblem Problem {
|
---|
| 45 | get { return problem; }
|
---|
| 46 | set { problem = (VehicleRoutingProblem)value; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 50 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 54 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 58 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public ILookupParameter<IVRPEncoding> VRPToursParameter {
|
---|
| 62 | get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 66 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public IValueParameter<IntValue> LambdaParameter {
|
---|
| 70 | get { return (IValueParameter<IntValue>)Parameters["Lambda"]; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public IValueParameter<IntValue> SampleSizeParameter {
|
---|
| 74 | get { return (IValueParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 78 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | [StorableConstructor]
|
---|
| 82 | protected AlbaLambdaInterchangeLocalImprovementOperator(bool deserializing) : base(deserializing) { }
|
---|
| 83 | protected AlbaLambdaInterchangeLocalImprovementOperator(AlbaLambdaInterchangeLocalImprovementOperator original, Cloner cloner)
|
---|
| 84 | : base(original, cloner) {
|
---|
| 85 | this.problem = cloner.Clone(original.problem);
|
---|
| 86 | }
|
---|
| 87 | public AlbaLambdaInterchangeLocalImprovementOperator()
|
---|
| 88 | : base() {
|
---|
| 89 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached.", new IntValue(10000)));
|
---|
| 90 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The amount of evaluated solutions (here a move is counted only as 4/n evaluated solutions with n being the length of the permutation)."));
|
---|
| 91 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
|
---|
| 92 | Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours to be manipulated."));
|
---|
| 93 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
|
---|
| 94 | Parameters.Add(new ValueParameter<IntValue>("Lambda", "The lambda value.", new IntValue(1)));
|
---|
| 95 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The number of moves to generate.", new IntValue(2000)));
|
---|
| 96 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 100 | return new AlbaLambdaInterchangeLocalImprovementOperator(this, cloner);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public static void Apply(AlbaEncoding solution, int maxIterations,
|
---|
| 104 | int lambda, int samples, IRandom random, ref double quality, out int evaluatedSolutions,
|
---|
| 105 | DoubleMatrix distanceMatrix, IntValue vehicles, DoubleArray dueTime, DoubleArray serviceTime, DoubleArray readyTime,
|
---|
| 106 | DoubleArray demand, DoubleValue capacity,
|
---|
| 107 | DoubleMatrix coordinates, DoubleValue fleetUsageFactor, DoubleValue timeFactor, DoubleValue distanceFactor,
|
---|
| 108 | DoubleValue overloadPenalty, DoubleValue tardinessPenalty, BoolValue useDistanceMatrix) {
|
---|
| 109 | ValueLookupParameter<DoubleMatrix> distanceMatrixParameter = new ValueLookupParameter<DoubleMatrix>("DistanceMatrix", distanceMatrix);
|
---|
| 110 |
|
---|
| 111 | evaluatedSolutions = 0;
|
---|
| 112 |
|
---|
| 113 | for (int i = 0; i < maxIterations; i++) {
|
---|
| 114 | AlbaLambdaInterchangeMove bestMove = null;
|
---|
| 115 | foreach (AlbaLambdaInterchangeMove move in AlbaStochasticLambdaInterchangeMultiMoveGenerator.GenerateAllMoves(solution, lambda, samples, random)) {
|
---|
| 116 | double moveQuality =
|
---|
| 117 | AlbaLambdaInterchangeMoveEvaluator.GetMoveQuality(solution,
|
---|
| 118 | move,
|
---|
| 119 | vehicles,
|
---|
| 120 | dueTime, serviceTime,readyTime,
|
---|
| 121 | demand, capacity,
|
---|
| 122 | coordinates, fleetUsageFactor,
|
---|
| 123 | timeFactor, distanceFactor,
|
---|
| 124 | overloadPenalty, tardinessPenalty,
|
---|
| 125 | distanceMatrixParameter,useDistanceMatrix).Quality;
|
---|
| 126 |
|
---|
| 127 | evaluatedSolutions++;
|
---|
| 128 | if (moveQuality < quality || quality == -1) {
|
---|
| 129 | quality = moveQuality;
|
---|
| 130 | bestMove = move;
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | if (bestMove != null)
|
---|
| 134 | AlbaLambdaInterchangeMoveMaker.Apply(solution, bestMove);
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | public override IOperation Apply() {
|
---|
| 139 | int maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
| 140 | ValueLookupParameter<DoubleMatrix> distanceMatrix = new ValueLookupParameter<DoubleMatrix>("DistanceMatrix", problem.DistanceMatrix);
|
---|
| 141 |
|
---|
| 142 | AlbaEncoding solution = null;
|
---|
| 143 |
|
---|
| 144 | if (VRPToursParameter.ActualValue is AlbaEncoding)
|
---|
| 145 | solution = VRPToursParameter.ActualValue as AlbaEncoding;
|
---|
| 146 | else
|
---|
| 147 | VRPToursParameter.ActualValue = solution = AlbaEncoding.ConvertFrom(VRPToursParameter.ActualValue, problem.VehiclesParameter.Value.Value,
|
---|
| 148 | distanceMatrix);
|
---|
| 149 |
|
---|
| 150 | int lambda = LambdaParameter.Value.Value;
|
---|
| 151 | int samples = SampleSizeParameter.Value.Value;
|
---|
| 152 | IRandom random = RandomParameter.ActualValue;
|
---|
| 153 |
|
---|
| 154 | double quality = QualityParameter.ActualValue.Value;
|
---|
| 155 | int evaluatedSolutions;
|
---|
| 156 |
|
---|
| 157 | Apply(solution, maxIterations, lambda, samples, random, ref quality, out evaluatedSolutions,
|
---|
| 158 | problem.DistanceMatrix, problem.Vehicles, problem.DueTime, problem.ServiceTime,
|
---|
| 159 | problem.ReadyTime, problem.Demand, problem.Capacity, problem.Coordinates,
|
---|
| 160 | problem.FleetUsageFactorParameter.Value, problem.TimeFactorParameter.Value,
|
---|
| 161 | problem.DistanceFactorParameter.Value, problem.OverloadPenaltyParameter.Value,
|
---|
| 162 | problem.TardinessPenaltyParameter.Value, problem.UseDistanceMatrix);
|
---|
| 163 |
|
---|
| 164 | EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
|
---|
| 165 | QualityParameter.ActualValue.Value = quality;
|
---|
| 166 |
|
---|
| 167 | return base.Apply();
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|