#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.Linq;
using HeuristicLab.Analysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Encodings.BinaryVectorEncoding;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.Programmable {
[Item("One Max New", "Represents a single-objective problem that can be programmed.")]
[Creatable("1 Test")]
[StorableClass]
public class OneMaxNew : SingleObjectiveProgrammableProblem {
public override bool Maximization {
get { return true; }
}
public OneMaxNew()
: base() {
Encoding = new BinaryEncoding("BinaryVector", 10);
var bestScopeSolutionAnalyzer = Operators.OfType().FirstOrDefault();
if (bestScopeSolutionAnalyzer != null) Operators.Remove(bestScopeSolutionAnalyzer);
}
[StorableConstructor]
protected OneMaxNew(bool deserializing) : base(deserializing) { }
protected OneMaxNew(OneMaxNew original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new OneMaxNew(this, cloner);
}
public override double Evaluate(Individual individual, IRandom random) {
return individual.BinaryVector().Count(b => b);
}
public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results) {
base.Analyze(individuals, qualities, results);
var best = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderByDescending(z => z.Quality).First();
if (!results.ContainsKey("Best Solution")) {
results.Add(new Result("Best Solution", typeof(BinaryVector)));
}
results["Best Solution"].Value = best.Individual.BinaryVector();
}
}
}