Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Alba/LocalImprovement/AlbaLambdaInterchangeLocalImprovementOperator.cs @ 10872

Last change on this file since 10872 was 10298, checked in by svonolfe, 11 years ago

Adapted all VRP related operators to subclass InstrumentedOperator (#2119)

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