Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/LocalImprovement/AlbaLambdaInterchangeLocalImprovementOperator.cs @ 17704

Last change on this file since 17704 was 17704, checked in by abeham, 4 years ago

#2521: worked on VRP

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29
30namespace HeuristicLab.Problems.VehicleRouting.Encodings.Alba {
31  [Item("AlbaLambdaInterchangeLocalImprovementOperator", "Takes a solution and finds the local optimum with respect to the lambda interchange neighborhood by decending along the steepest gradient.")]
32  [StorableType("84981F03-B886-4ADD-8DB5-C12628404335")]
33  public class AlbaLambdaInterchangeLocalImprovementOperator : VRPOperator, IStochasticOperator, ILocalImprovementOperator, ISingleObjectiveOperator, IAlbaOperator {
34
35    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
36      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
37    }
38
39    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
40      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
41    }
42
43    public ILookupParameter<ResultCollection> ResultsParameter {
44      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
45    }
46
47    public ILookupParameter<IVRPEncodedSolution> VRPToursParameter {
48      get { return (ILookupParameter<IVRPEncodedSolution>)Parameters["VRPTours"]; }
49    }
50
51    public ILookupParameter<DoubleValue> QualityParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
53    }
54
55    public IValueParameter<IntValue> LambdaParameter {
56      get { return (IValueParameter<IntValue>)Parameters["Lambda"]; }
57    }
58
59    public IValueParameter<IntValue> SampleSizeParameter {
60      get { return (IValueParameter<IntValue>)Parameters["SampleSize"]; }
61    }
62
63    public ILookupParameter<IRandom> RandomParameter {
64      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
65    }
66
67    [StorableConstructor]
68    protected AlbaLambdaInterchangeLocalImprovementOperator(StorableConstructorFlag _) : base(_) { }
69    protected AlbaLambdaInterchangeLocalImprovementOperator(AlbaLambdaInterchangeLocalImprovementOperator original, Cloner cloner)
70      : base(original, cloner) {
71    }
72    public AlbaLambdaInterchangeLocalImprovementOperator()
73      : base() {
74      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)));
75      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)."));
76      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
77      Parameters.Add(new LookupParameter<IVRPEncodedSolution>("VRPTours", "The VRP tours to be manipulated."));
78      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
79      Parameters.Add(new ValueParameter<IntValue>("Lambda", "The lambda value.", new IntValue(1)));
80      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The number of moves to generate.", new IntValue(2000)));
81      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new AlbaLambdaInterchangeLocalImprovementOperator(this, cloner);
86    }
87
88    public static void Apply(AlbaEncodedSolution solution, int maxIterations,
89      int lambda, int samples, IRandom random, IVRPProblemInstance problemInstance, ref double quality, out int evaluatedSolutions) {
90      evaluatedSolutions = 0;
91
92      for (int i = 0; i < maxIterations; i++) {
93        AlbaLambdaInterchangeMove bestMove = null;
94        foreach (AlbaLambdaInterchangeMove move in AlbaStochasticLambdaInterchangeMultiMoveGenerator.GenerateAllMoves(solution, problemInstance, lambda, samples, random)) {
95          AlbaEncodedSolution newSolution = solution.Clone() as AlbaEncodedSolution;
96          AlbaLambdaInterchangeMoveMaker.Apply(newSolution, move);
97          double moveQuality =
98            problemInstance.Evaluate(newSolution).Quality;
99
100          evaluatedSolutions++;
101          if (moveQuality < quality || quality == -1) {
102            quality = moveQuality;
103            bestMove = move;
104          }
105        }
106        if (bestMove != null)
107          AlbaLambdaInterchangeMoveMaker.Apply(solution, bestMove);
108      }
109    }
110
111    public override IOperation InstrumentedApply() {
112      int maxIterations = MaximumIterationsParameter.ActualValue.Value;
113      AlbaEncodedSolution solution = null;
114
115      if (VRPToursParameter.ActualValue is AlbaEncodedSolution)
116        solution = VRPToursParameter.ActualValue as AlbaEncodedSolution;
117      else
118        VRPToursParameter.ActualValue = solution = AlbaEncodedSolution.ConvertFrom(VRPToursParameter.ActualValue, ProblemInstance);
119
120      int lambda = LambdaParameter.Value.Value;
121      int samples = SampleSizeParameter.Value.Value;
122      IRandom random = RandomParameter.ActualValue;
123
124      double quality = QualityParameter.ActualValue.Value;
125      int evaluatedSolutions;
126
127      Apply(solution, maxIterations, lambda, samples, random, ProblemInstance, ref quality, out evaluatedSolutions);
128
129      EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
130      QualityParameter.ActualValue.Value = quality;
131
132      return base.InstrumentedApply();
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.