[4512] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4512] | 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.Algorithms.GeneticAlgorithm;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 29 |
|
---|
[9885] | 30 | namespace HeuristicLab.Tests {
|
---|
[4512] | 31 | [TestClass]
|
---|
| 32 | public class GeneticAlgorithmTest {
|
---|
| 33 | public GeneticAlgorithmTest() { }
|
---|
| 34 |
|
---|
| 35 | private TestContext testContextInstance;
|
---|
| 36 | public TestContext TestContext {
|
---|
[9885] | 37 | get { return testContextInstance; }
|
---|
| 38 | set { testContextInstance = value; }
|
---|
[4512] | 39 | }
|
---|
| 40 |
|
---|
| 41 | private EventWaitHandle trigger = new AutoResetEvent(false);
|
---|
| 42 | private Exception ex;
|
---|
| 43 |
|
---|
| 44 | [TestMethod]
|
---|
[9885] | 45 | [TestCategory("General")]
|
---|
| 46 | [TestProperty("Time", "long")]
|
---|
[4512] | 47 | public void GeneticAlgorithmPerformanceTest() {
|
---|
| 48 | ex = null;
|
---|
[9979] | 49 | GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize(@"Test Resources\GA_TSP.hl");
|
---|
[4512] | 50 | ga.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(ga_ExceptionOccurred);
|
---|
| 51 | ga.Stopped += new EventHandler(ga_Stopped);
|
---|
| 52 | ga.SetSeedRandomly.Value = false;
|
---|
| 53 | ga.Seed.Value = 0;
|
---|
| 54 |
|
---|
| 55 | ga.Prepare();
|
---|
| 56 | ga.Start();
|
---|
| 57 | trigger.WaitOne();
|
---|
| 58 | if (ex != null) throw ex;
|
---|
| 59 |
|
---|
| 60 | TestContext.WriteLine("Runtime: {0}", ga.ExecutionTime.ToString());
|
---|
| 61 |
|
---|
| 62 | double expectedBestQuality = 12332.0;
|
---|
| 63 | double expectedAverageQuality = 13123.2;
|
---|
| 64 | double expectedWorstQuality = 14538.0;
|
---|
| 65 | double bestQuality = (ga.Results["CurrentBestQuality"].Value as DoubleValue).Value;
|
---|
| 66 | double averageQuality = (ga.Results["CurrentAverageQuality"].Value as DoubleValue).Value;
|
---|
| 67 | double worstQuality = (ga.Results["CurrentWorstQuality"].Value as DoubleValue).Value;
|
---|
| 68 |
|
---|
| 69 | TestContext.WriteLine("");
|
---|
| 70 | TestContext.WriteLine("CurrentBestQuality: {0} (should be {1})", bestQuality, expectedBestQuality);
|
---|
| 71 | TestContext.WriteLine("CurrentAverageQuality: {0} (should be {1})", averageQuality, expectedAverageQuality);
|
---|
| 72 | TestContext.WriteLine("CurrentWorstQuality: {0} (should be {1})", worstQuality, expectedWorstQuality);
|
---|
| 73 |
|
---|
| 74 | Assert.AreEqual(bestQuality, expectedBestQuality);
|
---|
| 75 | Assert.AreEqual(averageQuality, expectedAverageQuality);
|
---|
| 76 | Assert.AreEqual(worstQuality, expectedWorstQuality);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private void ga_ExceptionOccurred(object sender, EventArgs<Exception> e) {
|
---|
| 80 | ex = e.Value;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | private void ga_Stopped(object sender, EventArgs e) {
|
---|
| 84 | trigger.Set();
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|