Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/18/15 18:55:40 (9 years ago)
Author:
pfleck
Message:

#2269

  • Added create sample script in unittests for the SymReg Sample.
  • Updated ALPS TSP and SymReg Samples.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/Samples/AlpsSampleTest.cs

    r13253 r13260  
    2424using HeuristicLab.Algorithms.ALPS;
    2525using HeuristicLab.Encodings.PermutationEncoding;
     26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2627using HeuristicLab.Persistence.Default.Xml;
     28using HeuristicLab.Problems.DataAnalysis.Symbolic;
     29using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
     30using HeuristicLab.Problems.Instances.DataAnalysis;
    2731using HeuristicLab.Problems.Instances.TSPLIB;
    2832using HeuristicLab.Problems.TravelingSalesman;
     
    3337  [TestClass]
    3438  public class AlpsTspSampleTest {
    35     private const string SampleFileName = "ALPSGA_TSP";
     39    private const string TspSampleFileName = "ALPSGA_TSP";
     40    private const string SymRegSampleFileName = "ALPSGP_SymReg";
    3641
    3742    [TestMethod]
     
    4045    public void CreateAlpsGaTspSampleTest() {
    4146      var alpsGa = CreateAlpsGaTspSample();
    42       string path = Path.Combine(SamplesUtils.SamplesDirectory, SampleFileName + SamplesUtils.SampleFileExtension);
     47      string path = Path.Combine(SamplesUtils.SamplesDirectory, TspSampleFileName + SamplesUtils.SampleFileExtension);
    4348      XmlGenerator.Serialize(alpsGa, path);
    4449    }
     50
     51    [TestMethod]
     52    [TestCategory("Samples.Create")]
     53    [TestProperty("Time", "medium")]
     54    public void CreateAlpsGaSymRegSampleTest() {
     55      var alpsGa = CreateAlpsGaSymRegSample();
     56      string path = Path.Combine(SamplesUtils.SamplesDirectory, SymRegSampleFileName + SamplesUtils.SampleFileExtension);
     57      XmlGenerator.Serialize(alpsGa, path);
     58    }
     59
    4560    [TestMethod]
    4661    [TestCategory("Samples.Execute")]
    4762    [TestProperty("Time", "long")]
    4863    public void RunAlpsGaTspSampleTest() {
    49       var alpsGaE = CreateAlpsGaTspSample(plusSelection: false);
    50       alpsGaE.SetSeedRandomly.Value = false;
    51       SamplesUtils.RunAlgorithm(alpsGaE);
    52       Assert.AreEqual(23661, SamplesUtils.GetDoubleResult(alpsGaE, "BestQuality"));
    53       Assert.AreEqual(35131.1625, SamplesUtils.GetDoubleResult(alpsGaE, "CurrentAverageQuality"));
    54       Assert.AreEqual(49699, SamplesUtils.GetDoubleResult(alpsGaE, "CurrentWorstQuality"));
    55       Assert.AreEqual(26640, SamplesUtils.GetIntResult(alpsGaE, "EvaluatedSolutions"));
    56 
    57       var alpsGaPs = CreateAlpsGaTspSample(plusSelection: true);
    58       alpsGaPs.SetSeedRandomly.Value = false;
    59       SamplesUtils.RunAlgorithm(alpsGaPs);
    60       Assert.AreEqual(21365, SamplesUtils.GetDoubleResult(alpsGaPs, "BestQuality"));
    61       Assert.AreEqual(30774.695, SamplesUtils.GetDoubleResult(alpsGaPs, "CurrentAverageQuality"));
    62       Assert.AreEqual(49699, SamplesUtils.GetDoubleResult(alpsGaPs, "CurrentWorstQuality"));
    63       Assert.AreEqual(26900, SamplesUtils.GetIntResult(alpsGaPs, "EvaluatedSolutions"));
     64      var alpsGa = CreateAlpsGaTspSample();
     65      alpsGa.SetSeedRandomly.Value = false;
     66      SamplesUtils.RunAlgorithm(alpsGa);
     67      Assert.AreEqual(7967, SamplesUtils.GetDoubleResult(alpsGa, "BestQuality"));
     68      Assert.AreEqual(17565.174444444445, SamplesUtils.GetDoubleResult(alpsGa, "CurrentAverageQuality"));
     69      Assert.AreEqual(50295, SamplesUtils.GetDoubleResult(alpsGa, "CurrentWorstQuality"));
     70      Assert.AreEqual(621900, SamplesUtils.GetIntResult(alpsGa, "EvaluatedSolutions"));
    6471    }
    6572
    66     private AlpsGeneticAlgorithm CreateAlpsGaTspSample(bool plusSelection = false) {
     73    [TestMethod]
     74    [TestCategory("Samples.Execute")]
     75    [TestProperty("Time", "long")]
     76    public void RunAlpsGaSymRegSampleTest() {
     77      var alpsGa = CreateAlpsGaSymRegSample();
     78      alpsGa.SetSeedRandomly.Value = false;
     79      SamplesUtils.RunAlgorithm(alpsGa);
     80      Assert.AreEqual(265855, SamplesUtils.GetIntResult(alpsGa, "EvaluatedSolutions"));
     81    }
     82
     83    private AlpsGeneticAlgorithm CreateAlpsGaTspSample() {
    6784      AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
    6885      #region Problem Configuration
     
    7794      alpsGa.Description = "An age-layered population structure genetic algorithm which solves the \"ch130\" traveling salesman problem (imported from TSPLIB)";
    7895      alpsGa.Problem = tspProblem;
    79       SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, OrderCrossover2, InversionManipulator>(alpsGa,
    80         numberOfLayers: 10,
    81         popSize: 100,
    82         mutationRate: 0.05,
    83         elites: 1,
    84         plusSelection: plusSelection,
    85         agingScheme: AgingScheme.Polynomial,
    86         ageGap: 20,
    87         ageInheritance: 1.0,
    88         maxGens: 100);
     96      SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, MultiPermutationCrossover, InversionManipulator>(alpsGa,
     97        numberOfLayers: 1000,
     98        popSize: 100,
     99        mutationRate: 0.05,
     100        elites: 1,
     101        plusSelection: true,
     102        agingScheme: AgingScheme.Polynomial,
     103        ageGap: 20,
     104        ageInheritance: 1.0,
     105        maxGens: 1000);
     106      var checkedCrossovers = new[] { typeof(EdgeRecombinationCrossover), typeof(MaximalPreservativeCrossover), typeof(OrderCrossover2) };
     107      var multiCrossover = (MultiPermutationCrossover)alpsGa.Crossover;
     108      var crossovers = multiCrossover.Operators.Where(c => checkedCrossovers.Any(cc => cc.IsInstanceOfType(c))).ToList();
     109      foreach (var c in multiCrossover.Operators)
     110        multiCrossover.Operators.SetItemCheckedState(c, crossovers.Contains(c));
     111      #endregion
     112      return alpsGa;
     113    }
     114
     115    private AlpsGeneticAlgorithm CreateAlpsGaSymRegSample() {
     116      AlpsGeneticAlgorithm alpsGa = new AlpsGeneticAlgorithm();
     117      #region Problem Configuration
     118      var provider = new VladislavlevaInstanceProvider();
     119      var instance = provider.GetDataDescriptors().Single(x => x.Name.StartsWith("Vladislavleva-5 F5"));
     120      var symbRegProblem = new SymbolicRegressionSingleObjectiveProblem();
     121      symbRegProblem.Load(provider.LoadData(instance));
     122
     123      symbRegProblem.MaximumSymbolicExpressionTreeDepth.Value = 35;
     124      symbRegProblem.MaximumSymbolicExpressionTreeLength.Value = 35;
     125
     126      var grammar = (TypeCoherentExpressionGrammar)symbRegProblem.SymbolicExpressionTreeGrammar;
     127      grammar.Symbols.OfType<Exponential>().Single().Enabled = false;
     128      grammar.Symbols.OfType<Logarithm>().Single().Enabled = false;
     129
     130      #endregion
     131      #region Algorithm Configuration
     132      alpsGa.Name = "ALPS Genetic Programming - Symbolic Regression";
     133      alpsGa.Description = "An ALPS-GP to solve a symbolic regression problem (Vladislavleva-5 dataset)";
     134      alpsGa.Problem = symbRegProblem;
     135      SamplesUtils.ConfigureAlpsGeneticAlgorithmParameters<GeneralizedRankSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(alpsGa,
     136        numberOfLayers: 1000,
     137        popSize: 100,
     138        mutationRate: 0.25,
     139        elites: 1,
     140        plusSelection: false,
     141        agingScheme: AgingScheme.Polynomial,
     142        ageGap: 15,
     143        ageInheritance: 1.0,
     144        maxGens: 500);
    89145      #endregion
    90146      return alpsGa;
Note: See TracChangeset for help on using the changeset viewer.