Optimization packages for R
Asked Answered
I

9

13

Does anyone know of any optimization packages out there for R (similar to NUOPT for S+)?

Ineradicable answered 11/12, 2008 at 14:2 Comment(4)
downvote for 'this question does not show any research effort'Priddy
I "could" have made this a long question talking about the details of what I needed and how I had investigated linprog and found it wanting. Or I could just ask a very simple question. I stand by that decision. BTW, while I disagree with your reasons for downvoting me, at least you had the decency to give a reason. Thank you for your courtesy.Ineradicable
If you'd told us where you had looked then that would have shown some research effort - even a simple "i searched google or cran for ' optimisation' " would have helped. We shouldn't have to point people to cran task views... And if you'd found linprog wanting, why accept the answer that says "I've used linprog"?Priddy
It appears 3rd in google search under "linear optimization r", meaning we should be practical about it and use it as a reference for future searchers.Elizabethelizabethan
H
21

R has many, many packages for optimization; check the CRAN Task view on Optimization: http://cran.r-project.org/web/views/Optimization.html. Of course, for nonlinear programs, there is optim(), which is standard and includes Broyden-Fletcher-Goldfarb-Shanno's algorithm, and Nelder-Mead. It's a good first start.

Hummingbird answered 27/7, 2009 at 21:23 Comment(0)
M
8

Try lpSolve with R.

A simple example:

# Maximize 
#   x1 + 9 x2 +   x3 
# Subject to: 
#   x1 + 2 x2 + 3 x3 <= 9
# 3 x1 + 2 x2 + 2 x3 <= 15
f.obj <- c(1, 9, 3)
f.con <- matrix(c(1, 2, 3, 3, 2, 2), nrow = 2, byrow = TRUE)
f.dir <- c("<=", "<=")
f.rhs <- c(9, 15)

lp("max", f.obj, f.con, f.dir, f.rhs)
lp("max", f.obj, f.con, f.dir, f.rhs)$solution
Marry answered 6/1, 2013 at 4:55 Comment(0)
M
8

you should also try the Rglpk package solve LP problems with GLPK (GNU Linear Programming Kit).

An example:

## Simple linear program.
## maximize:   2 x_1 + 4 x_2 + 3 x_3
## subject to: 3 x_1 + 4 x_2 + 2 x_3 <= 60
##             2 x_1 +   x_2 +   x_3 <= 40
##               x_1 + 3 x_2 + 2 x_3 <= 80
##               x_1, x_2, x_3 are non-negative real numbers

obj <- c(2, 4, 3)
mat <- matrix(c(3, 2, 1, 4, 1, 3, 2, 2, 2), nrow = 3)
dir <- c("<=", "<=", "<=")
rhs <- c(60, 40, 80)
max <- TRUE

Rglpk_solve_LP(obj, mat, dir, rhs, max = max)

R output:
(Note that $status an integer with status information about the solution returned. If the control parameter canonicalize_status is set (the default) then it will return 0 for the optimal solution being found, and non-zero otherwise. If the control parameter is set to FALSE it will return the GLPK status codes).

$optimum
[1] 76.66667

$solution
[1]  0.000000  6.666667 16.666667

$status
[1] 0
Marry answered 6/1, 2013 at 5:13 Comment(0)
C
7

Linprog, mentioned by Galwegian, focuses on linear programming via the simplex algorithm. In addition you may be interested in fPortfolio if you are doing portfolio optimization.

Curt answered 12/1, 2009 at 16:20 Comment(0)
D
4

I have used linprog for linear problems in the past.

Dickie answered 11/12, 2008 at 14:6 Comment(1)
+1 and thanks a lot. I haven' touched any optimization stuff since my TurboPascal days. There were a whole bunch of other optimization packages listed in the packages page (cran.r-project.org/web/packages).Ineradicable
G
4

Another package is ompr. An advantage of this package is there are many solvers that can be used and binary, continuous, integer all variables can easily be added. A simple example:

library(tidyverse)
library(ompr)
library(ompr.roi)


model <-  MIPModel() %>%
  add_variable(x1, type = "integer") %>%
  add_variable(x2, type = "integer") %>%
  set_bounds(x1, lb = 0) %>%
  set_bounds(x2, lb = 0) %>%
  set_objective(x1 - x2, "max") %>%
  add_constraint(x1 + 2*x2 <= 150) %>%
  add_constraint(x1 >= 30) %>%
  add_constraint(x2 >= 40)

Solving with glpk:

library(ROI.plugin.glpk)
result <- solve_model(model, with_ROI(solver = "glpk", verbose = TRUE))

get_solution(result, x1)
get_solution(result, x2)

It can also be solved with other solvers like symphony where the gap_limit can be set in case the problem is complex and will take many iterations to converge:

library(ROI.plugin.symphony)
result <- solve_model(model, with_ROI(solver = "symphony",
                                      verbosity=-1, gap_limit=1.5))
Gutenberg answered 3/11, 2021 at 5:51 Comment(0)
Y
3

I like Gurobi. It's very expensive for a license, but it can be obtained through many universities. See here http://www.gurobi.com/products/modeling-languages/r

Yolande answered 11/1, 2017 at 1:47 Comment(0)
C
3

Have a look on the NLoptr package. It has quite an extensive documentation with examples and lots of algorithms to choose from, depending what problem you are trying to resolve (etc. linear, non-linear, constraint)

Cyathus answered 20/6, 2019 at 13:44 Comment(0)
G
0

Ideally inject a dynamic momentum in nlopt or any optimizer, involving inertia and viscosity. Does not seem available in .R

Gardy answered 2/7, 2023 at 6:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.