Using the Solver Foundation Interior Point Solver
I've had a few requests on this topic, so I thought I would give a simple example of how to write code against Solver Foundation's interior point solver. If you are new to Solver Foundation, be warned that this is something of an "advanced topic". The most complete, powerful Solver Foundation programming interface is Solver Foundation Services. The solverfoundation.com site has links to more information about the SFS, and I have worked through several samples on this blog: production planning, two-way data binding, traveling salesman, parameterized models, LINQ-to-SQL.
The InteriorPointSolver class is capable of solving linear, quadratic, and (in our upcoming 2.0 release) second order conic optimization problems. Let's take as an example the production planning problem I wrote about in a previous post. It is a simple linear programming problem with two decisions (the production for each refinery) and three constraints (the demand for each refined product). In our SFS sample we simply declared the decisions, goals, and constraints and expressed them algebraically using the SolverContext and Model. The InteriorPointSolver class represents linear models in a slightly different way: using variables and rows. Variables are the analogue of SFS Decisions, whereas rows can represent either goals or constraints. Both variables and rows can have upper and/or lower bounds. In a linear model, both goals and constraints consist of linear combinations of variables, such as "3*x + 4*y + 5*z". Whereas in the SFS we can express these terms very naturally, with the InteriorPointSolver, the terms are expressed one at a time by specifying the coefficient associated with each variable in the row. Here is the C# code:
private static void PetrochemLinearModel() {
InteriorPointSolver solver = new InteriorPointSolver();
int sa, vz;
solver.AddVariable("SA", out sa);
solver.SetBounds(sa, 0, 9000);
solver.AddVariable("VZ", out vz);
solver.SetBounds(vz, 0, 6000);
int goal;
solver.AddRow("goal", out goal);
solver.AddGoal(goal, 0, true);
solver.SetCoefficient(goal, sa, 20);
solver.SetCoefficient(goal, vz, 15);
int demand1, demand2, demand3;
solver.AddRow("demand1", out demand1);
solver.SetCoefficient(demand1, sa, 0.3);
solver.SetCoefficient(demand1, vz, 0.4);
solver.SetLowerBound(demand1, 1126);
solver.AddRow("demand2", out demand2);
solver.SetCoefficient(demand2, sa, 0.4);
solver.SetCoefficient(demand2, vz, 0.2);
solver.SetLowerBound(demand2, 1500);
solver.AddRow("demand3", out demand3);
solver.SetCoefficient(demand3, sa, 0.2);
solver.SetCoefficient(demand3, vz, 0.3);
solver.SetLowerBound(demand3, 500);
var param = new InteriorPointSolverParams();
var solution = solver.Solve(param);
Console.WriteLine("goal = " + solution.GetValue(goal).ToDouble());
Console.WriteLine("sa = " + solution.GetValue(sa).ToDouble());
Console.WriteLine("vz = " + solution.GetValue(vz).ToDouble());
}
You'll also need to add "using Microsoft.SolverFoundation.Common;" and "using Microsoft.SolverFoundation.Solvers;" to the top of your program. Extending this code to solve a quadratic program is easy: there is an overload of SetCoefficient that takes two variables as input. In my next post I will talk about second order conic programming, and a few other improvements in the 2.0 version of the InteriorPointSolver.
Comments
Anonymous
April 04, 2011
In your SimplexDirective example, your lower bound for demand1 was 1900. In this InteriorPoint example it is 1126. It took me a few minutes to figure out why I was getting different solutions between the SimplexDirective example and the InteriorPointSolver since I assumed everything was the same. Some consistency between examples would be good.Anonymous
February 16, 2015
In the MSF-SolverProgrammingPrimer.pdf it is claimed: "We only support a single objective in our QP solver at present." Is this still the case? If yes, when is it planned to change this? Optimizing "Vector Objective Functions" could be encoded with multiple goals if this would work.