Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static double[] MinVariance(double[,] Q, double[] R)
- {
- double d = 0.0;
- double u = 1.0;
- double W_d = 0.1;
- double W_u = 0.5;
- double r = 0.5;
- int n = Q.GetLength(0);
- double[] b = new double[n];
- int[] indiceVector = new int[n];
- double[] combinedVector = new double[n];
- for (int i = 0; i < n; i++)
- {
- indiceVector[i] = i;
- combinedVector[i] = 1;
- }
- var constraints = new List<LinearConstraint>()
- {
- //Sum x_iR_i >= r=0.5
- new LinearConstraint(numberOfVariables: n)
- {
- VariablesAtIndices = indiceVector,
- CombinedAs = R,
- ShouldBe = ConstraintType.GreaterThanOrEqualTo,
- Value = r
- },
- // Sum x_i >= w_d=0.1
- new LinearConstraint(numberOfVariables: n)
- {
- VariablesAtIndices = indiceVector,
- CombinedAs = combinedVector,
- ShouldBe = ConstraintType.GreaterThanOrEqualTo,
- Value = W_d
- },
- // Sum x_i <= w_u=0.5
- new LinearConstraint(numberOfVariables: n)
- {
- VariablesAtIndices = indiceVector,
- CombinedAs = combinedVector,
- ShouldBe = ConstraintType.LesserThanOrEqualTo,
- Value = W_u
- }
- };
- //x_i >= d=0
- for (int i = 0; i < n; i++)
- {
- constraints.Add
- (
- new LinearConstraint(numberOfVariables: 1)
- {
- VariablesAtIndices = new[] { i },
- ShouldBe = ConstraintType.GreaterThanOrEqualTo,
- Value = d
- }
- );
- }
- //x_i <= u=1
- for (int i = 0; i < n; i++)
- {
- constraints.Add
- (
- new LinearConstraint(numberOfVariables: 1)
- {
- VariablesAtIndices = new[] { i },
- ShouldBe = ConstraintType.LesserThanOrEqualTo,
- Value = u
- }
- );
- }
- var solver = new GoldfarbIdnani(
- function: new QuadraticObjectiveFunction(Q, b),
- constraints: constraints);
- bool success = solver.Minimize();
- double[] solution = solver.Solution;
- double minValue = solver.Value;
- return solution;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement