source: branches/MPI/HeuristicLab.MPIAlgorithmRunner/3.3/Program.cs @ 7205

Last change on this file since 7205 was 7205, checked in by svonolfe, 8 years ago

Refactored solution (#1542)

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