环境:VS2017+MATLAB R2017a
①打开上次我们生成的BPClass.cs文件(目录:~//bin/BP/for_testing),得在C#程序中引用生成的dll文件(目录:~//bin/BP/for_redistribution_files_only)
②Methods里面写了调用方法,并给出了详尽的注释(这里的numArgsOut是输出参数的数量,可以看注释)
///
/// Provides the standard 7-input MWArray interface to the BP MATLAB function.
///
///
/// M-Documentation:
/// 以下六行是BP网络最核心的程序
/// 他们是权值(阈值)依据能量函数负梯度下降原理所作的每一步动态调整量
///
/// The number of output arguments to return.
/// Input argument #1
/// Input argument #2
/// Input argument #3
/// Input argument #4
/// Input argument #5
/// Input argument #6
/// Input argument #7
/// An Array of length "numArgsOut" containing the output
/// arguments.
///
public MWArray[] BP(int numArgsOut, MWArray gdp, MWArray steelProduction, MWArray
autoProduction, MWArray highwayMileage, MWArray outputAmount, MWArray
outputMoney, MWArray forecast)
{
return mcr.EvaluateFunction(numArgsOut, "BP", gdp, steelProduction, autoProduction, highwayMileage, outputAmount, outputMoney, forecast);
}
因为MATLAB中定义的函数是这样子的:
function [输出变量] = 函数名称(输入变量)
function ForecastResult=BP(gdp,steelProduction,autoProduction,highwayMileage,outputAmount,outputMoney,forecast)
所以我是这样调用的:
for (int q = 0; q < 20; q++)
{
gdpPre[q] = arr[q, 0];
steelProductionPre[q] = arr[q, 1];
autoProductionPre[q] = arr[q, 2];
highwayMileagePre[q] = arr[q, 3];
outputAmountPre[q] = arr[q, 4];
outputMoneyPre[q] = arr[q, 5];
}
MWNumericArray gdp = gdpPre;
MWNumericArray steelProduction = steelProductionPre;
MWNumericArray autoProduction = autoProductionPre;
MWNumericArray highwayMileage = highwayMileagePre;
MWNumericArray outputAmount = outputAmountPre;
MWNumericArray outputMoney = outputMoneyPre;
MWNumericArray forecast = new MWNumericArray(4, 1, new double[] { Convert.ToDouble(GDPtextBox.Text.Trim()), Convert.ToDouble(steelProductiontextBox.Text.Trim()), Convert.ToDouble(autoProductiontextBox.Text.Trim()), Convert.ToDouble(highwayMileagetextBox.Text.Trim()) });
BP.BPClass function = new BP.BPClass();
MWArray[] result = function.BP(1, (MWArray)gdp, (MWArray)steelProduction, (MWArray)autoProduction, (MWArray)highwayMileage, (MWArray)outputAmount, (MWArray)outputMoney, (MWArray)forecast);
MWNumericArray x = (MWNumericArray)result[0];
double[,] y = new double[1, 2];
y = (double[,])x.ToArray(MWArrayComponent.Real);
MessageBox.Show("预测销售数量:" + Convert.ToDouble(y[0, 0].ToString()) + "万件,预测销售利润:" + Convert.ToDouble(y[0, 1].ToString()) + "万元.", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);