[6348] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 28 | using System.Threading;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.MPIAlgorithmRunner {
|
---|
| 31 | class Program {
|
---|
[7205] | 32 | EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
|
---|
[6348] | 33 |
|
---|
[7205] | 34 | static void Main(string[] args) {
|
---|
| 35 | if (args.Length != 2) {
|
---|
| 36 | Console.WriteLine("Args:" + args.Length);
|
---|
[6348] | 37 |
|
---|
[7205] | 38 | throw new ArgumentException("You must provide two arguments - the algorithm file and the result file");
|
---|
| 39 | }
|
---|
[6394] | 40 |
|
---|
[7205] | 41 | string fileName = args[0];
|
---|
| 42 | string resultName = args[1];
|
---|
[6348] | 43 |
|
---|
[7205] | 44 | using (new MPI.Environment(ref args)) {
|
---|
| 45 | Program p = new Program();
|
---|
| 46 | p.StartAlgorithm(fileName, resultName + MPI.Communicator.world.Rank + ".hl");
|
---|
| 47 | }
|
---|
[6388] | 48 | }
|
---|
| 49 |
|
---|
[7205] | 50 | public void StartAlgorithm(string fileName, string resultName) {
|
---|
| 51 | IAlgorithm alg = XmlParser.Deserialize<HeuristicLab.Optimization.IAlgorithm>(fileName);
|
---|
[6388] | 52 |
|
---|
[7205] | 53 | alg.Stopped += new EventHandler(algorithm_Stopped);
|
---|
| 54 | waitHandle.Reset();
|
---|
[6388] | 55 |
|
---|
[7205] | 56 | alg.Prepare(true);
|
---|
| 57 | alg.Start();
|
---|
[6394] | 58 |
|
---|
[7205] | 59 | Thread.Sleep(10000);
|
---|
| 60 | Thread t = new Thread(delegate() {
|
---|
| 61 | while (true) {
|
---|
| 62 | if (alg.ExecutionState == Core.ExecutionState.Started) {
|
---|
| 63 | alg.Pause();
|
---|
[6354] | 64 |
|
---|
[7205] | 65 | while (alg.ExecutionState == Core.ExecutionState.Started)
|
---|
| 66 | Thread.Sleep(100);
|
---|
[6354] | 67 |
|
---|
[7205] | 68 | if (alg.Results.ContainsKey("Best valid Solution Distance")) {
|
---|
| 69 | Console.WriteLine("BestDistance: " + alg.Results["Best valid Solution Distance"].Value.ToString());
|
---|
| 70 | }
|
---|
[6354] | 71 |
|
---|
[7205] | 72 | if (alg.Results.ContainsKey("Best valid Solution Vehicles")) {
|
---|
| 73 | Console.WriteLine("BestVehicles: " + alg.Results["Best valid Solution Vehicles"].Value.ToString());
|
---|
[6354] | 74 | }
|
---|
| 75 |
|
---|
[7205] | 76 | XmlGenerator.Serialize(alg, resultName);
|
---|
| 77 | alg.Start();
|
---|
[6354] | 78 | }
|
---|
| 79 |
|
---|
[7205] | 80 | Thread.Sleep(300000);
|
---|
[6354] | 81 | }
|
---|
[7205] | 82 | });
|
---|
| 83 | t.IsBackground = true;
|
---|
| 84 | t.Start();
|
---|
[6348] | 85 |
|
---|
[6402] | 86 | waitHandle.WaitOne();
|
---|
[6354] | 87 |
|
---|
[7205] | 88 | XmlGenerator.Serialize(alg, resultName);
|
---|
[6348] | 89 | }
|
---|
| 90 |
|
---|
[7205] | 91 | void algorithm_Stopped(object sender, EventArgs e) {
|
---|
[6348] | 92 | waitHandle.Set();
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|