#region License Information /* HeuristicLab * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Threading; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Optimization; namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSearch { [Item("Multi-start Local Search (GQAP)", "Multi-start local search for the GQAP.")] [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)] [StorableType("DA79118A-BA6B-4A0F-81E6-752D750BF7F4")] public sealed class MultistartLS : StochasticAlgorithm { public override bool SupportsPause { get { return true; } } public override Type ProblemType { get { return typeof(GQAP); } } public new GQAP Problem { get { return (GQAP)base.Problem; } set { base.Problem = value; } } [StorableConstructor] private MultistartLS(StorableConstructorFlag _) : base(_) { } private MultistartLS(MultistartLS original, Cloner cloner) : base(original, cloner) { } public MultistartLS() { Problem = new GQAP(); } public override IDeepCloneable Clone(Cloner cloner) { return new MultistartLS(this, cloner); } protected override void Initialize(CancellationToken token) { base.Initialize(token); Context.Problem = Problem; Context.BestSolution = null; } protected override void Run(CancellationToken cancellationToken) { base.Run(cancellationToken); var lastUpdate = ExecutionTime; while (!StoppingCriterion()) { var lsevaluations = 0; var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length); var eval = Problem.ProblemInstance.Evaluate(assign); Context.EvaluatedSolutions++; var candidate = new GQAPSolution(assign, eval); OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations); Context.EvaluatedSolutions += lsevaluations; var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation); if (Context.Incumbent == null || candidateFit < Context.Incumbent.Fitness) { Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit)); Context.BestQuality = candidateFit; Context.BestSolution = (GQAPSolution)candidate.Clone(); } IResult result; if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) { if (Results.TryGetValue("Iterations", out result)) ((IntValue)result.Value).Value = Context.Iterations; else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); if (Results.TryGetValue("EvaluatedSolutions", out result)) ((IntValue)result.Value).Value = Context.EvaluatedSolutions; else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); lastUpdate = ExecutionTime; } if (Results.TryGetValue("BestQuality", out result)) ((DoubleValue)result.Value).Value = Context.BestQuality; else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality))); if (Results.TryGetValue("BestSolution", out result)) result.Value = Context.BestSolution; else Results.Add(new Result("BestSolution", Context.BestSolution)); try { Context.RunOperator(Analyzer, cancellationToken); } catch (OperationCanceledException) { } Context.Iterations++; if (cancellationToken.IsCancellationRequested) break; } IResult result2; if (Results.TryGetValue("Iterations", out result2)) ((IntValue)result2.Value).Value = Context.Iterations; else Results.Add(new Result("Iterations", new IntValue(Context.Iterations))); if (Results.TryGetValue("EvaluatedSolutions", out result2)) ((IntValue)result2.Value).Value = Context.EvaluatedSolutions; else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions))); } } }