1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 System.Threading;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Persistence.Default.Xml;
|
---|
28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Algorithms.GeneticAlgorithm_33.Tests {
|
---|
31 | /// <summary>
|
---|
32 | /// Summary description for UnitTest
|
---|
33 | /// </summary>
|
---|
34 | [TestClass]
|
---|
35 | public class UnitTest {
|
---|
36 | public UnitTest() {
|
---|
37 | //
|
---|
38 | // TODO: Add constructor logic here
|
---|
39 | //
|
---|
40 | }
|
---|
41 |
|
---|
42 | private TestContext testContextInstance;
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | ///Gets or sets the test context which provides
|
---|
46 | ///information about and functionality for the current test run.
|
---|
47 | ///</summary>
|
---|
48 | public TestContext TestContext {
|
---|
49 | get {
|
---|
50 | return testContextInstance;
|
---|
51 | }
|
---|
52 | set {
|
---|
53 | testContextInstance = value;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | #region Additional test attributes
|
---|
58 | //
|
---|
59 | // You can use the following additional attributes as you write your tests:
|
---|
60 | //
|
---|
61 | // Use ClassInitialize to run code before running the first test in the class
|
---|
62 | // [ClassInitialize()]
|
---|
63 | // public static void MyClassInitialize(TestContext testContext) { }
|
---|
64 | //
|
---|
65 | // Use ClassCleanup to run code after all tests in a class have run
|
---|
66 | // [ClassCleanup()]
|
---|
67 | // public static void MyClassCleanup() { }
|
---|
68 | //
|
---|
69 | // Use TestInitialize to run code before running each test
|
---|
70 | // [TestInitialize()]
|
---|
71 | // public void MyTestInitialize() { }
|
---|
72 | //
|
---|
73 | // Use TestCleanup to run code after each test has run
|
---|
74 | // [TestCleanup()]
|
---|
75 | // public void MyTestCleanup() { }
|
---|
76 | //
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | private EventWaitHandle trigger = new AutoResetEvent(false);
|
---|
80 | private Exception ex;
|
---|
81 |
|
---|
82 | [TestMethod]
|
---|
83 | [DeploymentItem(@"GA_TSP.hl")]
|
---|
84 | public void GeneticAlgorithmPerformanceTest() {
|
---|
85 | ex = null;
|
---|
86 | IAlgorithm ga = (IAlgorithm)XmlParser.Deserialize("GA_TSP.hl");
|
---|
87 | ga.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ga_ExceptionOccurred);
|
---|
88 | ga.Stopped += new EventHandler(ga_Stopped);
|
---|
89 | ga.Prepare();
|
---|
90 | ga.Start();
|
---|
91 | trigger.WaitOne();
|
---|
92 | TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
|
---|
93 |
|
---|
94 | double expectedBestQuality = 12332.0;
|
---|
95 | double expectedAverageQuality = 13123.2;
|
---|
96 | double expectedWorstQuality = 14538.0;
|
---|
97 | double bestQuality = (ga.Results["Current Best Quality"].Value as DoubleValue).Value;
|
---|
98 | double averageQuality = (ga.Results["Current Average Quality"].Value as DoubleValue).Value;
|
---|
99 | double worstQuality = (ga.Results["Current Worst Quality"].Value as DoubleValue).Value;
|
---|
100 |
|
---|
101 | TestContext.WriteLine("");
|
---|
102 | TestContext.WriteLine("Current Best Quality: {0} (should be {1})", bestQuality, expectedBestQuality);
|
---|
103 | TestContext.WriteLine("Current Average Quality: {0} (should be {1})", averageQuality, expectedAverageQuality);
|
---|
104 | TestContext.WriteLine("Current Worst Quality: {0} (should be {1})", worstQuality, expectedWorstQuality);
|
---|
105 |
|
---|
106 | Assert.AreEqual(bestQuality, expectedBestQuality);
|
---|
107 | Assert.AreEqual(averageQuality, expectedAverageQuality);
|
---|
108 | Assert.AreEqual(worstQuality, expectedWorstQuality);
|
---|
109 |
|
---|
110 | if (ex != null) throw ex;
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
114 | ex = e.Value;
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void ga_Stopped(object sender, EventArgs e) {
|
---|
118 | trigger.Set();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|