Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab-3.3/CollectObjectGraphTest.cs @ 9922

Last change on this file since 9922 was 9885, checked in by mkommend, 11 years ago

#2088: Merged all changesets regarding the unit test restructuring in the stable branch.

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26using System.Threading;
27using System.Threading.Tasks;
28using HeuristicLab.Algorithms.GeneticAlgorithm;
29using HeuristicLab.Common;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.Xml;
32using HeuristicLab.Problems.TestFunctions;
33using Microsoft.VisualStudio.TestTools.UnitTesting;
34
35namespace HeuristicLab.Tests {
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]
46    [TestCategory("General")]
47    [TestCategory("Essential")]
48    [TestProperty("Time", "medium")]
49    [DeploymentItem(@"HeuristicLab-3.3/Resources/GA_SymbReg.hl")]
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());
63      TestContext.WriteLine("HL objects discovered: {0}", objects.Count(o => o.GetType().Namespace.StartsWith("HeuristicLab")));
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)) {
74        TestContext.WriteLine("{0}: {1}", s, objects.Count(o => o.GetType().Namespace == s));
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    }
91
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>
98    [TestMethod]
99    [TestCategory("General")]
100    [TestProperty("Time", "long")]
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;
109        ga.Engine = new SequentialEngine.SequentialEngine();
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    }
122
123    /// <summary>
124    /// Test the execution of many algorithms in parallel
125    /// </summary>
126    [TestMethod]
127    [TestCategory("General")]
128    [TestProperty("Time", "medium")]
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;
141          ga.Engine = new SequentialEngine.SequentialEngine();
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    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.