Learn R Programming

cplexAPI (version 1.4.0)

cplexAPI-package: R Interface to C API of IBM ILOG CPLEX

Description

A low level interface to IBM ILOG CPLEX.

Arguments

Details

The package cplexAPI provides access to the callable library of IBM ILOG CPLEX from within R.

References

The IBM ILOG CPLEX home page at https://www.ibm.com/support/knowledgecenter/SSSA5P.

Examples

Run this code
# NOT RUN {
# load package
library(cplexAPI)

# Open a CPLEX environment
env <- openEnvCPLEX()

# Create a problem object
prob <- initProbCPLEX(env)

# Assign a name to the problem object
chgProbNameCPLEX(env, prob, "sample")

# Prepare data structures for the problem object
# Number of columns and rows
nc <- 3
nr <- 3

# Objective function
obj <- c(5, 4, 3)

# Right hand side
rhs <- c(5, 11, 8)

# Sense of the right hand side
sense <- rep("L", 3)

# Variable lower bounds
lb <- rep(0, 3)

# Variable upper bounds
ub <- rep(CPX_INFBOUND, 3)

# Column and row names
cn <- c("x1", "x2", "x3")
rn <- c("q1", "q2", "q3")

# The constraint matrix is passed in column major order format
# Be careful here: all indices start with 0! Begin indices of rows
beg <- c(0, 3, 6)

# Number of non-zero elements per row
cnt <- rep(3, 3)

# Column indices
ind <- c(0, 1, 2, 0, 1, 2, 0, 1, 2)

# Non-zero elements
val <- c(2, 4, 3, 3, 1, 4, 1, 2, 2)

# Load problem data
copyLpwNamesCPLEX(env, prob, nc, nr, CPX_MAX, obj, rhs, sense,
                  beg, cnt, ind, val, lb, ub, NULL, cn, rn)

# Solve the problem using the simplex algorithm
lpoptCPLEX(env, prob)

# Retrieve solution after optimization
solutionCPLEX(env, prob)

# Free memory, allacated to the problem object
delProbCPLEX(env, prob)
closeEnvCPLEX(env)
# }

Run the code above in your browser using DataLab