Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9764 was 9764, checked in by abeham, 11 years ago

#2088: Renamed namespaces (removed _33 and _34 postfixes)

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