Solver Foundation on DevLabs

Today we’re adding Solver Foundation to DevLabs.

Solver Foundation is a .NET library for mathematical programming, modeling, and optimization. Mathematical programming is all about decision making, and decision problems are everywhere: from supply chain management, project scheduling, logic puzzles like Sudoku, building sports schedules, or laying out UI controls. Solver Foundation provides superior functionality and usability for .NET developers seeking to use optimization in their solutions, making it possible for non-experts to define and solve models in their applications.

Solver Foundation uses a declarative programming model consisting of simple building blocks, applying solvers that employ operations research, metaheuristic, and combinatorial optimization techniques. Building a model in Solver Foundation is as easy as specifying the decisions to be made, constraints to be respected, the goals to be used to evaluate candidate solutions, and the historical or projected parameter data to be processed by the model. This can be done from any .NET language without having to worry about the details of solver technologies or search strategies.

Here’s the C# code for the famous four color map problem:

Solver Foundation allows you to build, analyze, and solve a wide variety of optimization models. The Solver Foundation Services object model provides many built-in functions and figures out which solver to call so you don’t have to. It’s also easy to connect models to almost any data source using LINQ and Solver Foundation’s data binding APIs. Solver Foundation includes a broad range of solvers, including a powerful constraint solver developed in conjunction with the Constraint Reasoning group in MSR Cambridge UK. Solver Foundation’s extensibility APIs enable many commercial and open source solvers to be plugged in without code changes.


      SolverContext context = SolverContext.GetContext();

      Model model = context.CreateModel();


      Domain colors = Domain.Enum(“blue “, “white “, “red “, “green “);


      Decision belgium = new Decision(colors, “Belgium”);

      Decision france = new Decision(colors, “France”);

      Decision denmark = new Decision(colors, “Denmark”);

      Decision germany = new Decision(colors, “Germany”);

      Decision netherlands = new Decision(colors, “Netherlands”);

      Decision luxembourg = new Decision(colors, “Luxembourg”);

      model.AddDecisions(belgium, france, denmark, germany, netherlands, luxembourg);



        Model.AllDifferent(france, belgium, germany, luxembourg),

        Model.AllDifferent(netherlands, belgium, germany),

        Model.AllDifferent(germany, denmark));


      Solution solution = context.Solve();





The Excel add-in for Solver Foundation makes it easy to build and solve models using spreadsheet data, and you can export models to C# with the click of a button. The following screenshot shows the Excel add-in being used to find the least expensive set of unit tests that ensure full test coverage.

Solver Foundation powers the Sho optimization library, also available through DevLabs. Together, Sho and Solver Foundation provide a great environment for solving and visualizing decision-based models. For example, the following Sho code solves the eight queens puzzle and displays the placement in a figure:

def queens(count):

       m = model()

       q = [, count 1) for i in range(0, count)]


       m.alldifferent([q[i] + const(i) for i in range(0, count)])

       m.alldifferent([q[i] const(i) for i in range(0, count)])


       b = zeros(count, count)

       for i in range(0, count): b[i, q[i].GetDouble()] = 1

       return b







Using Solver Foundation in F# support is elegant and fun. The F# ODSL provides the ability to define models in standard mathematical form, including units of measure. The following F# code defines a production planning model:

[<Measure>] type Dollar

[<Measure>] type Barrel

[<Measure>] type Day

[<Measure>] type ProductionRate = Barrel/Day

let ProductionPlanning() =

    let dailyOutput = 9000.0<Barrel/Day>

    let price = 140.0<Dollar/Barrel>

    let dailyCost = dailyOutput * price

    let a = 20.0<Dollar/Barrel>

    let b = 15.0<Dollar/Barrel>

    let sa = var<Barrel/Day>()

    let vz = var<_>()

    minimize (a * sa + b * vz)



            0.3 * sa + 0.4 * vz >= 2000.<_>;

            0.4 * sa + 0.2 * vz >= 1500.<_>;

            0.2 * sa + 0.3 * vz >= 500.<_>;

            sa <= 9000.<_>;

            vz <= 6000.<_>;

            sa >= 0.<_>;

            vz >= 0.<_> 





Try Solver Foundation for yourself. The Solver Foundation installation includes over 50 samples and full MSDN documentation. We would love to hear your feedback on this in the Solver Foundation forum.


Comments (13)

  1. John Sheehan says:

    What does adding it to DevLabs mean? Has the licensing for SF changed at all?

  2. Nate Brixius says:

    Hi John – I also saw your question on twitter. Thanks for asking! No, the licensing for Solver Foundation hasn't changed a bit. With the new site and forum we hope that it will be easier to download Solver Foundation, try it out, and provide feedback. We also hope that adding Solver Foundation to DevLabs will introduce this library to a whole new set of .NET programmers.

    Best regards, Nathan

  3. Mike James says:

    News item on I Programmer about Solver Foundation…/2361-solver-foundation-on-devlabs.html

  4. Jim Kennelly says:

    Thank you.  This should pave the way for people to start incorporating least cost & optimizations.  Currently working on a truck loading problem that could be helped by this.

  5. Eddie Amos says:

    This is really cool.  Can't wait to play with it tonight.

  6. Peter says:

    WOW. I'm sure this is going to be a real winner. At last all my quasi mathematical optomizing and solving can get handed over to a real expert.

  7. Peter says:

    WOW. I'm sure this is going to be a real winner. At last all my quasi mathematical optomizing and solving can get handed over to a real expert.

  8. Subba Rao says:

    Hi Soma,

    I am looking at playing with this to replace the IBM ILOG that we are using.

    Or am I way off the mark here. We mainly use it inSilicon Manufaturing to figure out Bin Spec Assingments based on Schedules.

  9. Jim Kennelly says:

    I was just running a solution for a truck loading problem (like a knapsack problem) and the run time grows with the number of items to load.   This could be a problem when item count gets large.

    I was wondering if the solver foundation library was offered in the Azure cloud?   If so,  I could push my solution (model/data) to your azure compute engine since the problem definition is only 60 lines of code.  Any thoughts?

  10. Nate Brixius says:

    @Jim Kennelly Thanks for your question. Solver Foundation is not available on Azure at the present time, though the team has had a few requests for this. Any future announcements about Solver Foundation for Azure will be made on the forum. (Soma provided a link in his post above.)


  11. Nate Brixius says:

    @Subba You are not off the mark – your scenario seems like something that could be addressed with Solver Foundation. I encourage you to give it a try and let us know how it goes in the Solver Foundation forum.


  12. NiCa60 says:

    Please update MSDN documentation, about installing, it still refere to Visual Studio 2010…

  13. DrMDodd123 says:

    I am new to using the Solver Foundation library, but believe that it can solve non-linear problem.  Here is one I am trying to formulated.   I have a number of points x,y, and z and I would like to know the best fitting elliptical cylinder for these points.  Now I thought I could use the generalized equation of an ellipse, project along z direction.  However, I haven't see examples where given xi and yi what is the best fit.

Skip to main content