Changeset 6495 for trunk/sources
- Timestamp:
- 06/28/11 23:29:26 (13 years ago)
- Location:
- trunk/sources/HeuristicLab/3.3/Tests
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab/3.3/Tests/AlgorithmExtensions.cs
r6205 r6495 22 22 using System; 23 23 using System.Threading; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Optimization;26 26 27 27 namespace HeuristicLab_33.Tests { 28 28 public static class AlgorithmExtensions { 29 public static void StartSync(this I Algorithm algorithm, CancellationToken cancellationToken) {30 var executor = new AlgorithmExecutor( algorithm, cancellationToken);29 public static void StartSync(this IExecutable executable, CancellationToken cancellationToken) { 30 var executor = new AlgorithmExecutor(executable, cancellationToken); 31 31 executor.StartSync(); 32 32 } … … 37 37 /// </summary> 38 38 internal class AlgorithmExecutor { 39 private I Algorithm algorithm;39 private IExecutable executable; 40 40 private AutoResetEvent waitHandle = new AutoResetEvent(false); 41 41 private CancellationToken cancellationToken; 42 private Exception occuredException; 42 43 43 public AlgorithmExecutor(I Algorithm algorithm, CancellationToken cancellationToken) {44 this. algorithm = algorithm;44 public AlgorithmExecutor(IExecutable executable, CancellationToken cancellationToken) { 45 this.executable = executable; 45 46 this.cancellationToken = cancellationToken; 47 this.occuredException = null; 46 48 } 47 49 48 50 public void StartSync() { 49 algorithm.Stopped += new EventHandler(algorithm_Stopped); 50 algorithm.Paused += new EventHandler(algorithm_Paused); 51 executable.Stopped += new EventHandler(executable_Stopped); 52 executable.Paused += new EventHandler(executable_Paused); 53 executable.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(executable_ExceptionOccurred); 51 54 52 55 using (CancellationTokenRegistration registration = cancellationToken.Register(new Action(cancellationToken_Canceled))) { 53 algorithm.Start();54 waitHandle.WaitOne( );56 executable.Start(); 57 waitHandle.WaitOne(-1, false); 55 58 waitHandle.Dispose(); 56 59 } 57 60 58 algorithm.Stopped -= new EventHandler(algorithm_Stopped);59 algorithm.Paused -= new EventHandler(algorithm_Paused);60 if ( algorithm.ExecutionState == ExecutionState.Started) {61 algorithm.Pause();61 executable.Stopped -= new EventHandler(executable_Stopped); 62 executable.Paused -= new EventHandler(executable_Paused); 63 if (executable.ExecutionState == ExecutionState.Started) { 64 executable.Pause(); 62 65 } 63 66 cancellationToken.ThrowIfCancellationRequested(); 67 if (occuredException != null) throw occuredException; 64 68 } 65 69 66 private void algorithm_Paused(object sender, EventArgs e) {70 private void executable_Paused(object sender, EventArgs e) { 67 71 waitHandle.Set(); 68 72 } 69 73 70 private void algorithm_Stopped(object sender, EventArgs e) {74 private void executable_Stopped(object sender, EventArgs e) { 71 75 waitHandle.Set(); 76 } 77 78 private void executable_ExceptionOccurred(object sender, EventArgs<Exception> e) { 79 occuredException = e.Value; // after an exception occured the executable pauses 72 80 } 73 81 -
trunk/sources/HeuristicLab/3.3/Tests/CollectObjectGraphTest.cs
r6209 r6495 25 25 using System.Linq; 26 26 using System.Threading; 27 using System.Threading.Tasks; 27 28 using HeuristicLab.Algorithms.GeneticAlgorithm; 28 29 using HeuristicLab.Common; … … 30 31 using HeuristicLab.Persistence.Default.Xml; 31 32 using HeuristicLab.Problems.TestFunctions; 32 using HeuristicLab.Random;33 33 using HeuristicLab.SequentialEngine; 34 34 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 96 96 [TestMethod] 97 97 public void AlgorithmExecutions() { 98 var random = new MersenneTwister(0);99 98 var algs = new List<IAlgorithm>(); 100 99 … … 117 116 } 118 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 } 119 154 } 120 155 }
Note: See TracChangeset
for help on using the changeset viewer.