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