LeastSquareSolution() { //calculate matrix A row length Contract.Assume(constraints.Count > 0); int l = constraints[0].coeffs.Length; int n = l; foreach (Constraint c in constraints) { if (c.relation != Relation.Equal) { n++; } } //this is code without any optimizations, well, almost any int m = constraints.Count; //number of rows in A var A = new double[m * n]; int offset = 0; int slackVarOffset = 0; for (int i = 0; i < m; i++) { double[] coeff = constraints[i].coeffs; //copy coefficients for (int j = 0; j < l; j++) { A[offset + j] = coeff[j]; } Relation r = constraints[i].relation; if (r == Relation.LessOrEqual) { A[offset + l + slackVarOffset++] = 1; //c[i]*x+1*ui=b[i] } else if (r == Relation.GreaterOrEqual) { A[offset + l + slackVarOffset++] = -1; //c[i]*x-1*ui=b[i] } offset += n; } var qp = new QP(); //setting Q for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { double q = 0; for (offset = 0; offset < A.Length; offset += n) { q += A[offset + i] * A[offset + j]; } if (i == j) { qp.SetQMember(i, j, q); } else { qp.SetQMember(i, j, q / 2); } } } var linearCost = new double[n]; for (int k = 0; k < n; k++) { double c = 0; offset = k; for (int j = 0; j < m; j++, offset += n) { c += constraints[j].rightSide * A[offset]; } linearCost[k] = c; } qp.SetLinearCosts(linearCost); //we have no constraints in qp except the default: solution has to be not negative double[] sol = qp.Solution; if (qp.Status != Status.Optimal) { throw new InvalidOperationException(); } var ret = new double[l]; for (int i = 0; i < l; i++) { ret[i] = sol[i]; } return(ret); }
LeastSquareSolution() { // F: the following lines are a Clousot-nice way of writing this.status == status.Infeasible ==> this.constraints.Count > 0 //Contract.Requires(this.Status == Status.Infeasible); Contract.Assume(this.constraintCoeffs.Count > 0); //calculate matrix A row length int l = this.constraintCoeffs[0].Length; int n = l; foreach (Relation r in relations) { if (r != Relation.Equal) { n++; } } //this is code without any optimizations, well, almost any int m = this.relations.Count;//number of rows in A //if (2 * (m + n) //number of variables in the QP // * (m + n) //number of constraints // > Microsoft.Glee.Channel.SplitThresholdMatrixSize * 4) // return null; double[] A = new double[m * n]; int offset = 0; int slackVarOffset = 0; for (int i = 0; i < m; i++) { double[] coeff = this.constraintCoeffs[i]; //copy coefficients for (int j = 0; j < l; j++) { A[offset + j] = coeff[j]; } Relation r = relations[i]; if (r == Relation.LessOrEqual) { A[offset + l + slackVarOffset++] = 1; //c[i]*x+1*ui=b[i] } else if (r == Relation.GreaterOrEqual) { A[offset + l + slackVarOffset++] = -1;//c[i]*x-1*ui=b[i] } offset += n; } QP qp = new QP(); //setting Q for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { double q = 0; for (offset = 0; offset < A.Length; offset += n) { q += A[offset + i] * A[offset + j]; } if (i == j) { qp.SetQMember(i, j, q); } else { qp.SetQMember(i, j, q / 2); } } } double[] linearCost = new double[n]; for (int k = 0; k < n; k++) { double c = 0; offset = k; for (int j = 0; j < m; j++, offset += n) { Contract.Assert(j < this.rightSides.Count); c += this.rightSides[j] * A[offset]; } linearCost[k] = c; } qp.SetLinearCosts(linearCost); //we have no constraints in qp except the default: solution has to be not negative double[] sol = qp.Solution; if (qp.Status != Status.Optimal) { throw new InvalidOperationException(); } double[] ret = new double[l]; for (int i = 0; i < l; i++) { ret[i] = sol[i]; } return(ret); }
LeastSquareSolution() { // F: the following lines are a Clousot-nice way of writing this.status == status.Infeasible ==> this.constraints.Count > 0 //Contract.Requires(this.Status == Status.Infeasible); Contract.Assume(this.constraintCoeffs.Count > 0); //calculate matrix A row length int l = this.constraintCoeffs[0].Length; int n = l; foreach (Relation r in relations) if (r != Relation.Equal) n++; //this is code without any optimizations, well, almost any int m = this.relations.Count;//number of rows in A //if (2 * (m + n) //number of variables in the QP // * (m + n) //number of constraints // > Microsoft.Glee.Channel.SplitThresholdMatrixSize * 4) // return null; double[] A = new double[m * n]; int offset = 0; int slackVarOffset = 0; for (int i = 0; i < m; i++) { double[] coeff = this.constraintCoeffs[i]; //copy coefficients for (int j = 0; j < l; j++) A[offset + j] = coeff[j]; Relation r = relations[i]; if (r == Relation.LessOrEqual) A[offset + l + slackVarOffset++] = 1; //c[i]*x+1*ui=b[i] else if (r == Relation.GreaterOrEqual) A[offset + l + slackVarOffset++] = -1;//c[i]*x-1*ui=b[i] offset += n; } QP qp = new QP(); //setting Q for (int i = 0; i < n; i++) for (int j = i; j < n; j++) { double q = 0; for (offset = 0; offset < A.Length; offset += n) q += A[offset + i] * A[offset + j]; if (i == j) qp.SetQMember(i, j, q); else qp.SetQMember(i, j, q / 2); } double[] linearCost = new double[n]; for (int k = 0; k < n; k++) { double c = 0; offset = k; for (int j = 0; j < m; j++, offset += n) { Contract.Assert(j < this.rightSides.Count); c += this.rightSides[j] * A[offset]; } linearCost[k] = c; } qp.SetLinearCosts(linearCost); //we have no constraints in qp except the default: solution has to be not negative double[] sol = qp.Solution; if (qp.Status != Status.Optimal) throw new InvalidOperationException(); double[] ret = new double[l]; for (int i = 0; i < l; i++) ret[i] = sol[i]; return ret; }