#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.RandomSearch {
[Item("Random Search (GQAP)", "Random search for the GQAP.")]
[Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
[StorableType("D6ED1A84-BBCD-4163-96A7-47F3DCACBE49")]
public sealed class RandomSearch : 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 RandomSearch(StorableConstructorFlag _) : base(_) { }
private RandomSearch(RandomSearch original, Cloner cloner)
: base(original, cloner) {
}
public RandomSearch() {
Problem = new GQAP();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new RandomSearch(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 assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
var eval = Problem.ProblemInstance.Evaluate(assign);
var candidate = new GQAPSolution(assign, eval);
Context.EvaluatedSolutions++;
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)));
}
}
}