1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Runtime.InteropServices;
|
---|
6 | using System.Text;
|
---|
7 | using System.Threading.Tasks;
|
---|
8 | using HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api.Types;
|
---|
9 |
|
---|
10 |
|
---|
11 | /* MLApp
|
---|
12 | * https://www.codeproject.com/Articles/594636/Using-Matlab-from-a-Csharp-application
|
---|
13 | * string Execute(string Name)
|
---|
14 | * void Feval(string bstrName, int nargout, out object pvarArgOut, [object arg1], [object arg2], [object arg3], [object arg4], [object arg5], [object arg6], [object arg7], [object arg8], [object arg9], [object arg10], [object arg11], [object arg12], [object arg13], [object arg14], [object arg15], [object arg16], [object arg17], [object arg18], [object arg19], [object arg20], [object arg21], [object arg22], [object arg23], [object arg24], [object arg25], [object arg26], [object arg27], [object arg28], [object arg29], [object arg30], [object arg31], [object arg32])
|
---|
15 | * string GetCharArray(string Name, string Workspace)
|
---|
16 | * void GetFullMatrix(string Name, string Workspace, ref System.Array pr, ref System.Array pi)
|
---|
17 | * dynamic GetVariable(string Name, string Workspace)
|
---|
18 | * void GetWorkspaceData(string Name, string Workspace, out object pdata)
|
---|
19 | * void MaximizeCommandWindow()
|
---|
20 | * void MinimizeCommandWindow()
|
---|
21 | * void PutCharArray(string Name, string Workspace, string charArray)
|
---|
22 | * void PutFullMatrix(string Name, string Workspace, System.Array pr, System.Array pi)
|
---|
23 | * void PutWorkspaceData(string Name, string Workspace, object data)
|
---|
24 | * void Quit()
|
---|
25 | *
|
---|
26 | * MLEval
|
---|
27 | * void XLEval(string bstrName, int nargout, ref object pvarArgOut, int nargin, object varArgIn)
|
---|
28 | * */
|
---|
29 | namespace HeuristicLab.Problems.Instances.DataAnalysis.Regression.Matlab.Api {
|
---|
30 | public class MatlabConnector : IMatlabConnector {
|
---|
31 | private MLApp.MLApp matLabApi;
|
---|
32 | private string workspace;
|
---|
33 |
|
---|
34 | public MatlabConnector(bool showcommandWindow = false, string workspace = "base") {
|
---|
35 | this.workspace = workspace;
|
---|
36 | OpenMatlab(showcommandWindow);
|
---|
37 | }
|
---|
38 |
|
---|
39 | private void OpenMatlab(bool showcommandWindow = false) {
|
---|
40 | Type matlabtype = Type.GetTypeFromProgID("matlab.application.single");
|
---|
41 | matLabApi = (MLApp.MLApp)Activator.CreateInstance(matlabtype);
|
---|
42 | matLabApi.Visible = showcommandWindow ? 1 : 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public void ExecuteScript(string script) {
|
---|
46 | string directoryName = Path.GetDirectoryName(script);
|
---|
47 | if (string.IsNullOrEmpty(directoryName)) {
|
---|
48 | directoryName = Environment.CurrentDirectory;
|
---|
49 | }
|
---|
50 |
|
---|
51 | ChangeDirectory(directoryName);
|
---|
52 | var result = matLabApi.Execute(Path.GetFileNameWithoutExtension(script));
|
---|
53 | if (!string.IsNullOrEmpty(result)) {
|
---|
54 | throw new InvalidOperationException(result);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void ChangeDirectory(string directoryName) {
|
---|
59 | var result = matLabApi.Execute(string.Format("cd '{0}'", directoryName));
|
---|
60 | if (!string.IsNullOrEmpty(result)) {
|
---|
61 | throw new InvalidOperationException(result);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void Execute(string command) {
|
---|
66 | matLabApi.Execute(command);
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | public IDictionary<string, MLDatatype> GetVariablesFromScript(string script) {
|
---|
71 | var variables = new Dictionary<string, MLDatatype>();
|
---|
72 | ExecuteScript(script);
|
---|
73 |
|
---|
74 | Execute("variables=whos();nrOfVariables=size(variables)");
|
---|
75 | var len = ((double[,])GetVariable("nrOfVariables"))[0, 0];
|
---|
76 |
|
---|
77 | for (int i = 1; i <= len; i++) {
|
---|
78 | var varName = GetVariable(string.Format("variables({0}).name", i)) as string;
|
---|
79 | var varSize = GetVariable(string.Format("variables({0}).size", i));
|
---|
80 | var varClass = GetVariable(string.Format("variables({0}).class", i)) as string;
|
---|
81 | // At the moment only timeseries are supported.
|
---|
82 | if (varClass == "timeseries") {
|
---|
83 | variables.Add(varName, MLDatatype.Timeseries);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | return variables;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | public MLTimeseries GetTimeseries(string varName) {
|
---|
92 | object times = GetVariable(varName + ".Time");
|
---|
93 | object data = GetVariable(varName + ".Data");
|
---|
94 | if (times == null || data == null) {
|
---|
95 | return null;
|
---|
96 | }
|
---|
97 |
|
---|
98 | return new MLTimeseries(varName, times, data);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public MLDouble GetDouble(string varName) {
|
---|
102 | return new MLDouble(varName, GetVariable(varName));
|
---|
103 | }
|
---|
104 |
|
---|
105 | #region Helper for getting the values of variables from the matlab api.
|
---|
106 | public object GetVariable(string varName) {
|
---|
107 | object value = null;
|
---|
108 | try {
|
---|
109 | if (IsStruct(varName)) {
|
---|
110 | value = GetValueOfStruct(varName);
|
---|
111 | } else {
|
---|
112 | matLabApi.GetWorkspaceData(varName, workspace, out value);
|
---|
113 | }
|
---|
114 | } catch (COMException) {
|
---|
115 | throw new ArgumentException(string.Format("The variable {0} does not exist in the current workspace {1}.", varName, workspace));
|
---|
116 | }
|
---|
117 | var t = value.GetType();
|
---|
118 | return value;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | private bool IsStruct(string varName) {
|
---|
123 | return varName.Contains(".");
|
---|
124 | }
|
---|
125 |
|
---|
126 | private object GetValueOfStruct(string varName) {
|
---|
127 | object value;
|
---|
128 | matLabApi.Execute(string.Format("tmp = {0}", varName));
|
---|
129 | matLabApi.GetWorkspaceData("tmp", workspace, out value);
|
---|
130 | return value;
|
---|
131 | }
|
---|
132 | #endregion
|
---|
133 |
|
---|
134 | public void Dispose() {
|
---|
135 | try {
|
---|
136 | if (matLabApi != null) {
|
---|
137 | matLabApi.Execute("exit");
|
---|
138 | }
|
---|
139 | } catch (COMException) {
|
---|
140 | } finally {
|
---|
141 | matLabApi = null;
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|