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
Line 
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;
23using System.Linq;
24using System.Threading;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32
33namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LAHC {
34  [Item("LAHC (GQAP)", "Late-acceptance hill climber for the GQAP.")]
35  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
36  [StorableType("41FABEE1-506A-45FE-A6D3-57932036AEC8")]
37  public sealed class LAHC : StochasticAlgorithm<LAHCContext, IntegerVectorEncoding> {
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
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
63    [StorableConstructor]
64    private LAHC(StorableConstructorFlag _) : base(_) { }
65    private LAHC(LAHC original, Cloner cloner)
66      : base(original, cloner) {
67      memorySizeParameter = cloner.Clone(original.memorySizeParameter);
68    }
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)));
71
72      Problem = new GQAP();
73    }
74   
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new LAHC(this, cloner);
77    }
78
79    protected override void Initialize(CancellationToken token) {
80      base.Initialize(token);
81
82      Context.Problem = Problem;
83      Context.LastSuccess = 0;
84     
85      var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
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);
91     
92      Context.ReplaceIncumbent(Context.ToScope(candidate, fit));
93      Context.BestQuality = fit;
94      Context.BestSolution = (GQAPSolution)candidate.Clone();
95     
96      Context.Memory = new DoubleArray(Enumerable.Repeat(Context.BestQuality, MemorySize).ToArray());
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
103      try {
104        Context.RunOperator(Analyzer, token);
105      } catch (OperationCanceledException) { }
106    }
107
108    protected override void Run(CancellationToken cancellationToken) {
109      base.Run(cancellationToken);
110      var lastUpdate = ExecutionTime;
111      while (!StoppingCriterion()) {
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];
126
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          }
139        }
140
141        if (Context.Incumbent.Fitness < prevFit)
142          Context.Memory[v] = Context.Incumbent.Fitness;
143
144        IResult result;
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        }
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
161        try {
162          Context.RunOperator(Analyzer, cancellationToken);
163        } catch (OperationCanceledException) { }
164
165        if (cancellationToken.IsCancellationRequested) break;
166      }
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)));
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.