求解器求解约束规划问题

发布于 2024-10-05 06:04:58 字数 1396 浏览 4 评论 0原文

有谁知道任何可以求解线性约束数学模型的简单求解器?一个简单模型的示例:

a + b + c = 100;
a/b/c = 2/3/4;
a > d

我正在使用 MS Solver 基础,这是我的 C# 代码,但它抛出 UnsolvableModelException:

        SolverContext solverContext = SolverContext.GetContext();
        Model model = solverContext.CreateModel();
        Decision a = new Decision(Domain.Real, "a");
        Decision b = new Decision(Domain.Real, "b");
        model.AddDecisions(a, b);
        model.AddConstraint("fator", a / b ==  4);
        model.AddConstraint("sum", a + b == 5);
        Solution solution = solverContext.Solve(new ConstraintProgrammingDirective());
        Report report = solution.GetReport();
        Console.WriteLine("a = {0} ; b = {1}", a, b);

感谢您的任何帮助

编辑:

因为 int CSP(约束 sstisfaction 编程)问题,你应该'解决问题时不要使用任何指令。 代码应该是:

    SolverContext solverContext = SolverContext.GetContext();
    Model model = solverContext.CreateModel();
    Decision a = new Decision(Domain.Real, "a");
    Decision b = new Decision(Domain.Real, "b");
    model.AddDecisions(a, b);
    model.AddConstraint("fator", a / b ==  4);
    model.AddConstraint("sum", a + b == 5);
    Solution solution = solverContext.Solve();
    Report report = solution.GetReport();
    Console.WriteLine("a = {0} ; b = {1}", a, b);

但是,我不知道为什么我跑得很慢我只需要解决方案集中的第一个解决方案。

Do anyone know any simple solver that can solve linear constraint math models? Example a simple model:

a + b + c = 100;
a/b/c = 2/3/4;
a > d

I'm using MS Solver foundation and this is my C# code, but it throw UnsolvableModelException:

        SolverContext solverContext = SolverContext.GetContext();
        Model model = solverContext.CreateModel();
        Decision a = new Decision(Domain.Real, "a");
        Decision b = new Decision(Domain.Real, "b");
        model.AddDecisions(a, b);
        model.AddConstraint("fator", a / b ==  4);
        model.AddConstraint("sum", a + b == 5);
        Solution solution = solverContext.Solve(new ConstraintProgrammingDirective());
        Report report = solution.GetReport();
        Console.WriteLine("a = {0} ; b = {1}", a, b);

Thanks for any help

Edit:

Because int CSP(constraint sstisfaction programming) problem, you shouldn't use any Directive when solving it.
The code should be:

    SolverContext solverContext = SolverContext.GetContext();
    Model model = solverContext.CreateModel();
    Decision a = new Decision(Domain.Real, "a");
    Decision b = new Decision(Domain.Real, "b");
    model.AddDecisions(a, b);
    model.AddConstraint("fator", a / b ==  4);
    model.AddConstraint("sum", a + b == 5);
    Solution solution = solverContext.Solve();
    Report report = solution.GetReport();
    Console.WriteLine("a = {0} ; b = {1}", a, b);

However, I don't know why I ran to slowly I only need the first solution in the solution set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2024-10-12 06:04:58

根据您的需要,Wolfram Alpha 可能有用(至少对于验证而言)。我怀疑你能否解决复杂的问题,因为输入的长度有限。但对于像你的例子这样的简单问题,这是可以的。

http://www.wolframalpha.com/input/?i=solve+a+%2B+ b+%2B+c+%3D%3D+100%3B+a%2Fb+%3D%3D+2%2F3%3B+b%2Fc%3D%3D3%2F4%3B+a+%3E+d

< img src="https://i.sstatic.net/wUmUY.png" alt="替代文本">

Depending on your needs, Wolfram Alpha may be useful (at least for verification). I doubt you can solve complex problems, because the input is lenght-limited. But for simple problems as your example, it's OK.

http://www.wolframalpha.com/input/?i=solve+a+%2B+b+%2B+c+%3D%3D+100%3B+a%2Fb+%3D%3D+2%2F3%3B+b%2Fc%3D%3D3%2F4%3B+a+%3E+d

alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文