我通过这个包装器在 c# 中使用优化库(NLOpt,为 c++ 编写):
https://github.com/roryclune/NLOptDotNet
当向问题添加超过 15 个约束时,我得到一个 SEHException。通过将 FunctionDelegate 传递给求解器来添加约束。
这是异常堆栈跟踪: System.Runtime.InteropServices.SEHException (0x80004005):外部组件已引发异常。在 _CxxThrowException(Void* , s _ThrowInfo* ) 在 nlopt.opt.mythrow(opt* , nlopt_result ret) 在 nlopt.opt.add_equality_constraint(opt* , IntPtr vf, Void* f_data, Double tol) 在 NLOptDotNet.NLOptWrapper.AddFunction( FunctionType ftype, FunctionDelegate funcDelegate, Object data, Double tol)
我是否遗漏了某些东西,或者它一定是包装器\库中的错误?这是代码,我在 --->>> 行得到了异常:
public NetDescriptor optimize(NetDescriptor netDescriptor){
//The optimization algorithm
Algorithm main_alg = Algorithm.LD_SLSQP;
//Create optimization object, setting algorithm type and number of variables
NLOptWrapper wrapper = new NLOptWrapper(main_alg, arcsNumber*2);
wrapper.VERBOSE_OUTPUT = false;
//Set stopping criteria
wrapper.MaxTime = 60;
wrapper.XTolRelative = 1e-8;
wrapper.FTolAbsolute = 1e-8;
wrapper.MaxEval = 5000;
wrapper.StopVal = 0;
//Set lower and upper bounds
double[] x = new double[arcsNumber*2];
for(int i = 0; i < arcsNumber*2; i++)
x[i] = 1e-8;
wrapper.SetLowerBounds(x);
for(int i = 0; i < arcsNumber*2; i+=2) {
x[i] = 1e8;
x[i+1] = 1;
}
wrapper.SetUpperBounds(x);
//FLOW AND MIX BALANCE CONSTRAINTS
for (int i = 1; i < (nodesNumber - 1); i++) {
ArrayList inArcs = (nodes[i] as Node).getInArcs();
ArrayList outArcs = (nodes[i] as Node).getOutArcs();
int[] coefficients = new int[arcsNumber];
for (int j = 0; j < arcsNumber; j++)
coefficients[j] = 0;
foreach(Arc a in inArcs)
coefficients[a.getIndex()] = 1;
foreach (Arc a in outArcs)
coefficients[a.getIndex()] = -1;
flowBalanceData FBD = new flowBalanceData(coefficients);
-->>> wrapper.AddEqualityConstraint(new FunctionDelegate(flowBalanceConstraint), FBD, 1e-2);