#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Random;
namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
[Item("Hill Climber", "Test algorithm.")]
[StorableClass]
[Creatable("Parameterless Population Pyramid")]
// In the GECCO paper, Section 2.1
public class HillClimber : AlgorithmBase {
[Storable]
private IRandom random;
private const string IterationsParameterName = "Iterations";
public IFixedValueParameter IterationsParameter {
get { return (IFixedValueParameter)Parameters[IterationsParameterName]; }
}
public int Iterations {
get { return IterationsParameter.Value.Value; }
set { IterationsParameter.Value.Value = value; }
}
[StorableConstructor]
protected HillClimber(bool deserializing) : base(deserializing) { }
protected HillClimber(HillClimber original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new HillClimber(this, cloner);
}
public HillClimber()
: base() {
random = new MersenneTwister();
Parameters.Add(new FixedValueParameter(IterationsParameterName, "", new IntValue(100)));
}
protected override void Run() {
var BestQuality = new DoubleValue(double.NaN);
Results.Add(new Result("Best quality", BestQuality));
for (int iteration = 0; iteration < Iterations; iteration++) {
bool[] solution = new bool[Problem.Length];
for (int i = 0; i < solution.Length; i++) {
solution[i] = random.Next(2) == 1;
}
var fitness = Problem.Evaluate(solution);
fitness = ImproveToLocalOptimum(Problem, solution, fitness, random);
if (double.IsNaN(BestQuality.Value) || Problem.IsBetter(fitness, BestQuality.Value)) {
BestQuality.Value = fitness;
}
}
}
public static double ImproveToLocalOptimum(IBinaryVectorProblem problem, bool[] solution, double fitness, IRandom rand) {
var tried = new HashSet();
do {
var options = Enumerable.Range(0, solution.Length).Shuffle(rand);
foreach (var option in options) {
if (tried.Contains(option)) continue;
solution[option] = !solution[option];
double newFitness = problem.Evaluate(solution);
if (problem.IsBetter(newFitness, fitness)) {
fitness = newFitness;
tried.Clear();
} else {
solution[option] = !solution[option];
}
tried.Add(option);
}
} while (tried.Count != solution.Length);
return fitness;
}
}
}