Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.Tests/HeuristicLab.JsonInterface/GeneratorInstantiatorTest.cs @ 17445

Last change on this file since 17445 was 17353, checked in by dpiringe, 5 years ago

#3026:

  • relocated GetMaxValue and GetMinValue from ValueTypeValueConverter into BaseConverter
  • fixed a bug in ConstrainedValueParameterConverter (from GetType().Name to ToString())
  • printing now PrettyNames for types
  • added comments
  • added StorableConverter.cs (not finished, maybe not a good converter)
  • added ValueRangeConverter.cs for DoubleRange and IntRange
  • added ParameterConverter.cs for default parameter conversion
File size: 2.3 KB
Line 
1using System;
2using System.IO;
3using System.Linq;
4using HeuristicLab.Algorithms.GeneticAlgorithm;
5using HeuristicLab.Encodings.PermutationEncoding;
6using HeuristicLab.JsonInterface;
7using HeuristicLab.Operators;
8using HeuristicLab.Optimization;
9using HeuristicLab.Problems.TravelingSalesman;
10using Microsoft.VisualStudio.TestTools.UnitTesting;
11
12namespace HeuristicLab.JsonInterface.Tests {
13  [TestClass]
14  public class GeneratorInstantiatorTest {
15    private string templateFilePath = Directory.GetCurrentDirectory()+"\\Template.json";
16    private string configFilePath = Directory.GetCurrentDirectory() + "\\Config.json";
17
18    [TestInitialize()]
19    public void CreateTempFiles() {
20      GeneticAlgorithm alg = new GeneticAlgorithm();
21      alg.Problem = new TravelingSalesmanProblem();
22      //File.WriteAllText(@"C:\Workspace\Template.json", gen.GenerateTemplate(alg, tsp));
23      File.WriteAllText(templateFilePath, JCGenerator.GenerateTemplate(alg));
24      File.WriteAllText(configFilePath, "["+
25        "{\"Name\": \"Seed\",\"Default\": 55555,\"Path\": \"Genetic Algorithm (GA).Seed\"},"+
26        "{\"Name\": \"Crossover\", \"Path\": \"Genetic Algorithm (GA).Crossover\", \"Default\": \"MultiPermutationCrossover\"}," +
27        "{\"Name\": \"Elites\", \"Path\": \"Genetic Algorithm (GA).Elites\", \"Default\": 5,\"Range\":[-2147483648,2147483647]}" +
28        "]");
29    }
30   
31    [TestCleanup()]
32    public void ClearTempFiles() {
33      File.Delete(templateFilePath);
34      File.Delete(configFilePath);
35    }
36
37    [TestMethod]
38    public void TestInstantiator() {
39      GeneticAlgorithm alg = (GeneticAlgorithm)JCInstantiator.Instantiate(templateFilePath, configFilePath);
40
41      Assert.AreEqual(55555, alg.Seed.Value);
42      Assert.IsTrue(alg.Crossover is MultiPermutationCrossover);
43      Assert.AreEqual(5, alg.Elites.Value);
44    }
45
46    [TestMethod]
47    [ExpectedException(typeof(ArgumentOutOfRangeException))]
48    public void TestRangeChangeWithConfig() {
49      File.WriteAllText(configFilePath, "[{\"Name\": \"MutationProbability\", \"Path\": \"Genetic Algorithm (GA).MutationProbability\", \"Default\": 2.0,\"Range\":[0.0,2.0]}]");
50      GeneticAlgorithm alg = (GeneticAlgorithm)JCInstantiator.Instantiate(templateFilePath, configFilePath);
51    }
52  }
53}
Note: See TracBrowser for help on using the repository browser.