Finding optimal locations of new stores

This tutorial includes everything you need to set up IBM Decision Optimization CPLEX Modeling for Python (DOcplex), build a Mathematical Programming model, and get its solution by solving the model on the cloud with IBM ILOG CPLEX Optimizer.

When you finish this tutorial, you’ll have a foundational knowledge of Prescriptive Analytics.

This notebook is part of Prescriptive Analytics for Python

It requires either an installation of CPLEX Optimizers or it can be run on IBM Watson Studio Cloud (Sign up for a free IBM Cloud account and you can start using Watson Studio Cloud right away).

Table of contents:

Describe the business problem

  • A fictional Coffee Company plans to open N shops in the near future and needs to determine where they should be located knowing the following:
    • Most of the customers of this coffee brewer enjoy reading and borrowing books, so the goal is to locate those shops in such a way that all the city public libraries are within minimal walking distance.
  • We use Chicago open data for this example.
  • We implement a K-Median model to get the optimal location of our future shops.

How decision optimization can help

  • Prescriptive analytics (decision optimization) technology recommends actions that are based on desired outcomes. It takes into account specific scenarios, resources, and knowledge of past and current events. With this insight, your organization can make better decisions and have greater control of business outcomes.

  • Prescriptive analytics is the next step on the path to insight-based actions. It creates value through synergy with predictive analytics, which analyzes data to predict future outcomes.

  • Prescriptive analytics takes that insight to the next level by suggesting the optimal way to handle that future situation. Organizations that can act fast in dynamic conditions and make superior decisions in uncertain environments gain a strong competitive advantage.

With prescriptive analytics, you can:

  • Automate the complex decisions and trade-offs to better manage your limited resources.
  • Take advantage of a future opportunity or mitigate a future risk.
  • Proactively update recommendations based on changing events.
  • Meet operational goals, increase customer loyalty, prevent threats and fraud, and optimize business processes.

Use decision optimization

Step 1: Import the library

Run the following code to import the Decision Optimization CPLEX Modeling library. The DOcplex library contains the two modeling packages, Mathematical Programming and Constraint Programming, referred to earlier.

Note that the more global package docplex contains another subpackage docplex.cp that is dedicated to Constraint Programming, another branch of optimization.

Step 2: Model the data

  • The data for this problem is quite simple: it is composed of the list of public libraries and their geographical locations.
  • The data is acquired from Chicago open data as a JSON file, which is in the following format: data” : [ [ 1, “13BFA4C7-78CE-4D83-B53D-B57C60B701CF”, 1, 1441918880, “885709”, 1441918880, “885709”, null, “Albany Park”, “M, W: 10AM-6PM; TU, TH: 12PM-8PM; F, SA: 9AM-5PM; SU: Closed”, “Yes”, “Yes”, “3401 W. Foster Avenue”, “CHICAGO”, “IL”, “60625”, “(773) 539-5450”, [ “”, null ], [ null, “41.975456”, “-87.71409”, null, false ] ] This code snippet represents library “3401 W. Foster Avenue” located at 41.975456, -87.71409

Disclaimer: This site provides applications using data that has been modified for use from its original source,, the official website of the City of Chicago. The City of Chicago makes no claims as to the content, accuracy, timeliness, or completeness of any of the data provided at this site. The data provided at this site is subject to change at any time. It is understood that the data provided at this site is being used at one’s own risk.

Step 3: Prepare the data

We need to collect the list of public libraries locations and keep their names, latitudes, and longitudes.

Define how to compute the earth distance between 2 points

To easily compute distance between 2 points, we use the Python package geopy

Declare the list of libraries

Parse the JSON file to get the list of libraries and store them as Python elements.

There are 81 public libraries in Chicago

Define number of shops to open

Create a constant that indicates how many coffee shops we would like to open.

We would like to open 5 coffee shops

Validate the data by displaying them

We will use the folium library to display a map with markers.

After running the above code, the data is displayed but it is impossible to determine where to ideally open the coffee shops by just looking at the map.

Let’s set up DOcplex to write and solve an optimization model that will help us determine where to locate the coffee shops in an optimal way.

Step 4: Set up the prescriptive model

* system is: Windows 64bit
* Python version 3.7.3, located at: c:\local\python373\python.exe
* docplex is present, version is (2, 11, 0)
* pandas is present, version is 0.25.1

Create the DOcplex model

The model contains all the business constraints and defines the objective.

Define the decision variables

Express the business constraints

First constraint: if the distance is suspect, it needs to be excluded from the problem.

Second constraint: each library must be linked to a coffee shop that is open.

Model: coffee shops
 - number of variables: 6642
   - binary=6642, integer=0, continuous=0
 - number of constraints: 6561
   - linear=6561
 - parameters: defaults
 - problem type is: MILP

Third constraint: each library is linked to exactly one coffee shop.

Model: coffee shops
 - number of variables: 6642
   - binary=6642, integer=0, continuous=0
 - number of constraints: 6642
   - linear=6642
 - parameters: defaults
 - problem type is: MILP

Fourth constraint: there is a fixed number of coffee shops to open.

Model: coffee shops
 - number of variables: 6642
   - binary=6642, integer=0, continuous=0
 - number of constraints: 6643
   - linear=6643
 - parameters: defaults
 - problem type is: MILP

Express the objective

The objective is to minimize the total distance from libraries to coffee shops so that a book reader always gets to our coffee shop easily.

Solve with Decision Optimization

Solve the model on the cloud.

# coffee shops locations = 81
# coffee shops           = 5

Step 5: Investigate the solution and then run an example analysis

The solution can be analyzed by displaying the location of the coffee shops on a map.

Total distance = 210.894
# coffee shops  = 5
new coffee shop: P_Austin-Irving_76
new coffee shop: P_Woodson Regional Library_77
new coffee shop: P_Manning_13
new coffee shop: P_Back of the Yards_20
new coffee shop: P_Sulzer Regional Library_27

Displaying the solution

Coffee shops are highlighted in red.


You learned how to set up and use IBM Decision Optimization CPLEX Modeling for Python to formulate a Mathematical Programming model and solve it with IBM Decision Optimization on Cloud.


Copyright � 2017-2019 IBM. IPLA licensed Sample Materials.