#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace HeuristicLab.Analysis.FitnessLandscape {
[Item("Random Walk Calculator", "Calculates characteristics from a random walk.")]
[StorableClass]
public class RandomWalkCalculator : NamedItem, ICharacteristicCalculator {
[Storable]
private IProblem problem;
public IProblem Problem {
get { return problem; }
set {
if (problem == value) return;
problem = value;
var soProblem = problem as ISingleObjectiveHeuristicOptimizationProblem;
walker.Problem = soProblem;
}
}
[Storable]
private RandomWalk walker;
[StorableConstructor]
private RandomWalkCalculator(bool deserializing) : base(deserializing) { }
private RandomWalkCalculator(RandomWalkCalculator original, Cloner cloner)
: base(original, cloner) {
problem = cloner.Clone(original.problem);
walker = cloner.Clone(original.walker);
characteristics = cloner.Clone(original.characteristics);
}
public RandomWalkCalculator() {
Name = ItemName;
Description = ItemDescription;
walker = new RandomWalk();
characteristics = new CheckedItemList(
new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
"PartialInformationContent", "DensityBasinInformation", "InformationStability",
"Diversity", "Regularity", "TotalEntropy", "PeakInformationContent",
"PeakDensityBasinInformation" }.Select(x => new StringValue(x)));
characteristics.SetItemCheckedState(1, false);
characteristics.SetItemCheckedState(5, false);
}
public override IDeepCloneable Clone(Cloner cloner) {
return new RandomWalkCalculator(this, cloner);
}
private CheckedItemList characteristics;
public ReadOnlyCheckedItemList Characteristics {
get { return characteristics.AsReadOnly(); }
}
public bool CanCalculate() {
return Problem is ISingleObjectiveHeuristicOptimizationProblem
&& Problem.Operators.Any(x => x is IManipulator);
}
public IEnumerable Calculate() {
walker.Prepare(true);
using (var waitHandle = new AutoResetEvent(false)) {
EventHandler evHandle = (sender, e) => {
if (walker.ExecutionState == ExecutionState.Paused
|| walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
};
walker.ExecutionStateChanged += evHandle;
walker.Start();
waitHandle.WaitOne();
walker.ExecutionStateChanged -= evHandle;
}
foreach (var p in characteristics.CheckedItems) {
yield return new Result("RandomWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value, walker.Results[p.Value.Value].Value);
}
walker.Prepare(true);
}
public void CollectParameterValues(IDictionary values) {
walker.CollectParameterValues(values);
}
public IKeyedItemCollection Parameters {
get { return ((IParameterizedItem)walker).Parameters; }
}
}
}