#region License Information
/* HeuristicLab
* Copyright (C) 2002-2019 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.IO;
using System.Linq;
using HEAL.Attic;
using HeuristicLab.Algorithms.GeneticAlgorithm;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
using HeuristicLab.Selection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace HeuristicLab.Tests {
[TestClass]
public class GPArtificialAntSampleTest {
private const string SampleFileName = "SGP_SantaFe";
private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
[TestMethod]
[TestCategory("Samples.Create")]
[TestProperty("Time", "medium")]
public void CreateGpArtificialAntSampleTest() {
var ga = CreateGpArtificialAntSample();
string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
serializer.Serialize(ga, path);
}
[TestMethod]
[TestCategory("Samples.Execute")]
[TestProperty("Time", "long")]
public void RunGpArtificialAntSampleTest() {
var ga = CreateGpArtificialAntSample();
ga.SetSeedRandomly.Value = false;
SamplesUtils.RunAlgorithm(ga);
Assert.AreEqual(81, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
Assert.AreEqual(48.19, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
Assert.AreEqual(50950, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
}
public GeneticAlgorithm CreateGpArtificialAntSample() {
GeneticAlgorithm ga = new GeneticAlgorithm();
#region Problem Configuration
Problem antProblem = new Problem();
antProblem.BestKnownQuality = 89;
antProblem.Encoding.TreeDepth = 10;
antProblem.Encoding.TreeLength = 100;
antProblem.Encoding.FunctionDefinitions = 3;
antProblem.Encoding.FunctionArguments = 3;
antProblem.MaxTimeSteps.Value = 600;
#endregion
#region Algorithm Configuration
ga.Name = "Genetic Programming - Artificial Ant";
ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
ga.Problem = antProblem;
SamplesUtils.ConfigureGeneticAlgorithmParameters(
ga, 1000, 1, 50, 0.15, 5);
var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
mutator.Operators.SetItemCheckedState(mutator.Operators
.OfType()
.Single(), false);
mutator.Operators.SetItemCheckedState(mutator.Operators
.OfType()
.Single(), false);
mutator.Operators.SetItemCheckedState(mutator.Operators
.OfType()
.Single(), false);
mutator.Operators.SetItemCheckedState(mutator.Operators
.OfType()
.Single(), false);
#endregion
return ga;
}
}
}