R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("quadprog-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('quadprog') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "solve.QP" > > ### * solve.QP > > flush(stderr()); flush(stdout()) > > ### Name: solve.QP > ### Title: Solve a Quadratic Programming Problem > ### Aliases: solve.QP > ### Keywords: optimize > > ### ** Examples > > # > # Assume we want to minimize: -(0 5 0) %*% b + 1/2 b^T b > # under the constraints: A^T b >= b0 > # with b0 = (-8,2,0)^T > # and (-4 2 0) > # A = (-3 1 -2) > # ( 0 0 1) > # we can use solve.QP as follows: > # > Dmat <- matrix(0,3,3) > diag(Dmat) <- 1 > dvec <- c(0,5,0) > Amat <- matrix(c(-4,-3,0,2,1,0,0,-2,1),3,3) > bvec <- c(-8,2,0) > solve.QP(Dmat,dvec,Amat,bvec=bvec) $solution [1] 0.4761905 1.0476190 2.0952381 $value [1] -2.380952 $unconstrainted.solution [1] 0 5 0 $iterations [1] 3 0 $iact [1] 3 2 > > > > cleanEx(); ..nameEx <- "solve.QP.compact" > > ### * solve.QP.compact > > flush(stderr()); flush(stdout()) > > ### Name: solve.QP.compact > ### Title: Solve a Quadratic Programming Problem > ### Aliases: solve.QP.compact > ### Keywords: optimize > > ### ** Examples > > # > # Assume we want to minimize: -(0 5 0) %*% b + 1/2 b^T b > # under the constraints: A^T b >= b0 > # with b0 = (-8,2,0)^T > # and (-4 2 0) > # A = (-3 1 -2) > # ( 0 0 1) > # we can use solve.QP.compact as follows: > # > Dmat <- matrix(0,3,3) > diag(Dmat) <- 1 > dvec <- c(0,5,0) > Aind <- rbind(c(2,2,2),c(1,1,2),c(2,2,3)) > Amat <- rbind(c(-4,2,-2),c(-3,1,1)) > bvec <- c(-8,2,0) > solve.QP.compact(Dmat,dvec,Amat,Aind,bvec=bvec) $solution [1] 0.4761905 1.0476190 2.0952381 $value [1] -2.380952 $unconstrained.solution [1] 0 5 0 $iterations [1] 3 0 $iact [1] 3 2 > > > > ### *