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.AddConstraints("map",

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

Model.AllDifferent(netherlands, belgium, germany),

Model.AllDifferent(germany, denmark));

Solution solution = context.Solve();

Console.WriteLine(solution.GetReport());

 

 

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 = [m.int(0, count - 1) for i in range(0, count)]

m.alldifferent(q)

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

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

m.solve()

b = zeros(count, count)

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

return b

imageview(queens(8))

 

 

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)

where

[

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.

Namaste!