Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/ExternalEvaluationProcessDriver.cs @ 3862

Last change on this file since 3862 was 3862, checked in by abeham, 14 years ago

#866

  • updated external evaluation problem
  • included in build config
File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Diagnostics;
24using System.IO;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.ExternalEvaluation {
29  [Item("ExternalEvaluationProcessDriver", "A driver for external evaluation problems that launches the external application in a new process.")]
30  [StorableClass]
31  public class LocalProcessDriver : ExternalEvaluationDriver {
32    private Process process;
33    [Storable]
34    private string executable;
35    [Storable]
36    private string arguments;
37    private ExternalEvaluationStreamDriver driver;
38
39    public LocalProcessDriver() : this(String.Empty, String.Empty) { }
40    public LocalProcessDriver(string executable, string arguments)
41      : base() {
42      this.executable = executable;
43      this.arguments = arguments;
44    }
45
46    #region IExternalDriver Members
47
48    public override void Start() {
49      base.Start();
50      process = new Process();
51      process.StartInfo = new ProcessStartInfo(executable, arguments);
52      process.StartInfo.UseShellExecute = false;
53      process.StartInfo.RedirectStandardInput = true;
54      process.StartInfo.RedirectStandardOutput = true;
55      process.Start();
56      Stream processStdOut = process.StandardOutput.BaseStream;
57      Stream processStdIn = process.StandardInput.BaseStream;
58      driver = new ExternalEvaluationStreamDriver(processStdOut, processStdIn);
59      driver.Start();
60    }
61
62    public override QualityMessage Evaluate(SolutionMessage solution) {
63      return driver.Evaluate(solution);
64    }
65
66    public override void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
67      driver.EvaluateAsync(solution, callback);
68    }
69
70    public override void Stop() {
71      base.Stop();
72      if (!process.HasExited) {
73        driver.Stop();
74        if (!process.HasExited) {
75          process.CloseMainWindow();
76          process.WaitForExit(1000);
77          process.Close();
78        }
79      }
80      process = null;
81    }
82
83    #endregion
84  }
85}
Note: See TracBrowser for help on using the repository browser.