求解器求解约束规划问题

时间:2010-11-26 04:08:20

标签: algorithm solver ms-solver-foundation

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

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(约束满意度编程)问题,在解决它时不应使用任何指令。 代码应该是:

    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);

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

1 个答案:

答案 0 :(得分:1)

根据您的需要,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

alt text