Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab-3.3/CollectObjectGraphTest.cs @ 10510

Last change on this file since 10510 was 9955, checked in by mkommend, 11 years ago

#2108: Added new resource folder for unit tests and removed run configuration.

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