You are on page 1of 5

getActiveConstraints = function(point) { x = point[1] y = point[2] E = c(1:4) #constraint identifier C = c(2-x-y, 2+x-2*y, x + 0*y,0*x + y) #Constraint Set Active_point = NULL #vector

containing the indexes of the active constra ints, initially empty for (i in E){ if (C[i] == 0) { Active_point = c(Active_point,i) } else{ Active_point = c(Active_point,0) } } return(Active_point) #vector containing the indexes of the active con straints return a vector [0,0,3,4], non zero entries indicate active constraints } getP_k = function(G,x_k,c,W,A_c){ g = G %*% x_k + c #compute g A = NULL bvec = NULL for (i in W){ if (i > 0){ A = rbind (A,A_c[i,]) } } #print("getP_k(): A is:") #print(A) #A is the matrix of constraints such that their indexes are in W i =1 while (i <= dim(A)[1]){ #bvec holds the right hand side of the constrait s bvec = c(bvec,0) #populate bvec i = i +1 } #Now solve the Quadratic subproblem minimizing p Dmat = G dvec = -g Amat = t(A) meq = dim(A)[1] list =solve.QP(Dmat,dvec,Amat,bvec,meq) p = list$solution print("p is:") print(p) return(p) #end the subproblem solver #the constraint set for the subproblem #the right hand side of the constraints (all 0's).

} getMultipliers = function(W,x_k,G,c,A_c){ g = G %*% x_k + c #compute g A = NULL #the constraint set a_i s.t i in W Lambda = NULL for (i in W){ #print(i) if (i > 0){ A = rbind (A,A_c[i,]) # active constraint matrix: rows a re a_i^T such that i's are in W } } #print("getMultipliers() A is:") #print(A) #print("A^T is:") #print(t(A)) #print("g is:") #print(g) #solve lambda: t(A) %*% lambda = g if(dim(t(A))[1] == dim(t(A))[2]){ Lambda = solve(t(A),g) #the Lambda_i's are in order of the a_i's which are in order of the i's in W #print("lambda are:") #print(Lambda) return(Lambda) } else{ Lambda =c(Lambda,g[1]/A[1]) } } greaterZero = function(Lambda){ flag = 1 #assume all lambdas are greater than or equal to 0 for (i in Lambda){ if (i < 0){ #if there is a vector less than 0 flag = 0 } } return(flag) #if all >=0 flag = 1 else flag = 0 } getJ = function(Lambda,W){ #get j the index of the constraint that has the minim um multiplier Lambda_j #Lambda_j = minimum(Lambda) W = W[!W == 0] #strip the 0's from W #print("getJ() stripped W") #print(W)

i = which.min(Lambda) #get the index of the minimum multiplier #print("minimum lambda Lambda_i index is:") #print(i) j = W[i] #use i to index into W[i] which gives j, index of the minimum m ultiplier #print("This i corresponds to constraint j:") #print(j) return(j) } getAlpha = function(b,p_k,x_k, W,A_c){ #print("getAlpha") notW = c(1,2,3,4) - W PotentialBlocking = NULL #hold the indexes for potential blocking constr aints ValueVector = NULL #print("these constraints dont belong in W") for (i in notW){ #notW = w = c(1,2,3,4)-c(1,2,0,0) if (i> 0) { #print(A_c[i,]) Value = A_c[i,] %*% p_k #for all i not in W compute a_i^T p_k if (Value < 0) { #select those i for which a_i^T p_k <0 ValueVector = c(ValueVector, (b[i] - A_c[i,] %*% x_k)/Value) #compute the (b[i] - A_c[i,] %*% x_k)/value for all these i PotentialBlocking = c(PotentialBlocking,i) #as ( b[i] - A_c[i,] %*% x_k)/value are added to ValueVector i's are added to Potentia lBlocking } } } minValue = min(ValueVector) #get the minimum value minIndex = which.min(ValueVector) #use this to index into PotentialBlock ing to obtain the blocking constraint BlockingConstraint = PotentialBlocking[minIndex] #this is the i, where c onstraint i minimizes 16.41 alpha = min(1,minValue) # \return alpha return(list(alpha,BlockingConstraint)) } QP.solve = function(init =c(1,1)) { #initial point is input x = NULL #1 *n y = NULL #1 *n vectors that hold the x_k's initially empty x[1] = init[1] y[1] = init[2] #initial point k=1 G = rbind(c(2,-2),c(-2,4)) #G matrix n*n c = rbind(-2,-6) # c vector n*1 A_c = rbind(c(-1,-1),c(1,-2),c(1,0),c(0,1)) #constraint matrix: rows are a_i^T b = c(-2,-2,0,0) #1*n

Active_point = getActiveConstraints(c(x[1],y[1])) #Active set at (x_1,y_ 1) returna a vector [0,0,3,4], non zero entries indicate active constraints W = NULL #working set is initially empty, W is a matrix where each row i s a vector with non zero entries indicate active constraints p = NULL #p is initially empty,p is a matrix with entry k being a vector p_k alpha = NULL W = rbind(W,Active_point) #working set is a subset of active set, in thi s case W_1 = Active set. for(k in c(1:10)){ print("k is") print(k) print("W_k is") print(W[k,]) x_k = rbind(x[k],y[k]) #x_k vector n*1 p_k = getP_k(G,x_k,c,W[k,],A_c) #find p_k using W_k, p_k is a ve ctor. 1*n p = rbind(p,p_k) #p is a matrix if (p[k,1] == 0 & p[k,2] == 0){ Lambda = getMultipliers(W[k,],x_k,G,c,A_c) #solve 16.42 with W = W_k and x = x_k if (greaterZero(Lambda)==1){ #all lamdas_i's are greater than 0 print("Solution found:") print(x_k) break } else { #there exists a lambda_i < 0 j = getJ(Lambda,W[k,]) #get j the index of the constraint that has the minimum multiplier x[k+1] = x[k] y[k+1] = y[k] #x_k+1 = x_k print("x_1 and x_2 are:") print(x[k+1]) print(y[k+1] ) W = rbind(W,W[k,]) #W_k+1 = W_k W[k+1,j] = 0 # W_k+1 = W_k+1 -j = W_k -j #print("W_k+1 is") #print(W[k+1,]) } } else { #p_k is not 0 alphaConstraint = getAlpha(b,p_k,x_k, W[k,],A_c) #return a list = [alpha,Blocking constraint] #print("the returned list: alpha and blocking constraint

is") #print(alphaConstraint[[1]]) #print(alphaConstraint[[2]]) alpha_k = alphaConstraint[[1]] ha_k BlockingConstraint = alphaConstraint[[2]] potential blocking constraint x_kp1 = x_k + alpha_k * p_k x[k+1] = x_kp1[1] y[k+1] = x_kp1[2] print("x_1 and x_2 are:") print(x[k+1]) print(y[k+1] ) if (alpha_k <1){ #step along p_k was blocked by some con straint (here the BlockingConstraint) W = rbind(W,W[k,]) #W_k+1 = W_k W[k+1,BlockingConstraint] = BlockingConstraint # W_k+1 = W_k+1 + i = W_k + i } else{ W = rbind(W,W[k,]) #W_k+1 = W_k } } } } QP.solve() #this is the #this is alp

#xk+1 = ......

You might also like