Changeset 11033


Ignore:
Timestamp:
06/23/14 16:56:04 (5 years ago)
Author:
mkommend
Message:

#2082: Further bug fixes in the MATLAB evaluator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.ExternalEvaluation Scientific/HeuristicLab.Problems.ExternalEvaluation.Matlab/3.3/MatlabParameterVectorEvaluator.cs

    r11028 r11033  
    2222using System;
    2323using System.IO;
     24using System.Runtime.InteropServices;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    6768
    6869    public override void ClearState() {
    69       matLabConnector.Execute("clear");
     70      //mkommend: necessary because matlabConnector.Quit() does not work
     71      try {
     72        matLabConnector.Execute("exit");
     73      }
     74      catch (COMException) { }
     75      finally {
     76        matLabConnector = null;
     77      }
     78
    7079      changedDirectory = false;
    71       executedInitializationScript = false;
    7280      base.ClearState();
    7381    }
     
    7583    private MLApp.MLApp matLabConnector;
    7684    private bool changedDirectory = false;
    77     private bool executedInitializationScript = false;
    7885    private readonly object locker = new object();
    7986
    8087    public override IOperation Apply() {
    8188      lock (locker) {
    82 
    83         if (matLabConnector == null) {
    84           Type matlabtype = Type.GetTypeFromProgID("matlab.application.single");
    85           matLabConnector = (MLApp.MLApp)Activator.CreateInstance(matlabtype);
    86           matLabConnector.Visible = 0;
    87         }
    88         string result;
    89 
    9089        var initializationScript = MatlabInitializationScriptParameter.ActualValue;
    9190        if (!string.IsNullOrEmpty(initializationScript.Value) && !initializationScript.Exists())
     
    9897          throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value));
    9998
    100         if (!string.IsNullOrEmpty(initializationScript.Value) && !executedInitializationScript) {
    101           var directoryName = Path.GetDirectoryName(initializationScript.Value);
    102           result = matLabConnector.Execute(string.Format("cd '{0}'", directoryName));
    103           if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
     99        string result;
     100        if (matLabConnector == null) {
     101          Type matlabtype = Type.GetTypeFromProgID("matlab.application.single");
     102          matLabConnector = (MLApp.MLApp)Activator.CreateInstance(matlabtype);
     103          matLabConnector.Visible = 0;
    104104
    105           result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(initializationScript.Value));
    106           if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
     105          if (!string.IsNullOrEmpty(initializationScript.Value)) {
     106            var directoryName = Path.GetDirectoryName(initializationScript.Value);
     107            if (string.IsNullOrEmpty(directoryName)) directoryName = Environment.CurrentDirectory;
     108            result = matLabConnector.Execute(string.Format("cd '{0}'", directoryName));
     109            if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
    107110
    108           executedInitializationScript = true;
     111            result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(initializationScript.Value));
     112            if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
     113          }
    109114        }
    110115
    111116        if (!changedDirectory) {
    112117          var directoryName = Path.GetDirectoryName(evaluationScript.Value);
     118          if (string.IsNullOrEmpty(directoryName)) directoryName = Environment.CurrentDirectory;
    113119          result = matLabConnector.Execute(string.Format("cd '{0}'", directoryName));
    114120          if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
Note: See TracChangeset for help on using the changeset viewer.