Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LAHC/LAHC.cs @ 17712

Last change on this file since 17712 was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 7.2 KB
RevLine 
[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
22using System;
[15563]23using System.Linq;
[15562]24using System.Threading;
[16728]25using HEAL.Attic;
[15562]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Optimization;
[15563]31using HeuristicLab.Parameters;
[15562]32
[15563]33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LAHC {
34  [Item("LAHC (GQAP)", "Late-acceptance hill climber for the GQAP.")]
[15562]35  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
[16728]36  [StorableType("41FABEE1-506A-45FE-A6D3-57932036AEC8")]
[15572]37  public sealed class LAHC : StochasticAlgorithm<LAHCContext, IntegerVectorEncoding> {
[15562]38
39    public override bool SupportsPause {
40      get { return true; }
41    }
42
43    public override Type ProblemType {
44      get { return typeof(GQAP); }
45    }
46
47    public new GQAP Problem {
48      get { return (GQAP)base.Problem; }
49      set { base.Problem = value; }
50    }
51
[15563]52    [Storable]
53    private FixedValueParameter<IntValue> memorySizeParameter;
54    public IFixedValueParameter<IntValue> MemorySizeParameter {
55      get { return memorySizeParameter; }
56    }
57
58    public int MemorySize {
59      get { return memorySizeParameter.Value.Value; }
60      set { memorySizeParameter.Value.Value = value; }
61    }
62
[15562]63    [StorableConstructor]
[16728]64    private LAHC(StorableConstructorFlag _) : base(_) { }
[15563]65    private LAHC(LAHC original, Cloner cloner)
[15562]66      : base(original, cloner) {
[15563]67      memorySizeParameter = cloner.Clone(original.memorySizeParameter);
[15562]68    }
[15563]69    public LAHC() {
70      Parameters.Add(memorySizeParameter = new FixedValueParameter<IntValue>("MemorySize", "The size of the memory, the shorter the more greedy LAHC performs.", new IntValue(100)));
[15562]71
72      Problem = new GQAP();
73    }
74   
75    public override IDeepCloneable Clone(Cloner cloner) {
[15563]76      return new LAHC(this, cloner);
[15562]77    }
78
79    protected override void Initialize(CancellationToken token) {
80      base.Initialize(token);
81
82      Context.Problem = Problem;
[15563]83      Context.LastSuccess = 0;
84     
85      var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
[15562]86      var eval = Problem.ProblemInstance.Evaluate(assign);
87      var fit = Problem.ProblemInstance.ToSingleObjective(eval);
88      Context.EvaluatedSolutions++;
89
90      var candidate = new GQAPSolution(assign, eval);
[15563]91     
[15562]92      Context.ReplaceIncumbent(Context.ToScope(candidate, fit));
93      Context.BestQuality = fit;
94      Context.BestSolution = (GQAPSolution)candidate.Clone();
[15563]95     
96      Context.Memory = new DoubleArray(Enumerable.Repeat(Context.BestQuality, MemorySize).ToArray());
[15562]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;
[15562]111      while (!StoppingCriterion()) {
[15563]112        var move = StochasticNMoveSingleMoveGenerator.GenerateOneMove(Context.Random,
113          Context.Incumbent.Solution.Assignment, Problem.ProblemInstance.Capacities);
114        var moveEval = GQAPNMoveEvaluator.Evaluate(move,
115          Context.Incumbent.Solution.Assignment,
116          Context.Incumbent.Solution.Evaluation, Problem.ProblemInstance);
117        if (Context.Iterations % Problem.ProblemInstance.Demands.Length == 0)
118          Context.EvaluatedSolutions++;
119        var nextFit = Problem.ProblemInstance.ToSingleObjective(moveEval);
120        var nextVec = new IntegerVector(Context.Incumbent.Solution.Assignment);
121        NMoveMaker.Apply(nextVec, move);
122       
123        var v = Context.Iterations % Context.Memory.Length;
124        Context.Iterations++;
125        var prevFit = Context.Memory[v];
[15562]126
[15563]127        var accept = nextFit <= Context.Incumbent.Fitness
128                  || nextFit <= prevFit;
129
130        if (accept && nextFit < Context.Incumbent.Fitness)
131          Context.LastSuccess = Context.Iterations;
132
133        if (accept) {
134          Context.ReplaceIncumbent(Context.ToScope(new GQAPSolution(nextVec, moveEval), nextFit));
135          if (nextFit < Context.BestQuality) {
136            Context.BestSolution = (GQAPSolution)Context.Incumbent.Solution.Clone();
137            Context.BestQuality = nextFit;
138          }
[15562]139        }
140
[15563]141        if (Context.Incumbent.Fitness < prevFit)
142          Context.Memory[v] = Context.Incumbent.Fitness;
143
[15562]144        IResult result;
[15563]145        if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
146          if (Results.TryGetValue("Iterations", out result))
147            ((IntValue)result.Value).Value = Context.Iterations;
148          else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
149          if (Results.TryGetValue("EvaluatedSolutions", out result))
150            ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
151          else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
152          lastUpdate = ExecutionTime;
153        }
[15562]154        if (Results.TryGetValue("BestQuality", out result))
155          ((DoubleValue)result.Value).Value = Context.BestQuality;
156        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
157        if (Results.TryGetValue("BestSolution", out result))
158          result.Value = Context.BestSolution;
159        else Results.Add(new Result("BestSolution", Context.BestSolution));
160
[15564]161        try {
[15574]162          Context.RunOperator(Analyzer, cancellationToken);
[15564]163        } catch (OperationCanceledException) { }
164
[15562]165        if (cancellationToken.IsCancellationRequested) break;
166      }
[15563]167      IResult result2;
168      if (Results.TryGetValue("Iterations", out result2))
169        ((IntValue)result2.Value).Value = Context.Iterations;
170      else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
171      if (Results.TryGetValue("EvaluatedSolutions", out result2))
172        ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
173      else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
[15562]174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.