[11450] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16140] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11450] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.IO;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
| 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 26 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[13056] | 27 | using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
|
---|
[11450] | 28 | using HeuristicLab.Selection;
|
---|
| 29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Tests {
|
---|
| 32 | [TestClass]
|
---|
| 33 | public class GPArtificialAntSampleTest {
|
---|
[11514] | 34 | private const string SampleFileName = "SGP_SantaFe";
|
---|
[11450] | 35 |
|
---|
| 36 | [TestMethod]
|
---|
| 37 | [TestCategory("Samples.Create")]
|
---|
| 38 | [TestProperty("Time", "medium")]
|
---|
| 39 | public void CreateGpArtificialAntSampleTest() {
|
---|
| 40 | var ga = CreateGpArtificialAntSample();
|
---|
[11514] | 41 | string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
|
---|
| 42 | XmlGenerator.Serialize(ga, path);
|
---|
[11450] | 43 | }
|
---|
| 44 |
|
---|
| 45 | [TestMethod]
|
---|
| 46 | [TestCategory("Samples.Execute")]
|
---|
| 47 | [TestProperty("Time", "long")]
|
---|
| 48 | public void RunGpArtificialAntSampleTest() {
|
---|
| 49 | var ga = CreateGpArtificialAntSample();
|
---|
| 50 | ga.SetSeedRandomly.Value = false;
|
---|
| 51 | SamplesUtils.RunAlgorithm(ga);
|
---|
| 52 | Assert.AreEqual(81, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
|
---|
| 53 | Assert.AreEqual(48.19, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
| 54 | Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
| 55 | Assert.AreEqual(50950, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public GeneticAlgorithm CreateGpArtificialAntSample() {
|
---|
| 59 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
[11514] | 60 |
|
---|
[11450] | 61 | #region Problem Configuration
|
---|
[13056] | 62 | Problem antProblem = new Problem();
|
---|
| 63 | antProblem.BestKnownQuality = 89;
|
---|
| 64 | antProblem.Encoding.TreeDepth = 10;
|
---|
| 65 | antProblem.Encoding.TreeLength = 100;
|
---|
| 66 | antProblem.Encoding.FunctionDefinitions = 3;
|
---|
| 67 | antProblem.Encoding.FunctionArguments = 3;
|
---|
[11450] | 68 | antProblem.MaxTimeSteps.Value = 600;
|
---|
| 69 | #endregion
|
---|
| 70 | #region Algorithm Configuration
|
---|
| 71 | ga.Name = "Genetic Programming - Artificial Ant";
|
---|
| 72 | ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
|
---|
| 73 | ga.Problem = antProblem;
|
---|
| 74 | SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
|
---|
| 75 | ga, 1000, 1, 50, 0.15, 5);
|
---|
| 76 | var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
|
---|
| 77 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
| 78 | .OfType<FullTreeShaker>()
|
---|
| 79 | .Single(), false);
|
---|
| 80 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
| 81 | .OfType<OnePointShaker>()
|
---|
| 82 | .Single(), false);
|
---|
| 83 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
| 84 | .OfType<ArgumentDeleter>()
|
---|
| 85 | .Single(), false);
|
---|
| 86 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
| 87 | .OfType<SubroutineDeleter>()
|
---|
| 88 | .Single(), false);
|
---|
| 89 | #endregion
|
---|
[11514] | 90 |
|
---|
[11450] | 91 | return ga;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|