source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/LocalImprovers/OneOptLocalSearch.cs @ 16077

Last change on this file since 16077 was 16077, checked in by abeham, 12 months ago

#2936: Added integration branch

File size: 8.1 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
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;
33using HeuristicLab.Random;
34
35namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
36  [Item("1-opt LocalSearch", "A simple exhaustive 1-change local search.")]
37  [StorableClass]
38  public class OneOptLocalSearch : SingleSuccessorOperator, IProblemInstanceAwareGQAPOperator,
39    IQualityAwareGQAPOperator, IGQAPLocalImprovementOperator, IAssignmentAwareGQAPOperator, IStochasticOperator {
40    public IProblem Problem { get; set; }
41    public Type ProblemType {
42      get { return typeof(GQAP); }
43    }
44
45    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
46      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
47    }
48    public ILookupParameter<IntegerVector> AssignmentParameter {
49      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
50    }
51    public ILookupParameter<DoubleValue> QualityParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
53    }
54    public ILookupParameter<Evaluation> EvaluationParameter {
55      get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; }
56    }
57    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
59    }
60    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
61      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
62    }
63    public ILookupParameter<IRandom> RandomParameter {
64      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
65    }
66    public ILookupParameter<ResultCollection> ResultsParameter {
67      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
68    }
69
70    [StorableConstructor]
71    protected OneOptLocalSearch(bool deserializing) : base(deserializing) { }
72    protected OneOptLocalSearch(OneOptLocalSearch original, Cloner cloner) : base(original, cloner) { }
73    public OneOptLocalSearch()
74      : base() {
75      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
76      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
77      Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
78      Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription));
79      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "This parameter is ignored by the operator.") { Hidden = true });
80      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents."));
81      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
82      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results."));
83    }
84
85    public override IDeepCloneable Clone(Cloner cloner) {
86      return new OneOptLocalSearch(this, cloner);
87    }
88
89    public static void Apply(IRandom random, GQAPSolution sol,
90      GQAPInstance problemInstance, out int evaluatedSolutions) {
91      var fit = problemInstance.ToSingleObjective(sol.Evaluation);
92      var eval = sol.Evaluation;
93      Apply(random, sol.Assignment, ref fit, ref eval, problemInstance, out evaluatedSolutions);
94      sol.Evaluation = eval;
95    }
96
97    /// <summary>
98    /// </summary>
99    /// <param name="random">The random number generator to use.</param>
100    /// <param name="assignment">The equipment-location assignment vector.</param>
101    /// <param name="quality">The solution quality.</param>
102    /// <param name="evaluation">The evaluation result of the solution.</param>
103    /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
104    /// <param name="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
105    /// <param name="maximumIterations">The maximum number of iterations that should be performed each time the candidate list is generated.</param>
106    /// <param name="problemInstance">The problem instance that contains the data.</param>
107    /// <param name="evaluatedSolutions">The number of evaluated solutions.</param>
108    /// <param name="greedy">Greedy selection performed better in 5 of 8 instances according to the paper</param>
109    public static void Apply(IRandom random, IntegerVector assignment,
110      ref double quality, ref Evaluation evaluation,
111      GQAPInstance problemInstance, out int evaluatedSolutions) {
112      evaluatedSolutions = 0;
113      var capacities = problemInstance.Capacities;
114      var demands = problemInstance.Demands;
115      var evaluations = 0.0;
116      var deltaEvaluationFactor = 1.0 / assignment.Length;
117      var change = true;
118      var moves = ExhaustiveOneMoveGenerator.GenerateAllNxM(problemInstance).ToList();
119      var slack = evaluation.Slack.ToList();
120      while (change) {
121        change = false;
122        var feasible = evaluation.IsFeasible;
123        foreach (var move in moves
124            .Where(x => {
125              var equip = x.Indices[0];
126              var assignTo = x.Reassignments[equip] - 1;
127              if (assignTo == assignment[equip]) return false;
128              var dem = problemInstance.Demands[equip];
129              if (feasible) return slack[assignTo] >= dem;
130              return slack[assignTo] - dem > slack[assignment[equip]];
131            })
132            .Shuffle(random)) {
133          var moveEval = GQAPNMoveEvaluator.Evaluate(move, assignment, evaluation, problemInstance);
134          evaluations += deltaEvaluationFactor;
135          var moveQuality = problemInstance.ToSingleObjective(moveEval);
136          if (moveQuality < quality) {
137            NMoveMaker.Apply(assignment, move);
138            quality = moveQuality;
139            evaluation = moveEval;
140            change = true;
141            break;
142          }
143        }
144      }
145      evaluatedSolutions += (int)Math.Ceiling(evaluations);
146    }
147
148    private static NMove Move(IRandom random, IntegerVector assignment, double oneMoveProbability, DoubleArray capacities) {
149      if (random.NextDouble() < oneMoveProbability)
150        return StochasticNMoveSingleMoveGenerator.GenerateOneMove(random, assignment, capacities);
151      return StochasticNMoveSingleMoveGenerator.GenerateTwoMove(random, assignment, capacities);
152    }
153
154    public override IOperation Apply() {
155      var evaluation = EvaluationParameter.ActualValue;
156      var quality = QualityParameter.ActualValue;
157      var fit = quality.Value;
158      var evaluatedSolutions = 0;
159
160      Apply(RandomParameter.ActualValue,
161        AssignmentParameter.ActualValue,
162        ref fit,
163        ref evaluation,
164        ProblemInstanceParameter.ActualValue,
165        out evaluatedSolutions);
166
167      EvaluationParameter.ActualValue = evaluation;
168      quality.Value = fit;
169      EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
170      return base.Apply();
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.