Getting solution values using Solver Foundation Services

Please note that Solver Foundation is no longer supported.

There was a question on the Solver Foundation forum about iterating through the results in a Decision. Here’s a code sample of how to do it in two simple cases.

Decision objects are either indexed or scalar. A decision with one index set is essentially a vector, two means matrix. You supply the Sets in the Decision constructor. In the code below, x is an indexed decision. If you don’t supply any sets then you have a non-indexed scalar Decision – that’s what we’re doing with y.

Once you solve the model you can use two different methods to pull out the results. The GetValues method returns an array: one for each value in an indexed decision. In this case, we have one index set that has 10 values, so GetValues will return 10 items. Each item is an array. The first element in the array is the value for the decision for the specific index. The remaining entries are the index values themselves. By running the program below you will get the idea.

Getting the values for non-indexed decisions is easy: just call the GetDouble method.

Here’s the code:

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

namespace DecisionTest {
  class Program {
    static void Main(string[] args) {
      SolverContext context = SolverContext.GetContext();
      Model model = context.CreateModel();
      // Let's create an indexed parameter.
      Set s = new Set(Domain.Real, "s");
      Parameter p = new Parameter(Domain.Real, "p", s);
      // Here's some dummy data.
      var input = Enumerable.Range(1, 10).Select(item => new { Id = item, Value = item });
      p.SetBinding(input, "Value", "Id");

      // Let's create an indexed decision.
      Decision x = new Decision(Domain.Real, "x", s);

      // Here's a scalar decision.
      Decision y = new Decision(Domain.Real, "y");

      // Here are some dummy constraints to tie everything together. Just for fun.
      model.AddConstraint("c1", Model.ForEach(s, i => p[i] == x[i]));
      model.AddConstraint("c2", Model.Sum(Model.ForEach(s, i => x[i])) == y);

      // Extract data from the indexed decision.
      foreach (object[] value in x.GetValues()) {
        Console.WriteLine("x[{0}] = {1}", value[1], value[0]);
      // Extract data from the scalar decision.
      Console.WriteLine("y = {0}", y.GetDouble());

Author: natebrix

Follow me on twitter at @natebrix.

10 thoughts on “Getting solution values using Solver Foundation Services”

  1. Hi Natebrix;

    I’m working on modeling an optimization problem with MSF, which requires indexing decision variables. I’ve posted it here on stackoverflow:
    So far I’ve not been able to find and answer for my question.
    I’ve tried c# arrays for indexing decisions variables, but apparently arrays are not very well suited for indexing Decisions. I know you’re very busy with your new position at FrontlineSolvers (Congrats btw), but I’d very much appreciate it if you could just take a look at my problem.
    Thank you

  2. Unfortunately, it doesn’t work. I can’t set the initial values for a decision (Decision CA0Val = new Decision(Domain.RealNonnegative, “CA0”); CA0Val.SetInitialValue(0);), and I can’t get the values of the decisions before it solves (CA0Val.GetDouble()).

    1. Hi William,

      (Note that Solver Foundation is no longer supported by Microsoft.) I believe the behavior that you can’t get the Decision value *before* you solve is by design (because you supplied the value). You could just use a dictionary to store the values in that case.

      1. “a dictionary to store the values”? I’m trying a simple curvefit with an objective function, just like Excel solver can be set up to do in 5 minutes – other blogs I’ve read say MSF was never set to be able to use an objective function with a set of inputs. There is no simple example like this anywhere I can find.

      2. I’m afraid I don’t know enough about your problem to be able to help you further – all I’ve got is what you told me in your comment, which is why I made the suggestion I made.

        If you have an unconstrained nonlinear model you can use the CompactQuasiNewtonSolver – you can provide a function that specifies the objective, and you’ll get intermediate callbacks as it progresses. Here is a link to a small example:

  3. Thanx for grate post, I have a problem, can you help me?

    I have a table with 3 column:
    -BaseBudget (decimal)
    -Factor (1, 2, 2.5, 3)
    -PaiedBudget (decimal)

    Factor calculate from BaseBudget & PaiedBudget:
    1. PaiedBudget/BaseBudget >= 2.5 ==> factor=3
    2. 2.5 > PaiedBudget/BaseBudget >= 2 ==> factor=2.5
    3. 2 > PaiedBudget/BaseBudget >= 1.5 ==> factor=2
    4. PaiedBudget/BaseBudget factor=1

    I must Maximize: Sum(PaiedBudget * Factor)
    but most: Sum(PaiedBudget) <= Sum(BaseBudget)

    Can you help me please?

  4. Hi Natebrix
    I have a silly question after I read your post.
    About the mathod Model.foreach,
    s=> means I clair index s to use in my function,right?
    But how can it know my index s range?
    Thank you very much!!!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s