首页 > 解决方案 > C#:使用 Microsoft 求解器求解非线性系统

问题描述

到目前为止,使用 Microsoft Solver Foundation,我只解决了线性问题。我现在正在尝试解决一个非常简单的非线性问题,但由于某些原因,Microsoft 求解器无法解决它。

问题是最大化 a0*a1 , a0<10 和 a1<20 。这是我正在使用的代码:

using System;
using Microsoft.SolverFoundation.Services;

namespace SolverFoundationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("\nBegin Solver demo\n");

            var solver = SolverContext.GetContext();
            var model = solver.CreateModel();

            var decision1 = new Decision(Domain.RealNonnegative, "a0"); model.AddDecision(decision1);
            var decision2 = new Decision(Domain.RealNonnegative, "a1"); model.AddDecision(decision2);


            model.AddConstraint("Constraint0", "a0 <=10");
            model.AddConstraint("Constraint1", "a1 <=20");


             model.AddGoal("Goal", GoalKind.Maximize, " a0*a1 ");


            var solution = solver.Solve();

  
            Console.WriteLine("\nEnd Solver demo\n");
            Console.ReadLine();
        } 
    } 
} 

我得到的错误是“模型不是凸的”,这是真的,但我希望微软求解器足够聪明,无论如何都能找到解决方案。

非常感谢您的反馈。

问候,

标签: c#optimizationsolvernonlinear-optimization

解决方案


实际上,Microsoft Solver 默认选择 NLP。由于某些原因,如果我添加一个无用的约束,例如 a0*a1 < 1000000,它会起作用。如果我不添加这个约束,它就不起作用..我对这个 Solver 真的不满意。Erwin,如果我想从 Microsoft 求解器切换,你能告诉我应该使用哪个求解器吗?

我的程序是用C#写的。我在 Datable 中有我所有的决定和约束,我将它传递给求解器,如下所示。为了避免重写整个东西,我想要一个可以使用相同方法提供的求解器。

string Comment = Convert.ToString(Table_Constraints.Rows[i]["Comment"]);

字符串约束 = Convert.ToString(Table_Constraints.Rows[i]["Constraint"]);

model.AddConstraint(评论,约束);


推荐阅读