c# - 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();
}
}
}
我得到的错误是“模型不是凸的”,这是真的,但我希望微软求解器足够聪明,无论如何都能找到解决方案。
非常感谢您的反馈。
问候,
解决方案
实际上,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(评论,约束);
推荐阅读
- bash - 如果在 shell 命令中运行,tmux resizep 无法按预期工作
- reactjs - 从服务器获取的数据未显示在前端屏幕(MERN 堆栈)
- c++ - C ++如何制作接受类函数作为参数的函数重载
- vue.js - 如何使用服务器端分页实现 Quasar q-table?
- visual-studio-code - 如何在 Visual Studio Code 中忽略特定文件夹中的特定文件扩展名
- flutter - 当我转到下一页或关闭应用程序时,如何在文本字段中保存(保留)“最后一个”值?
- java - 如果输入对 if 语句无效,如何使用一个循环?
- python - 可变对象与不可变对象特别列出
- sql-server - 使用 EF Core 从数据库获取数据时忽略来自实体的额外属性
- flutter - 我的应用程序停止使用 google_mobile_ads 启动:^0.13.0 package flutter