ROI.plugin.gurobi Examples

Sys.setenv("ROI_LOAD_PLUGINS" = FALSE)
suppressPackageStartupMessages(library("ROI"))
library("ROI.plugin.gurobi")

Linear programming (LP)

[ \begin{array}{rrrrr} \text{minimize} & 7 x_1 & + & 8 x_2 \ \text{subject to} & 3 x_1 & + & 4 x_2 & = 9 \ & 2 x_1 & + & 1 x_2 & \geq 3 \end{array} ]

$$ -100 \leq x_1, x_2, \leq 100$$

lp  <- OP(objective = L_objective(c(7, 8), names=c("x", "y")),
          constraints = L_constraint(L = rbind(c(3, 4), c(2, 1)), 
                                     dir = c("==", ">="), rhs = c(9, 3)),
          bounds = V_bound(li = 1:2, ui = 1:2, 
                           lb = c(-100, -100), ub = c(100, 100)))
ROI_applicable_solvers(lp)
(sol <- ROI_solve(lp, solver = "gurobi"))
solution(sol)

Mixed integer linear programming (MILP)

$$\begin{array}{rrrrrrr} \text{maximize} & 7 x_1 & + & 3 x_2 & + & 1 x_3 & \ \text{subject to} & 6 x_1 & + & 4 x_2 & + & 5 x_3 & \leq 60 \ & 8 x_1 & + & x_2 & + & 2 x_3 & \leq 80 \ & 9 x_1 & + & 1 x_2 & + & 7 x_3 & \leq 70 \end{array} $$ $$x_1, x_3 \in \mathbb{Z}_{\geq 0}$$ $$x_2 \geq 0$$

A <- rbind(c(6, 4, 5), c(8, 0, 2), c(9, 1, 7))
milp <- OP(objective = L_objective(c(7, 1, 3), c("x", "y", "z")),
           constraints = L_constraint(L = rbind(c(6, 4, 5), c(8, 0, 2), c(9, 1, 7)),
                                      dir = c("<=", "<=", "<="),
                                      rhs = c(60, 80, 70)),
           types = c("I", "C", "I"), 
           maximum = TRUE)
(sol <- ROI_solve(milp))
solution(sol)

Multiple soltutions MILP

library("slam")
op <- OP(c(-1, -1, -1, -1, -99))
mat <- simple_triplet_matrix(rep(1:3, 2), c(c(1, 3, 4), c(2, 4, 5)), rep(1, 6))
constraints(op) <- L_constraint(mat, dir = leq(3), rhs = rep.int(1, 3))
types(op) <- rep("B", length(objective(op)))
(x <- ROI_solve(op, "gurobi", nsol_max = 5L))
solution(x)

Quadratic programming

Quadratic objective with linear constraints (QP)

$$ \text{minimize} \ \ x_1 + 2x_2 + 3x_3 + \frac{1}{2} (x_1^2 + x_2^2 + x_3^2) $$ $$ \begin{array}{rrrrrrrr} \text{subject to} & x_1 & + & x_2 & & & \geq & 1 \ & & & x_2 & + & x_3 & \geq & 2 \ & x_1 & & & + & x_3 & \geq & 3 \end{array} $$

qp <- OP(Q_objective(diag(3), c(1, 2, 3), c("x", "y", "z")),
         L_constraint(L = rbind(c(1, 1, 0), c(0, 1, 1), c(1, 0, 1)), 
                      dir = c(">=", ">=", ">="), rhs = c(1, 2, 3)))
(sol <- ROI_solve(qp, solver = "gurobi"))
solution(sol)

Quadratic objective with quadratic constraints (QCQP)

$$ \text{maximize} \ \ 90 x_1 + 110 x_2 + 160 x_3 - \frac{1}{2} (x_1^2 + x_2^2 + x_3^2) $$ $$ \begin{array}{rrrrr} \text{subject to} & x_1^2 + x_2^2 + 4 x_3 & \leq & 4 & \ & x_2^2 + x_3^2 + x_1 + x_3 & \leq & 3 & \ & x_1^2 + x_3^2 + 2 x_1 x_3 & \leq & 2 & \ & x_1, x_2, x_3 \geq 0 & & & \end{array} $$

qcqp <- OP(Q_objective(-diag(3), c(90, 110, 160), c("x", "y", "z")),
           Q_constraint(Q = list(rbind(c(2, 0, 0), c(0, 2, 0), c(0, 0, 0)),
                                 rbind(c(0, 0, 0), c(0, 2, 0), c(0, 0, 2)),
                                 rbind(c(2, 0, 2), c(0, 0, 0), c(2, 0, 2))),
                        L = rbind(c(0, 0, 4), c(1, 0, 1), c(0, 0, 0)),
                        dir = rep("<=", 3), rhs = c(4, 3, 2)),
           maximum = TRUE)
(sol <- ROI_solve(qcqp, solver = "gurobi"))
solution(sol)

Non-Convex Quadratic objective with quadratic constraints (QCQP)

If we change the sign of the quadratic terms the problem change from a convex to a non-convex problem. To be able to solve the new problem we have to set the parameter NonConvex equal to 2. $$ \text{maximize} \ \ 90 x_1 + 110 x_2 + 160 x_3 + \frac{1}{2} (x_1^2 + x_2^2 + x_3^2) $$ $$ \begin{array}{rrrrr} \text{subject to} & x_1^2 + x_2^2 + 4 x_3 & \leq & 4 & \ & x_2^2 + x_3^2 + x_1 + x_3 & \leq & 3 & \ & x_1^2 + x_3^2 + 2 x_1 x_3 & \leq & 2 & \ & x_1, x_2, x_3 \geq 0 & & & \end{array} $$

qcqp <- OP(Q_objective(diag(3), c(90, 110, 160), c("x", "y", "z")),
           Q_constraint(Q = list(rbind(c(2, 0, 0), c(0, 2, 0), c(0, 0, 0)),
                                 rbind(c(0, 0, 0), c(0, 2, 0), c(0, 0, 2)),
                                 rbind(c(2, 0, 2), c(0, 0, 0), c(2, 0, 2))),
                        L = rbind(c(0, 0, 4), c(1, 0, 1), c(0, 0, 0)),
                        dir = rep("<=", 3), rhs = c(4, 3, 2)),
           maximum = TRUE)
(sol <- ROI_solve(qcqp, solver = "gurobi", NonConvex = 2L))
solution(sol)

Binding constraints

The check below shows that all three constraints are binding.

s <- solution(sol)
sapply(as.F_constraint(constraints(qcqp))$F, function(fun) fun(s))


roigrp/ROI.plugin.gurobi documentation built on Dec. 22, 2021, 5:16 p.m.