R Under development (unstable) (2024-07-18 r86905 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 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(lpSolveAPI) > > dirSep <- .Platform$file.sep > tests.dir <- paste(system.file(package = "lpSolveAPI"), "tests", sep = dirSep) > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "absolute1.lp", sep = dirSep)) > > m <- dim(lp)[1] > n <- dim(lp)[2] > > R <- make.lp(0, n) > > obj <- numeric(n) > for(j in 1:n) obj[j] <- get.mat(lp, 0, j) > set.objfn(R, obj) > > for(i in 1:m) { + xt <- numeric(n) + for(j in 1:n) xt[j] <- get.mat(lp, i, j) + add.constraint(R, xt, get.constr.type(lp)[i], get.constr.value(lp)[i]) + } > > dimnames(R) <- list(paste("R", 1:9, sep = ""), + c("x1", "x2", "x3", "x4", "x2abs", "x4abs")) > > lp.control(R, sense = "max")$sense [1] "maximize" > set.bounds(R, lower = rep(-Inf, 2), upper = rep(Inf, 2), columns = c(2, 4)) > set.bounds(R, lower = 1.1, upper = 10, columns = 3) > > solve(lp) [1] 0 > solve(R) [1] 0 > > vars <- c(3.75, 1.25, 1.1, -0.25, 1.25, 0.25) > opt <- 2.6 > > stopifnot(all.equal(get.variables(lp), vars)) > stopifnot(all.equal(get.variables(R), vars)) > > stopifnot(all.equal(get.objective(lp), opt)) > stopifnot(all.equal(get.objective(R), opt)) > > rm(lp, m, n, R, obj, j, i, xt, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "binary1.lp", sep = dirSep)) > > R <- make.lp(4, 4) > dimnames(R) <- list(paste("r", 1:4, sep = "_"), paste("x", 1:4, sep = "")) > > set.column(R, 1, c(1, 2, -1, 0)) > set.column(R, 2, c(1, -1, 3, 0)) > set.column(R, 3, c(0, 0, 0, 1)) > set.column(R, 4, c(0, 0, 0, 1)) > > set.objfn(R, c(-1, -2, 0.1, 3)) > > set.constr.type(R, c("<=", rep(">=", 3))) > set.rhs(R, c(5, 0, 0, 0.5)) > > set.type(R, 3, "binary") > > solve(lp) [1] 0 > solve(R) [1] 0 > > vars <- c(1.66667, 3.33333, 1, 0) > opt <- -8.23333 > > stopifnot(all.equal(get.variables(lp), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(R), vars, tolerance = 1e-5)) > > stopifnot(all.equal(get.objective(lp), opt, tolerance = 1e-5)) > stopifnot(all.equal(get.objective(R), opt, tolerance = 1e-5)) > > rm(lp, R, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "free1.lp", sep = dirSep)) > mps <- read.lp(paste(tests.dir, "free1.mps", sep = dirSep)) > > R <- make.lp(0, 4) > row.add.mode(R, "on") [1] "on" > set.objfn(R, c(1, 2, -4, -3)) > add.constraint(R, c(1, 1, 0, 0), "<=", 5) > add.constraint(R, c(2, -1, 0, 0), ">=", 0) > add.constraint(R, c(-1, 3, 0, 0), ">=", 0) > add.constraint(R, c(0, 0, 1, 1), ">=", 0.5) > row.add.mode(R, "off") [1] "off" > > dimnames(R) <- list(paste("R", 1:4, sep = ""), paste("x", 1:4, sep = "")) > > lp.control(R, sense = "max")$sense [1] "maximize" > set.bounds(R, lower = rep(-Inf, 2), upper = rep(Inf, 2), columns = c(2, 4)) > set.bounds(R, lower = 1.1, upper = 10, columns = 3) > > solve(lp) [1] 0 > solve(mps) [1] 0 > solve(R) [1] 0 > > vars <- c(1.66667, 3.33333, 1.1, -0.6) > opt <- 5.73333 > > stopifnot(all.equal(get.variables(lp), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(mps), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(R), vars, tolerance = 1e-5)) > > stopifnot(all.equal(get.objective(lp), opt, tolerance = 1e-5)) > stopifnot(all.equal(-get.objective(mps), opt, tolerance = 1e-5)) > stopifnot(all.equal(get.objective(R), opt, tolerance = 1e-5)) > > rm(lp, mps, R, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "integer1.lp", sep = dirSep)) > mps <- read.lp(paste(tests.dir, "integer1.mps", sep = dirSep)) > > R <- make.lp(4, 4) > dimnames(R) <- list(paste("r", 1:4, sep = "_"), paste("x", 1:4, sep = "")) > > set.column(R, 1, c(1, 2, -1, 0)) > set.column(R, 2, c(1, -1, 3, 0)) > set.column(R, 3, c(0, 0, 0, 1)) > set.column(R, 4, c(0, 0, 0, 1)) > > set.objfn(R, c(-1, -2, 0.1, 3)) > > set.constr.type(R, c("<=", rep(">=", 3))) > set.rhs(R, c(5, 0, 0, 0.5)) > > set.type(R, 3, "integer") > set.bounds(R, lower = 1.1, columns = 3) > > solve(lp) [1] 0 > solve(mps) [1] 0 > solve(R) [1] 0 > > vars <- c(1.66667, 3.33333, 2, 0) > opt <- -8.13333 > > stopifnot(all.equal(get.variables(lp), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(mps), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(R), vars, tolerance = 1e-5)) > > stopifnot(all.equal(get.objective(lp), opt, tolerance = 1e-5)) > stopifnot(all.equal(get.objective(mps), opt, tolerance = 1e-5)) > stopifnot(all.equal(get.objective(R), opt, tolerance = 1e-5)) > > rm(lp, mps, R, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "semi-continuous1.lp", sep = dirSep)) > mps <- read.lp(paste(tests.dir, "semi-continuous1.mps", sep = dirSep)) > > R <- make.lp(4, 4) > dimnames(R) <- list(paste("r", 1:4, sep = "_"), paste("x", 1:4, sep = "")) > > set.column(R, 1, c(1, 2, -1, 0)) > set.column(R, 2, c(1, -1, 3, 0)) > set.column(R, 3, c(0, 0, 0, 1)) > set.column(R, 4, c(0, 0, 0, 1)) > > set.objfn(R, c(1, 2, -4, -3)) > > set.constr.type(R, c("<=", rep(">=", 3))) > set.rhs(R, c(5, 0, 0, 0.5)) > > lp.control(R, sense = "max")$sense [1] "maximize" > set.bounds(R, lower = 1.1, upper = 10, columns = 3) > set.semicont(R, 3) > > vars <- c(1.66667, 3.33333, 0, 0.5) > opt <- 6.83333 > > solve(lp) [1] 0 > solve(mps) [1] 0 > solve(R) [1] 0 > > stopifnot(all.equal(get.variables(lp), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(mps), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(R), vars, tolerance = 1e-5)) > > stopifnot(all.equal(get.objective(lp), opt, tolerance = 1e-5)) > stopifnot(all.equal(-get.objective(mps), opt, tolerance = 1e-5)) > stopifnot(all.equal(get.objective(R), opt, tolerance = 1e-5)) > > rm(lp, mps, R, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "semi-continuous2.lp", sep = dirSep)) > > R <- make.lp(4, 4) > dimnames(R) <- list(paste("R", 1:4, sep = ""), paste("x", 1:4, sep = "")) > > set.column(R, 1, c(1, 2, -1, 0)) > set.column(R, 2, c(1, -1, 3, 0)) > set.column(R, 3, c(0, 0, 0, 1)) > set.column(R, 4, c(0, 0, 0, 1)) > > set.objfn(R, c(1, 2, -0.1, -3)) > > set.constr.type(R, c("<=", rep(">=", 3))) > set.rhs(R, c(5, 0, 0, 0.5)) > > lp.control(R, sense = "max")$sense [1] "maximize" > set.bounds(R, lower = 1.1, upper = 10, columns = 3) > set.semicont(R, 3) > > vars <- c(1.66667, 3.33333, 1.1, 0) > opt <- 8.22333 > > solve(lp) [1] 0 > solve(R) [1] 0 > > stopifnot(all.equal(get.variables(lp), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(R), vars, tolerance = 1e-5)) > > stopifnot(all.equal(get.objective(lp), opt, tolerance = 1e-5)) > stopifnot(all.equal(get.objective(R), opt, tolerance = 1e-5)) > > rm(lp, R, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "semi-continuous3.lp", sep = dirSep)) > > R <- make.lp(4, 4) > dimnames(R) <- list(paste("R", 1:4, sep = ""), paste("x", 1:4, sep = "")) > > set.column(R, 1, c(1, 2, -1, 0)) > set.column(R, 2, c(1, -1, 3, 0)) > set.column(R, 3, c(0, 0, 0, 1)) > set.column(R, 4, c(0, 0, 0, 1)) > > set.objfn(R, c(1, 2, -0.1, -3)) > > set.constr.type(R, c("<=", rep(">=", 3))) > set.rhs(R, c(5, 0, 0, 0.5)) > > lp.control(R, sense = "max")$sense [1] "maximize" > set.bounds(R, lower = 1.1, upper = 10, columns = 3) > set.semicont(R, 3) > set.type(R, 3, "integer") > > vars <- c(1.66667, 3.33333, 2, 0) > opt <- 8.13333 > > solve(lp) [1] 0 > solve(R) [1] 0 > > stopifnot(all.equal(get.variables(lp), vars, tolerance = 1e-5)) > stopifnot(all.equal(get.variables(R), vars, tolerance = 1e-5)) > > stopifnot(all.equal(get.objective(lp), opt, tolerance = 1e-5)) > stopifnot(all.equal(get.objective(R), opt, tolerance = 1e-5)) > > rm(lp, R, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "sos1.lp", sep = dirSep)) > mps <- read.lp(paste(tests.dir, "sos1.mps", sep = dirSep)) > > R <- make.lp(2, 5) > dimnames(R) <- list(paste("Rc", 1:2, sep = ""), paste("x", 1:5, sep = "")) > > set.objfn(R, c(-1, -1, -3, -2, -2)) > set.row(R, 1, c(-1, -1, 1, 1, 0)) > set.row(R, 2, c(1, 0, 1, -3, 0)) > set.constr.type(R, rep("<=", 2)) > set.rhs(R, rep(30, 2)) > > set.bounds(R, upper = c(40, 1, Inf, Inf, 1)) > add.SOS(R, "SOS1", 1, 1, 1:5, rep(1, 5)) > > vars <- c(0, 0, 30, 0, 0) > opt <- -90 > > solve(lp) [1] 0 > solve(mps) [1] 0 > solve(R) [1] 0 > > > # These tests fail w/ clang-3.5 > #stopifnot(all.equal(get.variables(lp), vars)) > #stopifnot(all.equal(get.variables(mps), vars)) > #stopifnot(all.equal(get.variables(R), vars)) > > #stopifnot(all.equal(get.objective(lp), opt)) > #stopifnot(all.equal(get.objective(mps), opt)) > #stopifnot(all.equal(get.objective(R), opt)) > > rm(lp, mps, R, vars, opt) > > > ################################################################################ > ################################################################################ > > lp <- read.lp(paste(tests.dir, "sos2.lp", sep = dirSep)) > mps <- read.lp(paste(tests.dir, "sos2.mps", sep = dirSep)) > > R <- make.lp(2, 5) > dimnames(R) <- list(paste("Rc", 1:2, sep = ""), paste("x", 1:5, sep = "")) > > set.objfn(R, c(-1, -1, -3, -2, -2)) > set.row(R, 1, c(-1, -1, 1, 1, 0)) > set.row(R, 2, c(1, 0, 1, -3, 0)) > set.constr.type(R, rep("<=", 2)) > set.rhs(R, rep(30, 2)) > > set.bounds(R, upper = c(40, 1, Inf, Inf, 1)) > add.SOS(R, "SOS2", 2, 1, 1:5, rep(1, 5)) > > vars <- c(0, 1, 30, 0, 0) > opt <- -91 > > solve(lp) [1] 0 > solve(mps) [1] 0 > solve(R) [1] 0 > > # These tests will also probably fail w/ clang-3.5 > #stopifnot(all.equal(get.variables(lp), vars)) > #stopifnot(all.equal(get.variables(mps), vars)) > #stopifnot(all.equal(get.variables(R), vars)) > > #stopifnot(all.equal(get.objective(lp), opt)) > #stopifnot(all.equal(get.objective(mps), opt)) > #stopifnot(all.equal(get.objective(R), opt)) > > rm(lp, mps, R, vars, opt) > > > ################################################################################ > ################################################################################ > # > #lp <- read.lp(paste(tests.dir, "sos3.lp", sep = dirSep)) > #mps <- read.lp(paste(tests.dir, "sos3.mps", sep = dirSep)) > # > #R <- make.lp(2, 5) > #dimnames(R) <- list(paste("Rc", 1:2, sep = ""), paste("x", 1:5, sep = "")) > # > #set.objfn(R, c(-1, -1, -3, -2, -2)) > #set.row(R, 1, c(-1, -1, 1, 1, 0)) > #set.row(R, 2, c(1, 0, 1, -3, 0)) > #set.constr.type(R, rep("<=", 2)) > #set.rhs(R, rep(30, 2)) > # > #set.bounds(R, upper = c(40, 1, Inf, Inf, 1)) > #add.SOS(R, "SOS3", 3, 1, 1:5, rep(1, 5)) > # > #vars <- c(0, 1, 30.75, 0.25, 0) > #opt <- -93.75 > # > #solve(lp) > #solve(mps) > #solve(R) > # > #stopifnot(all.equal(get.variables(lp), vars)) > #stopifnot(all.equal(get.variables(mps), vars)) > #stopifnot(all.equal(get.variables(R), vars)) > # > #stopifnot(all.equal(get.objective(lp), opt)) > #stopifnot(all.equal(get.objective(mps), opt)) > #stopifnot(all.equal(get.objective(R), opt)) > # > #rm(lp, mps, R, vars, opt) > # > # > ################################################################################ > ################################################################################ > # > #lp <- read.lp(paste(tests.dir, "sos4.lp", sep = dirSep)) > #mps <- read.lp(paste(tests.dir, "sos4.mps", sep = dirSep)) > # > #R <- make.lp(2, 5) > #dimnames(R) <- list(paste("Rc", 1:2, sep = ""), paste("x", 1:5, sep = "")) > # > #set.objfn(R, c(-1, -1, -3, -2, -2)) > #set.row(R, 1, c(-1, -1, 1, 1, 0)) > #set.row(R, 2, c(1, 0, 1, -3, 0)) > #set.constr.type(R, rep("<=", 2)) > #set.rhs(R, rep(30, 2)) > # > #set.bounds(R, upper = c(40, 1, Inf, Inf, 1)) > #add.SOS(R, "SOS4", 4, 1, 1:5, rep(1, 5)) > # > #vars <- c(40, 1, 50.75, 20.25, 0) > #opt <- -233.75 > # > #solve(lp) > #solve(mps) > #solve(R) > # > #stopifnot(all.equal(get.variables(lp), vars)) > #stopifnot(all.equal(get.variables(mps), vars)) > #stopifnot(all.equal(get.variables(R), vars)) > # > #stopifnot(all.equal(get.objective(lp), opt)) > #stopifnot(all.equal(get.objective(mps), opt)) > #stopifnot(all.equal(get.objective(R), opt)) > # > #rm(lp, mps, R, vars, opt) > # > # > ################################################################################ > ################################################################################ > # > #lp <- read.lp(paste(tests.dir, "sos5.lp", sep = dirSep)) > #mps <- read.lp(paste(tests.dir, "sos5.mps", sep = dirSep)) > # > #R <- make.lp(2, 5) > #dimnames(R) <- list(paste("Rc", 1:2, sep = ""), paste("x", 1:5, sep = "")) > # > #set.objfn(R, c(-1, -1, -3, -2, -2)) > #set.row(R, 1, c(-1, -1, 1, 1, 0)) > #set.row(R, 2, c(1, 0, 1, -3, 0)) > #set.constr.type(R, rep("<=", 2)) > #set.rhs(R, rep(30, 2)) > # > #set.bounds(R, upper = c(40, 1, Inf, Inf, 1)) > #add.SOS(R, "SOS5", 5, 1, 1:5, rep(1, 5)) > # > #vars <- c(40, 1, 50.75, 20.25, 1) > #opt <- -235.75 > # > #solve(lp) > #solve(mps) > #solve(R) > # > #stopifnot(all.equal(get.variables(lp), vars)) > #stopifnot(all.equal(get.variables(mps), vars)) > #stopifnot(all.equal(get.variables(R), vars)) > # > #stopifnot(all.equal(get.objective(lp), opt)) > #stopifnot(all.equal(get.objective(mps), opt)) > #stopifnot(all.equal(get.objective(R), opt)) > # > #rm(lp, mps, R, vars, opt) > # > # > ################################################################################ > ################################################################################ > > rm(dirSep, tests.dir) > > > ################################################################################ > ################################################################################ > > lp <- make.lp(0, 3) > > set.objfn(lp, c(1, 1, 1)) > > add.constraint(lp, c(0, 1, 0), "=", 2) > add.constraint(lp, c(1, 1, 0), "<=", 6) > add.constraint(lp, c(1, 0, -2), ">=", 2) > add.constraint(lp, c(0, 1, 3), "<=", 5) > > lp.control(lp, sense = "max")$sense [1] "maximize" > lp.control(lp, presolve = c("rows"))$presolve [1] "rows" > > print(lp) Model name: C1 C2 C3 Maximize 1 1 1 R1 0 1 0 = 2 R2 1 1 0 <= 6 R3 1 0 -2 >= 2 R4 0 1 3 <= 5 Kind Std Std Std Type Real Real Real Upper Inf Inf Inf Lower 0 0 0 > solve(lp) [1] 0 > print(lp) Model name: C1 C2 C3 Maximize 1 1 1 R2 1 1 0 <= 6 R3 1 0 -2 >= 2 R4 0 1 3 <= 5 Kind Std Std Std Type Real Real Real Upper Inf 2 Inf Lower 0 2 0 > rm(lp) > > > ################################################################################ > ################################################################################ > > lp <- make.lp(0, 3) > > set.objfn(lp, c(1, 1, 1)) > > add.constraint(lp, c(0, 1, 0), "=", 2) > add.constraint(lp, c(1, 1, 0), "<=", 6) > add.constraint(lp, c(1, 0, -2), ">=", 2) > add.constraint(lp, c(0, 1, 3), "<=", 5) > > lp.control(lp, sense = "max")$sense [1] "maximize" > lp.control(lp, presolve = c("cols", "rows"))$presolve [1] "rows" "cols" > > print(lp) Model name: C1 C2 C3 Maximize 1 1 1 R1 0 1 0 = 2 R2 1 1 0 <= 6 R3 1 0 -2 >= 2 R4 0 1 3 <= 5 Kind Std Std Std Type Real Real Real Upper Inf Inf Inf Lower 0 0 0 > solve(lp) [1] 0 > print(lp) Model name: C1 C3 Maximize 1 1 R3 1 -2 >= 2 Kind Std Std Type Real Real Upper 4 1 Lower 0 0 > rm(lp) > > > ################################################################################ > ################################################################################ > > > > proc.time() user system elapsed 0.14 0.07 0.20