public override void ClearState() { base.ClearState(); if (startedScilab) { scilab = null; } startedScilab = false; }
public override IOperation Apply() { var evaluationScript = ScilabEvaluationScriptParameter.ActualValue; if (string.IsNullOrEmpty(evaluationScript.Value)) throw new FileNotFoundException("The evaluation script in the problem is not set."); if (!evaluationScript.Exists()) throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value)); var initializationScript = ScilabInitializationScriptParameter.ActualValue; if (!string.IsNullOrEmpty(initializationScript.Value) && !initializationScript.Exists()) throw new FileNotFoundException(string.Format("The initialization script \"{0}\" cannot be found.", initializationScript.Value)); int result; // Scilab is used via a c++ wrapper that calls static methods. Hence it is not possible to parallelize the evaluation. // It is also not possible to run multiple algorithms solving separate Scilab optimization problems at the same time. lock (locker) { //initialize scilab and execute initialization script if (scilab == null) { startedScilab = true; scilab = new ScilabConnector(false); if (!string.IsNullOrEmpty(initializationScript.Value)) { result = scilab.execScilabScript(initializationScript.Value); if (result != 0) ThrowSciLabException(initializationScript.Value, result); } } else if (!startedScilab) { throw new InvalidOperationException("Could not run multiple optimization algorithms in parallel."); } var parameterVector = ParameterVectorParameter.ActualValue; var parameterNames = ParameterNamesParameter.ActualValue; if (parameterNames.Any(string.IsNullOrEmpty)) throw new ArgumentException("Not all parameter names are provided. Change the 'ParameterNames' parameter in the 'Problem' tab."); if (ProblemSizeParameter.ActualValue.Value != parameterVector.Length || ProblemSizeParameter.ActualValue.Value != parameterNames.Length) throw new ArgumentException("The size of the parameter vector or the parameter names vector does not match the problem size."); for (int i = 0; i < ProblemSizeParameter.ActualValue.Value; i++) { result = scilab.createNamedMatrixOfDouble(parameterNames[i], 1, 1, new double[] { parameterVector[i] }); if (result != 0) ThrowSciLabException("setting parameter " + parameterNames[i], result); } string script = ScilabEvaluationScriptParameter.ActualValue.Value; result = scilab.execScilabScript(script); if (result != 0) ThrowSciLabException(script, result); string qualityVariableName = QualityVariableParameter.ActualValue.Value; double[] values = scilab.readNamedMatrixOfDouble(qualityVariableName); if (values == null) throw new InvalidOperationException(string.Format("Could not find the variable \"{0}\" in the Scilab workspace, that should hold the quality value.", qualityVariableName)); double quality = values[0]; var worstQualityValue = MaximizationParameter.ActualValue.Value ? double.MinValue : double.MaxValue; if (double.IsNaN(quality)) quality = worstQualityValue; if (double.IsInfinity(quality)) quality = worstQualityValue; QualityParameter.ActualValue = new DoubleValue(quality); return base.Apply(); } }
public override void ClearState() { base.ClearState(); if (startedScilab) scilab = null; startedScilab = false; }
public override IOperation Apply() { var evaluationScript = ScilabEvaluationScriptParameter.ActualValue; if (string.IsNullOrEmpty(evaluationScript.Value)) { throw new FileNotFoundException("The evaluation script in the problem is not set."); } if (!evaluationScript.Exists()) { throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value)); } var initializationScript = ScilabInitializationScriptParameter.ActualValue; if (!string.IsNullOrEmpty(initializationScript.Value) && !initializationScript.Exists()) { throw new FileNotFoundException(string.Format("The initialization script \"{0}\" cannot be found.", initializationScript.Value)); } int result; // Scilab is used via a c++ wrapper that calls static methods. Hence it is not possible to parallelize the evaluation. // It is also not possible to run multiple algorithms solving separate Scilab optimization problems at the same time. lock (locker) { //initialize scilab and execute initialization script if (scilab == null) { startedScilab = true; scilab = new ScilabConnector(false); if (!string.IsNullOrEmpty(initializationScript.Value)) { result = scilab.execScilabScript(initializationScript.Value); if (result != 0) { ThrowSciLabException(initializationScript.Value, result); } } } else if (!startedScilab) { throw new InvalidOperationException("Could not run multiple optimization algorithms in parallel."); } var parameterVector = ParameterVectorParameter.ActualValue; var parameterNames = ParameterNamesParameter.ActualValue; if (parameterNames.Any(string.IsNullOrEmpty)) { throw new ArgumentException("Not all parameter names are provided. Change the 'ParameterNames' parameter in the 'Problem' tab."); } if (ProblemSizeParameter.ActualValue.Value != parameterVector.Length || ProblemSizeParameter.ActualValue.Value != parameterNames.Length) { throw new ArgumentException("The size of the parameter vector or the parameter names vector does not match the problem size."); } for (int i = 0; i < ProblemSizeParameter.ActualValue.Value; i++) { result = scilab.createNamedMatrixOfDouble(parameterNames[i], 1, 1, new double[] { parameterVector[i] }); if (result != 0) { ThrowSciLabException("setting parameter " + parameterNames[i], result); } } string script = ScilabEvaluationScriptParameter.ActualValue.Value; result = scilab.execScilabScript(script); if (result != 0) { ThrowSciLabException(script, result); } string qualityVariableName = QualityVariableParameter.ActualValue.Value; double[] values = scilab.readNamedMatrixOfDouble(qualityVariableName); if (values == null) { throw new InvalidOperationException(string.Format("Could not find the variable \"{0}\" in the Scilab workspace, that should hold the quality value.", qualityVariableName)); } double quality = values[0]; var worstQualityValue = MaximizationParameter.ActualValue.Value ? double.MinValue : double.MaxValue; if (double.IsNaN(quality)) { quality = worstQualityValue; } if (double.IsInfinity(quality)) { quality = worstQualityValue; } QualityParameter.ActualValue = new DoubleValue(quality); return(base.Apply()); } }