求解器求解约束规划问题
有谁知道任何可以求解线性约束数学模型的简单求解器?一个简单模型的示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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
< 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