Solving a Knapsack problem with Solver Foundation and LINQ

On an internal message board, somebody asked how to solve the following problem:

Let’s say I have this list of days and prices:

List<ReservationPrice> prices = new List<ReservationPrice>(); 
prices.Add(new ReservationPrice { NumberOfDays = 1, Price = 1000 }); 
prices.Add(new ReservationPrice { NumberOfDays = 2, Price = 1200 }); 
prices.Add(new ReservationPrice { NumberOfDays = 3, Price = 2500 }); 
prices.Add(new ReservationPrice { NumberOfDays = 4, Price = 3100 }); 
prices.Add(new ReservationPrice { NumberOfDays = 7, Price = 4000 }); 

What I would like to able to do now is: give me the best price from the list based on a number of days.

So if ask for 3 days the best price from the list is from child one (1000) and two (1200), but there are of course different combinations. How would an algorithm that found the best price from this list look like ?

Here’s how you could do it in Solver Foundation. You’ll note that the way we bind the solution (which items to choose) back to the ReservationPrice data structure kind of stinks. Other than that the code is pretty straightforward.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SolverFoundation.Services;

namespace SolverFoundationSample {

  public class ReservationPrice {
    private double _selectedValue;

    public int Id { get; set; }
    public int NumberOfDays { get; set; }
    public int Price { get; set; }
    public bool Selected {
      get { return _selectedValue > 0.0; }
      set { _selectedValue = value ? 1.0 : 0.0; }
    // It's too bad that we need this field. It would be nicer to bind
    // the Decision to the Selected property - but we need a double-valued
    // property.
    public double SelectedValue {
      get { return _selectedValue; }
      set { _selectedValue = value; }

    public override string ToString() {
      return "Number of days = " + NumberOfDays + ", price = " + Price;

  public class Knapsack {

    public static void Main(string[] args) {

    private static IEnumerable<ReservationPrice> GetData() {
      List<ReservationPrice> prices = new List<ReservationPrice>();
      prices.Add(new ReservationPrice { NumberOfDays = 1, Price = 1000, Id = 0 });
      prices.Add(new ReservationPrice { NumberOfDays = 2, Price = 1200, Id = 1 });
      prices.Add(new ReservationPrice { NumberOfDays = 3, Price = 2500, Id = 2 });
      prices.Add(new ReservationPrice { NumberOfDays = 4, Price = 3100, Id = 3 });
      prices.Add(new ReservationPrice { NumberOfDays = 7, Price = 4000, Id = 4 });
      return prices;

    private static void SolveKnapsack() {
      SolverContext context = SolverContext.GetContext();
      Model model = context.CreateModel();
      var items = new Set(Domain.Any, "items");

      var theData = GetData();

      // Duration of each reservation
      var length = new Parameter(Domain.IntegerNonnegative, "length", items);
      length.SetBinding(theData, "NumberOfDays", "Id");

      // Price for each reservation.
      var price = new Parameter(Domain.IntegerNonnegative, "price", items);
      price.SetBinding(theData, "Price", "Id");

      // The duration requirement.
      int duration = 3;

      var choose = new Decision(Domain.IntegerRange(0, 1), "choose", items);
      choose.SetBinding(theData, "SelectedValue", "Id");
      // Reserve the right number of days.
      model.AddConstraint("c", (Model.Sum(Model.ForEach(items, i => (choose[i] * length[i]))) == duration));

      // Spend as little as possible.
      model.AddGoal("g", GoalKind.Minimize, Model.Sum(Model.ForEach(items, i => (price[i] * choose[i]))));

      var solution = context.Solve();

      var selected = from d in theData
                where d.Selected
                select d.ToString();

      foreach (var s in selected) {

Solving traveling salesman problems using Solver Foundation

Update: see the comments below for some helpful hints. If you are unable to run this with your version of Solver Foundation and Gurobi, consider installing the lp_solve plugin for MSF. More details on this thread.

Here’s an example that I walked through during yesterday’s INFORMS session.  Erwin has two blog postings about Solver Foundation and the traveling salesman problem, but I want to throw in my two cents because I want to emphasize a couple of points:

  1. By combining C# and Solver Foundation Services it is possible to express complex models clearly and succinctly.
  2. It is very easy to build powerful, reusable model libraries using C# and Solver Foundation Services.
  3. Solver Foundation Services code can be used in many different application environments (ASP.Net, silverlight, DB, command line apps, WPF, …) with minimal changes.

The traveling salesman problem is a classical problem in computer science, and you should bow your head in shame if you don’t know about it (and turn in your conference badge if you happen to be in Phoenix). William Cook’s book on the traveling salesman problem is a wonderful read. A salesperson needs to make a tour of a number of cities.  The restrictions are that she wants to visit each city once and only once, and she wants to minimize the distance travelled.  This is perhaps the definitive example of an NP-hard problem.

TSP can be solved using mixed integer programming – optimizing a linear goal with linear constraints, where some of the decision variables are integer.  In this first post I will show how to formulate and solve a TSP model using Solver Foundation Services.  In my second post I will show how to use the Gurobi MIP solver using SFS.   There are many different ways to model the TSP – here is a nice introduction.  My goal is to provide a clear, complete example – not build a “production level” TSP model, so I am going to choose a model formulation that dates back to 1960!  First, I need to establish a couple of building blocks that will help me construct the data for the model.  We need to know the distances between each pair of cities.  Typically we are provided the coordinates of the cities and need to derive the distances.  So I will introduce a Coordinate class that contains properties for the (x, y) coordinates, and properties to convert to latitude and longitude.  Finally, a method that computes the distance between points.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SolverFoundation.Services;

namespace Microsoft.SolverFoundation.Samples {
  class TravelingSalesman {
    // TSP coordinate.
    public class Coordinate {
      public int Name { get; set; }

      // X-coordinate (from TSPLIB)
      public double X { get; set; }

      // Y-coordinate (from TSPLIB)
      public double Y { get; set; }

      public Coordinate(int name, double x, double y) {
        Name = name;
        X = x;
        Y = y;

      // Latitude in radians.
      public double Latitude {
        get { return Math.PI * (Math.Truncate(X) + 5 * (X - Math.Truncate(X)) / 3) / 180; }

      // Longitude in radians.
      public double Longitude {
        get { return Math.PI * (Math.Truncate(Y) + 5 * (Y - Math.Truncate(Y)) / 3) / 180; }

      // Geographic distance between two points (as an integer).
      public int Distance(Coordinate p) {
        double q1 = Math.Cos(Longitude - p.Longitude);
        double q2 = Math.Cos(Latitude - p.Latitude);
        double q3 = Math.Cos(Latitude + p.Latitude);
        // There may rounding difficulties her if the points are close together...just sayin'.
        return (int)(6378.388 * Math.Acos(0.5 * ((1 + q1) * q2 - (1 - q1) * q3)) + 1);

    // TSP city-city arc.
    public class Arc {
      public int City1 { get; set; }
      public int City2 { get; set; }
      public double Distance { get; set; }

    // Burma14 from TSPLIB. Optimal tour = 3323.
    private static Coordinate[] data = new Coordinate[] {
      new Coordinate(0, 16.47, 96.10),
      new Coordinate(1, 16.47, 94.44),
      new Coordinate(2, 20.09, 92.54),
      new Coordinate(3, 22.39, 93.37),
      new Coordinate(4, 25.23, 97.24),
      new Coordinate(5, 22.00, 96.05),
      new Coordinate(6, 20.47, 97.02),
      new Coordinate(7, 17.20, 96.29),
      new Coordinate(8, 16.30, 97.38),
      new Coordinate(9, 14.05, 98.12),
      new Coordinate(10, 16.53, 97.38),
      new Coordinate(11, 21.52, 95.59),
      new Coordinate(12, 19.41, 97.13),
      new Coordinate(13, 20.09, 94.55)

(The data for this 14-city problem comes from the TSPLIB library). If you’ve been following my blog you know that the building blocks of a Solver Foundation model are: sets, parameters, decisions, goals, and constraints. I am going to implement a simple formulation that is centered around the following (indexed) decisions:

  • Assign[i,j]: this is equal to 1 if the optimal tour contains a trip (or arc) from city i to city j.
  • Rank[i]: this is equal to the number of cities visited after arriving at city i.

We have one parameter in our model:

  • Distance[I,j]: the distance from city i to city j.

With that in mind, here’s the model.  Explanation of the goals and constraints follow.

public static void Run() {
      SolverContext context = SolverContext.GetContext();
      Model model = context.CreateModel();

      // ------------
      // Parameters
      Set city = new Set(Domain.IntegerNonnegative, "city");
      Parameter dist = new Parameter(Domain.Real, "dist", city, city);
      var arcs = from p1 in data
                 from p2 in data
                 select new Arc { City1 = p1.Name, City2 = p2.Name, Distance = p1.Distance(p2) };
      dist.SetBinding(arcs, "Distance", "City1", "City2");

      // ------------
      // Decisions
      Decision assign = new Decision(Domain.IntegerRange(0, 1), "assign", city, city);
      Decision rank = new Decision(Domain.RealNonnegative, "rank", city);
      model.AddDecisions(assign, rank);

      // ------------
      // Goal: minimize the length of the tour.
      Goal goal = model.AddGoal("TourLength", GoalKind.Minimize,
        Model.Sum(Model.ForEach(city, i => Model.ForEachWhere(city, j => dist[i, j] * assign[i, j], j => i != j))));

      // ------------
      // Enter and leave each city only once.
      int N = data.Length;
        Model.ForEach(city, i => Model.Sum(Model.ForEachWhere(city, j => assign[i, j],
          j => i != j)) == 1));
        Model.ForEach(city, j => Model.Sum(Model.ForEachWhere(city, i => assign[i, j], i => i != j)) == 1));

      model.AddConstraint("A1", Model.ForEach(city, i => Model.Sum(Model.ForEachWhere(city, j => assign[i, j], j => i != j)) == 1));
      model.AddConstraint("A2", Model.ForEach(city, j => Model.Sum(Model.ForEachWhere(city, i => assign[i, j], i => i != j)) == 1));

      // Forbid subtours (Miller, Tucker, Zemlin - 1960...)
          i => Model.ForEachWhere(city,
            j => rank[i] + 1 <= rank[j] + N * (1 - assign[i, j]),
            j => Model.And(i != j, i >= 1, j >= 1)

      Solution solution = context.Solve();

      // Retrieve solution information.
      Console.WriteLine("Cost = {0}", goal.ToDouble());
      var tour = from p in assign.GetValues() where (double)p[0] > 0.9 select p[2];
      foreach (var i in tour.ToArray()) {
        Console.Write(i + " -> ");

In my humble opinion, the “Parameter data =” line is an awesome example of the power of LINQ data binding in Solver Foundation.  We generate the 2D matrix of distances using a single LINQ expression. It would be incredibly easy to change the code to retrieve the coordinate data from a database (perhaps using a LINQ expression once again), a file, or even a user application.

The goal is straightforward: minimize the distance traveled.  This is a product of the selected arcs and the distance matrix.   We have two types of constraints:

  • Assignment constraints: these ensure that we enter and leave each city only once.
  • Subtour constraints: these ensure that we do not have any subtours. In a four city problem {A, B, C, D}, for example, we cannot have two cycles (A, B), (C, D). We need to have one tour that contains all the cities.

The assignment constraints are easy using the ForEach and ForEachWhere operations.  I use ForEachWhere because I want to disallow arcs that enter and leave the same city – that doesn’t make sense.  The subtour constraint is a little more complicated. It relates the “assign” and “rank” decisions. The key fact is that if there is an arc from city i to city j, rank[i] + 1 == j. Of course, if the (i, j) arc is not part of the optimal tour then all bets are off. Last note: notice that I can mix parameters, decisions, and C# variables in my expressions.

Getting the cost is very easy using goal.ToDouble().  We can get the tour using either Assign or Rank.  I have chosen to use Assign because it gives me another opportunity to use LINQ.  When you call GetValues() on a decision, you get arrays that contain the value along with the indexes for each decision.  In this case, the last entry in the array is the one we are interested in. There are other ways to conveniently query decsision results, I’ll save that for another time.

The next post will show how we can use Solver Foundation’s plug-in model to tune the behavior of the Gurobi MIP solver.

Two way data binding using LINQ and Solver Foundation Services

Recently on the Microsoft Solver Foundation discussion boards there was a question about two-way data binding and Solver Foundation Services.  The MSF-SFS Programming Primer has plenty of examples for helping you learn to use Solver Foundation Services in your .Net applications.  Sample 5 is about binding output values – user “detond” from our discussion boards extended the example to go against a SQL DB, and I have cleaned up the code.  A few notes:

    1. You’ll need to create a table named “ProductionCapacity” with nvarchar(max) Country, int MaxProduction, float Output columns.
    2. Create rows for Venezuela and SaudiArabia, with MaxProduction = 9000, 6000 to reproduce sample 5.
    3. The using() statement is necessary to prevent a SQL connection from leaking.
    4. db.SubmitChanges() is necessary to commit the changes back to the DB.  Otherwise only your in-memory copy is modified.
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Text;
using Microsoft.SolverFoundation.Services;

namespace OutputBindings {

  [Table(Name = "ProductionCapacity")]
  public class Production {
    [Column(IsPrimaryKey = true)]
    public string Country;

    private int _MaxProduction;

    [Column(Storage = "_MaxProduction")]
    public int MaxProduction {
      get { return this._MaxProduction; }
      set { this._MaxProduction = value; }

    private double _Output;

    [Column(Storage = "_Output")]
    public double Output {
      get { return this._Output; }
      set { this._Output = value; }

  class Program {
    static void Main(string[] args) {

      Decision vz, sa;
      Solution solution = null;
      using (DataContext db = new DataContext(@"Data Source=xxx\yyy; Initial Catalog=MSF; Integrated Security=True")) {
        db.Log = Console.Out; // Let's see what's happening.

        // Get a typed table to run queries
        Table<Production> Productions = db.GetTable<Production>();

        // Get the context and creating a new model.
        SolverContext context = SolverContext.GetContext();
        Model model = context.CreateModel();

        // Create two decision variables representing the number of barrels to
        // purchase from two countries.
        // AddDecisions tells the model about the two variables.
        vz = new Decision(Domain.RealNonnegative, "barrels_venezuela");
        sa = new Decision(Domain.RealNonnegative, "barrels_saudiarabia");

        vz.SetBinding(from row in Productions where row.Country == "Venezuela"
           select row, "Output");
        sa.SetBinding(from row in Productions where row.Country == "SaudiArabia"
           select row, "Output");
        model.AddDecisions(vz, sa);

        Parameter maxvz = new Parameter(Domain.RealNonnegative, 
        Parameter maxsa = new Parameter(Domain.RealNonnegative,   

        // To get the same results as in the sample, set MaxProduction=9000 for  
        // Venezuela, MaxProduction=6000 for SaudiArabia in your DB table.
        maxvz.SetBinding(from row in Productions where row.Country == "Venezuela" 
            select row, "MaxProduction");
        maxsa.SetBinding(from row in Productions where row.Country == "SaudiArabia" 
            select row, "MaxProduction");
        model.AddParameters(maxvz, maxsa);

        // Adding five contraints. The first line is two contraints giving the
        // allowable range for the two decision varibles. The other contraints put
        // minimums on the total yield of three products.
        0 <= vz <= maxvz,
        0 <= sa <= maxsa);
        0.3 * sa + 0.4 * vz >= 2000,
        0.4 * sa + 0.2 * vz >= 1500,
        0.2 * sa + 0.3 * vz >= 500);

        model.AddGoal("cost", GoalKind.Minimize, 20 * sa + 15 * vz);
        solution = context.Solve(new SimplexDirective());

        context.PropagateDecisions(); // Propagate values to the in-memory rep.
        db.SubmitChanges(); // Commit to DB.

      if (solution != null) {
        Report report = solution.GetReport();
        Console.WriteLine("vz: {0}, sa: {1}", vz, sa);
        Console.Write("{0}", report);