# Istallation instruction optisim + mulder_algo
## Table of Contents
[TOC]
## Installation
_Note:_ This instruction assumes the OS distro is Ubuntu 18.04LTS. Installation in other distros should be possible but will require some adjustments.
Out of the box, Ubuntu requires some preparation:
```bash
sudo apt-get update -y
sudo apt-get install gcc g++ python3.7 python3.7-venv python3.7-dev python3-wheel libgomp1 llvm-10
sudo ln -s /usr/bin/llvm-config-10 /usr/bin/llvm-config
```
Create a root folder `optiroot`. In `optiroot` clone the following repositories:
```bash
mkdir optiroot
cd optiroot
git config --global credential.helper 'cache --timeout=300'
git clone https://gitlab.com/supsi-dacd-isaac/optisim.git
git clone https://gitlab.com/supsi-dacd-isaac/mulder-algo.git
git clone https://github.com/hivepower/art-models-forecaster.git
git clone https://github.com/hivepower/platform-gridmodeler-simulator-conf.git opticonf
```
Create a python3.7 venv: In the venv install with pip (assuming you sit in `optiroot`)
```bash
python3.7 -m venv venv
source venv/bin/activate
pip install -r optisim/requirements.txt
pip install -r mulder-algo/requirements.txt
pip install -r forecaster/requirements.txt
```
Add `optisim`, `mulder-algo` and `forecaster` folders to the `PYTHONPATH` environment variable (sitting in `optiroot`)
```bash
WD=$(pwd)
OPTISIM_FLD=$WD"/optisim/"
MULDERALGO_FLD=$WD"/mulder-algo/"
FORECASTER_FLD=$WD"/art-models-forecaster/"
export PYTHONPATH=$PYTHONPATH::$OPTISIM_FLD:$MULDERALGO_FLD:$FORECASTER_FLD
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so
```
## Install additional solvers (Optional)
### CBC (it can take a while)
First, make sure that you have a Fortran compiler installed:
```
sudo apt-get install gfortran
```
Then clone, build and install Coinbrew:
```bash
git clone https://github.com/coin-or/coinbrew
cd coinbrew
./coinbrew fetch Cbc@stable/2.10
./coinbrew build Cbc --enable-cbc-parallel --prefix=~/cbc --test
./coinbrew install Cbc
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/cbc/lib" >> ~/.bashrc
echo "export COIN_INSTALL_DIR=~/cbc" >> ~/.bashrc
source ~/.bashrc
cd ..
rm -rf coinbrew
pip install git+https://github.com/coin-or/CyLP
```
### SCIP
Download SCIPOptSuite-7.0.1-Linux.deb from https://scipopt.org/index.php#download
```bash
sudo apt install libgfortran4 libboost-program-options1.65.1
sudo apt install ./SCIPOptSuite-7.0.1-Linux.deb
pip install pyscipopt
```
### CVXOPT
```bash
pip install cvxopt
```
### GUROBI
Download Gurobi from the [website](https://www.gurobi.com/downloads/) and install it.
You might need to set the env variables. To be honest I don't remember if it is required only if you're using venv (so maybe it's not needed if you don't)
```bash
export GUROBI_HOME=[PATHTOGUROBI]/linux64
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GUROBI_HOME/lib
export PATH=$PATH:$GUROBI_HOME/bin/
```
In your shell/terminal, go to Gurobi's installation folder (GUROBI_HOME) and run
```bash
python setup.py install
```
## Test run
Copy the `conns_HP.json` file into `optiroot/opticonf/connections`
Put yourself in `optiroot` and activate the venv
```bash
cd optiroot
source venv/bin/activate
```
Run the output data manager
```bash
nohup python optisim/output_data_manager.py -c opticonf/connections/conns_HP.json &> output_data_manager.out &
```
Run the simulations
```bash
nohup python mulder-algo/mulder_algo/run_mulder.py -c opticonf/grid_simulations/toyota/outputs/sims/sim_1m_1uc1pv1bat.json -ct opticonf/grid_simulations/toyota/outputs/algos/algo_1m_1uc1pv1bat.json &
python optisim/sim_grid.py -c opticonf/grid_simulations/toyota/outputs/sims/sim_1m_1uc1pv1bat.json &> mulder-algo.out
```