#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.IO; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ParameterOptimization; namespace HeuristicLab.Problems.ExternalEvaluation.Matlab { [Item("MatLabParameterVectorEvaluator", "An evaluator which takes a parameter vector and returns a quality value, calculated by a Scilab script.")] [StorableClass] public sealed class MatlabParameterVectorEvaluator : ParameterVectorEvaluator { private const string QualityVariableParameterName = "QualityVariableName"; private const string MatlabEvaluationScriptParameterName = "MatlabEvaluationScript"; private const string InitializationScriptParameterName = "InitializationScript"; #region parameters public ILookupParameter QualityVariableParameter { get { return (ILookupParameter)Parameters[QualityVariableParameterName]; } } public ILookupParameter MatlabEvaluationScriptParameter { get { return (ILookupParameter)Parameters[MatlabEvaluationScriptParameterName]; } } public IFixedValueParameter InitializationScriptParameter { get { return (IFixedValueParameter)Parameters[InitializationScriptParameterName]; } } #endregion public TextFile InitializationScript { get { return InitializationScriptParameter.Value; } } [StorableConstructor] private MatlabParameterVectorEvaluator(bool deserializing) : base(deserializing) { } private MatlabParameterVectorEvaluator(MatlabParameterVectorEvaluator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new MatlabParameterVectorEvaluator(this, cloner); } public MatlabParameterVectorEvaluator() : base() { Parameters.Add(new LookupParameter(QualityVariableParameterName, "The name of the quality variable of the Matlab script.")); Parameters.Add(new LookupParameter(MatlabEvaluationScriptParameterName, "The path to the Matlab evaluation script.")); Parameters.Add(new FixedValueParameter(InitializationScriptParameterName, "The path to a Matlab script the should be execute before the evaluation starts.", new TextFile())); } private MLApp.IMLApp matLabConnector; public override void InitializeState() { base.InitializeState(); Type matlabtype = Type.GetTypeFromProgID("matlab.application.single"); matLabConnector = (MLApp.IMLApp)Activator.CreateInstance(matlabtype); if (string.IsNullOrEmpty(InitializationScript.Value)) return; if (!InitializationScript.Exists()) throw new FileNotFoundException(string.Format("The initialization script \"{0}\" cannot be found.", InitializationScript.Value)); string result = matLabConnector.Execute(string.Format("run({0});", InitializationScript.Value)); //if (result != 0) ThrowSciLabException(InitializationScript.Value, result); } public override void ClearState() { base.ClearState(); matLabConnector.Quit(); } private readonly object locker = new object(); public override IOperation Apply() { var evaluationScript = MatlabEvaluationScriptParameter.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)); lock (locker) { string result; var parameterVector = ParameterVectorParameter.ActualValue; var parameterNames = ParameterNamesParameter.ActualValue; if (parameterNames.Any(string.IsNullOrEmpty)) throw new ArgumentException("Not all parameter names are provided."); for (int i = 0; i < ProblemSizeParameter.ActualValue.Value; i++) { matLabConnector.PutWorkspaceData(parameterNames[i], "base", parameterVector[i]); } string script = MatlabEvaluationScriptParameter.ActualValue.Value; result = matLabConnector.Execute(string.Format("run({0});", script)); //if (result != 0) ThrowSciLabException(script, result); string qualityVariableName = QualityVariableParameter.ActualValue.Value; var quality = matLabConnector.GetVariable(qualityVariableName, "base"); //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)); if (double.IsNaN(quality)) quality = double.MaxValue; if (double.IsInfinity(quality)) quality = double.MaxValue; QualityParameter.ActualValue = new DoubleValue(quality); return base.Apply(); } } //private void ThrowSciLabException(string fileName, int errorCode) { // const string code = "errorMsg = lasterror();"; // int result = DotNetScilab.Scilab.Instance.SendScilabJob(code); // if (result != 0) throw new InvalidOperationException(string.Format("An error occured during the execution of the Scilab script {0}.", fileName)); // string errorMessage = DotNetScilab.Scilab.Instance.readNamedMatrixOfString("errorMsg")[0]; // string message = string.Format("The error {1} occured during the execution of the Scilab script {0}. \r\n\r\n {2}", fileName, errorCode, errorMessage); // throw new InvalidOperationException(message); //} } }