[15562] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2017 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.Threading;
|
---|
[16728] | 24 | using HEAL.Attic;
|
---|
[15562] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[15572] | 28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
[15562] | 29 | using HeuristicLab.Optimization;
|
---|
[15564] | 30 | using HeuristicLab.Parameters;
|
---|
[15562] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSearch {
|
---|
| 33 | [Item("Iterated Local Search (GQAP)", "Iterated local search for the GQAP.")]
|
---|
| 34 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
|
---|
[16728] | 35 | [StorableType("2C8EE433-B133-4382-8B4F-4EFFA536D759")]
|
---|
[15572] | 36 | public sealed class IteratedLS : StochasticAlgorithm<LocalSearchContext, IntegerVectorEncoding> {
|
---|
[15562] | 37 |
|
---|
| 38 | public override bool SupportsPause {
|
---|
| 39 | get { return true; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public override Type ProblemType {
|
---|
| 43 | get { return typeof(GQAP); }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public new GQAP Problem {
|
---|
| 47 | get { return (GQAP)base.Problem; }
|
---|
| 48 | set { base.Problem = value; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[15564] | 51 | [Storable]
|
---|
| 52 | private IFixedValueParameter<PercentValue> perturbationStrengthParameter;
|
---|
| 53 | public IFixedValueParameter<PercentValue> PerturbationStrengthParameter {
|
---|
| 54 | get { return perturbationStrengthParameter; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public double PerturbationStrength {
|
---|
| 58 | get { return perturbationStrengthParameter.Value.Value; }
|
---|
| 59 | set { perturbationStrengthParameter.Value.Value = value; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[15562] | 62 | [StorableConstructor]
|
---|
[16728] | 63 | private IteratedLS(StorableConstructorFlag _) : base(_) { }
|
---|
[15562] | 64 | private IteratedLS(IteratedLS original, Cloner cloner)
|
---|
| 65 | : base(original, cloner) {
|
---|
[15629] | 66 | perturbationStrengthParameter = cloner.Clone(original.perturbationStrengthParameter);
|
---|
[15562] | 67 | }
|
---|
| 68 | public IteratedLS() {
|
---|
[15564] | 69 | Parameters.Add(perturbationStrengthParameter = new FixedValueParameter<PercentValue>("PerturbationStrength", "The expected length of the random walk relative to the size of the solution vector.", new PercentValue(0.5)));
|
---|
[15562] | 70 |
|
---|
| 71 | Problem = new GQAP();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 75 | return new IteratedLS(this, cloner);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | protected override void Initialize(CancellationToken token) {
|
---|
| 79 | base.Initialize(token);
|
---|
| 80 |
|
---|
| 81 | Context.Problem = Problem;
|
---|
| 82 | Context.BestSolution = null;
|
---|
| 83 |
|
---|
| 84 | var assign = GreedyRandomizedSolutionCreator.CreateSolution(Context.Random, Problem.ProblemInstance, 10, true, token);
|
---|
| 85 | var eval = Problem.ProblemInstance.Evaluate(assign);
|
---|
| 86 | var fit = Problem.ProblemInstance.ToSingleObjective(eval);
|
---|
| 87 | Context.EvaluatedSolutions++;
|
---|
| 88 |
|
---|
| 89 | var candidate = new GQAPSolution(assign, eval);
|
---|
| 90 | var lsevaluations = 0;
|
---|
| 91 | OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
|
---|
| 92 | Context.EvaluatedSolutions += lsevaluations;
|
---|
| 93 |
|
---|
| 94 | Context.ReplaceIncumbent(Context.ToScope(candidate, fit));
|
---|
| 95 | Context.BestQuality = fit;
|
---|
| 96 | Context.BestSolution = (GQAPSolution)candidate.Clone();
|
---|
| 97 |
|
---|
| 98 | Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
|
---|
| 99 | Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
|
---|
| 100 | Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
|
---|
| 101 | Results.Add(new Result("BestSolution", Context.BestSolution));
|
---|
| 102 |
|
---|
[15574] | 103 | try {
|
---|
| 104 | Context.RunOperator(Analyzer, token);
|
---|
| 105 | } catch (OperationCanceledException) { }
|
---|
[15562] | 106 | }
|
---|
| 107 |
|
---|
| 108 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[15700] | 109 | base.Run(cancellationToken);
|
---|
[15563] | 110 | var lastUpdate = ExecutionTime;
|
---|
| 111 |
|
---|
[15562] | 112 | while (!StoppingCriterion()) {
|
---|
| 113 | var lsevaluations = 0;
|
---|
| 114 | var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone();
|
---|
[15564] | 115 | RelocateEquipmentManipluator.Apply(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, 1.0 / (PerturbationStrength * candidate.Assignment.Length));
|
---|
[15562] | 116 | candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment);
|
---|
| 117 | Context.EvaluatedSolutions++;
|
---|
| 118 | OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
|
---|
| 119 | Context.EvaluatedSolutions += lsevaluations;
|
---|
| 120 |
|
---|
| 121 | var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
|
---|
| 122 | if (candidateFit < Context.Incumbent.Fitness) {
|
---|
| 123 | Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
|
---|
| 124 | Context.BestQuality = candidateFit;
|
---|
| 125 | Context.BestSolution = (GQAPSolution)candidate.Clone();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | IResult result;
|
---|
[15563] | 129 | if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
|
---|
| 130 | if (Results.TryGetValue("Iterations", out result))
|
---|
| 131 | ((IntValue)result.Value).Value = Context.Iterations;
|
---|
| 132 | else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
|
---|
| 133 | if (Results.TryGetValue("EvaluatedSolutions", out result))
|
---|
| 134 | ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
|
---|
| 135 | else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
|
---|
| 136 | lastUpdate = ExecutionTime;
|
---|
| 137 | }
|
---|
[15562] | 138 | if (Results.TryGetValue("BestQuality", out result))
|
---|
| 139 | ((DoubleValue)result.Value).Value = Context.BestQuality;
|
---|
| 140 | else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
|
---|
| 141 | if (Results.TryGetValue("BestSolution", out result))
|
---|
| 142 | result.Value = Context.BestSolution;
|
---|
| 143 | else Results.Add(new Result("BestSolution", Context.BestSolution));
|
---|
| 144 |
|
---|
[15564] | 145 | try {
|
---|
[15574] | 146 | Context.RunOperator(Analyzer, cancellationToken);
|
---|
[15564] | 147 | } catch (OperationCanceledException) { }
|
---|
[15562] | 148 |
|
---|
| 149 | Context.Iterations++;
|
---|
| 150 | if (cancellationToken.IsCancellationRequested) break;
|
---|
| 151 | }
|
---|
[15563] | 152 | IResult result2;
|
---|
| 153 | if (Results.TryGetValue("Iterations", out result2))
|
---|
| 154 | ((IntValue)result2.Value).Value = Context.Iterations;
|
---|
| 155 | else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
|
---|
| 156 | if (Results.TryGetValue("EvaluatedSolutions", out result2))
|
---|
| 157 | ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
|
---|
| 158 | else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
|
---|
[15562] | 159 | }
|
---|
| 160 | }
|
---|
| 161 | }
|
---|