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 |
|
---|
22 | using System;
|
---|
23 | using HEAL.Attic;
|
---|
24 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Tests {
|
---|
30 | [TestClass]
|
---|
31 | public class GeneticAlgorithmTest {
|
---|
32 | private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
|
---|
33 | public GeneticAlgorithmTest() { }
|
---|
34 |
|
---|
35 | private TestContext testContextInstance;
|
---|
36 | public TestContext TestContext {
|
---|
37 | get { return testContextInstance; }
|
---|
38 | set { testContextInstance = value; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | private Exception ex;
|
---|
42 |
|
---|
43 | [TestMethod]
|
---|
44 | [TestCategory("General")]
|
---|
45 | [TestProperty("Time", "long")]
|
---|
46 | public void GeneticAlgorithmPerformanceTest() {
|
---|
47 | ex = null;
|
---|
48 | GeneticAlgorithm ga = (GeneticAlgorithm)serializer.Deserialize(@"Test Resources\GA_TSP.hl");
|
---|
49 | ga.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ga_ExceptionOccurred);
|
---|
50 | ga.SetSeedRandomly.Value = false;
|
---|
51 | ga.Seed.Value = 0;
|
---|
52 |
|
---|
53 | ga.Prepare();
|
---|
54 | ga.Start();
|
---|
55 | if (ex != null) throw ex;
|
---|
56 |
|
---|
57 | TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
|
---|
58 |
|
---|
59 | double expectedBestQuality = 12332.0;
|
---|
60 | double expectedAverageQuality = 13123.2;
|
---|
61 | double expectedWorstQuality = 14538.0;
|
---|
62 | double bestQuality = (ga.Results["CurrentBestQuality"].Value as DoubleValue).Value;
|
---|
63 | double averageQuality = (ga.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
|
---|
64 | double worstQuality = (ga.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
|
---|
65 |
|
---|
66 | TestContext.WriteLine("");
|
---|
67 | TestContext.WriteLine("CurrentBestQuality: {0} (should be {1})", bestQuality, expectedBestQuality);
|
---|
68 | TestContext.WriteLine("CurrentAverageQuality: {0} (should be {1})", averageQuality, expectedAverageQuality);
|
---|
69 | TestContext.WriteLine("CurrentWorstQuality: {0} (should be {1})", worstQuality, expectedWorstQuality);
|
---|
70 |
|
---|
71 | Assert.AreEqual(bestQuality, expectedBestQuality);
|
---|
72 | Assert.AreEqual(averageQuality, expectedAverageQuality);
|
---|
73 | Assert.AreEqual(worstQuality, expectedWorstQuality);
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
77 | ex = e.Value;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|