Get RSymphony working on OSX

By | 4. September 2016

The problem with RSypmhony is that you can’t get binaries for the installation on OSX, so you have to compile the binary on your own.

The following steps are necessary:

  1. Install the “Command Line Tools” for OSX.
  2. Install the package manager “Homebrew”.
  3. Install “Symphony” solver with “Homebrew”
  4. Install a Fortran compiler
  5. Install “RSymphony”

So there are quite a lot of things to do, but it is not very complicated:

Install “Command Line Tools”

Source code compilers for OSX already come with the “XCode”-package or the “Command Line Tools”. You can check in the “Terminal” App if you have “XCode” or the “Command Line Tools” already installed with:

xcode-select -p

If you have “Command Line Tools” installed you will see “/Library/Developer/CommandLineTools”. If you have “XCode” install you will see “/Applications/Xcode.app/Contents/Developer”. You can save time and disk space and install just the “Command Line Tools” by entering in the “Terminal”:

xcode-select --install

Otherwise you can get “XCode” here.

Check the instalation of the “Command Line Tools” and “XCode” with:

xcode-select -p

Install “Homebrew”

Enter in “Terminal” App:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

For further details see here.

Install “Symphony”

Enter in “Terminal” App:

brew install homebrew/science/symphony

For further details see here.

Install “Fortran”

The problem with the Fortran compiler is: „Although Apple XCode Tools includes gcc 4.X, it is not a complete implementation and lacks gfortran.“. More details here and here. There are a lot of distributors for a GNU fortran compiler for OSX. But do not install the compiler from GCC Wiki. It is very convenient to install just the .dmg but it doesn’t work with R. So the best is to install the version from R Tools. But to get the most recent version enter in “Terminal” App:

curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /

Test the installation by entering:

gfortran –v

and

g++ -v -lgfortran

You can test the fortran compiler by compiling a textfile “hello.f” in the “Terminal” App containing these lines of code:

      *, "Hello World!"
      END

It is important to keep six spaces at the beginning of each of the lines. Compile and run:

gfortran -o hello hello.f
./hello

Install “RSymphony”

I recommend to use RStudio IDE for the next steps. If you want to install RSymphony at first in a quarantine then i recommend to install the devtools first. Because: “The devtools::dev_mode function switches your version of R into “development mode”. In this mode, R will install packages to ~/R-dev. “. This means it installs the new compiled “RSymphony” into a user library not into your R system library. To do so enter:

packages("devtools", lib="/Applications/RStudio.app/Contents/Resources/R/library")

To switch on the development mode enter:

 dev_mode()

To close the development mode just reenter the last line later.

In RStudio install then “RSymphony”:

packages("Rsymphony", type = "source")

Test it by loading:

library("Rsymphony", lib.loc="~/R-dev")

and then run the example from RSymphony in RStudio:

## 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 + 2x_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, 1, 2), nrow = 3)
dir <- c("<=", "<=", "<=")
rhs <- c(60, 40, 80)
max <- TRUE
Rsymphony_solve_LP(obj, mat, dir, rhs, max = max)
Category: OSX