0

问题

我有一个需要解决线性规划 (LP) 问题的 C# 项目。我使用Microsoft.SolverFoundation.InteriorPointSolver https://docs.microsoft.com/en-us/previous-versions/visualstudio/ff525706(v=vs.93)我遇到了有时会找到不正确解决方案的问题。我尽可能地减少了样本(见下面的代码)。对于第一个块,正确的解决方案是 y = 5,但在输出中,我得到的数字非常接近 -5。然而,一个非常相似的第二个块可以正常工作。

技术细节

IDE:VisualStudio 2019 社区
项目:C# 控制台应用程序 .NET 5
Microsoft.SolverFoundation v3.1.0

假设

我认为问题在于我对solver.AddGoal最后一个参数(`bool minimize)的错误理解,因为它是前两个块之间的两个差异之一。但是块 III 和 IV 工作正常。所以我找不到原因。谢谢!

using Microsoft.SolverFoundation.Solvers;
using System;

namespace SolverFoundationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                // I.
                // y -> max
                // x = 1
                // 5x - y >= 0
                var solver = new InteriorPointSolver();
                solver.AddVariable("y", out var y);
                solver.AddVariable("x", out var x);
                solver.AddRow("goal", out var g);
                solver.AddGoal(g, 0, false);
                solver.SetCoefficient(g, y, 1);
                solver.SetBounds(x, 1, 1);
                solver.AddRow("constraint", out var c);
                solver.SetLowerBound(c, 0);
                solver.SetCoefficient(c, x, 5);
                solver.SetCoefficient(c, y, -1);
                var solution = solver.Solve(new InteriorPointSolverParams());
                var score = (double)solution.GetValue(y);
                Console.WriteLine(score);
                // -5,000000000129852 WRONG!
            }
            {
                // II.
                // y -> min
                // x = 1
                // 5x - y <= 0
                var solver = new InteriorPointSolver();
                solver.AddVariable("y", out var y);
                solver.AddVariable("x", out var x);
                solver.AddRow("goal", out var g);
                solver.AddGoal(g, 0, true);
                solver.SetCoefficient(g, y, 1);
                solver.SetBounds(x, 1, 1);
                solver.AddRow("constraint", out var c);
                solver.SetUpperBound(c, 0);
                solver.SetCoefficient(c, x, 5);
                solver.SetCoefficient(c, y, -1);
                var solution = solver.Solve(new InteriorPointSolverParams());
                var score = (double)solution.GetValue(y);
                Console.WriteLine(score);
                // 5,000000000129852 correct
            }
            {
                // III.
                // y -> min
                // -2 <= y <= 3
                var solver = new InteriorPointSolver();
                solver.AddVariable("y", out var y);
                solver.SetBounds(y, -2, 3);
                solver.AddRow("goal", out var g);
                solver.AddGoal(g, 0, true);
                solver.SetCoefficient(g, y, 1);
                var solution = solver.Solve(new InteriorPointSolverParams());
                var score = (double)solution.GetValue(y);
                Console.WriteLine(score);
                // -2 correct
            }
            {
                // IV.
                // y -> max
                // -2 <= y <= 3
                var solver = new InteriorPointSolver();
                solver.AddVariable("y", out var y);
                solver.SetBounds(y, -2, 3);
                solver.AddRow("goal", out var g);
                solver.AddGoal(g, 0, false);
                solver.SetCoefficient(g, y, 1);
                var solution = solver.Solve(new InteriorPointSolverParams());
                var score = (double)solution.GetValue(y);
                Console.WriteLine(score);
                // 3 correct
            }
        }
    }
}
4

0 回答 0