Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Tests/HeuristicLab-3.3/CollectObjectGraphTest.cs @ 17946

Last change on this file since 17946 was 17696, checked in by abeham, 4 years ago

#2521:

  • fixed bug in EngineAlgorithm in Problem setter
  • Added storable type to IEncodedProblem
  • fixed some bugs and tests
File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Tasks;
27using HEAL.Attic;
28using HeuristicLab.Algorithms.GeneticAlgorithm;
29using HeuristicLab.Common;
30using HeuristicLab.Optimization;
31using HeuristicLab.Problems.TestFunctions;
32using HeuristicLab.Problems.TravelingSalesman;
33using Microsoft.VisualStudio.TestTools.UnitTesting;
34
35namespace HeuristicLab.Tests {
36  [TestClass]
37  public class CollectObjectGraphTest {
38    private static readonly ProtoBufSerializer serializer = new ProtoBufSerializer();
39
40    private TestContext testContextInstance;
41    public TestContext TestContext {
42      get { return testContextInstance; }
43      set { testContextInstance = value; }
44    }
45
46    [TestMethod]
47    [Description("Verify that the object graph traversal is working by checking the number of objects after traversal.")]
48    [TestCategory("General")]
49    [TestCategory("Essential")]
50    [TestProperty("Time", "medium")]
51    public void TestObjectGraphTraversal() {
52      /* TODO, sample does not load (SolutionCreatorParameter moved from Problem to Algorithm)
53       * TODO, this is more of a serialization test?
54      GeneticAlgorithm ga = (GeneticAlgorithm)serializer.Deserialize(@"Test Resources\GA_SymbReg.hl");
55      var objects = ga.GetObjectGraphObjects().ToList();
56
57      // Should be 3982, but count may change slightly as members are added or removed
58      Assert.IsTrue(objects.Count > 1, "Number of objects in the object graph seems to small.");
59      */
60      var ga = new GeneticAlgorithm();
61      ga.Problem = new TSP();
62      var objects = ga.GetObjectGraphObjects().ToList();
63      Assert.IsTrue(objects.Count > 1000, "Number of objects in the object graph seems to small.");
64    }
65
66    [TestMethod]
67    [TestCategory("General")]
68    [TestCategory("Essential")]
69    [TestProperty("Time", "medium")]
70    public void CollectGASample() {
71      /* TODO, sample does not load (SolutionCreatorParameter moved from Problem to Algorithm)
72      GeneticAlgorithm ga = (GeneticAlgorithm)serializer.Deserialize(@"Test Resources\GA_SymbReg.hl");
73
74      Stopwatch watch = new Stopwatch();
75      watch.Start();
76      for (int i = 0; i < 1; i++)
77        ga.GetObjectGraphObjects().Count();
78      watch.Stop();
79
80      var objects = ga.GetObjectGraphObjects().ToList();
81
82      TestContext.WriteLine("Time elapsed {0}", watch.Elapsed);
83      TestContext.WriteLine("Objects discovered: {0}", objects.Count());
84      TestContext.WriteLine("HL objects discovered: {0}", objects.Count(o => o.GetType().Namespace.StartsWith("HeuristicLab")));
85      TestContext.WriteLine("");
86
87      Dictionary<Type, List<object>> objs = new Dictionary<Type, List<object>>();
88      foreach (object o in objects) {
89        if (!objs.ContainsKey(o.GetType()))
90          objs.Add(o.GetType(), new List<object>());
91        objs[o.GetType()].Add(o);
92      }
93
94      foreach (string s in objects.Select(o => o.GetType().Namespace).Distinct().OrderBy(s => s)) {
95        TestContext.WriteLine("{0}: {1}", s, objects.Count(o => o.GetType().Namespace == s));
96      }
97      TestContext.WriteLine("");
98
99
100      TestContext.WriteLine("Analysis of contained objects per name");
101      foreach (var pair in objs.OrderBy(x => x.Key.ToString())) {
102        TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
103      }
104      TestContext.WriteLine("");
105
106      TestContext.WriteLine("Analysis of contained objects");
107      foreach (var pair in from o in objs orderby o.Value.Count descending select o) {
108        TestContext.WriteLine("{0}: {1}", pair.Key, pair.Value.Count);
109      }
110      TestContext.WriteLine("");
111      */
112    }
113
114    /// <summary>
115    /// Tests if performance of multiple executions of a GA stays constant (as discussed in #1424)
116    /// Tests if object collection works after multiple executions of a GA
117    /// (for example the traversal of `ThreadLocal` objects in CollectObjectGraphObjects
118    /// causes a StackOverflow occurs after some executions)
119    /// </summary>
120    [TestMethod]
121    [TestCategory("General")]
122    [TestProperty("Time", "long")]
123    public void AlgorithmExecutions() {
124      var algs = new List<IAlgorithm>();
125
126      Stopwatch sw = new Stopwatch();
127      for (int i = 0; i < 100; i++) {
128        GeneticAlgorithm ga = new GeneticAlgorithm();
129        ga.PopulationSize.Value = 5;
130        ga.MaximumGenerations.Value = 5;
131        ga.Engine = new SequentialEngine.SequentialEngine();
132        ga.Problem = new SingleObjectiveTestFunctionProblem();
133
134        sw.Start();
135        algs.Add(ga);
136
137        ga.Start();
138        sw.Stop();
139        TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed);
140        sw.Reset();
141      }
142    }
143
144    /// <summary>
145    /// Test the execution of many algorithms in parallel
146    /// </summary>
147    [TestMethod]
148    [TestCategory("General")]
149    [TestProperty("Time", "medium")]
150    public void ParallelAlgorithmExecutions() {
151      int n = 60;
152      var tasks = new Task[n];
153
154      TestContext.WriteLine("creating tasks...");
155      for (int i = 0; i < n; i++) {
156        tasks[i] = new Task((iobj) => {
157          int locali = (int)iobj;
158          GeneticAlgorithm ga = new GeneticAlgorithm();
159          ga.Name = "Alg " + locali;
160          ga.PopulationSize.Value = 5;
161          ga.MaximumGenerations.Value = 5;
162          ga.Engine = new SequentialEngine.SequentialEngine();
163          ga.Problem = new SingleObjectiveTestFunctionProblem();
164          ga.Prepare(true);
165          Console.WriteLine("{0}; Objects before execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
166          var sw = new Stopwatch();
167          sw.Start();
168          ga.Start();
169          sw.Stop();
170          Console.WriteLine("{0}; Objects after execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count());
171          Console.WriteLine("{0}; ExecutionTime: {1} ", ga.Name, sw.Elapsed);
172        }, i);
173      }
174      TestContext.WriteLine("starting tasks...");
175      for (int i = 0; i < n; i++) {
176        tasks[i].Start();
177      }
178      TestContext.WriteLine("waiting for tasks to finish...");
179      Task.WaitAll(tasks);
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.