Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab/3.3/Tests/CollectObjectGraphTest.cs @ 6479

Last change on this file since 6479 was 6232, checked in by epitzer, 13 years ago

rebased on current trunk r6223 (#1530)

File size: 4.4 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    /// <summary>
91    /// Tests if performance of multiple executions of a GA stays constant (as discussed in #1424)
92    /// Tests if object collection works after multiple executions of a GA
93    /// (for example the traversal of `ThreadLocal` objects in CollectObjectGraphObjects
94    /// causes a StackOverflow occurs after some executions)
95    /// </summary>
96    [TestMethod]
97    public void AlgorithmExecutions() {
98      var random = new MersenneTwister(0);
99      var algs = new List<IAlgorithm>();
100
101      Stopwatch sw = new Stopwatch();
102      for (int i = 0; i < 100; i++) {
103        GeneticAlgorithm ga = new GeneticAlgorithm();
104        ga.PopulationSize.Value = 5;
105        ga.MaximumGenerations.Value = 5;
106        ga.Engine = new SequentialEngine();
107        ga.Problem = new SingleObjectiveTestFunctionProblem();
108
109        sw.Start();
110        algs.Add(ga);
111
112        var cancellationTokenSource = new CancellationTokenSource();
113        ga.StartSync(cancellationTokenSource.Token);
114        sw.Stop();
115        TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed);
116        sw.Reset();
117      }
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.