Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/20/14 15:39:24 (10 years ago)
Author:
mkommend
Message:

#2082: Implemented reviewer comments for the MATLAB parameter optimization problem.

File:
1 edited

Legend:

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

    r10594 r11028  
    3535    private const string QualityVariableParameterName = "QualityVariableName";
    3636    private const string MatlabEvaluationScriptParameterName = "MATLABEvaluationScript";
    37     private const string InitializationScriptParameterName = "InitializationScript";
     37    private const string MatlabInitializationScriptParameterName = "MATLABInitializationScript";
    3838
    3939    #region parameters
     
    4444      get { return (ILookupParameter<TextFileValue>)Parameters[MatlabEvaluationScriptParameterName]; }
    4545    }
    46     public IFixedValueParameter<TextFileValue> InitializationScriptParameter {
    47       get { return (IFixedValueParameter<TextFileValue>)Parameters[InitializationScriptParameterName]; }
     46    public ILookupParameter<TextFileValue> MatlabInitializationScriptParameter {
     47      get { return (ILookupParameter<TextFileValue>)Parameters[MatlabInitializationScriptParameterName]; }
    4848    }
    4949    #endregion
    50 
    51     public TextFileValue InitializationScript {
    52       get { return InitializationScriptParameter.Value; }
    53     }
    5450
    5551    [StorableConstructor]
     
    6662      Parameters.Add(new LookupParameter<StringValue>(QualityVariableParameterName, "The name of the quality variable calculated in the MATLAB script."));
    6763      Parameters.Add(new LookupParameter<TextFileValue>(MatlabEvaluationScriptParameterName, "The path to the MATLAB evaluation script."));
    68       Parameters.Add(new FixedValueParameter<TextFileValue>(InitializationScriptParameterName, "The path to a MATLAB script the should be execute before the evaluation starts.", new TextFileValue()));
     64      Parameters.Add(new LookupParameter<TextFileValue>(MatlabInitializationScriptParameterName, "The path to a MATLAB script the should be execute before the evaluation starts."));
    6965    }
    7066
    71     private MLApp.MLApp matLabConnector;
    72     public override void InitializeState() {
    73       base.InitializeState();
    74       changedDirectory = false;
    75 
    76       if (matLabConnector == null) {
    77         Type matlabtype = Type.GetTypeFromProgID("matlab.application.single");
    78         matLabConnector = (MLApp.MLApp)Activator.CreateInstance(matlabtype);
    79         matLabConnector.Visible = 0;
    80       }
    81 
    82       if (string.IsNullOrEmpty(InitializationScript.Value)) return;
    83       if (!InitializationScript.Exists()) throw new FileNotFoundException(string.Format("The initialization script \"{0}\" cannot be found.", InitializationScript.Value));
    84 
    85       string initScript = InitializationScript.Value;
    86       var directoryName = Path.GetDirectoryName(initScript);
    87       string result = matLabConnector.Execute(string.Format("cd '{0}'", directoryName));
    88       if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
    89 
    90       result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(initScript));
    91       if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
    92     }
    9367
    9468    public override void ClearState() {
    9569      matLabConnector.Execute("clear");
    9670      changedDirectory = false;
     71      executedInitializationScript = false;
    9772      base.ClearState();
    9873    }
    9974
     75    private MLApp.MLApp matLabConnector;
    10076    private bool changedDirectory = false;
     77    private bool executedInitializationScript = false;
    10178    private readonly object locker = new object();
     79
    10280    public override IOperation Apply() {
    103       var evaluationScript = MatlabEvaluationScriptParameter.ActualValue;
    104       if (string.IsNullOrEmpty(evaluationScript.Value)) throw new FileNotFoundException("The evaluation script in the problem is not set.");
    105       if (!evaluationScript.Exists()) throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value));
     81      lock (locker) {
    10682
    107       lock (locker) {
    108         if (matLabConnector == null) InitializeState();
     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;
    10989
    110         string result;
    111         string script = evaluationScript.Value;
     90        var initializationScript = MatlabInitializationScriptParameter.ActualValue;
     91        if (!string.IsNullOrEmpty(initializationScript.Value) && !initializationScript.Exists())
     92          throw new FileNotFoundException(string.Format("The initialization script \"{0}\" cannot be found.", initializationScript.Value));
     93
     94        var evaluationScript = MatlabEvaluationScriptParameter.ActualValue;
     95        if (string.IsNullOrEmpty(evaluationScript.Value))
     96          throw new FileNotFoundException("The evaluation script in the problem is not set.");
     97        if (!evaluationScript.Exists())
     98          throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value));
     99
     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);
     104
     105          result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(initializationScript.Value));
     106          if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
     107
     108          executedInitializationScript = true;
     109        }
    112110
    113111        if (!changedDirectory) {
    114           var directoryName = Path.GetDirectoryName(script);
     112          var directoryName = Path.GetDirectoryName(evaluationScript.Value);
    115113          result = matLabConnector.Execute(string.Format("cd '{0}'", directoryName));
    116114          if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
     
    125123        }
    126124
    127         result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(script));
     125        result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(evaluationScript.Value));
    128126        if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
    129127
Note: See TracChangeset for help on using the changeset viewer.