For years, Solver Foundation has provided the ability to plug-in third party solvers so that they can be used using the same programming model as Solver Foundation’s built-in solvers. In the past, solvers were registered using configuration files, as we have described in previous blog posts.

Solver Foundation 3.1 provides new programming interfaces so that solvers can be registered in code at runtime. This is useful in a number of different scenarios, including in cases where configuration files are hard to specify, as in Iron Python.

The following Iron Python sample code registers the Gurobi solver to handle linear (LP) models. Other solvers are registered in a similar fashion. You should be able to adapt this code to other .Net languages even if you are not familiar with Iron Python: the RegisteredSolvers.Add line is key. (The MSDN documentation for this new method will go live soon.)

import clr
## Bring in Solver Foundation
clr.AddReference("Microsoft.Solver.Foundation")
from Microsoft.SolverFoundation.Services import *

## Some helper functions for Solver Foundation.
def const(i):
    return Term.op_Implicit(i)
def lessequal(term, i):
    return Model.LessEqual(term, const(i))

c = SolverContext()

## Let's add a plug-in solver.
c.RegisteredSolvers.Add(SolverRegistration("gurobi", ## name of the entry (your choice)
  SolverCapability.LP, ## type of model
  "Microsoft.SolverFoundation.Services.ILinearSolver", ## the interface
  "GurobiPlugIn.dll", ## plugin dll 
  "SolverFoundation.Plugin.Gurobi.GurobiSolver", ## the solver class
  "SolverFoundation.Plugin.Gurobi.GurobiDirective", ## the directive class
  "SolverFoundation.Plugin.Gurobi.GurobiParams")) ## solver parameters class

m = c.CreateModel()
x = Decision(Domain.RealNonnegative, "x")
y = Decision(Domain.RealNonnegative, "y")
m.AddDecisions(x, y)
m.AddConstraint("c1", lessequal(x + y, const(5)))
m.AddConstraint("c2", lessequal(const(2) * x + y, const(7)))
m.AddGoal("g", GoalKind.Maximize, x + y)
s = c.Solve()
print s.GetReport()