[6201] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6201] | 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.Collections.Generic;
|
---|
| 24 | using System.Diagnostics;
|
---|
| 25 | using System.Linq;
|
---|
[6205] | 26 | using System.Threading;
|
---|
[6495] | 27 | using System.Threading.Tasks;
|
---|
[6201] | 28 | using HeuristicLab.Algorithms.GeneticAlgorithm;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
[6205] | 30 | using HeuristicLab.Optimization;
|
---|
[6201] | 31 | using HeuristicLab.Persistence.Default.Xml;
|
---|
[6205] | 32 | using HeuristicLab.Problems.TestFunctions;
|
---|
[6201] | 33 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 34 |
|
---|
[9885] | 35 | namespace HeuristicLab.Tests {
|
---|
[6201] | 36 | [TestClass]
|
---|
| 37 | public class CollectObjectGraphTest {
|
---|
| 38 |
|
---|
| 39 | private TestContext testContextInstance;
|
---|
| 40 | public TestContext TestContext {
|
---|
| 41 | get { return testContextInstance; }
|
---|
| 42 | set { testContextInstance = value; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | [TestMethod]
|
---|
[13879] | 46 | [Description("Verify that the object graph traversal is working by checking the number of objects after traversal.")]
|
---|
[9885] | 47 | [TestCategory("General")]
|
---|
| 48 | [TestCategory("Essential")]
|
---|
| 49 | [TestProperty("Time", "medium")]
|
---|
[13879] | 50 | public void TestObjectGraphTraversal() {
|
---|
| 51 | GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize(@"Test Resources\GA_SymbReg.hl");
|
---|
| 52 | var objects = ga.GetObjectGraphObjects().ToList();
|
---|
| 53 |
|
---|
| 54 | // Should be 3982, but count may change slightly as members are added or removed
|
---|
| 55 | Assert.IsTrue(objects.Count > 1, "Number of objects in the object graph seems to small.");
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | [TestMethod]
|
---|
| 59 | [TestCategory("General")]
|
---|
| 60 | [TestCategory("Essential")]
|
---|
| 61 | [TestProperty("Time", "medium")]
|
---|
[6201] | 62 | public void CollectGASample() {
|
---|
[9979] | 63 | GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize(@"Test Resources\GA_SymbReg.hl");
|
---|
[6201] | 64 |
|
---|
| 65 | Stopwatch watch = new Stopwatch();
|
---|
| 66 | watch.Start();
|
---|
| 67 | for (int i = 0; i < 1; i++)
|
---|
| 68 | ga.GetObjectGraphObjects().Count();
|
---|
| 69 | watch.Stop();
|
---|
| 70 |
|
---|
| 71 | var objects = ga.GetObjectGraphObjects().ToList();
|
---|
| 72 |
|
---|
| 73 | TestContext.WriteLine("Time elapsed {0}", watch.Elapsed);
|
---|
| 74 | TestContext.WriteLine("Objects discovered: {0}", objects.Count());
|
---|
[8309] | 75 | TestContext.WriteLine("HL objects discovered: {0}", objects.Count(o => o.GetType().Namespace.StartsWith("HeuristicLab")));
|
---|
[6201] | 76 | TestContext.WriteLine("");
|
---|
| 77 |
|
---|
| 78 | Dictionary<Type, List<object>> objs = new Dictionary<Type, List<object>>();
|
---|
| 79 | foreach (object o in objects) {
|
---|
| 80 | if (!objs.ContainsKey(o.GetType()))
|
---|
| 81 | objs.Add(o.GetType(), new List<object>());
|
---|
| 82 | objs[o.GetType()].Add(o);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | foreach (string s in objects.Select(o => o.GetType().Namespace).Distinct().OrderBy(s => s)) {
|
---|
[8309] | 86 | TestContext.WriteLine("{0}: {1}", s, objects.Count(o => o.GetType().Namespace == s));
|
---|
[6201] | 87 | }
|
---|
| 88 | TestContext.WriteLine("");
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | TestContext.WriteLine("Analysis of contained objects per name");
|
---|
| 92 | foreach (var pair in objs.OrderBy(x => x.Key.ToString())) {
|
---|
| 93 | TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
|
---|
| 94 | }
|
---|
| 95 | TestContext.WriteLine("");
|
---|
| 96 |
|
---|
| 97 | TestContext.WriteLine("Analysis of contained objects");
|
---|
| 98 | foreach (var pair in from o in objs orderby o.Value.Count descending select o) {
|
---|
| 99 | TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
|
---|
| 100 | }
|
---|
| 101 | TestContext.WriteLine("");
|
---|
| 102 | }
|
---|
[6205] | 103 |
|
---|
[6209] | 104 | /// <summary>
|
---|
| 105 | /// Tests if performance of multiple executions of a GA stays constant (as discussed in #1424)
|
---|
| 106 | /// Tests if object collection works after multiple executions of a GA
|
---|
| 107 | /// (for example the traversal of `ThreadLocal` objects in CollectObjectGraphObjects
|
---|
| 108 | /// causes a StackOverflow occurs after some executions)
|
---|
| 109 | /// </summary>
|
---|
[6205] | 110 | [TestMethod]
|
---|
[9885] | 111 | [TestCategory("General")]
|
---|
| 112 | [TestProperty("Time", "long")]
|
---|
[6205] | 113 | public void AlgorithmExecutions() {
|
---|
| 114 | var algs = new List<IAlgorithm>();
|
---|
| 115 |
|
---|
| 116 | Stopwatch sw = new Stopwatch();
|
---|
| 117 | for (int i = 0; i < 100; i++) {
|
---|
| 118 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 119 | ga.PopulationSize.Value = 5;
|
---|
| 120 | ga.MaximumGenerations.Value = 5;
|
---|
[9885] | 121 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
[6205] | 122 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
| 123 |
|
---|
| 124 | sw.Start();
|
---|
| 125 | algs.Add(ga);
|
---|
| 126 |
|
---|
| 127 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
| 128 | ga.StartSync(cancellationTokenSource.Token);
|
---|
| 129 | sw.Stop();
|
---|
| 130 | TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed);
|
---|
| 131 | sw.Reset();
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
[6495] | 134 |
|
---|
| 135 | /// <summary>
|
---|
| 136 | /// Test the execution of many algorithms in parallel
|
---|
| 137 | /// </summary>
|
---|
| 138 | [TestMethod]
|
---|
[9885] | 139 | [TestCategory("General")]
|
---|
| 140 | [TestProperty("Time", "medium")]
|
---|
[6495] | 141 | public void ParallelAlgorithmExecutions() {
|
---|
| 142 | int n = 60;
|
---|
| 143 | var tasks = new Task[n];
|
---|
| 144 |
|
---|
| 145 | TestContext.WriteLine("creating tasks...");
|
---|
| 146 | for (int i = 0; i < n; i++) {
|
---|
| 147 | tasks[i] = new Task((iobj) => {
|
---|
| 148 | int locali = (int)iobj;
|
---|
| 149 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 150 | ga.Name = "Alg " + locali;
|
---|
| 151 | ga.PopulationSize.Value = 5;
|
---|
| 152 | ga.MaximumGenerations.Value = 5;
|
---|
[9885] | 153 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
[6495] | 154 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
| 155 | ga.Prepare(true);
|
---|
| 156 | Console.WriteLine("{0}; Objects before execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
|
---|
| 157 | var sw = new Stopwatch();
|
---|
| 158 | sw.Start();
|
---|
| 159 | ga.StartSync(new CancellationToken());
|
---|
| 160 | sw.Stop();
|
---|
| 161 | Console.WriteLine("{0}; Objects after execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
|
---|
| 162 | Console.WriteLine("{0}; ExecutionTime: {1} ", ga.Name, sw.Elapsed);
|
---|
| 163 | }, i);
|
---|
| 164 | }
|
---|
| 165 | TestContext.WriteLine("starting tasks...");
|
---|
| 166 | for (int i = 0; i < n; i++) {
|
---|
| 167 | tasks[i].Start();
|
---|
| 168 | }
|
---|
| 169 | TestContext.WriteLine("waiting for tasks to finish...");
|
---|
| 170 | Task.WaitAll(tasks);
|
---|
| 171 | }
|
---|
[6201] | 172 | }
|
---|
| 173 | }
|
---|