使用MS Solver Foundation获得多个最佳解决方案

问题描述:

大家好,我正在开展一个小型项目来学习MS Solver Foundation。我有一个应该接受一些约束和目标的应用程序,然后显示所有最佳解决方案。这是我为了获得解决方案而进行的调用: 

Hi everyone, i'm working on a small project to learn MS Solver Foundation. I've got an application that's supposed to accept some constraints and a goal, then display ALL the optimal solutions. This is the call that i make in order to get a solution: 

var solution = solver.Solve(new ConstraintProgrammingDirective());

但是,我得到以下异常(UnsolvableModelException):"找不到可以接受给定模型类型和指令的模型的求解器"。

However, i get the following exception (an UnsolvableModelException): "No solver could be found that can accept the model given the model type and directive(s)".

但是,如果我只是按如下方式编写代码,则不会出现异常:

However, if i just write the code as follows i don't get an exception:

var solution = solver.Solve();

但在这种情况下,我没有得到所有最佳解决方案,我只得到一个。我想向用户显示所有最佳解决方案。

But then in this case i don't get ALL the optimal solutions, i just get one. I would like to display ALL the optimal solutions to the user.

谢谢。

Thanks.

嗨Arlvin,

Hi Arlvin,

很高兴看到没有人回答你的问题。对于支持返回多个解决方案的求解器,您需要在解决方案对象上调用GetNext方法:

Sorry to see that nobody has answered your question. For solvers that support returning more than one solution you need to call the GetNext method on the solution object:

var nextSolution = solution.GetNext();

并非所有解算器都支持此方法 - 事实上,ConstraintProgramming求解器可能是唯一的解算器。要使用ConstraintProgramming求解器,请确保您的模型不包含任何实值参数或决策。以下是GetNext方法的更多文档

Not all solvers support this method - in fact, the ConstraintProgramming solver might be the only one. In order to use the ConstraintProgramming solver, make sure that your model does not contain any real-valued parameters or decisions. Here is more documentation on the GetNext method:

http://msdn.microsoft.com/en-us/library/microsoft.solverfoundation.services.solution.getnext(v = vs.93

希望这会有所帮助,Nate

Hope this helps, Nate