Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/Samples/GPArtificialAntSampleTest.cs

Last change on this file was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System.IO;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Algorithms.GeneticAlgorithm;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Problems.GeneticProgramming.ArtificialAnt;
28using HeuristicLab.Selection;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Tests {
32  [TestClass]
33  public class GPArtificialAntSampleTest {
34    private const string SampleFileName = "SGP_SantaFe";
35
36    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
37
38    [TestMethod]
39    [TestCategory("Samples.Create")]
40    [TestProperty("Time", "medium")]
41    public void CreateGpArtificialAntSampleTest() {
42      var ga = CreateGpArtificialAntSample();
43      string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
44      serializer.Serialize(ga, path);
45    }
46
47    [TestMethod]
48    [TestCategory("Samples.Execute")]
49    [TestProperty("Time", "long")]
50    public void RunGpArtificialAntSampleTest() {
51      var ga = CreateGpArtificialAntSample();
52      ga.SetSeedRandomly.Value = false;
53      SamplesUtils.RunAlgorithm(ga);
54      Assert.AreEqual(81, SamplesUtils.GetDoubleResult(ga, "BestQuality"));
55      Assert.AreEqual(48.19, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality"));
56      Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality"));
57      Assert.AreEqual(50950, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions"));
58    }
59
60    public GeneticAlgorithm CreateGpArtificialAntSample() {
61      GeneticAlgorithm ga = new GeneticAlgorithm();
62
63      #region Problem Configuration
64      Problem antProblem = new Problem();
65      antProblem.BestKnownQuality = 89;
66      antProblem.Encoding.TreeDepth = 10;
67      antProblem.Encoding.TreeLength = 100;
68      antProblem.Encoding.FunctionDefinitions = 3;
69      antProblem.Encoding.FunctionArguments = 3;
70      antProblem.MaxTimeSteps.Value = 600;
71      #endregion
72      #region Algorithm Configuration
73      ga.Name = "Genetic Programming - Artificial Ant";
74      ga.Description = "A standard genetic programming algorithm to solve the artificial ant problem (Santa-Fe trail)";
75      ga.Problem = antProblem;
76      SamplesUtils.ConfigureGeneticAlgorithmParameters<TournamentSelector, SubtreeCrossover, MultiSymbolicExpressionTreeArchitectureManipulator>(
77        ga, 1000, 1, 50, 0.15, 5);
78      var mutator = (MultiSymbolicExpressionTreeArchitectureManipulator)ga.Mutator;
79      mutator.Operators.SetItemCheckedState(mutator.Operators
80        .OfType<FullTreeShaker>()
81        .Single(), false);
82      mutator.Operators.SetItemCheckedState(mutator.Operators
83        .OfType<OnePointShaker>()
84        .Single(), false);
85      mutator.Operators.SetItemCheckedState(mutator.Operators
86        .OfType<ArgumentDeleter>()
87        .Single(), false);
88      mutator.Operators.SetItemCheckedState(mutator.Operators
89        .OfType<SubroutineDeleter>()
90        .Single(), false);
91      #endregion
92
93      return ga;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.