1 | using System;
|
---|
2 | using System.IO;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
5 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
6 | using HeuristicLab.JsonInterface;
|
---|
7 | using HeuristicLab.Operators;
|
---|
8 | using HeuristicLab.Optimization;
|
---|
9 | using HeuristicLab.Problems.TravelingSalesman;
|
---|
10 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
11 |
|
---|
12 | namespace 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 | }
|
---|