Changeset 10595 for branches/HeuristicLab.ExternalEvaluation Scientific/HeuristicLab.Problems.ExternalEvaluation.Scilab/3.3/ScilabParameterVectorEvaluator.cs
- Timestamp:
- 03/16/14 16:07:44 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.ExternalEvaluation Scientific/HeuristicLab.Problems.ExternalEvaluation.Scilab/3.3/ScilabParameterVectorEvaluator.cs
r10594 r10595 36 36 private const string QualityVariableParameterName = "QualityVariableName"; 37 37 private const string ScilabEvaluationScriptParameterName = "ScilabEvaluationScript"; 38 private const string InitializationScriptParameterName = "InitializationScript";38 private const string ScilabInitializationScriptParameterName = "ScilabInitializationScript"; 39 39 40 40 #region parameters … … 45 45 get { return (ILookupParameter<TextFileValue>)Parameters[ScilabEvaluationScriptParameterName]; } 46 46 } 47 public I FixedValueParameter<TextFileValue>InitializationScriptParameter {48 get { return (I FixedValueParameter<TextFileValue>)Parameters[InitializationScriptParameterName]; }47 public ILookupParameter<TextFileValue> ScilabInitializationScriptParameter { 48 get { return (ILookupParameter<TextFileValue>)Parameters[ScilabInitializationScriptParameterName]; } 49 49 } 50 50 #endregion 51 52 public TextFileValue InitializationScript {53 get { return InitializationScriptParameter.Value; }54 }55 51 56 52 [StorableConstructor] … … 67 63 Parameters.Add(new LookupParameter<StringValue>(QualityVariableParameterName, "The name of the quality variable of the Scilab script.")); 68 64 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.")); 86 66 } 87 67 88 68 private readonly object locker = new object(); 69 private static DotNetScilab.Scilab scilab = null; 70 private bool startedScilab = false; 71 89 72 public override IOperation Apply() { 90 73 var evaluationScript = ScilabEvaluationScriptParameter.ActualValue; … … 92 75 if (!evaluationScript.Exists()) throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value)); 93 76 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; 94 81 //Scilab is used via a c++ wrapper that calls static methods. Hence it is not possible to parallelize the evaluation. 95 82 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 97 95 var parameterVector = ParameterVectorParameter.ActualValue; 98 96 var parameterNames = ParameterNamesParameter.ActualValue; 99 97 if (parameterNames.Any(string.IsNullOrEmpty)) throw new ArgumentException("Not all parameter names are provided."); 100 var scilab = DotNetScilab.Scilab.Instance;101 98 102 99 for (int i = 0; i < ProblemSizeParameter.ActualValue.Value; i++) { 103 100 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 + ").");105 101 if (result != 0) ThrowSciLabException("setting parameter " + parameterNames[i], result); 106 102 } … … 123 119 } 124 120 121 public override void ClearState() { 122 base.ClearState(); 123 if (startedScilab) 124 scilab = null; 125 startedScilab = false; 126 } 127 125 128 private void ThrowSciLabException(string fileName, int errorCode) { 126 129 const string code = "errorMsg = lasterror();";
Note: See TracChangeset
for help on using the changeset viewer.