(This article was first published on

**progRamming for the fun of it**, and kindly contributed to R-bloggers)Site optimization is the process of finding an optimal location for a plant or a warehouse to minimize transportation costs and duration. A simple model only consists of one good and no restrictions regarding transportation capacities or delivery time. The optimizing algorithms are often hard to understand. Fortunately, R is a great tool to make them more comprehensible.

**The basic math**

A basic contineous optimization problem is to deliver goods to $n$ customers. Every customer has a individual demand $a_j$ and is approached directly from our warehouse. The single routes have a length of $d_j$. The objective function to be minimized is:

$$min(\sum^{n}_{j=1} a_j \cdot d_j)$$

How can on calculate distances? Typical distance measures for flat surfaces most of you are probably familiar with are

How can on calculate distances? Typical distance measures for flat surfaces most of you are probably familiar with are

*Taxicab distance*and*Euclidean distance.*We will calculate spherical distances to get a better solution for large routes: $$\\d_{a,b} = arccos[sin(y_a)\cdot sin(y_b)+cos(y_a)\cdot cos(y_b)\cdot cos(x_a-x_b)] \cdot R\\$$*R*denotes the Earth radius: ~ 6,370 km.**Keep in mind:**- …To use geographical coordinates in the form of decimal degrees
- … To transform them to radian units (by multiplying pi/180).

**How to optimize**

We will use an iterative way for optimizing:

- set an initial warehouse location: $x_{start}$ and $y_{start}$
- for every iteration the current warehouse location $(x_w, y_w)$ is calculated by: $$x_w=\frac{\sum_{j=1}^{n}{a_j \cdot \overline{x}/d_j}}{\sum_{j=1}^{n}{a_j/d_j}} \quad \quad y_w=\frac{\sum_{j=1}^{n}{a_j \cdot \overline{y}/d_j}}{\sum_{j=1}^{n}{a_j/d_j}}$$

**Results**

Red circles are customer locations. Their surface area represents the individual demands. The single iterations start with dark blue circles and end with the final warehouse location, colored in green.

Click here to download the data file. Below the code:

Click here to download the data file. Below the code:

To

**leave a comment**for the author, please follow the link and comment on their blog:**progRamming for the fun of it**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...