Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10216 was 7970, checked in by abeham, 13 years ago

#1614: restructured architecture to allow for different evaluator with different penalty strategies

File size: 13.8 KB
RevLine 
[7407]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 {
[7419]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.")]
[7407]36  [StorableClass]
[7419]37  public class ApproximateLocalSearch : SingleSuccessorOperator, IDemandsAwareGQAPOperator, ICapacitiesAwareGQAPOperator,
38    IWeightsAwareGQAPOperator, IDistancesAwareGQAPOperator, IInstallationCostsAwareGQAPOperator,
[7970]39    ITransportationCostsAwareGQAPOperator, IExpectedRandomQualityAwareGQAPOperator,
40    IAssignmentAwareGQAPOperator, IQualityAwareGQAPOperator, IGQAPLocalImprovementOperator,
41    IEvaluatorAwareGQAPOperator, IStochasticOperator {
[7407]42    public IProblem Problem { get; set; }
43    public Type ProblemType {
44      get { return typeof(GeneralizedQuadraticAssignmentProblem); }
45    }
46
[7419]47    public ILookupParameter<DoubleArray> DemandsParameter {
48      get { return (ILookupParameter<DoubleArray>)Parameters["Demands"]; }
[7407]49    }
[7419]50    public ILookupParameter<DoubleArray> CapacitiesParameter {
51      get { return (ILookupParameter<DoubleArray>)Parameters["Capacities"]; }
[7407]52    }
[7419]53    public ILookupParameter<DoubleMatrix> WeightsParameter {
54      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
[7407]55    }
[7419]56    public ILookupParameter<DoubleMatrix> DistancesParameter {
57      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
58    }
59    public ILookupParameter<DoubleMatrix> InstallationCostsParameter {
60      get { return (ILookupParameter<DoubleMatrix>)Parameters["InstallationCosts"]; }
61    }
62    public IValueLookupParameter<DoubleValue> TransportationCostsParameter {
63      get { return (IValueLookupParameter<DoubleValue>)Parameters["TransportationCosts"]; }
64    }
[7970]65    public IValueLookupParameter<DoubleValue> ExpectedRandomQualityParameter {
66      get { return (IValueLookupParameter<DoubleValue>)Parameters["ExpectedRandomQuality"]; }
[7419]67    }
68    public ILookupParameter<IntegerVector> AssignmentParameter {
69      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
70    }
71    public ILookupParameter<BoolValue> MaximizationParameter {
72      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
73    }
[7407]74    public ILookupParameter<DoubleValue> QualityParameter {
75      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
76    }
[7412]77    public ILookupParameter<DoubleValue> FlowDistanceQualityParameter {
78      get { return (ILookupParameter<DoubleValue>)Parameters["FlowDistanceQuality"]; }
[7407]79    }
[7412]80    public ILookupParameter<DoubleValue> InstallationQualityParameter {
81      get { return (ILookupParameter<DoubleValue>)Parameters["InstallationQuality"]; }
82    }
83    public ILookupParameter<DoubleValue> OverbookedCapacityParameter {
84      get { return (ILookupParameter<DoubleValue>)Parameters["OverbookedCapacity"]; }
85    }
[7419]86    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
87      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
[7407]88    }
[7419]89    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
90      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
91    }
92    public ILookupParameter<IRandom> RandomParameter {
93      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
94    }
[7407]95    public IValueLookupParameter<IntValue> MaximumCandidateListSizeParameter {
96      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumCandidateListSize"]; }
97    }
[7412]98    public IValueLookupParameter<PercentValue> OneMoveProbabilityParameter {
99      get { return (IValueLookupParameter<PercentValue>)Parameters["OneMoveProbability"]; }
100    }
[7419]101    public ILookupParameter<ResultCollection> ResultsParameter {
102      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
103    }
[7970]104    public IValueLookupParameter<IGQAPEvaluator> EvaluatorParameter {
105      get { return (IValueLookupParameter<IGQAPEvaluator>)Parameters["Evaluator"]; }
106    }
[7407]107
108    [StorableConstructor]
109    protected ApproximateLocalSearch(bool deserializing) : base(deserializing) { }
110    protected ApproximateLocalSearch(ApproximateLocalSearch original, Cloner cloner) : base(original, cloner) { }
111    public ApproximateLocalSearch()
112      : base() {
[7419]113      Parameters.Add(new LookupParameter<DoubleArray>("Demands", GeneralizedQuadraticAssignmentProblem.DemandsDescription));
114      Parameters.Add(new LookupParameter<DoubleArray>("Capacities", GeneralizedQuadraticAssignmentProblem.CapacitiesDescription));
115      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", GeneralizedQuadraticAssignmentProblem.WeightsDescription));
116      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", GeneralizedQuadraticAssignmentProblem.DistancesDescription));
117      Parameters.Add(new LookupParameter<DoubleMatrix>("InstallationCosts", GeneralizedQuadraticAssignmentProblem.InstallationCostsDescription));
118      Parameters.Add(new ValueLookupParameter<DoubleValue>("TransportationCosts", GeneralizedQuadraticAssignmentProblem.TransportationCostsDescription));
[7970]119      Parameters.Add(new ValueLookupParameter<DoubleValue>("ExpectedRandomQuality", GeneralizedQuadraticAssignmentProblem.ExpectedRandomQualityDescription));
[7419]120      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
121      Parameters.Add(new LookupParameter<BoolValue>("Maximization", GeneralizedQuadraticAssignmentProblem.MaximizationDescription));
122      Parameters.Add(new LookupParameter<DoubleValue>("Quality", GQAPEvaluator.QualityDescription));
123      Parameters.Add(new LookupParameter<DoubleValue>("FlowDistanceQuality", GQAPEvaluator.FlowDistanceQualityDescription));
124      Parameters.Add(new LookupParameter<DoubleValue>("InstallationQuality", GQAPEvaluator.InstallationQualityDescription));
125      Parameters.Add(new LookupParameter<DoubleValue>("OverbookedCapacity", GQAPEvaluator.OverbookedCapacityDescription));
[7407]126      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations that should be performed."));
127      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents."));
[7419]128      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
[7407]129      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));
[7412]130      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)));
[7419]131      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results."));
[7970]132      Parameters.Add(new ValueLookupParameter<IGQAPEvaluator>("Evaluator", "The evaluator that is used to evaluate GQAP solutions."));
[7407]133    }
134
135    public override IDeepCloneable Clone(Cloner cloner) {
136      return new ApproximateLocalSearch(this, cloner);
137    }
138
139    /// <summary>
140    /// The implementation differs slightly from Mateus et al. in that the maximumIterations parameter defines a cap
141    /// on the number of steps that the local search can perform. While the maxSampleSize parameter corresponds to
142    /// the maxItr parameter defined by Mateus et al.
143    /// </summary>
144    /// <param name="random">The random number generator to use.</param>
145    /// <param name="assignment">The equipment-location assignment vector.</param>
146    /// <param name="quality">The solution quality.</param>
[7412]147    /// <param name="flowDistanceQuality">The quality regarding the flow-distance criteria.</param>
148    /// <param name="installationQuality">The quality regarding the installation costs.</param>
149    /// <param name="overbookedCapacity">The sum of the overbooked capacities relative to the capacity of each location.</param>
[7407]150    /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
[7419]151    /// <param name="maximumIterations">The maximum number of iterations that should be performed each time the candidate list is generated.</param>
[7407]152    /// <param name="weights">The weights matrix describes the flows between the equipments.</param>
153    /// <param name="distances">The distances matrix describes the distances between the locations at which the equipment can be installed.</param>
154    /// <param name="installationCosts">The installation costs matrix describes the installation costs of installing equipment i at location j</param>
[7412]155    /// <param name="demands">The demands vector describes the space requirements of the equipments.</param>
156    /// <param name="capacities">The capacities vector describes the available space at the locations.</param>
[7407]157    /// <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>
[7412]158    /// <param name="overbookedCapacityPenalty"></param>
159    /// <param name="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
160    public static void Apply(IRandom random, IntegerVector assignment,
161      DoubleValue quality, DoubleValue flowDistanceQuality, DoubleValue installationQuality, DoubleValue overbookedCapacity,
[7419]162      IntValue maxCLS, IntValue maximumIterations,
[7412]163      DoubleMatrix weights, DoubleMatrix distances, DoubleMatrix installationCosts, DoubleArray demands, DoubleArray capacities,
[7970]164      DoubleValue transportationCosts, DoubleValue expectedRandomQuality, PercentValue oneMoveProbability,
165      IGQAPEvaluator evaluator) {
[7412]166
[7419]167      while (true) {
[7407]168        int count = 0;
[7412]169        var CLS = new List<Tuple<NMove, double, double, double, double>>();
[7407]170        double sum = 0.0;
171        do {
[7412]172          NMove move;
173          if (random.NextDouble() < oneMoveProbability.Value)
174            move = StochasticNMoveSingleMoveGenerator.GenerateExactlyN(random, assignment, 1, capacities);
175          else move = StochasticNMoveSingleMoveGenerator.GenerateExactlyN(random, assignment, 2, capacities);
176
177          double moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity;
178          GQAPNMoveEvaluator.Evaluate(move, assignment, weights, distances, installationCosts,
[7970]179            demands, capacities, evaluator, out moveFlowDistanceQuality, out moveInstallationQuality, out moveOverbookedCapacity);
180          double moveQuality = evaluator.GetFitness(moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity,
181            transportationCosts.Value, expectedRandomQuality.Value);
[7412]182
183          if (moveOverbookedCapacity <= 0.0 && moveQuality < 0.0) {
184            CLS.Add(Tuple.Create(move, moveQuality, moveFlowDistanceQuality, moveInstallationQuality, moveOverbookedCapacity));
185            sum += 1.0 / moveQuality;
[7407]186          }
187          count++;
[7419]188        } while (CLS.Count < maxCLS.Value && count < maximumIterations.Value);
[7412]189
190        if (CLS.Count == 0)
191          return; // END
[7407]192        else {
193          var ball = random.NextDouble() * sum;
194          var selected = CLS.Last();
195          foreach (var candidate in CLS) {
[7412]196            ball -= 1.0 / candidate.Item2;
[7407]197            if (ball <= 0.0) {
198              selected = candidate;
199              break;
200            }
201          }
202          NMoveMaker.Apply(assignment, selected.Item1);
[7412]203          quality.Value += selected.Item2;
204          flowDistanceQuality.Value += selected.Item3;
205          installationQuality.Value += selected.Item4;
206          overbookedCapacity.Value += selected.Item5;
[7407]207        }
208      }
209    }
210
211    public override IOperation Apply() {
212      Apply(RandomParameter.ActualValue,
213        AssignmentParameter.ActualValue,
214        QualityParameter.ActualValue,
[7412]215        FlowDistanceQualityParameter.ActualValue,
216        InstallationQualityParameter.ActualValue,
217        OverbookedCapacityParameter.ActualValue,
[7407]218        MaximumCandidateListSizeParameter.ActualValue,
219        MaximumIterationsParameter.ActualValue,
220        WeightsParameter.ActualValue, DistancesParameter.ActualValue,
221        InstallationCostsParameter.ActualValue,
[7412]222        DemandsParameter.ActualValue, CapacitiesParameter.ActualValue,
223        TransportationCostsParameter.ActualValue,
[7970]224        ExpectedRandomQualityParameter.ActualValue,
225        OneMoveProbabilityParameter.ActualValue,
226        EvaluatorParameter.ActualValue);
[7407]227      return base.Apply();
228    }
229  }
230}
Note: See TracBrowser for help on using the repository browser.