Examples of mathematical programming

Downloading the examples

The examples can be downloaded from GitHub: docplex examples

Examples content and source

The sample archive docplex_examples.zip contains subdirectories for different sample categories:

  • examples/mp/modeling contains examples that target modeling.

    • diet: This example solves a simple variation of the well-known diet problem that was posed by George Stigler and George Dantzig. How does a planner choose foods that satisfy nutritional requirements while minimizing costs or maximizing satiety?
    • production: This example solves a production planning problem. How does a company decide what proportion of its products to produce inside the company and what to buy from outside the company?
    • sport_scheduling: How can a sports league schedule matches between teams in different divisions such that the teams play each other the appropriate number of times and maximize the objective of scheduling intradivision matches as late as possible in the season?
    • nurses: This example solves the problem of finding an optimal assignment of nurses to shifts.
    • nurses with multiobjective: This example solves the problem of finding an optimal assignment of nurses to shifts using multi objective.
  • examples/mp/workflow contains examples that target optimization workflow.

    • cutstock: The cutting stock problem in this example is sometimes known in math programming terms as a knapsack problem with reduced cost in the objective function. Generally, a cutting stock problem begins with a supply of rolls of material of fixed length (the stock). Strips are cut from these rolls. All the strips that are cut from one roll are known together as a pattern. The point of this example is to use as few rolls of stock as possible to satisfy some specified demand of strips.
    • lagrangian_relaxation: This example solves the generalized assignment problem, with or without Lagrangian relaxation.
    • load_balancing: This example looks at cloud load balancing to keep a service running in the cloud at reasonable cost by reducing the expense of running cloud servers while minimizing risk and human time due to rebalancing and doing balance sleeping models across servers.
    • populate.py: This sample shows how to run the populate algorithm,
      either on a model file or a model instance.
  • examples/mp/jupyter contains Jupyter Notebooks, working on http://datascience.ibm.com.

    • boxes : This example illustrates assigning objects to boxes in a manner that minimizes the total distance between each object and its assigned box.
    • chicago_coffee_shops : A K-Median model is used to determine where N coffee shops should be located to minimize the distance from local libraries.
    • marketing_campaign : A marketing department wants to achieve more profitable results in future campaigns by matching the right offer of financial services to each customer.
    • mining_pandas : This mining operations optimization problem is an implementation of Problem 7 from “Model Building in Mathematical Programming” by H.P. Williams. The operational decisions that need to be made are which mines should be operated each year and how much each mine should produce.
    • nurses_pandas : This is the same example as the nurses example, but describes how to use pandas in the model.
    • nurses_scheduling : This example solves the problem of finding an optimal assignment of nurses to shifts.
    • oil_blending : The oil-blending problem consists of calculating different blends of gasoline according to specific quality criteria. Blending problems are a typical industry application on Linear Programming.
    • sports_scheduling : How can a sports league schedule matches between teams in different divisions such that the teams play each other the appropriate number of times and maximize the objective of scheduling intradivision matches as late as possible in the season?
    • ucp_pandas : The Unit Commitment Problem answers the question, “Which power generators should I run at which times and at what level in order to satisfy the demand for electricity?”

Running the examples

Examples files are Python scripts that contain problem data. Debug traces and warnings are included in the examples to help you diagnose the cloud or local solve and to give a brief overview of your installation (Is CPLEX® in the path?, Is matplotlib installed?…). Running a sample submits the problem to the optimization engine and displays the result.

For instance, to run diet, type the following command at a command prompt:

c:\docplex\examples\mp\modeling> python diet.py
* model solved as function with objective: 2.69041
1> Servings of ({Spaghetti W/ Sauce}) = 2.155
2> Servings of ({Chocolate Chip Cookies}) = 10.000
3> Servings of ({2% Lowfat Milk}) = 1.831
4> Servings of ({Hotdog}) = 0.930
* KPI: Total Calories=2000.000
* KPI: Total Calcium=800.000
* KPI: Total Iron=11.278
* KPI: Total Vit_A=8518.433
* KPI: Total Dietary_Fiber=25.000
* KPI: Total Carbohydrates=256.806
* KPI: Total Protein=51.174