R Under development (unstable) (2023-12-13 r85679 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 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 an HTML browser interface to help. Type 'q()' to quit R. > > library(rcdd) If you want correct answers, use rational arithmetic. See the Warnings sections in help pages for functions that do computational geometry. > > ### optimal solution exists -- file samplelp1.ine in cddlib ### > > hrep <- rbind( + c(0, 1, 1, 0, 0), + c(0, 1, 0, 1, 0), + c(0, 1, 0, 0, 1), + c(0, 1, -1, 0, 0), + c(0, 1, 0, -1, 0), + c(0, 1, 0, 0, -1)) > > a <- c(1, 1, 1) > > lpcdd(hrep, a, minimize = FALSE) $solution.type [1] "Optimal" $primal.solution [1] 1 1 1 $dual.solution [1] 0 0 0 1 1 1 $optimal.value [1] 3 > > ### optimal solution exists -- file samplelp2.ine in cddlib ### > > hrep <- rbind( + c("0", "0", "1", "1", "0", "0"), + c("0", "0", "0", "2", "0", "0"), + c("1", "3", "0", "-1", "0", "0"), + c("1", "9/2", "0", "0", "-1", "-1")) > > a <- c("2", "3/5", "0", "0") > > lpcdd(hrep, a) $solution.type [1] "Optimal" $primal.solution [1] "-3" "3" "9/2" "0" $dual.solution [1] "-2" "0" "-7/5" "0" $optimal.value [1] "-21/5" > > ### primal inconsistent problem ### > > hrep <- rbind( + c("0", "0", "1", "0"), + c("0", "0", "0", "1"), + c("0", "-2", "-1", "-1")) > > a <- c("1", "1") > > lpcdd(hrep, a) $solution.type [1] "Inconsistent" $dual.direction [1] "1" "1" "1" > > lpcdd(q2d(hrep), q2d(a)) $solution.type [1] "Inconsistent" $dual.direction [1] 1 1 1 > > ### dual inconsistent problem ### > > hrep <- rbind( + c("0", "0", "1", "0"), + c("0", "0", "0", "1")) > > a <- c("1", "1") > > lpcdd(hrep, a, minimize = FALSE) $solution.type [1] "DualInconsistent" $primal.direction [1] "1" "0" > > lpcdd(q2d(hrep), q2d(a), minimize = FALSE) $solution.type [1] "DualInconsistent" $primal.direction [1] 1 0 > > ### negative Lagrange multipliers > > # needed because of the change in R function "sample" in R-devel > suppressWarnings(RNGversion("3.5.2")) > > set.seed(42) > > d <- 20 > k <- 6 > foo <- matrix(sample(seq(-1000, 1000), k * d, replace = TRUE), ncol = d) > foo <- rbind(foo, diag(d)) > foo <- rbind(foo, - diag(d)) > foo <- cbind(c(rep(0, k), rep(1, 2 * d)), foo) > foo <- cbind(c(rep(1, k), rep(0, 2 * d)), foo) > > w <- sample(seq(-1000, 1000), d, replace = TRUE) > > out <- lpcdd(d2q(foo), d2q(w)) > > out$solution.type [1] "Optimal" > > q2d(out$primal.solution) [1] 0.006018942 -0.683091012 -1.000000000 -1.000000000 -0.110030285 [6] -1.000000000 -1.000000000 -1.000000000 -0.613259326 -1.000000000 [11] -1.000000000 -1.000000000 -0.683061014 -1.000000000 -1.000000000 [16] -0.815424962 -1.000000000 1.000000000 -1.000000000 -1.000000000 > > x <- out$primal.solution > lambda <- qneg(out$dual.solution) ### see tutorial > l <- foo[ , 1] > b <- foo[ , 2] > v <- foo[ , - c(1, 2)] > > ##### check gradient of Lagrangian function zero > all(qsign(qmq(w, qmatmult(rbind(lambda), v))) == 0) [1] TRUE > > ##### check primal feasibility > slack <- qpq(b, qmatmult(v, cbind(x))) > all(qsign(slack) >= 0) [1] TRUE > > ##### check dual feasibility > all(qsign(lambda) * (1 - l) >= 0) [1] TRUE > > ##### check complementary slackness > all(qsign(slack) * qsign(lambda) == 0) [1] TRUE > > ##### number of negative lagrange multipliers (shows exercised relevant code) > sum(qsign(lambda) < 0) [1] 4 > > proc.time() user system elapsed 0.10 0.07 0.17