title: C# Simple Constrained Weighted Average Algorithm with/without Solver
body: <p>I'm at a loss as to why I can't get this seemingly simple problem solved using Microsoft Solver Foundation.</p>
  
  <p>All I need is to modify the weights (numbers) of certain observations to ensure that no 1 observation's weight AS A PERCENTAGE exceeds 25%. This is for the purposes of later calculating a constrained weighted average with the results of this algorithm.</p>
  
  <p>For example, given the 5 weights of { 45, 100, 33, 500, 28 }, I would expect the result of this algorithm to be { 45, <strong>53</strong>, 33, <strong>53</strong>, 28 }, where 2 of the numbers had to be reduced such that they're within the 25% threshold of the new total (<strong>212</strong> = 45+53+33+53+28) while the others remained untouched. <em>Note that even though initially, the 2nd weight of 100 was only 14% of the total (706), as a result of decreasing the 4th weight of 500, it subsequently pushed up the % of the other observations and therein lies the only challenge with this.</em></p>
  
  <p>I tried to recreate this using Solver only for it to tell me that it is the solution is "Infeasible" and it just returns all 1s. <strong>Update:</strong> <em>solution need not use Solver, any alternative is welcome so long as it is fast when dealing with a decent number of weights.</em></p>
  
  <pre><code>var solver = SolverContext.GetContext();
  var model = solver.CreateModel();
  
  var decisionList = new List&lt;Decision&gt;();
  decisionList.Add(new Decision(Domain.IntegerRange(1, 45), "Dec1"));
  decisionList.Add(new Decision(Domain.IntegerRange(1, 100), "Dec2"));
  decisionList.Add(new Decision(Domain.IntegerRange(1, 33), "Dec3"));
  decisionList.Add(new Decision(Domain.IntegerRange(1, 500), "Dec4"));
  decisionList.Add(new Decision(Domain.IntegerRange(1, 28), "Dec5"));
  model.AddDecisions(decisionList.ToArray());
  
  int weightLimit = 25;
  foreach (var decision in model.Decisions)
  {
      model.AddConstraint(decision.Name + "weightLimit", 100 * (decision / Model.Sum(model.Decisions.ToArray())) &lt;= weightLimit);
  }
  model.AddGoal("calcGoal", GoalKind.Maximize, Model.Sum(model.Decisions.ToArray()));
  
  var solution = solver.Solve();
  foreach (var decision in model.Decisions)
  {
      Debug.Print(decision.GetDouble().ToString());
  }
  Debug.Print("Solution Quality: " + solution.Quality.ToString());
  </code></pre>
  
  <p><strong><em>Any help with this would be very much appreciated, thanks in advance.</em></strong></p>