Changeset 10595


Ignore:
Timestamp:
03/16/14 16:07:44 (6 years ago)
Author:
mkommend
Message:

#2082: Implemented reviewer comments for the Scilab parameter optimization problem & evaluator.

Location:
branches/HeuristicLab.ExternalEvaluation Scientific/HeuristicLab.Problems.ExternalEvaluation.Scilab/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.ExternalEvaluation Scientific/HeuristicLab.Problems.ExternalEvaluation.Scilab/3.3/ScilabParameterOptimizationProblem.cs

    r10594 r10595  
    3434    private const string QualityVariableParameterName = "QualityVariableName";
    3535    private const string ScilabEvaluationScriptParameterName = "ScilabEvaluationScript";
     36    private const string ScilabInitializationScriptParameterName = "ScilabInitializationScript";
    3637
    3738    #region parameters
     
    4142    public IFixedValueParameter<TextFileValue> ScilabEvaluationScriptParameter {
    4243      get { return (IFixedValueParameter<TextFileValue>)Parameters[ScilabEvaluationScriptParameterName]; }
     44    }
     45    public IFixedValueParameter<TextFileValue> ScilabInitializationScriptParameter {
     46      get { return (IFixedValueParameter<TextFileValue>)Parameters[ScilabInitializationScriptParameterName]; }
    4347    }
    4448    #endregion
     
    5256      get { return ScilabEvaluationScriptParameter.Value; }
    5357    }
     58    public TextFileValue ScilabInitializationScript {
     59      get { return ScilabInitializationScriptParameter.Value; }
     60    }
     61
    5462    #endregion
    5563
     
    6775      Parameters.Add(new FixedValueParameter<StringValue>(QualityVariableParameterName, "The name of the quality variable of the Scilab script.", new StringValue("quality")));
    6876      Parameters.Add(new FixedValueParameter<TextFileValue>(ScilabEvaluationScriptParameterName, "The path to the Scilab evaluation script.", new TextFileValue()));
     77      Parameters.Add(new FixedValueParameter<TextFileValue>(ScilabInitializationScriptParameterName, "The path to a Scilab script the should be execute before the evaluation starts.", new TextFileValue()));
    6978
    7079      ScilabEvaluationScript.FileDialogFilter = @"Scilab Scripts|*.sce|All files|*.*";
     80      ScilabInitializationScript.FileDialogFilter = @"Scilab Scripts|*.sce|All files|*.*";
    7181    }
    7282  }
  • branches/HeuristicLab.ExternalEvaluation Scientific/HeuristicLab.Problems.ExternalEvaluation.Scilab/3.3/ScilabParameterVectorEvaluator.cs

    r10594 r10595  
    3636    private const string QualityVariableParameterName = "QualityVariableName";
    3737    private const string ScilabEvaluationScriptParameterName = "ScilabEvaluationScript";
    38     private const string InitializationScriptParameterName = "InitializationScript";
     38    private const string ScilabInitializationScriptParameterName = "ScilabInitializationScript";
    3939
    4040    #region parameters
     
    4545      get { return (ILookupParameter<TextFileValue>)Parameters[ScilabEvaluationScriptParameterName]; }
    4646    }
    47     public IFixedValueParameter<TextFileValue> InitializationScriptParameter {
    48       get { return (IFixedValueParameter<TextFileValue>)Parameters[InitializationScriptParameterName]; }
     47    public ILookupParameter<TextFileValue> ScilabInitializationScriptParameter {
     48      get { return (ILookupParameter<TextFileValue>)Parameters[ScilabInitializationScriptParameterName]; }
    4949    }
    5050    #endregion
    51 
    52     public TextFileValue InitializationScript {
    53       get { return InitializationScriptParameter.Value; }
    54     }
    5551
    5652    [StorableConstructor]
     
    6763      Parameters.Add(new LookupParameter<StringValue>(QualityVariableParameterName, "The name of the quality variable of the Scilab script."));
    6864      Parameters.Add(new LookupParameter<TextFileValue>(ScilabEvaluationScriptParameterName, "The path to the Scilab evaluation script."));
    69       Parameters.Add(new FixedValueParameter<TextFileValue>(InitializationScriptParameterName, "The path to a Scilab script the should be execute before the evaluation starts.", new TextFileValue()));
    70 
    71       InitializationScript.FileDialogFilter = @"Scilab Scripts|*.sce|All files|*.*";
    72     }
    73 
    74     //[StorableHook(HookType.AfterDeserialization)]
    75     //private void AfterDeserialization() {
    76     //  InitializeState();
    77     //}
    78 
    79     public override void InitializeState() {
    80       base.InitializeState();
    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       int result = DotNetScilab.Scilab.Instance.execScilabScript(InitializationScript.Value);
    85       if (result != 0) ThrowSciLabException(InitializationScript.Value, result);
     65      Parameters.Add(new LookupParameter<TextFileValue>(ScilabInitializationScriptParameterName, "The path to a Scilab script the should be execute before the evaluation starts."));
    8666    }
    8767
    8868    private readonly object locker = new object();
     69    private static DotNetScilab.Scilab scilab = null;
     70    private bool startedScilab = false;
     71
    8972    public override IOperation Apply() {
    9073      var evaluationScript = ScilabEvaluationScriptParameter.ActualValue;
     
    9275      if (!evaluationScript.Exists()) throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value));
    9376
     77      var initializationScript = ScilabInitializationScriptParameter.ActualValue;
     78      if (!string.IsNullOrEmpty(initializationScript.Value) && !initializationScript.Exists()) throw new FileNotFoundException(string.Format("The initialization script \"{0}\" cannot be found.", initializationScript.Value));
     79
     80      int result;
    9481      //Scilab is used via a c++ wrapper that calls static methods. Hence it is not possible to parallelize the evaluation.
    9582      lock (locker) {
    96         int result;
     83        //initialize scilab and execute initialization script
     84        if (scilab == null) {
     85          startedScilab = true;
     86          scilab = new DotNetScilab.Scilab(false);
     87          if (!string.IsNullOrEmpty(initializationScript.Value)) {
     88            result = DotNetScilab.Scilab.Instance.execScilabScript(initializationScript.Value);
     89            if (result != 0) ThrowSciLabException(initializationScript.Value, result);
     90          }
     91        } else if (!startedScilab) {
     92          throw new InvalidOperationException("Could not run multiple optimization algorithms in parallel.");
     93        }
     94
    9795        var parameterVector = ParameterVectorParameter.ActualValue;
    9896        var parameterNames = ParameterNamesParameter.ActualValue;
    9997        if (parameterNames.Any(string.IsNullOrEmpty)) throw new ArgumentException("Not all parameter names are provided.");
    100         var scilab = DotNetScilab.Scilab.Instance;
    10198
    10299        for (int i = 0; i < ProblemSizeParameter.ActualValue.Value; i++) {
    103100          result = scilab.createNamedMatrixOfDouble(parameterNames[i], 1, 1, new double[] { parameterVector[i] });
    104           //if (result != 0) throw new InvalidOperationException("Error while setting the parameter " + parameterNames[i] + " to " + parameterVector[i] + "  (ErrorCode: " + result + ").");
    105101          if (result != 0) ThrowSciLabException("setting parameter " + parameterNames[i], result);
    106102        }
     
    123119    }
    124120
     121    public override void ClearState() {
     122      base.ClearState();
     123      if (startedScilab)
     124        scilab = null;
     125      startedScilab = false;
     126    }
     127
    125128    private void ThrowSciLabException(string fileName, int errorCode) {
    126129      const string code = "errorMsg = lasterror();";
Note: See TracChangeset for help on using the changeset viewer.