1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.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 | using HeuristicLab.Problems.VehicleRouting.Interfaces;
|
---|
34 |
|
---|
35 | namespace 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 |
|
---|
40 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
41 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
45 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
49 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public ILookupParameter<IVRPEncoding> VRPToursParameter {
|
---|
53 | get { return (ILookupParameter<IVRPEncoding>)Parameters["VRPTours"]; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
57 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public IValueParameter<IntValue> LambdaParameter {
|
---|
61 | get { return (IValueParameter<IntValue>)Parameters["Lambda"]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public IValueParameter<IntValue> SampleSizeParameter {
|
---|
65 | get { return (IValueParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
66 | }
|
---|
67 |
|
---|
68 | public ILookupParameter<IRandom> RandomParameter {
|
---|
69 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
70 | }
|
---|
71 |
|
---|
72 | [StorableConstructor]
|
---|
73 | protected AlbaLambdaInterchangeLocalImprovementOperator(bool deserializing) : base(deserializing) { }
|
---|
74 | protected AlbaLambdaInterchangeLocalImprovementOperator(AlbaLambdaInterchangeLocalImprovementOperator original, Cloner cloner)
|
---|
75 | : base(original, cloner) {
|
---|
76 | }
|
---|
77 | public AlbaLambdaInterchangeLocalImprovementOperator()
|
---|
78 | : base() {
|
---|
79 | 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)));
|
---|
80 | 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)."));
|
---|
81 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
|
---|
82 | Parameters.Add(new LookupParameter<IVRPEncoding>("VRPTours", "The VRP tours to be manipulated."));
|
---|
83 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
|
---|
84 | Parameters.Add(new ValueParameter<IntValue>("Lambda", "The lambda value.", new IntValue(1)));
|
---|
85 | Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The number of moves to generate.", new IntValue(2000)));
|
---|
86 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
90 | return new AlbaLambdaInterchangeLocalImprovementOperator(this, cloner);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public static void Apply(AlbaEncoding solution, int maxIterations,
|
---|
94 | int lambda, int samples, IRandom random, IVRPProblemInstance problemInstance, ref double quality, out int evaluatedSolutions) {
|
---|
95 | evaluatedSolutions = 0;
|
---|
96 |
|
---|
97 | for (int i = 0; i < maxIterations; i++) {
|
---|
98 | AlbaLambdaInterchangeMove bestMove = null;
|
---|
99 | foreach (AlbaLambdaInterchangeMove move in AlbaStochasticLambdaInterchangeMultiMoveGenerator.GenerateAllMoves(solution, problemInstance, lambda, samples, random)) {
|
---|
100 | AlbaEncoding newSolution = solution.Clone() as AlbaEncoding;
|
---|
101 | AlbaLambdaInterchangeMoveMaker.Apply(newSolution, move);
|
---|
102 | double moveQuality =
|
---|
103 | problemInstance.Evaluate(newSolution).Quality;
|
---|
104 |
|
---|
105 | evaluatedSolutions++;
|
---|
106 | if (moveQuality < quality || quality == -1) {
|
---|
107 | quality = moveQuality;
|
---|
108 | bestMove = move;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | if (bestMove != null)
|
---|
112 | AlbaLambdaInterchangeMoveMaker.Apply(solution, bestMove);
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override IOperation InstrumentedApply() {
|
---|
117 | int maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
118 | AlbaEncoding solution = null;
|
---|
119 |
|
---|
120 | if (VRPToursParameter.ActualValue is AlbaEncoding)
|
---|
121 | solution = VRPToursParameter.ActualValue as AlbaEncoding;
|
---|
122 | else
|
---|
123 | VRPToursParameter.ActualValue = solution = AlbaEncoding.ConvertFrom(VRPToursParameter.ActualValue, ProblemInstance);
|
---|
124 |
|
---|
125 | int lambda = LambdaParameter.Value.Value;
|
---|
126 | int samples = SampleSizeParameter.Value.Value;
|
---|
127 | IRandom random = RandomParameter.ActualValue;
|
---|
128 |
|
---|
129 | double quality = QualityParameter.ActualValue.Value;
|
---|
130 | int evaluatedSolutions;
|
---|
131 |
|
---|
132 | Apply(solution, maxIterations, lambda, samples, random, ProblemInstance, ref quality, out evaluatedSolutions);
|
---|
133 |
|
---|
134 | EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
|
---|
135 | QualityParameter.ActualValue.Value = quality;
|
---|
136 |
|
---|
137 | return base.InstrumentedApply();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|