public void ParallelAlgorithmExecutions() { int n = 60; var tasks = new Task[n]; TestContext.WriteLine("creating tasks..."); for (int i = 0; i < n; i++) { tasks[i] = new Task((iobj) => { int locali = (int)iobj; GeneticAlgorithm ga = new GeneticAlgorithm(); ga.Name = "Alg " + locali; ga.PopulationSize.Value = 5; ga.MaximumGenerations.Value = 5; ga.Engine = new SequentialEngine.SequentialEngine(); ga.Problem = new SingleObjectiveTestFunctionProblem(); ga.Prepare(true); Console.WriteLine("{0}; Objects before execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count()); var sw = new Stopwatch(); sw.Start(); ga.StartSync(new CancellationToken()); sw.Stop(); Console.WriteLine("{0}; Objects after execution: {1}", ga.Name, ga.GetObjectGraphObjects().Count()); Console.WriteLine("{0}; ExecutionTime: {1} ", ga.Name, sw.Elapsed); }, i); } TestContext.WriteLine("starting tasks..."); for (int i = 0; i < n; i++) { tasks[i].Start(); } TestContext.WriteLine("waiting for tasks to finish..."); Task.WaitAll(tasks); }
public void AlgorithmExecutions() { var algs = new List<IAlgorithm>(); Stopwatch sw = new Stopwatch(); for (int i = 0; i < 100; i++) { GeneticAlgorithm ga = new GeneticAlgorithm(); ga.PopulationSize.Value = 5; ga.MaximumGenerations.Value = 5; ga.Engine = new SequentialEngine.SequentialEngine(); ga.Problem = new SingleObjectiveTestFunctionProblem(); sw.Start(); algs.Add(ga); var cancellationTokenSource = new CancellationTokenSource(); ga.StartSync(cancellationTokenSource.Token); sw.Stop(); TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed); sw.Reset(); } }