Search code examples
c#linear-programmingms-solver-foundation

Force C# SolverFoundation SimplexSolver to find an int solution


It is stated here that SimplexSolver "Defines a branch-and-bound search for optimizing mixed integer problems." which should mean that it finds an integer solution for a given task but it finds a precise solution with a double values.

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?


Solution

  • 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?

    No need to implement B&B algorithm, just declare your variables as integers and SimplexSolver should be able to solve it and provide integer optimal solution. See example here. Relevant snippet below:

    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]);
    }