使用Microsoft Solver Foundation中的实数范围

时间:2012-04-25 12:18:14

标签: c# ms-solver-foundation

我正在研究的解决方案项目(C#)需要测试是否存在解决方案,无论质量如何,或者是否存在某些输入属于某个预定义实数范围内的问题。

我将以下示例放在一起,其中包含一个约束,表示值(参数类型)和由两个变量(决策类型)组成的等式之间的简单相等性测试。

        const double DESIRED_OUTPUT_VALUE = -2.5;

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

        //Defined a value to be tested against
        Parameter output = new Parameter(Domain.Real, "output");
        output.SetBinding(DESIRED_OUTPUT_VALUE);

        //Defined a range between 1 & 10 for the input variables.
        Domain inputDomain = Domain.RealRange(1, 10);
        Decision inputA = new Decision(inputDomain, "inputA");
        Decision inputB = new Decision(inputDomain, "inputB");

        model.AddDecision(inputA);
        model.AddDecision(inputB);
        model.AddParameter(output);

        //The constraint, which given the value of output currently is not solvable.
        Constraint constraint = model.AddConstraint("result", output == inputA / inputB);

        /*Expected that the solver would report back quickly that this is no feasable solution.
         *However instead it just sits there with a blank screen... 
         */
        Solution solution = solver.Solve();
        Report report = solution.GetReport();
        Console.WriteLine(report);
        Console.ReadLine();

我所观察到的是,如果约束被改变以使得没有解,并且其中表示的等式是除法或乘法,则求解器似乎停滞,没有给出关于它是否仍在求解的任何反馈或不。

我怀疑这种类似拖延的行为与解算器处理实数的事实有关,并且处于一些详尽的搜索中,但是如果约束被改变以便有一个已知的解决方案它可以很快地工作

在浏览了各种论坛后,我仍然不确定是什么原因导致这种行为,或者,鉴于这是我第一次使用Microsoft Solver Foundation,如果我的实现方法是正确的。

是否有其他人遇到此问题或确实有解决方案?

d。

1 个答案:

答案 0 :(得分:1)

Solver Foundation选择的解算器取决于许多因素。一个重要因素是您如何在目标和约束中使用决策。在这种情况下,您要划分两个决策,这意味着需要使用非常通用的求解器。

如果您可以编写模型以使目标和约束在Decision对象中呈线性,那么您将获得更好的成功。我意识到这并不总是可行,但在这种特殊情况下,它可以:将output == inputA / inputB更改为inputB * output == inputA。

我认为这应该有所帮助。 内特