1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Operators {
|
---|
35 | [Item("ApproximateLocalSearch", @"The approximate local search is described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.
|
---|
36 |
|
---|
37 | The implementation differs slightly from Mateus et al. in that the maximumIterations parameter defines a cap on the number of steps that the local search can perform. While the maxSampleSize parameter corresponds to the maxItr parameter defined by Mateus et al.")]
|
---|
38 | [StorableClass]
|
---|
39 | public class ApproximateLocalSearch : SingleSuccessorOperator, IGQAPLocalImprovementOperator, IStochasticOperator {
|
---|
40 | public IProblem Problem { get; set; }
|
---|
41 | public Type ProblemType {
|
---|
42 | get { return typeof(GeneralizedQuadraticAssignmentProblem); }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public ILookupParameter<IRandom> RandomParameter {
|
---|
46 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
47 | }
|
---|
48 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
49 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
50 | }
|
---|
51 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
52 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
55 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<DoubleValue> InfeasibilityParameter {
|
---|
58 | get { return (ILookupParameter<DoubleValue>)Parameters["Infeasibility"]; }
|
---|
59 | }
|
---|
60 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
61 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
62 | }
|
---|
63 | public IValueLookupParameter<IntValue> MaximumCandidateListSizeParameter {
|
---|
64 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumCandidateListSize"]; }
|
---|
65 | }
|
---|
66 | public IValueLookupParameter<IntValue> MaximumSampleSizeParameter {
|
---|
67 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumSampleSize"]; }
|
---|
68 | }
|
---|
69 | public ILookupParameter<IntegerVector> AssignmentParameter {
|
---|
70 | get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
|
---|
71 | }
|
---|
72 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
73 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
74 | }
|
---|
75 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
76 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
77 | }
|
---|
78 | public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
|
---|
79 | get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
|
---|
80 | }
|
---|
81 | public ILookupParameter<DoubleValue> TransportationCostsParameter {
|
---|
82 | get { return (ILookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
|
---|
83 | }
|
---|
84 | public ILookupParameter<DoubleArray> DemandsParameter {
|
---|
85 | get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
|
---|
86 | }
|
---|
87 | public ILookupParameter<DoubleArray> CapacitiesParameter {
|
---|
88 | get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | [StorableConstructor]
|
---|
93 | protected ApproximateLocalSearch(bool deserializing) : base(deserializing) { }
|
---|
94 | protected ApproximateLocalSearch(ApproximateLocalSearch original, Cloner cloner) : base(original, cloner) { }
|
---|
95 | public ApproximateLocalSearch()
|
---|
96 | : base() {
|
---|
97 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
98 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations that should be performed."));
|
---|
99 | Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents."));
|
---|
100 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
|
---|
101 | Parameters.Add(new LookupParameter<DoubleValue>("Infeasibility", "The infeasibility describes the sum of the overbooked capacities."));
|
---|
102 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results."));
|
---|
103 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));
|
---|
104 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumSampleSize", "The maximum number of candidates that should be sampled in each step.", new IntValue(100)));
|
---|
105 | Parameters.Add(new LookupParameter<IntegerVector>("Assignment", "The equipment-location assignment vector."));
|
---|
106 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments."));
|
---|
107 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed."));
|
---|
108 | Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j."));
|
---|
109 | Parameters.Add(new LookupParameter<DoubleValue>("TransportationCosts", "The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights matrix already."));
|
---|
110 | Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments."));
|
---|
111 | Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
|
---|
112 | }
|
---|
113 |
|
---|
114 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
115 | return new ApproximateLocalSearch(this, cloner);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /// <summary>
|
---|
119 | /// The implementation differs slightly from Mateus et al. in that the maximumIterations parameter defines a cap
|
---|
120 | /// on the number of steps that the local search can perform. While the maxSampleSize parameter corresponds to
|
---|
121 | /// the maxItr parameter defined by Mateus et al.
|
---|
122 | /// </summary>
|
---|
123 | /// <param name="random">The random number generator to use.</param>
|
---|
124 | /// <param name="assignment">The equipment-location assignment vector.</param>
|
---|
125 | /// <param name="quality">The solution quality.</param>
|
---|
126 | /// <param name="infeasibility">The infeasibility describes the sum of the overbooked capacities.</param>
|
---|
127 | /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
|
---|
128 | /// <param name="maxSampleSize">The maximum number of candidates that should be sampled in each step.</param>
|
---|
129 | /// <param name="maximumIterations">The maximum number of iterations that should be performed.</param>
|
---|
130 | /// <param name="demands">The demands vector describes the space requirements of the equipments.</param>
|
---|
131 | /// <param name="capacities">The capacities vector describes the available space at the locations.</param>
|
---|
132 | /// <param name="weights">The weights matrix describes the flows between the equipments.</param>
|
---|
133 | /// <param name="distances">The distances matrix describes the distances between the locations at which the equipment can be installed.</param>
|
---|
134 | /// <param name="installationCosts">The installation costs matrix describes the installation costs of installing equipment i at location j</param>
|
---|
135 | /// <param name="transportationCosts">The transportation cost represents the flow-unit per distance-unit cost factor. This value can also be set to 1 if these costs are factored into the weights matrix already.</param>
|
---|
136 | public static void Apply(IRandom random, IntegerVector assignment, DoubleValue quality, DoubleValue infeasibility,
|
---|
137 | IntValue maxCLS, IntValue maxSampleSize, IntValue maximumIterations,
|
---|
138 | DoubleArray demands, DoubleArray capacities,
|
---|
139 | DoubleMatrix weights, DoubleMatrix distances,
|
---|
140 | DoubleMatrix installationCosts,
|
---|
141 | DoubleValue transportationCosts) {
|
---|
142 | for (int i = 0; i < maximumIterations.Value; i++) {
|
---|
143 | int count = 0;
|
---|
144 | var CLS = new List<Tuple<NMove, DoubleValue, DoubleValue>>();
|
---|
145 | double sum = 0.0;
|
---|
146 | do {
|
---|
147 | var move = StochasticNMoveSingleMoveGenerator.Generate(random, assignment, 2, capacities);
|
---|
148 | DoubleValue moveInfeasibility;
|
---|
149 | var moveQuality = GQAPNMoveEvaluator.Evaluate(move, assignment, quality,
|
---|
150 | demands, capacities, weights, distances,
|
---|
151 | installationCosts, transportationCosts, out moveInfeasibility);
|
---|
152 | if (moveInfeasibility.Value <= 0.0 && moveQuality.Value < quality.Value) {
|
---|
153 | CLS.Add(Tuple.Create(move, moveQuality, moveInfeasibility));
|
---|
154 | sum += 1.0 / moveQuality.Value;
|
---|
155 | }
|
---|
156 | count++;
|
---|
157 | } while (CLS.Count < maxCLS.Value && count < maxSampleSize.Value);
|
---|
158 | if (CLS.Count == 0) return; // END
|
---|
159 | else {
|
---|
160 | var ball = random.NextDouble() * sum;
|
---|
161 | var selected = CLS.Last();
|
---|
162 | foreach (var candidate in CLS) {
|
---|
163 | ball -= 1.0 / candidate.Item2.Value;
|
---|
164 | if (ball <= 0.0) {
|
---|
165 | selected = candidate;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | NMoveMaker.Apply(assignment, selected.Item1);
|
---|
170 | quality.Value = selected.Item2.Value;
|
---|
171 | infeasibility.Value = selected.Item3.Value;
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | public override IOperation Apply() {
|
---|
177 | Apply(RandomParameter.ActualValue,
|
---|
178 | AssignmentParameter.ActualValue,
|
---|
179 | QualityParameter.ActualValue,
|
---|
180 | InfeasibilityParameter.ActualValue,
|
---|
181 | MaximumCandidateListSizeParameter.ActualValue,
|
---|
182 | MaximumSampleSizeParameter.ActualValue,
|
---|
183 | MaximumIterationsParameter.ActualValue,
|
---|
184 | DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
|
---|
185 | WeightsParameter.ActualValue, DistancesParameter.ActualValue,
|
---|
186 | InstallationCostsParameter.ActualValue,
|
---|
187 | TransportationCostsParameter.ActualValue);
|
---|
188 | return base.Apply();
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|