maxcovr was created to make it easy for a non expert to correctly solve the maximum covering location problem described by Church. Implementations of this problem (such as optimimum AED placement) may use commercial software such as AMPL, Gurobi, or CPLEX, which require an expensive license, or use open source licenses but not provide source code to the analysis performed (e.g., Bonnet 2014) This builds a substantial barrier to implement and reproduce these analyses.

maxcovr was created to make results easy to implement, reproduce, and extend by using:

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

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

How to Install

warning: maxcovr is still undergoing development and testing

Using maxcovr

Disclaimer: The following is a fictitious example using real world data.

Consider the toy example where we are playing a tower defense game and we need 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.

In this game we already have a few towers built, which are placed on top of the listed buildings with a grade of I. We will call this dataset york_selected, and the remaining building locations york_unselected

The purpose of the game is to build towers in places so that they are within 100m of crime. 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

Currently the coverage looks alright, but let’s 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 instead return a dataframe which has every building in the rows, and the nearest crime to the building, by simply changing the order.

To evaluate the coverage we can use coverage. This reads as find the coverage of the york buildings (below) to the crimes. Coverage of the first thing on the second thing. Or, how many of the second thing are covered by the first thing.

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

Alternatively, one can also use the below code:

Maximising coverage

Say then we want to add another 20 surveillance towers, but we want to use the best 20, we use max_coverage.

max_coverage actually returns a dataframe of lists.

This is handy because it means that later when you want to explore multiple n_added, say you want to explore how coverage changes for 20, 40, 60, 80, 100 n_added, then these are added as rows in the dataframe, which makes it easier to do summaries and manipulate.

Important features here of this dataframe are:

  • facility_selected: A dataframe from proposed_facilities, containing the facilities selected by the optimisation.
  • user_affected: A dataframe from user, that contains the users that were affected by the new optimised placement
  • model_coverage: A dataframe containing summary info on the number of users covered, the percentage of coverage, and the average distance.
  • existing_coverage: returns a similar summary dataframe the original coverage, from existing_facilities.
  • summary: returns the binded model_coverage and existing_coverage.
  • n_added: The number of things added
  • distance_cutoff: the distance cutoff selected

One can also use map from purrr to fit many different configurations of n_added. (Future work will look into allowing n_added to take a vector of arguments).

This returns a list of dataframes, which we can bind together like so:


map_cov_results <- bind_rows(map_mc_model$model_coverage)

We can then visualise the effect on coverage:

library(ggplot2)
bind_rows(map_mc_model$existing_coverage[[1]],
          map_cov_results) %>%
    ggplot(aes(x = factor(n_added),
               y = pct_cov)) + 
    geom_point() +
    geom_line(group = 1) + 
    theme_minimal()

You can read more about the use of max_coverage, covering topics like cross validation in the vignette.

Known Issues

  • max_coverage() may take a bit of time to run, depending on your data size. From initial testing, if the product of the number of rows of the proposed_facilities and users exceeds 100 million, it might take more than 1 minute. Of course, this may depend on the structure / complexity of your data and problem.

Future Work

I will be focussing on making maxcovr work well within the tidyverse. This includes providing sensible standard summaries using key function verbs from broom, adding exploratory plots, improving speed using Rcpp, and allowing users to select the solver they want to use.

If you have any suggestions, please file an issue and I will get to it as soon as I can.

Acknowledgements

Oliver Czibula, for this initial help in helping me understand the Maximum Covering Location Problem. Angelo Auricchio and Antonietta Mira for their supervision. Alessio Quaglino and Jost Reinhold for their help in implementing the first implementation of this problem in lpSolve. Martin Weiser for his suggestion for the relocation process. Matt Sutton for very his very thoughtful explanations on how to interact with high level solvers, which led to implementing maxcovr in lpsolve, glpk, and gurobi. And Alex Simmons, for teachiing me how to write better C++ code.