简体   繁体   English

在Microsoft Solver Foundation中处理实数范围

[英]On working with ranges of real numbers in Microsoft Solver Foundation

A solver project I am working on (C#) requires to test if there is a solution, regardless of quality, or not to a problem where some of the inputs fall within some pre-defined range of real numbers. 我正在研究的求解器项目(C#)要求测试是否有解决方案,而不论其质量如何,或者是否要解决某些输入落在某些预定的实数范围内的问题。

I put together the following example that contains one constraint representing a simple equality test between a value (a Parameter type) and an equation comprised of two variables (Decision types). 我将以下示例放在一起,其中包含一个约束,表示一个值(参数类型)和由两个变量组成的方程(决策类型)之间的简单相等性测试。

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

What I have observed is that if the constraint is changed so that that there is no solution, and the equation represented therein is a division or multiplication, the solver seems to stall, not giving any feedback as to whether it is still solving or not. 我观察到的是,如果更改了约束条件以至于没有解,并且其中表示的方程是除法或乘法,则求解器似乎停滞了,未给出关于是否仍在求解的任何反馈。

I suspect this stalling-like behaviour is to do with the fact the solver is dealing with real numbers and is in the midst of some exhaustive search, however if the constraint is altered so that there is a known solution it works very quickly. 我怀疑这种类似停滞的行为与求解器正在处理实数并且处于某些穷举搜索之中有关,但是,如果更改了约束以使存在已知的解决方案,则它会非常快速地工作。

Having scoured various forums I am still not sure what could be causing this behaviour or, given this is my first experience using Microsoft Solver Foundation, if my implementation approach is the correct one to take. 遍历各种论坛后,我仍然不确定是什么引起了这种现象,或者鉴于这是我第一次使用Microsoft Solver Foundation,因此我的实施方法是否正确。

Has anyone else experienced this issue or indeed has a solution? 其他人是否遇到过此问题或确实有解决方案?

d. d。

The solver that is chosen by Solver Foundation depends on a number of factors. Solver Foundation选择的求解器取决于许多因素。 A big factor is how you use the Decisions in your goals and constraints. 一个重要因素是您如何在目标和约束中使用决策。 In this case, you are dividing two decisions, which means that a very general solver needs to be used. 在这种情况下,您将划分两个决策,这意味着需要使用非常通用的求解器。

You'll have much better success if you can write your model so that the goals and constraints are linear in the Decision objects. 如果可以编写模型,使决策对象中的目标和约束线性化,那么您将获得更大的成功。 I realize this is not always possible, but in this specific case, it can: change output == inputA / inputB to inputB * output == inputA. 我意识到这并非总是可能的,但是在这种特定情况下,它可以:将输出== inputA / inputB更改为inputB * output == inputA。

I think this should help. 我认为这应该有所帮助。 Nate 内特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM