Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. static double[] MinVariance(double[,] Q, double[] R)
  2. {
  3.  
  4. double d = 0.0;
  5. double u = 1.0;
  6. double W_d = 0.1;
  7. double W_u = 0.5;
  8. double r = 0.5;
  9.  
  10. int n = Q.GetLength(0);
  11. double[] b = new double[n];
  12.  
  13.  
  14. int[] indiceVector = new int[n];
  15. double[] combinedVector = new double[n];
  16. for (int i = 0; i < n; i++)
  17. {
  18. indiceVector[i] = i;
  19. combinedVector[i] = 1;
  20. }
  21.  
  22. var constraints = new List<LinearConstraint>()
  23. {
  24. //Sum x_iR_i >= r=0.5
  25. new LinearConstraint(numberOfVariables: n)
  26. {
  27. VariablesAtIndices = indiceVector,
  28. CombinedAs = R,
  29. ShouldBe = ConstraintType.GreaterThanOrEqualTo,
  30. Value = r
  31. },
  32.  
  33. // Sum x_i >= w_d=0.1
  34. new LinearConstraint(numberOfVariables: n)
  35. {
  36. VariablesAtIndices = indiceVector,
  37. CombinedAs = combinedVector,
  38. ShouldBe = ConstraintType.GreaterThanOrEqualTo,
  39. Value = W_d
  40. },
  41.  
  42. // Sum x_i <= w_u=0.5
  43. new LinearConstraint(numberOfVariables: n)
  44. {
  45. VariablesAtIndices = indiceVector,
  46. CombinedAs = combinedVector,
  47. ShouldBe = ConstraintType.LesserThanOrEqualTo,
  48. Value = W_u
  49. }
  50. };
  51.  
  52. //x_i >= d=0
  53. for (int i = 0; i < n; i++)
  54. {
  55. constraints.Add
  56. (
  57. new LinearConstraint(numberOfVariables: 1)
  58. {
  59. VariablesAtIndices = new[] { i },
  60. ShouldBe = ConstraintType.GreaterThanOrEqualTo,
  61. Value = d
  62. }
  63. );
  64. }
  65.  
  66. //x_i <= u=1
  67. for (int i = 0; i < n; i++)
  68. {
  69. constraints.Add
  70. (
  71. new LinearConstraint(numberOfVariables: 1)
  72. {
  73. VariablesAtIndices = new[] { i },
  74. ShouldBe = ConstraintType.LesserThanOrEqualTo,
  75. Value = u
  76. }
  77. );
  78. }
  79.  
  80.  
  81. var solver = new GoldfarbIdnani(
  82. function: new QuadraticObjectiveFunction(Q, b),
  83. constraints: constraints);
  84.  
  85. bool success = solver.Minimize();
  86.  
  87. double[] solution = solver.Solution;
  88. double minValue = solver.Value;
  89.  
  90. return solution;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement