[9690] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.IO;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Problems.ParameterOptimization;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.ExternalEvaluation.Matlab {
|
---|
| 32 | [Item("MatLabParameterVectorEvaluator", "An evaluator which takes a parameter vector and returns a quality value, calculated by a Scilab script.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | public sealed class MatlabParameterVectorEvaluator : ParameterVectorEvaluator {
|
---|
| 35 | private const string QualityVariableParameterName = "QualityVariableName";
|
---|
| 36 | private const string MatlabEvaluationScriptParameterName = "MatlabEvaluationScript";
|
---|
| 37 | private const string InitializationScriptParameterName = "InitializationScript";
|
---|
| 38 |
|
---|
| 39 | #region parameters
|
---|
| 40 | public ILookupParameter<StringValue> QualityVariableParameter {
|
---|
| 41 | get { return (ILookupParameter<StringValue>)Parameters[QualityVariableParameterName]; }
|
---|
| 42 | }
|
---|
[9715] | 43 | public ILookupParameter<TextFileValue> MatlabEvaluationScriptParameter {
|
---|
| 44 | get { return (ILookupParameter<TextFileValue>)Parameters[MatlabEvaluationScriptParameterName]; }
|
---|
[9690] | 45 | }
|
---|
[9715] | 46 | public IFixedValueParameter<TextFileValue> InitializationScriptParameter {
|
---|
| 47 | get { return (IFixedValueParameter<TextFileValue>)Parameters[InitializationScriptParameterName]; }
|
---|
[9690] | 48 | }
|
---|
| 49 | #endregion
|
---|
| 50 |
|
---|
[9715] | 51 | public TextFileValue InitializationScript {
|
---|
[9690] | 52 | get { return InitializationScriptParameter.Value; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | [StorableConstructor]
|
---|
| 56 | private MatlabParameterVectorEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 57 | private MatlabParameterVectorEvaluator(MatlabParameterVectorEvaluator original, Cloner cloner)
|
---|
| 58 | : base(original, cloner) {
|
---|
| 59 | }
|
---|
| 60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 61 | return new MatlabParameterVectorEvaluator(this, cloner);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public MatlabParameterVectorEvaluator()
|
---|
| 65 | : base() {
|
---|
| 66 | Parameters.Add(new LookupParameter<StringValue>(QualityVariableParameterName, "The name of the quality variable of the Matlab script."));
|
---|
[9715] | 67 | 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()));
|
---|
[9690] | 69 | }
|
---|
| 70 |
|
---|
[9715] | 71 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 72 | private void AfterDeserialization() {
|
---|
| 73 | InitializeState();
|
---|
| 74 | }
|
---|
[9690] | 75 |
|
---|
[9715] | 76 | private MLApp.MLApp matLabConnector;
|
---|
[9690] | 77 | public override void InitializeState() {
|
---|
| 78 | base.InitializeState();
|
---|
[9715] | 79 | changedDirectory = false;
|
---|
[9690] | 80 |
|
---|
[9715] | 81 | if (matLabConnector == null) {
|
---|
| 82 | Type matlabtype = Type.GetTypeFromProgID("matlab.application.single");
|
---|
| 83 | matLabConnector = (MLApp.MLApp)Activator.CreateInstance(matlabtype);
|
---|
| 84 | matLabConnector.Visible = 0;
|
---|
| 85 | }
|
---|
[9690] | 86 |
|
---|
| 87 | if (string.IsNullOrEmpty(InitializationScript.Value)) return;
|
---|
| 88 | if (!InitializationScript.Exists()) throw new FileNotFoundException(string.Format("The initialization script \"{0}\" cannot be found.", InitializationScript.Value));
|
---|
[9715] | 89 |
|
---|
| 90 | string initScript = InitializationScript.Value;
|
---|
| 91 | string result = matLabConnector.Execute("cd " + Path.GetDirectoryName(initScript));
|
---|
| 92 | if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
|
---|
| 93 |
|
---|
| 94 | result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(initScript));
|
---|
| 95 | if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
|
---|
[9690] | 96 | }
|
---|
| 97 |
|
---|
| 98 | public override void ClearState() {
|
---|
[9715] | 99 | matLabConnector.Execute("clear");
|
---|
| 100 | changedDirectory = false;
|
---|
[9690] | 101 | base.ClearState();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[9715] | 104 | private bool changedDirectory = false;
|
---|
[9690] | 105 | private readonly object locker = new object();
|
---|
| 106 | public override IOperation Apply() {
|
---|
| 107 | var evaluationScript = MatlabEvaluationScriptParameter.ActualValue;
|
---|
| 108 | if (string.IsNullOrEmpty(evaluationScript.Value)) throw new FileNotFoundException("The evaluation script in the problem is not set.");
|
---|
| 109 | if (!evaluationScript.Exists()) throw new FileNotFoundException(string.Format("The evaluation script \"{0}\" cannot be found.", evaluationScript.Value));
|
---|
| 110 |
|
---|
| 111 | lock (locker) {
|
---|
| 112 | string result;
|
---|
[9715] | 113 | string script = evaluationScript.Value;
|
---|
| 114 |
|
---|
| 115 | if (!changedDirectory) {
|
---|
| 116 | result = matLabConnector.Execute("cd " + Path.GetDirectoryName(script));
|
---|
| 117 | if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
|
---|
| 118 | changedDirectory = true;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[9690] | 121 | var parameterVector = ParameterVectorParameter.ActualValue;
|
---|
| 122 | var parameterNames = ParameterNamesParameter.ActualValue;
|
---|
| 123 |
|
---|
| 124 | for (int i = 0; i < ProblemSizeParameter.ActualValue.Value; i++) {
|
---|
| 125 | matLabConnector.PutWorkspaceData(parameterNames[i], "base", parameterVector[i]);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[9715] | 128 | result = matLabConnector.Execute(Path.GetFileNameWithoutExtension(script));
|
---|
| 129 | if (!string.IsNullOrEmpty(result)) throw new InvalidOperationException(result);
|
---|
[9690] | 130 |
|
---|
| 131 | string qualityVariableName = QualityVariableParameter.ActualValue.Value;
|
---|
| 132 | var quality = matLabConnector.GetVariable(qualityVariableName, "base");
|
---|
| 133 |
|
---|
| 134 | if (double.IsNaN(quality)) quality = double.MaxValue;
|
---|
| 135 | if (double.IsInfinity(quality)) quality = double.MaxValue;
|
---|
| 136 |
|
---|
| 137 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
| 138 | return base.Apply();
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[9715] | 143 |
|
---|