[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;
|
---|
| 28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 |
|
---|
[15703] | 31 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.RandomSearch {
|
---|
[15698] | 32 | [Item("Random Search (GQAP)", "Random search for the GQAP.")]
|
---|
[15562] | 33 | [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
|
---|
[16728] | 34 | [StorableType("D6ED1A84-BBCD-4163-96A7-47F3DCACBE49")]
|
---|
[15703] | 35 | public sealed class RandomSearch : StochasticAlgorithm<RandomSearchContext, IntegerVectorEncoding> {
|
---|
[15562] | 36 |
|
---|
| 37 | public override bool SupportsPause {
|
---|
| 38 | get { return true; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public override Type ProblemType {
|
---|
| 42 | get { return typeof(GQAP); }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public new GQAP Problem {
|
---|
| 46 | get { return (GQAP)base.Problem; }
|
---|
| 47 | set { base.Problem = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
[16728] | 51 | private RandomSearch(StorableConstructorFlag _) : base(_) { }
|
---|
[15698] | 52 | private RandomSearch(RandomSearch original, Cloner cloner)
|
---|
[15562] | 53 | : base(original, cloner) {
|
---|
| 54 | }
|
---|
[15698] | 55 | public RandomSearch() {
|
---|
[15562] | 56 | Problem = new GQAP();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[15698] | 60 | return new RandomSearch(this, cloner);
|
---|
[15562] | 61 | }
|
---|
| 62 |
|
---|
| 63 | protected override void Initialize(CancellationToken token) {
|
---|
| 64 | base.Initialize(token);
|
---|
| 65 |
|
---|
| 66 | Context.Problem = Problem;
|
---|
| 67 | Context.BestSolution = null;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[15700] | 71 | base.Run(cancellationToken);
|
---|
[15563] | 72 | var lastUpdate = ExecutionTime;
|
---|
| 73 |
|
---|
[15562] | 74 | while (!StoppingCriterion()) {
|
---|
| 75 | var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
|
---|
| 76 | var eval = Problem.ProblemInstance.Evaluate(assign);
|
---|
[15698] | 77 | var candidate = new GQAPSolution(assign, eval);
|
---|
[15562] | 78 | Context.EvaluatedSolutions++;
|
---|
| 79 |
|
---|
| 80 | var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
|
---|
| 81 | if (Context.Incumbent == null || candidateFit < Context.Incumbent.Fitness) {
|
---|
| 82 | Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
|
---|
| 83 | Context.BestQuality = candidateFit;
|
---|
| 84 | Context.BestSolution = (GQAPSolution)candidate.Clone();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | IResult result;
|
---|
[15563] | 88 | if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
|
---|
| 89 | if (Results.TryGetValue("Iterations", out result))
|
---|
| 90 | ((IntValue)result.Value).Value = Context.Iterations;
|
---|
| 91 | else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
|
---|
| 92 | if (Results.TryGetValue("EvaluatedSolutions", out result))
|
---|
| 93 | ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
|
---|
| 94 | else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
|
---|
| 95 | lastUpdate = ExecutionTime;
|
---|
| 96 | }
|
---|
[15562] | 97 | if (Results.TryGetValue("BestQuality", out result))
|
---|
| 98 | ((DoubleValue)result.Value).Value = Context.BestQuality;
|
---|
| 99 | else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
|
---|
| 100 | if (Results.TryGetValue("BestSolution", out result))
|
---|
| 101 | result.Value = Context.BestSolution;
|
---|
| 102 | else Results.Add(new Result("BestSolution", Context.BestSolution));
|
---|
| 103 |
|
---|
[15564] | 104 | try {
|
---|
[15574] | 105 | Context.RunOperator(Analyzer, cancellationToken);
|
---|
[15564] | 106 | } catch (OperationCanceledException) { }
|
---|
[15562] | 107 |
|
---|
| 108 | Context.Iterations++;
|
---|
| 109 | if (cancellationToken.IsCancellationRequested) break;
|
---|
| 110 | }
|
---|
[15563] | 111 |
|
---|
| 112 | IResult result2;
|
---|
| 113 | if (Results.TryGetValue("Iterations", out result2))
|
---|
| 114 | ((IntValue)result2.Value).Value = Context.Iterations;
|
---|
| 115 | else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
|
---|
| 116 | if (Results.TryGetValue("EvaluatedSolutions", out result2))
|
---|
| 117 | ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
|
---|
| 118 | else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
|
---|
[15562] | 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|