[6201] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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]
|
---|
[9885] | 46 | [TestCategory("General")]
|
---|
| 47 | [TestCategory("Essential")]
|
---|
| 48 | [TestProperty("Time", "medium")]
|
---|
[6844] | 49 | [DeploymentItem(@"HeuristicLab-3.3/Resources/GA_SymbReg.hl")]
|
---|
[6201] | 50 | public void CollectGASample() {
|
---|
| 51 | GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize("GA_SymbReg.hl");
|
---|
| 52 |
|
---|
| 53 | Stopwatch watch = new Stopwatch();
|
---|
| 54 | watch.Start();
|
---|
| 55 | for (int i = 0; i < 1; i++)
|
---|
| 56 | ga.GetObjectGraphObjects().Count();
|
---|
| 57 | watch.Stop();
|
---|
| 58 |
|
---|
| 59 | var objects = ga.GetObjectGraphObjects().ToList();
|
---|
| 60 |
|
---|
| 61 | TestContext.WriteLine("Time elapsed {0}", watch.Elapsed);
|
---|
| 62 | TestContext.WriteLine("Objects discovered: {0}", objects.Count());
|
---|
[8309] | 63 | TestContext.WriteLine("HL objects discovered: {0}", objects.Count(o => o.GetType().Namespace.StartsWith("HeuristicLab")));
|
---|
[6201] | 64 | TestContext.WriteLine("");
|
---|
| 65 |
|
---|
| 66 | Dictionary<Type, List<object>> objs = new Dictionary<Type, List<object>>();
|
---|
| 67 | foreach (object o in objects) {
|
---|
| 68 | if (!objs.ContainsKey(o.GetType()))
|
---|
| 69 | objs.Add(o.GetType(), new List<object>());
|
---|
| 70 | objs[o.GetType()].Add(o);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | foreach (string s in objects.Select(o => o.GetType().Namespace).Distinct().OrderBy(s => s)) {
|
---|
[8309] | 74 | TestContext.WriteLine("{0}: {1}", s, objects.Count(o => o.GetType().Namespace == s));
|
---|
[6201] | 75 | }
|
---|
| 76 | TestContext.WriteLine("");
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | TestContext.WriteLine("Analysis of contained objects per name");
|
---|
| 80 | foreach (var pair in objs.OrderBy(x => x.Key.ToString())) {
|
---|
| 81 | TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
|
---|
| 82 | }
|
---|
| 83 | TestContext.WriteLine("");
|
---|
| 84 |
|
---|
| 85 | TestContext.WriteLine("Analysis of contained objects");
|
---|
| 86 | foreach (var pair in from o in objs orderby o.Value.Count descending select o) {
|
---|
| 87 | TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
|
---|
| 88 | }
|
---|
| 89 | TestContext.WriteLine("");
|
---|
| 90 | }
|
---|
[6205] | 91 |
|
---|
[6209] | 92 | /// <summary>
|
---|
| 93 | /// Tests if performance of multiple executions of a GA stays constant (as discussed in #1424)
|
---|
| 94 | /// Tests if object collection works after multiple executions of a GA
|
---|
| 95 | /// (for example the traversal of `ThreadLocal` objects in CollectObjectGraphObjects
|
---|
| 96 | /// causes a StackOverflow occurs after some executions)
|
---|
| 97 | /// </summary>
|
---|
[6205] | 98 | [TestMethod]
|
---|
[9885] | 99 | [TestCategory("General")]
|
---|
| 100 | [TestProperty("Time", "long")]
|
---|
[6205] | 101 | public void AlgorithmExecutions() {
|
---|
| 102 | var algs = new List<IAlgorithm>();
|
---|
| 103 |
|
---|
| 104 | Stopwatch sw = new Stopwatch();
|
---|
| 105 | for (int i = 0; i < 100; i++) {
|
---|
| 106 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 107 | ga.PopulationSize.Value = 5;
|
---|
| 108 | ga.MaximumGenerations.Value = 5;
|
---|
[9885] | 109 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
[6205] | 110 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
| 111 |
|
---|
| 112 | sw.Start();
|
---|
| 113 | algs.Add(ga);
|
---|
| 114 |
|
---|
| 115 | var cancellationTokenSource = new CancellationTokenSource();
|
---|
| 116 | ga.StartSync(cancellationTokenSource.Token);
|
---|
| 117 | sw.Stop();
|
---|
| 118 | TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed);
|
---|
| 119 | sw.Reset();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
[6495] | 122 |
|
---|
| 123 | /// <summary>
|
---|
| 124 | /// Test the execution of many algorithms in parallel
|
---|
| 125 | /// </summary>
|
---|
| 126 | [TestMethod]
|
---|
[9885] | 127 | [TestCategory("General")]
|
---|
| 128 | [TestProperty("Time", "medium")]
|
---|
[6495] | 129 | public void ParallelAlgorithmExecutions() {
|
---|
| 130 | int n = 60;
|
---|
| 131 | var tasks = new Task[n];
|
---|
| 132 |
|
---|
| 133 | TestContext.WriteLine("creating tasks...");
|
---|
| 134 | for (int i = 0; i < n; i++) {
|
---|
| 135 | tasks[i] = new Task((iobj) => {
|
---|
| 136 | int locali = (int)iobj;
|
---|
| 137 | GeneticAlgorithm ga = new GeneticAlgorithm();
|
---|
| 138 | ga.Name = "Alg " + locali;
|
---|
| 139 | ga.PopulationSize.Value = 5;
|
---|
| 140 | ga.MaximumGenerations.Value = 5;
|
---|
[9885] | 141 | ga.Engine = new SequentialEngine.SequentialEngine();
|
---|
[6495] | 142 | ga.Problem = new SingleObjectiveTestFunctionProblem();
|
---|
| 143 | ga.Prepare(true);
|
---|
| 144 | Console.WriteLine("{0}; Objects before execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
|
---|
| 145 | var sw = new Stopwatch();
|
---|
| 146 | sw.Start();
|
---|
| 147 | ga.StartSync(new CancellationToken());
|
---|
| 148 | sw.Stop();
|
---|
| 149 | Console.WriteLine("{0}; Objects after execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
|
---|
| 150 | Console.WriteLine("{0}; ExecutionTime: {1} ", ga.Name, sw.Elapsed);
|
---|
| 151 | }, i);
|
---|
| 152 | }
|
---|
| 153 | TestContext.WriteLine("starting tasks...");
|
---|
| 154 | for (int i = 0; i < n; i++) {
|
---|
| 155 | tasks[i].Start();
|
---|
| 156 | }
|
---|
| 157 | TestContext.WriteLine("waiting for tasks to finish...");
|
---|
| 158 | Task.WaitAll(tasks);
|
---|
| 159 | }
|
---|
[6201] | 160 | }
|
---|
| 161 | }
|
---|