1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
27 | using HeuristicLab.Problems.ArtificialAnt;
|
---|
28 | using HeuristicLab.Selection;
|
---|
29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Tests {
|
---|
32 | /// <summary>
|
---|
33 | /// Summary description for GPArtificialAntSampleTest
|
---|
34 | /// </summary>
|
---|
35 | [TestClass]
|
---|
36 | public class GPArtificialAntSampleTest {
|
---|
37 | private const string samplesDirectory = SamplesUtils.Directory;
|
---|
38 | [ClassInitialize]
|
---|
39 | public static void MyClassInitialize(TestContext testContext) {
|
---|
40 | if (!Directory.Exists(samplesDirectory))
|
---|
41 | Directory.CreateDirectory(samplesDirectory);
|
---|
42 | }
|
---|
43 |
|
---|
44 | [TestMethod]
|
---|
45 | [TestCategory("Samples.Create")]
|
---|
46 | [TestProperty("Time", "medium")]
|
---|
47 | public void CreateGpArtificialAntSampleTest() {
|
---|
48 | var ga = CreateGpArtificialAntSample();
|
---|
49 | XmlGenerator.Serialize(ga, @"Samples\SGP_SantaFe.hl");
|
---|
50 | }
|
---|
51 |
|
---|
52 | [TestMethod]
|
---|
53 | [TestCategory("Samples.Execute")]
|
---|
54 | [TestProperty("Time", "long")]
|
---|
55 | public void RunGpArtificialAntSampleTest() {
|
---|
56 | var ga = CreateGpArtificialAntSample();
|
---|
57 | ga.SetSeedRandomly.Value = false;
|
---|
58 | SamplesUtils.RunAlgorithm(ga);
|
---|
59 | Assert.AreEqual(81, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
|
---|
60 | Assert.AreEqual(48.19, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
|
---|
61 | Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
|
---|
62 | Assert.AreEqual(50950, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
|
---|
63 | }
|
---|
64 |
|
---|
65 | public GeneticAlgorithm CreateGpArtificialAntSample() {
|
---|
66 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
67 | #region Problem Configuration
|
---|
68 | ArtificialAntProblem antProblem = new ArtificialAntProblem();
|
---|
69 | antProblem.BestKnownQuality.Value = 89;
|
---|
70 | antProblem.MaxExpressionDepth.Value = 10;
|
---|
71 | antProblem.MaxExpressionLength.Value = 100;
|
---|
72 | antProblem.MaxFunctionArguments.Value = 3;
|
---|
73 | antProblem.MaxFunctionDefinitions.Value = 3;
|
---|
74 | antProblem.MaxTimeSteps.Value = 600;
|
---|
75 | #endregion
|
---|
76 | #region Algorithm Configuration
|
---|
77 | ga.Name = "Genetic Programming - Artificial Ant";
|
---|
78 | ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
|
---|
79 | ga.Problem = antProblem;
|
---|
80 | SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
|
---|
81 | ga, 1000, 1, 50, 0.15, 5);
|
---|
82 | var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
|
---|
83 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
84 | .OfType<FullTreeShaker>()
|
---|
85 | .Single(), false);
|
---|
86 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
87 | .OfType<OnePointShaker>()
|
---|
88 | .Single(), false);
|
---|
89 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
90 | .OfType<ArgumentDeleter>()
|
---|
91 | .Single(), false);
|
---|
92 | mutator.Operators.SetItemCheckedState(mutator.Operators
|
---|
93 | .OfType<SubroutineDeleter>()
|
---|
94 | .Single(), false);
|
---|
95 | #endregion
|
---|
96 | return ga;
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|