Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/LocalImprovers/OneOptLocalSearch.cs

Last change on this file was 16728, checked in by abeham, 5 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 8.2 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;
34using HEAL.Attic;
35
36namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
37  [Item("1-opt LocalSearch", "A simple exhaustive 1-change local search.")]
38  [StorableType("C574770B-AB1E-46BE-8B3F-C8C5B2AD3ACF")]
39  public class OneOptLocalSearch : SingleSuccessorOperator, IProblemInstanceAwareGQAPOperator,
40    IQualityAwareGQAPOperator, IGQAPLocalImprovementOperator, IAssignmentAwareGQAPOperator, IStochasticOperator {
41    public IProblem Problem { get; set; }
42    public Type ProblemType {
43      get { return typeof(GQAP); }
44    }
45
46    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
47      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
48    }
49    public ILookupParameter<IntegerVector> AssignmentParameter {
50      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
51    }
52    public ILookupParameter<DoubleValue> QualityParameter {
53      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
54    }
55    public ILookupParameter<Evaluation> EvaluationParameter {
56      get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; }
57    }
58    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
59      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
60    }
61    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
62      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
63    }
64    public ILookupParameter<IRandom> RandomParameter {
65      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
66    }
67    public ILookupParameter<ResultCollection> ResultsParameter {
68      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
69    }
70
71    [StorableConstructor]
72    protected OneOptLocalSearch(StorableConstructorFlag _) : base(_) { }
73    protected OneOptLocalSearch(OneOptLocalSearch original, Cloner cloner) : base(original, cloner) { }
74    public OneOptLocalSearch()
75      : base() {
76      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
77      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
78      Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
79      Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription));
80      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "This parameter is ignored by the operator.") { Hidden = true });
81      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents."));
82      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
83      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results."));
84    }
85
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new OneOptLocalSearch(this, cloner);
88    }
89
90    public static void Apply(IRandom random, GQAPSolution sol,
91      GQAPInstance problemInstance, out int evaluatedSolutions) {
92      var fit = problemInstance.ToSingleObjective(sol.Evaluation);
93      var eval = sol.Evaluation;
94      Apply(random, sol.Assignment, ref fit, ref eval, problemInstance, out evaluatedSolutions);
95      sol.Evaluation = eval;
96    }
97
98    /// <summary>
99    /// </summary>
100    /// <param name="random">The random number generator to use.</param>
101    /// <param name="assignment">The equipment-location assignment vector.</param>
102    /// <param name="quality">The solution quality.</param>
103    /// <param name="evaluation">The evaluation result of the solution.</param>
104    /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
105    /// <param name="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
106    /// <param name="maximumIterations">The maximum number of iterations that should be performed each time the candidate list is generated.</param>
107    /// <param name="problemInstance">The problem instance that contains the data.</param>
108    /// <param name="evaluatedSolutions">The number of evaluated solutions.</param>
109    /// <param name="greedy">Greedy selection performed better in 5 of 8 instances according to the paper</param>
110    public static void Apply(IRandom random, IntegerVector assignment,
111      ref double quality, ref Evaluation evaluation,
112      GQAPInstance problemInstance, out int evaluatedSolutions) {
113      evaluatedSolutions = 0;
114      var capacities = problemInstance.Capacities;
115      var demands = problemInstance.Demands;
116      var evaluations = 0.0;
117      var deltaEvaluationFactor = 1.0 / assignment.Length;
118      var change = true;
119      var moves = ExhaustiveOneMoveGenerator.GenerateAllNxM(problemInstance).ToList();
120      var slack = evaluation.Slack.ToList();
121      while (change) {
122        change = false;
123        var feasible = evaluation.IsFeasible;
124        foreach (var move in moves
125            .Where(x => {
126              var equip = x.Indices[0];
127              var assignTo = x.Reassignments[equip] - 1;
128              if (assignTo == assignment[equip]) return false;
129              var dem = problemInstance.Demands[equip];
130              if (feasible) return slack[assignTo] >= dem;
131              return slack[assignTo] - dem > slack[assignment[equip]];
132            })
133            .Shuffle(random)) {
134          var moveEval = GQAPNMoveEvaluator.Evaluate(move, assignment, evaluation, problemInstance);
135          evaluations += deltaEvaluationFactor;
136          var moveQuality = problemInstance.ToSingleObjective(moveEval);
137          if (moveQuality < quality) {
138            NMoveMaker.Apply(assignment, move);
139            quality = moveQuality;
140            evaluation = moveEval;
141            change = true;
142            break;
143          }
144        }
145      }
146      evaluatedSolutions += (int)Math.Ceiling(evaluations);
147    }
148
149    private static NMove Move(IRandom random, IntegerVector assignment, double oneMoveProbability, DoubleArray capacities) {
150      if (random.NextDouble() < oneMoveProbability)
151        return StochasticNMoveSingleMoveGenerator.GenerateOneMove(random, assignment, capacities);
152      return StochasticNMoveSingleMoveGenerator.GenerateTwoMove(random, assignment, capacities);
153    }
154
155    public override IOperation Apply() {
156      var evaluation = EvaluationParameter.ActualValue;
157      var quality = QualityParameter.ActualValue;
158      var fit = quality.Value;
159      var evaluatedSolutions = 0;
160
161      Apply(RandomParameter.ActualValue,
162        AssignmentParameter.ActualValue,
163        ref fit,
164        ref evaluation,
165        ProblemInstanceParameter.ActualValue,
166        out evaluatedSolutions);
167
168      EvaluationParameter.ActualValue = evaluation;
169      quality.Value = fit;
170      EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
171      return base.Apply();
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.