---
title: "Using max_coverage_relocation"
author: "Nicholas Tierney"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using max_coverage_relocation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

This vignette provides a guide to using `maxcovr` to solve the maximum covering location problem, assuming that you can relocate facilities. 

```{r libraries}
library(dplyr)
library(maxcovr)
```

The "fixed location" method provided in `max_coverage` is limited in that it assumes that you cannot move existing facilities. `max_coverage_relocation` assumes that you can move already existing facilities, but that there is some cost of removal, and a cost of installation.

To illustrate the benefits of relocation, we are first going to consider an example where the existing facilities provide no coverage to users. We identify facility locations that are really far away.

```{r build-worst}

york_selected_worst_id <- york_crime %>% 
    nearest(york) %>%
    arrange(-distance) %>%
    slice(1:20) %>%
    select(desig_id) %>%
    distinct(desig_id)
    
    
# york_worst <- york_selected_worst_id %>% 
york_existing_worst <- york_selected_worst_id %>% 
    left_join(york,
              by = "desig_id")

```

Then we identify some really ideal locations.

```{r identify-really-ideal-locations}

york_not_selected_worst <- york %>%
    anti_join(york_existing_worst,
              by = "desig_id")

york_proposed_best <- 
 york_crime %>%
    nearest(york_not_selected_worst) %>%
    arrange(distance) %>%
    slice(1:100) %>%
    select(desig_id) %>%
    distinct(desig_id) %>%
    left_join(york,
              by = "desig_id")


```

```{r print-best-worst}

york_proposed_best
york_existing_worst

```

Let's look at the coverage of these places, 

```{r explore-coverage}

coverage(york_existing_worst,
         york_crime,
         distance_cutoff = 100)

```

So these are *worst* placements - we should choose some better facilities. However, if we consider the possibility that we could move these existing facilities to better locations, then we might be able to save a lot of money! We can do this with `max_coverage_relocation`.

Here we use `max_coverage_relocation`, which is similar in form to `max_coverage`.

Here, we specify:

- `existing_facility` with `york_existing_worst`
- `proposed_facility` with `york_proposed_best`
- `user` is `york_crime`,
- `distance_cutoff` is 100 - for 100m

instead of specifying `n_added`, as in `max_coverage`, we specify three `cost_` parameters:

- `cost_install` is the cost of buying a new facility and installing it
- `cost_removal` is the cost of removing a facility and installing it in a new location
- `cost_total` is the total available money to spend.

Here we state that the installation cost is 10, the remocal cost is 1, and the total available cost is 10. There is only enough total resources to install one facility.

```{r max-coverage-relocation}
system.time(
mc_relocate_worst <-  max_coverage_relocation(
    existing_facility = york_existing_worst,
    proposed_facility = york_proposed_best,
    user = york_crime,
    distance_cutoff = 100,
    cost_install = 500,
    cost_removal = 100,
    cost_total = 1000)
)

mc_relocate_worst
summary(mc_relocate_worst)

dim(york_existing_worst)
dim(york_proposed_best)

mc_relocate_worst$existing_facility[[1]]
mc_relocate_worst$proposed_facility[[1]]
mc_relocate_worst$facilities_selected[[1]]


```


You can then start to toy with the different pricings if you like.


```{r explore-pricings}

system.time(
mc_relocate_worst_eq <-  max_coverage_relocation(
    existing_facility = york_existing_worst,
    proposed_facility = york_proposed_best,
    user = york_crime,
    distance_cutoff = 100,
    cost_install = 10,
    cost_removal = 100,
    cost_total = 10)
)

mc_relocate_worst_eq

summary(mc_relocate_worst_eq)

```


