#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.Xml;
using System.Threading;
namespace HeuristicLab.MPIAlgorithmRunner {
class Program {
EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
static void Main(string[] args) {
if (args.Length != 2) {
Console.WriteLine("Args:" + args.Length);
throw new ArgumentException("You must provide two arguments - the algorithm file and the result file");
}
string fileName = args[0];
string resultName = args[1];
using (new MPI.Environment(ref args)) {
Program p = new Program();
p.StartAlgorithm(fileName, resultName + MPI.Communicator.world.Rank + ".hl");
}
}
public void StartAlgorithm(string fileName, string resultName) {
IAlgorithm alg = XmlParser.Deserialize(fileName);
alg.Stopped += new EventHandler(algorithm_Stopped);
waitHandle.Reset();
alg.Prepare(true);
alg.Start();
Thread.Sleep(10000);
Thread t = new Thread(delegate() {
while (true) {
if (alg.ExecutionState == Core.ExecutionState.Started) {
alg.Pause();
while (alg.ExecutionState == Core.ExecutionState.Started)
Thread.Sleep(100);
if (alg.Results.ContainsKey("Best valid Solution Distance")) {
Console.WriteLine("BestDistance: " + alg.Results["Best valid Solution Distance"].Value.ToString());
}
if (alg.Results.ContainsKey("Best valid Solution Vehicles")) {
Console.WriteLine("BestVehicles: " + alg.Results["Best valid Solution Vehicles"].Value.ToString());
}
XmlGenerator.Serialize(alg, resultName);
alg.Start();
}
Thread.Sleep(300000);
}
});
t.IsBackground = true;
t.Start();
waitHandle.WaitOne();
XmlGenerator.Serialize(alg, resultName);
}
void algorithm_Stopped(object sender, EventArgs e) {
waitHandle.Set();
}
}
}