maxcovr provides tools to make it easy to solve the “maximum covering location problem”, a binary optimisation problem described by Church. Currently it uses the lp solver from the lpsolve package.

maxcovr aims to provide researchers and analysts with a fast, accurate and extendible implementation of Church’s maximal covering location problem, while adhering as best as it can to tidyverse principles. In the future maxcovr will be able to talk to commercial solvers like CPLEX and Gurobi, and will have a mechanism for extensions.

This vignette aims to get users started with using maxcovr. In this vignette you will learn about:

  • The motivation behind maxcovr
  • How to assess current coverage of facilities
  • How to maximise coverage using some new proposed facilities
  • Explore results of the new locations
  • How to explore the new locations proposed

Other vignettes are (or will be!) provided in the package:

  • “Cross Validation with maxcovr”, which explains how to perform and interpret cross validation results in a maximum covering location framework
  • “Understanding the maximum covering location problem”
  • “Performing the maximal coverage relocation problem”

Why maxcovr

maxcovr was created to make it easy for a non expert to correctly solve the maximum covering location problem. This problem is beginning to be applied in problems in AED placement, but has been applied in many different areas. However, many of the implementations in papers commercial software such as AMPL, Gurobi, or CPLEX. Additionally, the code that they use in the paper to implement the optimisation is not provided and has to be requested. As a result, these analyses are more difficult to implement and more difficult to reproduce.

maxcovr was created to address these shortcomings, using:

  • R, a free and open source language
  • Open source solvers, lpSolve, and glpk, that can be used on Linux, Windows, and OSX.
  • Real-world, open source example data.
  • Tidyverse principles to make it easy to use, code, and extend.

As a result of this, results are easy to implement, reproduce, and extend.

The problem

Consider the toy example where we want to place crime surveillance towers to detect crime. We have two datasets, york, and york_crime:

  • york contains listed building GPS locations in the city of York, provided by the city of york
  • york_crime contains a set of crime data from the ukpolice package, containing crime data from September 2016.

We already have a few surveillance towers built, which are placed on top of the listed buildings with a grade of I. We will call this dataset york_existing, and the remaining building locations york_proposed. Here, york_existing is the currently locations of facilities, and york_proposed is the potential facility locations.

## Warning: package 'dplyr' was built under R version 3.5.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

We are interested in placing surveillance towers so that they are within 100m of the largest number of crimes. We are going to use the crime data that we have to help us choose ideal locations to place towers.

This can be illustrated with the following graphic, where the red circles indicate the current coverage of the building locations, so those blue crimes within the circles are within the coverage.

## Assuming "long" and "lat" are longitude and latitude, respectively
## Assuming "long" and "lat" are longitude and latitude, respectively

Visually, the coverage looks OK, but to get a better estimate, we can verify the coverage using the nearest() function.

nearest() takes two dataframes and returns the nearest lat/long coords from the first dataframe to the second dataframe, along with the distances between them and the appropriate columns from the building dataframe. You can think of reading this as “What is the nearest (nearest_df) to (to_df)”.

## # A tibble: 6 x 22
##   to_id nearest_id distance category  persistent_id   date  lat_to long_to
##   <dbl>      <dbl>    <dbl> <chr>     <chr>           <chr>  <dbl>   <dbl>
## 1     1       1787   18.2   anti-soc… 62299914865f07… 2016…   54.0   -1.08
## 2     2        794  512.    anti-soc… 4e34f53d247f23… 2016…   54.0   -1.12
## 3     3        350    0.993 anti-soc… 2a0062f3dfac81… 2016…   54.0   -1.08
## 4     4        327   20.5   anti-soc… eb53e09ae46a0f… 2016…   54.0   -1.09
## 5     5       1860  135.    anti-soc… 6139f131b7243f… 2016…   54.0   -1.08
## 6     6        404   22.8   anti-soc… d8de26d5af47ef… 2016…   54.0   -1.08
## # ... with 14 more variables: street_id <chr>, street_name <chr>,
## #   context <chr>, id <chr>, location_type <chr>, location_subtype <chr>,
## #   outcome_status <chr>, long_nearest <dbl>, lat_nearest <dbl>,
## #   object_id <int>, desig_id <chr>, pref_ref <int>, name <chr>,
## #   grade <chr>

Note that maxcovr records the positions of locations must be named “lat” and “long” for latitude and longitude, respectively.

The dataframe dat_dist contains the nearest proposed facility to each crime. This means that we have a dataframe that contains “to_id” - the ID number (labelled from 1 to the number of rows in the to dataset), “nearest_id” describes the row numer of “nearest_df” that corresponds to the row location of that data.frame. We also have the rest of the columns of york_crime. These are returned to make it easy to do other data manipulation / exploration tasks, if you wish.

You can instead return a dataframe which has every building in the rows, and the nearest crime to the building, by simply changing the order.

## # A tibble: 6 x 22
##   to_id nearest_id distance long_to lat_to object_id desig_id pref_ref
##   <dbl>      <dbl>    <dbl>   <dbl>  <dbl>     <int> <chr>       <int>
## 1     1         33     36.0   -1.09   54.0      6144 DYO1195    463280
## 2     2        183     35.8   -1.09   54.0      6142 DYO1373    462942
## 3     3        503     95.3   -1.08   54.0      3463 DYO365     464845
## 4     4        273     44.3   -1.08   54.0      3461 DYO583     464427
## 5     5        908     26.5   -1.08   54.0      3460 DYO916     463764
## 6     6        495    326.    -1.13   54.0      3450 DYO1525    328614
## # ... with 14 more variables: name <chr>, grade <chr>, category <chr>,
## #   persistent_id <chr>, date <chr>, lat_nearest <dbl>,
## #   long_nearest <dbl>, street_id <chr>, street_name <chr>, context <chr>,
## #   id <chr>, location_type <chr>, location_subtype <chr>,
## #   outcome_status <chr>

To evaluate the coverage we can use summarise_coverage. Here, we need to create a column called “is_covered”, which is TRUE if the distance measure from nearest is less than some set distance, and FALSE otherwise.

dat_dist %>% 
    mutate(is_covered = distance <= 100) %>%
    summarise_coverage()
## # A tibble: 1 x 7
##   distance_within n_cov n_not_cov pct_cov pct_not_cov dist_avg dist_sd
##             <dbl> <int>     <int>   <dbl>       <dbl>    <dbl>   <dbl>
## 1             100   685      1129   0.378       0.622     355.    422.

This contains useful summary information:

  • distance_within - this is the distance used to determine coverae
  • n_cov - this is the number of events that are covered
  • n_not_cov - the number of events not covered
  • pct_cov - the proportion of events covered
  • pct_not_cov - the proportion of events not covered
  • dist_avg - the average distance from the rows to the nearest facility or user
  • dist_avg - the standard deviation of the distance from the rows to the nearest facility or user.

This tells us that out of all the crime, 37.76% of it is within 100m, 339 crimes are covered, but the mean distance to the surveillance camera is 1400m.

using max_coverage

Knowing this information, you might be interested in improving this coverage. To do so we can use the max_coverage function.

This function takes 5 arguments:

  • existing_facility = the facilities currently installed
  • proposed_facility = the facilities proposed to install
  • user = the users that you want to maximise coverage over
  • n_added = the number of new facilities that you can install
  • distance_cutoff = the distance that we consider coverage to be within.

Similar to using nearest - the data frames for existing_facility, proposed_facility, and user need to contain columns of latitude and longitude, and they must be named “lat”, and “long”, respectively. These are used to calculate the distance.

We can look at a quick summary of the model with summary

## 
## ------------------------------------------- 
## Model Fit: maxcovr fixed location model 
## ------------------------------------------- 
## Distance Cutoff: 100m 
## Facilities: 
##     Added:       20 
## Coverage (Previous): 
##     # Users:     540    (339) 
##     Proportion:  0.2977 (0.1869) 
## Distance (m) to Facility (Previous): 
##     Avg:         986 (1400) 
##     SD:          1188 (1597) 
## -------------------------------------------

Here this tells us useful information about the distance cutoff, the number of facilities added, and the number of users covered, and previousl, and the proportion of events covered.

To get this information out we can use the information in each of the columns below. The information each of these is a list, which might seem strange, but it becomes very useful when you are assessing different levels of n_added. We will go into more detail for this soon.

Firstly, we have the data input into n_added and distance_cutoff - the same information that we entered.

## [1] 20
## [1] 100

We can then get summary information about the model coverage. We can first get the existing, or previous coverage with existing_coverage

## # A tibble: 1 x 8
##   n_added distance_within n_cov pct_cov n_not_cov pct_not_cov dist_avg
##     <dbl>           <dbl> <int>   <dbl>     <int>       <dbl>    <dbl>
## 1       0             100   339   0.187      1475       0.813    1400.
## # ... with 1 more variable: dist_sd <dbl>

This provides us with the information that we saw previously with summarise_coverage.

We can then get the information of the coverage from the model with the added additional AEDs with model_coverage.

## # A tibble: 1 x 8
##   n_added distance_within n_cov pct_cov n_not_cov pct_not_cov dist_avg
##     <dbl>           <dbl> <int>   <dbl>     <int>       <dbl>    <dbl>
## 1      20             100   540   0.298      1274       0.702     986.
## # ... with 1 more variable: dist_sd <dbl>

We can then get both pieces of information from summary

## # A tibble: 2 x 8
##   n_added distance_within n_cov pct_cov n_not_cov pct_not_cov dist_avg
##     <dbl>           <dbl> <int>   <dbl>     <int>       <dbl>    <dbl>
## 1       0             100   339   0.187      1475       0.813    1400.
## 2      20             100   540   0.298      1274       0.702     986.
## # ... with 1 more variable: dist_sd <dbl>

You can drill deeper, and get more information about the facilities using facility_selected, which returns facilities selected from the proposed_facility data.

## # A tibble: 20 x 7
##     long   lat object_id desig_id pref_ref name                      grade
##    <dbl> <dbl>     <int> <chr>       <int> <chr>                     <chr>
##  1 -1.09  54.0      5978 DYO1383    462917 <NA>                      II   
##  2 -1.08  54.0      5909 DYO1297    463072 <NA>                      II   
##  3 -1.08  54.0      5872 DYO1244    463186 <NA>                      II   
##  4 -1.08  54.0      5847 DYO1216    463242 <NA>                      II   
##  5 -1.12  54.0      5759 DYO1108    463434 FORMER JUNIOR SCHOOL BUI… II   
##  6 -1.08  54.0      5748 DYO1096    463469 <NA>                      II   
##  7 -1.08  54.0      5745 DYO1093    463465 <NA>                      II   
##  8 -1.08  54.0      5739 DYO1086    463457 CHURCH OF ST GEORGE AND … II   
##  9 -1.09  54.0      5638 DYO957     463691 <NA>                      II   
## 10 -1.09  53.9      5606 DYO920     463771 PRESS STAND AT YORK RACE… II   
## 11 -1.06  54.0      5592 DYO903     463788 <NA>                      II   
## 12 -1.08  54.0      5529 DYO829     463938 CHURCH OF ST THOMAS       II   
## 13 -1.08  54.0      5454 DYO705     464133 NUMBERS 45-51 (ODD) AND … II   
## 14 -1.12  54.0      5349 DYO572     464451 POPPLETON ROAD SCHOOL     II   
## 15 -1.08  54.0      3991 DYO544     464508 WOODS MILL                II   
## 16 -1.09  54.0      3863 DYO406     464772 ROYAL YORK HOTEL AND ARE… II   
## 17 -1.10  54.0      3250 DYO310     464928 ELM BANK HOTEL            II*  
## 18 -1.00  54.0      3215 DYO1581    491367 STOCKTON GRANGE AND ATTA… II   
## 19 -1.08  54.0      3213 DYO1580    490659 NEW CHAPEL AT ST JOHN'S … II   
## 20 -1.08  54.0      4803 DYO1734        NA The Swan Public House     II

We can then get information about the users with augmented_users

## # A tibble: 1,814 x 19
##    to_id nearest_id distance category  persistent_id  date  lat_to long_to
##    <dbl>      <dbl>    <dbl> <chr>     <chr>          <chr>  <dbl>   <dbl>
##  1     1          3     84.3 anti-soc… 62299914865f0… 2016…   54.0   -1.08
##  2     2         14    512.  anti-soc… 4e34f53d247f2… 2016…   54.0   -1.12
##  3     3          3     65.2 anti-soc… 2a0062f3dfac8… 2016…   54.0   -1.08
##  4     4         31    286.  anti-soc… eb53e09ae46a0… 2016…   54.0   -1.09
##  5     5         20    231.  anti-soc… 6139f131b7243… 2016…   54.0   -1.08
##  6     6          8     22.8 anti-soc… d8de26d5af47e… 2016…   54.0   -1.08
##  7     7         22   2136.  anti-soc… 5b742b1cb9188… 2016…   54.0   -1.09
##  8     8         26   2329.  anti-soc… d2112b4fd0a09… 2016…   54.0   -1.11
##  9     9         49     37.4 anti-soc… 30bf5bfa97c5a… 2016…   54.0   -1.09
## 10    10          5   3150.  anti-soc… d75bbe6c18bfe… 2016…   54.0   -1.17
## # ... with 1,804 more rows, and 11 more variables: street_id <chr>,
## #   street_name <chr>, context <chr>, id <chr>, location_type <chr>,
## #   location_subtype <chr>, outcome_status <chr>, user_id <int>,
## #   lat_nearest <dbl>, long_nearest <dbl>, type <chr>

This returns the dataframe of users, with the distance to their nearest AED, and at the end, provides information about the type of AED that is used.

Now try and run the code for n_added = 40, and call it “mc_40”

Interpreting results

We can assess what happens if we add 100 facilities.

## 
## ------------------------------------------- 
## Model Fit: maxcovr fixed location model 
## ------------------------------------------- 
## Distance Cutoff: 100m 
## Facilities: 
##     Added:       100 
## Coverage (Previous): 
##     # Users:     693    (339) 
##     Proportion:  0.382 (0.1869) 
## Distance (m) to Facility (Previous): 
##     Avg:         560 (1400) 
##     SD:          720 (1597) 
## -------------------------------------------

So then, if we want to add information to discover the differences between 20 and 100, we can bind these two pieces together using bind_rows.

mc_20_100 <- bind_rows(mc_20, mc_100)

We can then look at the summary row, and expand the information out here using tidyr::unnest()

## # A tibble: 4 x 8
##   n_added distance_within n_cov pct_cov n_not_cov pct_not_cov dist_avg
##     <dbl>           <dbl> <int>   <dbl>     <int>       <dbl>    <dbl>
## 1       0             100   339   0.187      1475       0.813    1400.
## 2      20             100   540   0.298      1274       0.702     986.
## 3       0             100   339   0.187      1475       0.813    1400.
## 4     100             100   693   0.382      1121       0.618     560.
## # ... with 1 more variable: dist_sd <dbl>

This information can then be plotted, for example, like so:

You can then produce a plot of the average distances.

If you would like to calculate your own summaries on the distances, I would recommend something like:

## # A tibble: 1 x 5
##   mean_dist sd_dist median_dist lower_975_dist upper_975_dist
##       <dbl>   <dbl>       <dbl>          <dbl>          <dbl>
## 1      986.   1188.        620.           18.0          4593.

You can then package this up in a function and apply it to all rows

This can then be used to create a new column with purrr::map().

mc_20_100 %>%
    mutate(new_summary = purrr::map(augmented_users,
                                    my_dist_summary)) %>%
    select(new_summary) %>%
    tidyr::unnest()
## # A tibble: 2 x 5
##   mean_dist sd_dist median_dist lower_975_dist upper_975_dist
##       <dbl>   <dbl>       <dbl>          <dbl>          <dbl>
## 1      986.   1188.        620.           18.0          4593.
## 2      560.    720.        316.           16.2          2277.

Other graphics options

You might be interested in plotting the existing users, the proposed facilities, and the coverage.

You can plot the existing facilities with leaflet.

## Assuming "long" and "lat" are longitude and latitude, respectively

You might want to then add the user information to this plot. You can add new circles based on new datasets, and then change the colour, so that they are visible.

## Assuming "long" and "lat" are longitude and latitude, respectively
## Assuming "long" and "lat" are longitude and latitude, respectively

With leaflet you can also specify the radius in metres of the circles. This means that we can set the radius of the circles to be 100, and this is 100m.

## Assuming "long" and "lat" are longitude and latitude, respectively
## Assuming "long" and "lat" are longitude and latitude, respectively

to make this a bit clearer we can remove the fill (fill = FALSE), and also change the map that is used with addProviderTiles("CartoDB.Positron").

## Assuming "long" and "lat" are longitude and latitude, respectively
## Assuming "long" and "lat" are longitude and latitude, respectively

Applying this to the coverage data

So using this knowledge, we can visualise:

  • crime (york_crime)
  • existing facilities (york_existing)
  • newly placed facilities (mc_20$facility_selected[[1]])
## Assuming "long" and "lat" are longitude and latitude, respectively
## Assuming "long" and "lat" are longitude and latitude, respectively
## Assuming "long" and "lat" are longitude and latitude, respectively

To include leaflet in a