knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

rAMPL

Travis build status AppVeyor Build Status

Provides convenience wrappers around the Python package amplpy using reticulate.

Installation

You can install AMPL from GitHub with:

# install.packages("devtools")
devtools::install_github("ruaridhw/r-AMPL")

As this package relies on the amplpy API you will need a Python installation and the required package.

You can check if you have Python installed by opening a Terminal and running:

python -V

If this doesn't print a Python version, you will need to install it first and then run:

```{bash pip-install, eval=FALSE} pip install amplpy

You can obviously run this with Anaconda or a Python installation other
than the system default.

## Usage

This is a basic example which shows you how to solve a common problem.

First, start an AMPL environment. The default path is `/Applications/ampl`
and assumes you have an AMPL binary installed here.

```r
library(AMPL)
ampl <- ampl_env()

Next, we will point to the location of the model and data files. This package includes the AMPL book's example models so we'll use its "steel" model:

model_file <- system.file("models/steel.mod", package = "AMPL")
data_file <- system.file("models/steel.dat", package = "AMPL")

Finally, run the model:

ampl <- set_solver(ampl, "/Applications/ampl/cplex")
ampl <- read_model(ampl, model_file)
ampl <- read_data(ampl, data_file)
ampl <- solve_model(ampl)
ampl$close()

Using the magrittr pipe to chain commands, this can be executed without continuously repeating assignment:

ampl <- ampl_env() %>%
  set_solver("/Applications/ampl/cplex") %>%
  read_model(model_file) %>%
  read_data(data_file) %>%
  solve_model()
steel_results <- get_data_all(ampl)
ampl$close()

steel_results$Parameters$market
steel_results$Variables
steel_results$Objectives

Additional Info

Configure Python

If you installed amplpy using a Python installation other than the system default, such as conda, you can specify which version to use with a command similar to the following prior to loading AMPL:

reticulate::use_python("/Users/username/anaconda3/envs/my_env/bin/python")

Configure AMPL

The default ampl binary location is /Applications/ampl however if you have AMPLDev installed, you can use this binary instead:

ampl <- ampl_env("/Applications/AMPLDev.app/Contents/Resources/AMPL")

If you do this, there are a couple of steps to be aware of:



ruaridhw/r-AMPL documentation built on May 4, 2019, 10:55 a.m.