Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6205 was 6205, checked in by cneumuel, 12 years ago

#1522

  • simplified check for EventHandler (by simply checking for delegate)
  • special handling types which use a hashtable internally (dictionaries, hashset, hashtable, ...) (as discussed with swagner)
  • added testcase which shows a stack overflow if ThreadLocal is included and compiler is on Debug (it does not happen in Release)
File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Algorithms.GeneticAlgorithm;
28using HeuristicLab.Common;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.Xml;
31using HeuristicLab.Problems.TestFunctions;
32using HeuristicLab.Random;
33using HeuristicLab.SequentialEngine;
34using Microsoft.VisualStudio.TestTools.UnitTesting;
35
36namespace HeuristicLab_33.Tests {
37  [TestClass]
38  public class CollectObjectGraphTest {
39
40    private TestContext testContextInstance;
41    public TestContext TestContext {
42      get { return testContextInstance; }
43      set { testContextInstance = value; }
44    }
45
46    [TestMethod]
47    [DeploymentItem(@"GA_SymbReg.hl")]
48    public void CollectGASample() {
49      GeneticAlgorithm ga = (GeneticAlgorithm)XmlParser.Deserialize("GA_SymbReg.hl");
50
51      Stopwatch watch = new Stopwatch();
52      watch.Start();
53      for (int i = 0; i < 1; i++)
54        ga.GetObjectGraphObjects().Count();
55      watch.Stop();
56
57      var objects = ga.GetObjectGraphObjects().ToList();
58
59      TestContext.WriteLine("Time elapsed {0}", watch.Elapsed);
60      TestContext.WriteLine("Objects discovered: {0}", objects.Count());
61      TestContext.WriteLine("HL objects discovered: {0}", objects.Where(o => o.GetType().Namespace.StartsWith("HeuristicLab")).Count());
62      TestContext.WriteLine("");
63
64      Dictionary<Type, List<object>> objs = new Dictionary<Type, List<object>>();
65      foreach (object o in objects) {
66        if (!objs.ContainsKey(o.GetType()))
67          objs.Add(o.GetType(), new List<object>());
68        objs[o.GetType()].Add(o);
69      }
70
71      foreach (string s in objects.Select(o => o.GetType().Namespace).Distinct().OrderBy(s => s)) {
72        TestContext.WriteLine("{0}: {1}", s, objects.Where(o => o.GetType().Namespace == s).Count());
73      }
74      TestContext.WriteLine("");
75
76
77      TestContext.WriteLine("Analysis of contained objects per name");
78      foreach (var pair in objs.OrderBy(x => x.Key.ToString())) {
79        TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
80      }
81      TestContext.WriteLine("");
82
83      TestContext.WriteLine("Analysis of contained objects");
84      foreach (var pair in from o in objs orderby o.Value.Count descending select o) {
85        TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
86      }
87      TestContext.WriteLine("");
88    }
89
90    [TestMethod]
91    public void AlgorithmExecutions() {
92      var random = new MersenneTwister(0);
93      var algs = new List<IAlgorithm>();
94
95      Stopwatch sw = new Stopwatch();
96      for (int i = 0; i < 100; i++) {
97        GeneticAlgorithm ga = new GeneticAlgorithm();
98        ga.PopulationSize.Value = 5;
99        ga.MaximumGenerations.Value = 5;
100        ga.Engine = new SequentialEngine();
101        ga.Problem = new SingleObjectiveTestFunctionProblem();
102
103        sw.Start();
104        algs.Add(ga);
105
106        var cancellationTokenSource = new CancellationTokenSource();
107        ga.StartSync(cancellationTokenSource.Token);
108        sw.Stop();
109        TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed);
110        sw.Reset();
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.