我有一个函数包装器,如下所示:这里我传入一组系数( inputArr )和一个包含2个双数组的元组。
预期输出是“ WminObjectivefunction ”被调用的次数,并且还返回“ FuncValue ”,这基本上是“ WminObjectivefunction “评估在” inputArr “。每次调用“ WrapFunction ”时,传递的数组“ inputArr ”都会有所不同。
public void WrapFunction(out int ncalls, out double FuncValue, double[] inputArr, Tuple<List<double>, List<double>> arguments)
{
int calls = 0;//Number_of_FunctionEvaluations
// MASTER class instance
Master prismpy = new Master();
calls += 1;//Number_of_FunctionEvaluations_Increment
ncalls = calls;//Return_Number_of_FunctionEvaluations
FuncValue = prismpy.WminObjectivefunction(inputArr, arguments.Item1, arguments.Item2);//Return_FunctionValuation
}
问题1:我想将 FuncValue 存储为Array fsim 中的元素,如何命令 WrapFunction 将值存储为由索引表示的数组元素。以下是我尝试过的,错误是:不能隐式地将'void'转换为'double'
// FSIM: put array in array of arrays
double[] fsim = new double[5];
int fcall;
fsim[0] = WrapFunction(out fcall, out fsim[0], _x0, args);
问题2:是否有必要每次都使用所有输出参数调用WrapFunction?如果我只想获得这两个输出中的任何一个,有没有办法呢?
答案 0 :(得分:1)
不确定你的意思,但我猜是这样的:
public double WrapFunction(out int ncalls, out double FuncValue, double[] inputArr, Tuple<List<double>, List<double>> arguments)
{
int calls = 0;//Number_of_FunctionEvaluations
// MASTER class instance
Master prismpy = new Master();
calls += 1;//Number_of_FunctionEvaluations_Increment
ncalls = calls;//Return_Number_of_FunctionEvaluations
FuncValue = prismpy.WminObjectivefunction(inputArr, arguments.Item1, arguments.Item2);//Return_FunctionValuation
return FuncValue;
// NOTE: the FuncValue parameter may be redundant -- RBarryYoung
}
这是另一个版本,反映了一些评论/讨论:
int calls = 0;//Number_of_FunctionEvaluations
public double WrapFunction(out int ncalls, double[] inputArr, Tuple<List<double>, List<double>> arguments)
{
// MASTER class instance
Master prismpy = new Master();
calls += 1;//Number_of_FunctionEvaluations_Increment
ncalls = calls;//Return_Number_of_FunctionEvaluations
return prismpy.WminObjectivefunction(inputArr, arguments.Item1, arguments.Item2);//Return_FunctionValuation
}