Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Tests/HeuristicLab-3.3/CollectObjectGraphTest.cs @ 11981

Last change on this file since 11981 was 8660, checked in by gkronber, 12 years ago

#1847 merged r8205:8635 from trunk into branch

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 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(@"HeuristicLab-3.3/Resources/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.Count(o => o.GetType().Namespace.StartsWith("HeuristicLab")));
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.Count(o => o.GetType().Namespace == s));
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 algs = new List<IAlgorithm>();
99
100      Stopwatch sw = new Stopwatch();
101      for (int i = 0; i < 100; i++) {
102        GeneticAlgorithm ga = new GeneticAlgorithm();
103        ga.PopulationSize.Value = 5;
104        ga.MaximumGenerations.Value = 5;
105        ga.Engine = new SequentialEngine();
106        ga.Problem = new SingleObjectiveTestFunctionProblem();
107
108        sw.Start();
109        algs.Add(ga);
110
111        var cancellationTokenSource = new CancellationTokenSource();
112        ga.StartSync(cancellationTokenSource.Token);
113        sw.Stop();
114        TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed);
115        sw.Reset();
116      }
117    }
118
119    /// <summary>
120    /// Test the execution of many algorithms in parallel
121    /// </summary>
122    [TestMethod]
123    public void ParallelAlgorithmExecutions() {
124      int n = 60;
125      var tasks = new Task[n];
126
127      TestContext.WriteLine("creating tasks...");
128      for (int i = 0; i < n; i++) {
129        tasks[i] = new Task((iobj) => {
130          int locali = (int)iobj;
131          GeneticAlgorithm ga = new GeneticAlgorithm();
132          ga.Name = "Alg " + locali;
133          ga.PopulationSize.Value = 5;
134          ga.MaximumGenerations.Value = 5;
135          ga.Engine = new SequentialEngine();
136          ga.Problem = new SingleObjectiveTestFunctionProblem();
137          ga.Prepare(true);
138          Console.WriteLine("{0}; Objects before execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
139          var sw = new Stopwatch();
140          sw.Start();
141          ga.StartSync(new CancellationToken());
142          sw.Stop();
143          Console.WriteLine("{0}; Objects after execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
144          Console.WriteLine("{0}; ExecutionTime: {1} ", ga.Name, sw.Elapsed);
145        }, i);
146      }
147      TestContext.WriteLine("starting tasks...");
148      for (int i = 0; i < n; i++) {
149        tasks[i].Start();
150      }
151      TestContext.WriteLine("waiting for tasks to finish...");
152      Task.WaitAll(tasks);
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.