title: nonlinear optimization using the Solver Foundation Services
body: <p>I have a non-linear optimization problem with constraints. It can be solved in Microsoft Excel with the Solver add-in, but I am having trouble replicating that in C#.</p>
  
  <p>I installed the Microsoft Solver Foundation dll</p>
  
  <p>This is the example where I adapted my code from : 
  <a href="http://msdn.microsoft.com/en-us/library/gg261758(v=vs.93).aspx?cs-save-lang=1&amp;cs-lang=csharp#code-snippet-3" rel="nofollow">http://msdn.microsoft.com/en-us/library/gg261758(v=vs.93).aspx?cs-save-lang=1&amp;cs-lang=csharp#code-snippet-3</a></p>
  
  <p>Basically, in my code, I have 2 parameters called RHO and NU. I need to get their optimal values which minimizes the sum of squares. I have ONLY 1 value for RHO and 1 value for NU, so I know that putting RHO[s] and NU[s] in my code is wrong, but when I replace them with RHO and NU, I get errors. </p>
  
  <p>Another issue is that, in the example, the code looks for one optimal variable called "fuel" of type "Goal". In my case, I have 2 variables which need to be optimized.</p>
  
  <p>Obviously, the program breaks at the last line, it says: "there is no solution to propagate".</p>
  
  <p>Anyone has an idea how my code could be corrected ?? any help is appreciated !</p>
  
  <p>This is my code: </p>
  
  <pre><code>    class Segment
      {
          public double Strike { get; set; }
          public double VolImp { get; set; }
          public double _Rho_ { get; set; }
          public double _Nu_ { get; set; }
          public double _Alpha_ { get; set; }
      }
  
  
  
         Segment[] segmentData = new Segment[] { 
              new Segment { Strike = 5, VolImp = 53.2608},
              new Segment { Strike = 10, VolImp = 48.3773},
              new Segment { Strike = 20, VolImp = 43.8949},
              new Segment { Strike = 30, VolImp = 40.9367},
              new Segment { Strike = 40, VolImp = 38.891},
              new Segment { Strike = 50, VolImp = 37.417},
              new Segment { Strike = 60, VolImp = 36.2838},
              new Segment { Strike = 70, VolImp = 35.3713},
              new Segment { Strike = 80, VolImp = 34.6192},
              new Segment { Strike = 90, VolImp = 33.9774},
              new Segment { Strike = 100, VolImp = 33.4359},
              new Segment { Strike = 110, VolImp = 32.9747},
              new Segment { Strike = 120, VolImp = 32.5635},
              new Segment { Strike = 130, VolImp = 32.2025},
              new Segment { Strike = 140, VolImp = 31.8917},
              new Segment { Strike = 150, VolImp = 31.6209},
              new Segment { Strike = 160, VolImp = 31.3702},
              new Segment { Strike = 170, VolImp = 31.1596},
              new Segment { Strike = 180, VolImp = 30.9591},
         };
  
  
          SolverContext context = SolverContext.GetContext();
          Model model = context.CreateModel();
  
  
  
          // Parameters
          Set segments = new Set(Domain.Integer, "segments");
  
          Parameter RHO = new Parameter(Domain.Real, "RHO", segments);
          RHO.SetBinding(segmentData, "_Rho_", "Strike");
  
          Parameter NU = new Parameter(Domain.RealNonnegative, "NU", segments);
          NU.SetBinding(segmentData, "_Nu_", "Strike");
  
          Parameter ALPHA = new Parameter(Domain.RealNonnegative, "ALPHA", segments);
          ALPHA.SetBinding(segmentData, "_Alpha_", "Strike");
  
          Parameter MyStrike = new Parameter(Domain.RealNonnegative, "MyStrike", segments);
          MyStrike.SetBinding(segmentData, "Strike", "Strike");
  
          Parameter MyImpVol = new Parameter(Domain.RealNonnegative, "MyImpVol", segments);
          MyImpVol.SetBinding(segmentData, "VolImp", "Strike");
  
          model.AddParameters(RHO, NU, ALPHA, MyStrike, MyImpVol);
  
  
          //Constraints
          model.AddConstraint("rho_bound", Model.ForEach(segments, s =&gt; (-1 &lt;= RHO[s] &lt;= 1))) ;
          model.AddConstraint("nu_bound", Model.ForEach(segments, s =&gt; (0 &lt;= NU[s]) )) ;
          model.AddConstraint("alpha_bound", Model.ForEach(segments, s =&gt; (0 &lt; ALPHA[s]))) ;
  
          double Forward = 100 ;
  
          //Goal
          Goal Mygoal = model.AddGoal("Mygoal", GoalKind.Minimize, Model.Sum(Model.ForEach(segments, s =&gt; Model.Power((MyImpVol[s] - (ALPHA[s] * (NU[s] / ALPHA[s] * Model.Log(Forward / MyStrike[s])) * (Model.Log((Model.Sqrt(1 - 2 * RHO[s] * (NU[s] / ALPHA[s] * Model.Log(Forward / MyStrike[s])) + Model.Power((NU[s] / ALPHA[s] * Model.Log(Forward / MyStrike[s])), 2)) + (NU[s] / ALPHA[s] * Model.Log(Forward / MyStrike[s])) - RHO[s]) / (1 - RHO[s]))) * (1 + (0.25 * RHO[s] * NU[s] * ALPHA[s] + (2 - 3 * RHO[s] * RHO[s]) / 24 * NU[s] * NU[s]) * 6.46407))), 2)))) ;
  
  
          //solve
          context.Solve();
          context.PropagateDecisions();
  </code></pre>