Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/ApproximateLocalSearch.cs @ 7413

Last change on this file since 7413 was 7412, checked in by abeham, 13 years ago

#1614: worked on GQAP and GRASP+PR

File size: 13.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace 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
37The 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> FlowDistanceQualityParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
59    }
60    public ILookupParameter<DoubleValue> InstallationQualityParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
62    }
63    public ILookupParameter<DoubleValue> OverbookedCapacityParameter {
64      get { return (ILookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
65    }
66    public ILookupParameter<ResultCollection> ResultsParameter {
67      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
68    }
69    public IValueLookupParameter<IntValue> MaximumCandidateListSizeParameter {
70      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumCandidateListSize"]; }
71    }
72    public IValueLookupParameter<IntValue> MaximumSampleSizeParameter {
73      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumSampleSize"]; }
74    }
75    public ILookupParameter<IntegerVector> AssignmentParameter {
76      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
77    }
78    public ILookupParameter<DoubleMatrix> WeightsParameter {
79      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
80    }
81    public ILookupParameter<DoubleMatrix> DistancesParameter {
82      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
83    }
84    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
85      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
86    }
87    public ILookupParameter<DoubleArray> DemandsParameter {
88      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
89    }
90    public ILookupParameter<DoubleArray> CapacitiesParameter {
91      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
92    }
93    public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
94      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
95    }
96    public IValueLookupParameter<DoubleValue> OverbookedCapacityPenaltyParameter {
97      get { return (IValueLookupParameter<DoubleValue>)Parameters["OverbookedCapacityPenalty"]; }
98    }
99    public IValueLookupParameter<PercentValue> OneMoveProbabilityParameter {
100      get { return (IValueLookupParameter<PercentValue>)Parameters["OneMoveProbability"]; }
101    }
102
103    [StorableConstructor]
104    protected ApproximateLocalSearch(bool deserializing) : base(deserializing) { }
105    protected ApproximateLocalSearch(ApproximateLocalSearch original, Cloner cloner) : base(original, cloner) { }
106    public ApproximateLocalSearch()
107      : base() {
108      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
109      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations that should be performed."));
110      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents."));
111      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
112      Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", "The quality regarding the flow-distance criteria."));
113      Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", "The quality regarding the installation costs."));
114      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", "The sum of the overbooked capacities relative to the capacity of each location."));
115      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results."));
116      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));
117      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumSampleSize", "The maximum number of candidates that should be sampled in each step.", new IntValue(100)));
118      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", "The equipment-location assignment vector."));
119      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix describes the flows between the equipments."));
120      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix describes the distances between the locations at which the equipment can be installed."));
121      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", "The installation costs matrix describes the installation costs of installing equipment i at location j."));
122      Parameters.Add(new LookupParameter<DoubleArray>("Demands", "The demands vector describes the space requirements of the equipments."));
123      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", "The capacities vector describes the available space at the locations."));
124      Parameters.Add(new ValueLookupParameter<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."));
125      Parameters.Add(new ValueLookupParameter<DoubleValue>("OverbookedCapacityPenalty", "The multiplier for the constraint violation when added to the quality."));
126      Parameters.Add(new ValueLookupParameter<PercentValue>("OneMoveProbability", "The probability for performing a 1-move, which is the opposite of performing a 2-move.", new PercentValue(.5)));
127    }
128
129    public override IDeepCloneable Clone(Cloner cloner) {
130      return new ApproximateLocalSearch(this, cloner);
131    }
132
133    /// <summary>
134    /// The implementation differs slightly from Mateus et al. in that the maximumIterations parameter defines a cap
135    /// on the number of steps that the local search can perform. While the maxSampleSize parameter corresponds to
136    /// the maxItr parameter defined by Mateus et al.
137    /// </summary>
138    /// <param name="random">The random number generator to use.</param>
139    /// <param name="assignment">The equipment-location assignment vector.</param>
140    /// <param name="quality">The solution quality.</param>
141    /// <param name="flowDistanceQuality">The quality regarding the flow-distance criteria.</param>
142    /// <param name="installationQuality">The quality regarding the installation costs.</param>
143    /// <param name="overbookedCapacity">The sum of the overbooked capacities relative to the capacity of each location.</param>
144    /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
145    /// <param name="maxSampleSize">The maximum number of candidates that should be sampled in each step.</param>
146    /// <param name="maximumIterations">The maximum number of iterations that should be performed.</param>
147    /// <param name="weights">The weights matrix describes the flows between the equipments.</param>
148    /// <param name="distances">The distances matrix describes the distances between the locations at which the equipment can be installed.</param>
149    /// <param name="installationCosts">The installation costs matrix describes the installation costs of installing equipment i at location j</param>
150    /// <param name="demands">The demands vector describes the space requirements of the equipments.</param>
151    /// <param name="capacities">The capacities vector describes the available space at the locations.</param>
152    /// <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>
153    /// <param name="overbookedCapacityPenalty"></param>
154    /// <param name="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
155    public static void Apply(IRandom random, IntegerVector assignment,
156      DoubleValue quality, DoubleValue flowDistanceQuality, DoubleValue installationQuality, DoubleValue overbookedCapacity,
157      IntValue maxCLS, IntValue maxSampleSize, IntValue maximumIterations,
158      DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, DoubleArray demands, DoubleArray capacities,
159      DoubleValue transportationCosts, DoubleValue overbookedCapacityPenalty, PercentValue oneMoveProbability) {
160
161      for (int i = 0; i < maximumIterations.Value; i++) {
162        int count = 0;
163        var CLS = new List<Tuple<NMove, double, double, double, double>>();
164        double sum = 0.0;
165        do {
166          NMove move;
167          if (random.NextDouble() < oneMoveProbability.Value)
168            move = StochasticNMoveSingleMoveGenerator.GenerateExactlyN(random, assignment, 1, capacities);
169          else move = StochasticNMoveSingleMoveGenerator.GenerateExactlyN(random, assignment, 2, capacities);
170
171          double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
172          GQAPNMoveEvaluator.Evaluate(move, assignment, weights, distances, installationCosts,
173            demands, capacities, out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
174          double moveQuality = GQAPEvaluator.GetCombinedQuality(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
175            transportationCosts.Value, overbookedCapacityPenalty.Value);
176
177          if (moveOverbookedCapacity <= 0.0 && moveQuality < 0.0) {
178            CLS.Add(Tuple.Create(move, moveQuality, moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity));
179            sum += 1.0 / moveQuality;
180          }
181          count++;
182        } while (CLS.Count < maxCLS.Value && count < maxSampleSize.Value);
183
184        if (CLS.Count == 0)
185          return; // END
186        else {
187          var ball = random.NextDouble() * sum;
188          var selected = CLS.Last();
189          foreach (var candidate in CLS) {
190            ball -= 1.0 / candidate.Item2;
191            if (ball <= 0.0) {
192              selected = candidate;
193              break;
194            }
195          }
196          NMoveMaker.Apply(assignment, selected.Item1);
197          quality.Value += selected.Item2;
198          flowDistanceQuality.Value += selected.Item3;
199          installationQuality.Value += selected.Item4;
200          overbookedCapacity.Value += selected.Item5;
201        }
202      }
203    }
204
205    public override IOperation Apply() {
206      Apply(RandomParameter.ActualValue,
207        AssignmentParameter.ActualValue,
208        QualityParameter.ActualValue,
209        FlowDistanceQualityParameter.ActualValue,
210        InstallationQualityParameter.ActualValue,
211        OverbookedCapacityParameter.ActualValue,
212        MaximumCandidateListSizeParameter.ActualValue,
213        MaximumSampleSizeParameter.ActualValue,
214        MaximumIterationsParameter.ActualValue,
215        WeightsParameter.ActualValue, DistancesParameter.ActualValue,
216        InstallationCostsParameter.ActualValue,
217        DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
218        TransportationCostsParameter.ActualValue,
219        OverbookedCapacityPenaltyParameter.ActualValue,
220        OneMoveProbabilityParameter.ActualValue);
221      return base.Apply();
222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.