强制 C# SolverFoundation SimplexSolver 找到一个 int 解决方案

Force C# SolverFoundation SimplexSolver to find an int solution

here SimplexSolver "Defines a branch-and-bound search for optimizing mixed integer problems." 这应该意味着它为给定任务找到了一个整数解决方案,但它找到了一个具有双精度值的精确解决方案。

有没有办法强制它找到一个整数解,或者我应该在给定的双解之上实现我自己的分支定界?

Is there a way to force it to find an integer solution or i should implement my own branch-and-bound on top of given double solutions?

无需实现 B&B 算法,只需将变量声明为整数,SimplexSolver 应该能够求解并提供整数最优解。参见 example here。以下相关片段:

SimplexSolver solver = new SimplexSolver();

// ...

for (int i = 0; i < 5; i++) {
  solver.AddVariable(string.Format("project{0}", i),
                      out chooseProjectX[i]);
  solver.SetBounds(chooseProjectX[i], 0, 1);
  solver.SetIntegrality(chooseProjectX[i], true);
  // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  solver.SetCoefficient(profit, chooseProjectX[i],
                         estimatedProfitOfProjectX[i]);
  solver.SetCoefficient(expenditure, chooseProjectX[i],
                         capitalRequiredForProjectX[i]);
}