我如何在C#中模拟Microsoft Excel的Solverfunction(GRG非线性)?

我有一个约束的非线性优化问题。 它可以在Microsoft Excel中使用Solver加载项解决,但是我在C#中复制它时遇到了问题。

我的问题显示在下面的电子表格中 。 我正在解决经典的A x = b问题,但要注意x的所有组件都必须是非负的。 因此,我不使用标准的线性代数,而是使用求解器的非负约束,最小化平方差的总和,并得到一个合理的解决scheme。 我试图在C#中使用Microsoft Solver Foundation或Solver SDK复制这个。 然而,我似乎无法得到任何与他们的任何地方,因为无国界医生我无法弄清楚如何定义的目标,并与Solver SDK我总是得到状态“最佳”和解决scheme的所有零,这是绝对不是本地最小。

这是我的Solver SDK代码:

static double[][] A = new double[][] { new double[] { 1, 0, 0, 0, 0 }, new double[] { 0.760652602, 1, 0, 0, 0 }, new double[] { 0.373419404, 0.760537565, 1, 0, 0 }, new double[] { 0.136996731, 0.373331934, 0.760422587, 1, 0 }, new double[] { 0.040625222, 0.136953801, 0.373244464, 0.76030755, 1 } }; static double[][] b = new double[][] { new double[] { 2017159 }, new double[] { 1609660 }, new double[] { 837732.8125 }, new double[] { 330977.3125 }, new double[] { 87528.38281 } }; static void Main(string[] args) { using(Problem problem = new Problem(Solver_Type.Minimize, 5, 0)) { problem.VarDecision.LowerBound.Array = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0 }; problem.VarDecision.UpperBound.Array = new double[] { Constants.PINF, Constants.PINF, Constants.PINF, Constants.PINF, Constants.PINF }; problem.Evaluators[Eval_Type.Function].OnEvaluate += new EvaluateEventHandler(SumOfSquaredErrors); problem.ProblemType = Problem_Type.OptNLP; problem.Solver.Optimize(); Optimize_Status status = problem.Solver.OptimizeStatus; Console.WriteLine(status.ToString()); foreach(double x in problem.VarDecision.FinalValue.Array) { Console.WriteLine(x); } } } static Engine_Action SumOfSquaredErrors(Evaluator evaluator) { double[][] x = new double[evaluator.Problem.Variables[0].Value.Array.Length][]; for(int i = 0; i < x.Length; i++) { x[i] = new double[1] { evaluator.Problem.Variables[0].Value.Array[i] }; } double[][] b_calculated = MatrixMultiply(A, x); double sum_sq = 0.0; for(int i = 0; i < b_calculated.Length; i++) { sum_sq += Math.Pow(b_calculated[i][0] - b[i][0], 2); } evaluator.Problem.FcnObjective.Value[0] = sum_sq; return Engine_Action.Continue; } static double[][] MatrixMultiply(double[][] left, double[][] right) { if(left[0].Length != right.Length) { throw new ArgumentException(); } double[][] sum = new double[left.Length][]; for(int i = sum.GetLowerBound(0); i <= sum.GetUpperBound(0); i++) { sum[i] = new double[right[i].Length]; } for(int i = 0; i < sum.Length; i++) { for(int j = 0; j < sum[0].Length; j++) { for(int k = 0; k < right.Length; k++) { sum[i][j] += left[i][k] * right[k][j]; } } } return sum; } 

我没有Microsoft Solver Foundation的任何代码,因为我不认为目标函数可以写在一行中,也不允许Solver SDK这样的代理。

另一种方法是将其作为一个LP问题来形成:

最小化x中元素的总和

服从Ax> = b

基于其中一个LP样本,使用Solver Foundation进行制定应该相当简单。

7月5日更新

上面的方法看起来过于复杂,但也许这是由Frontline Solver API造成的。 使用Microsoft Solver Foundation, 最小化平方差的总和,以下程序:

 private static void Main(string[] args) { var solver = SolverContext.GetContext(); var model = solver.CreateModel(); var A = new[,] { { 1, 0, 0, 0, 0 }, { 0.760652602, 1, 0, 0, 0 }, { 0.373419404, 0.760537565, 1, 0, 0 }, { 0.136996731, 0.373331934, 0.760422587, 1, 0 }, { 0.040625222, 0.136953801, 0.373244464, 0.76030755, 1 } }; var b = new[] { 2017159, 1609660, 837732.8125, 330977.3125, 87528.38281 }; var n = A.GetLength(1); var x = new Decision[n]; for (var i = 0; i < n; ++i) model.AddDecision(x[i] = new Decision(Domain.RealNonnegative, null)); // START NLP SECTION var m = A.GetLength(0); Term goal = 0.0; for (var j = 0; j < m; ++j) { Term Ax = 0.0; for (var i = 0; i < n; ++i) Ax += A[j, i] * x[i]; goal += Model.Power(Ax - b[j], 2.0); } model.AddGoal(null, GoalKind.Minimize, goal); // END NLP SECTION var solution = solver.Solve(); Console.WriteLine("f = {0}", solution.Goals.First().ToDouble()); for (var i = 0; i < n; ++i) Console.WriteLine("x[{0}] = {1}", i, x[i].GetDouble()); } 

生成以下解决scheme,这应该与链接的Excel工作表的解决scheme一致:

 f = 254184688.179922 x[0] = 2017027.31820845 x[1] = 76226.6063397686 x[2] = 26007.3375581303 x[3] = 1.00650383558278E-07 x[4] = 4.18546775823669E-09 

如果我没有弄错,与GRG不同,Solver Foundation不能支持一般的非线性约束,我相信你将需要额外的插件来处理这些约束。 对于你的问题,这当然不是一个问题。

为了完整起见,为了制定LP问题,请用以下代码在START NLP SECTIONEND NLP SECTION之间交换代码:

  var m = A.GetLength(0); var constraints = new Term[m]; for (var j = 0; j < m; ++j) { Term Ax = 0.0; for (var i = 0; i < n; ++i) Ax += A[j, i] * x[i]; model.AddConstraint(null, constraints[j] = Model.GreaterEqual(Ax, b[j])); } model.AddGoal(null, GoalKind.Minimize, Model.Sum(x)); 

这将产生以下输出(注意在两种情况下目标函数是不同的,因此在f有很大的差异):

 f = 2125502.27815564 x[0] = 2017159 x[1] = 75302.7580022821 x[2] = 27215.9247379241 x[3] = 5824.5954154355 x[4] = 0