This tutorial includes everything you need to set up decision optimization engines, build a mathematical programming model, then incrementally modify it. You will learn how to:
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:
A possible descriptive model of the telephone production problem is as follows:
This is a type of discrete optimization problem that can be solved by using either Integer Programming (IP) or Constraint Programming (CP).
Integer Programming is the class of problems defined as the optimization of a linear function, subject to linear constraints over integer variables.
Constraint Programming problems generally have discrete decision variables, but the constraints can be logical, and the arithmetic expressions are not restricted to being linear.
For the purposes of this tutorial, we will illustrate a solution with mathematical programming (MP).
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:
In [ ]:
import sys
try:
import docplex.mp
except:
raise Exception('Please install docplex. See https://pypi.org/project/docplex/')
A restart of the kernel might be needed.
Convert the descriptive model into a mathematical model:
To express the last two constraints, we model assembly time and painting time as linear combinations of the two productions, resulting in the following mathematical model:
maximize: 12 desk_production+20 cell_production
subject to:
desk_production>=100
cell_production>=100
0.2 desk_production+0.4 cell_production<=400
0.5 desk_production+0.4 cell_production<=490
</code>
In [ ]:
# first import the Model class from docplex.mp
from docplex.mp.model import Model
# create one model instance, with a name
m = Model(name='telephone_production')
The continuous variable desk represents the production of desk telephones. The continuous variable cell represents the production of cell phones.
In [ ]:
# by default, all variables in Docplex have a lower bound of 0 and infinite upper bound
desk = m.integer_var(name='desk')
cell = m.integer_var(name='cell')
In [ ]:
m.maximize(12 * desk + 20 * cell)
# write constraints
# constraint #1: desk production is greater than 100
m.add_constraint(desk >= 100, "desk")
# constraint #2: cell production is greater than 100
m.add_constraint(cell >= 100, "cell")
# constraint #3: assembly time limit
ct_assembly = m.add_constraint( 0.2 * desk + 0.4 * cell <= 400, "assembly_limit")
# constraint #4: paiting time limit
ct_painting = m.add_constraint( 0.5 * desk + 0.4 * cell <= 490, "painting_limit")
In [ ]:
m.print_information()
msol = m.solve()
In [ ]:
assert msol is not None, "model can't solve"
m.print_solution()
The model object provides getters to retrieve variables and constraints by name:
Let's say we want to build 2000 cells and 1000 desks maximum.
And let's say we want to increase the production of both of them from 100 to 350
In [ ]:
# Access by name
m.get_var_by_name("desk").ub = 2000
# acess via the object
cell.ub = 1000
m.get_constraint_by_name("desk").rhs = 350
m.get_constraint_by_name("cell").rhs = 350
In [ ]:
msol = m.solve()
assert msol is not None, "model can't solve"
m.print_solution()
The production plan has been updated accordingly to our small changes.
We now want to introduce a new type of product: the "hybrid" telephone.
In [ ]:
hybrid = m.integer_var(name='hybrid')
We need to:
In [ ]:
m.add_constraint(hybrid >= 350)
;
The objective will move from
maximize: 12 desk_production+20 cell_production
to
maximize: 12 desk_production+20 cell_production + 10 hybrid_prodction
In [ ]:
m.get_objective_expr().add_term(hybrid, 10)
;
The time constraints will be updated from
0.2 desk_production+0.4 cell_production<=400
0.5 desk_production+0.4 cell_production<=490
to
0.2 desk_production+0.4 cell_production + 0.2 hybrid_production<=400
0.5 desk_production+0.4 cell_production + 0.2 hybrid_production<=490
When you add a constraint to a model, its object is returned to you by the method add_constraint. If you don't have it, you can access it via its name
In [ ]:
m.get_constraint_by_name("assembly_limit").lhs.add_term(hybrid, 0.2)
ct_painting.lhs.add_term(hybrid, 0.2)
;
We can now compute the new production plan for our 3 products
In [ ]:
msol = m.solve()
assert msol is not None, "model can't solve"
m.print_solution()
Let's now say we improved our painting process, the distribution of the coefficients in the painting limits is not [0.5, 0.4, 0.2] anymore but [0.1, 0.1, 0.1] When you have the hand on an expression, you can modify the coefficient variable by variable with set_coefficient or via a list of (variable, coeff) with set_coefficients
In [ ]:
ct_painting.lhs.set_coefficients([(desk, 0.1), (cell, 0.1), (hybrid, 0.1)])
In [ ]:
msol = m.solve()
assert msol is not None, "model can't solve"
m.print_solution()
Let's now introduce a new constraint: polishing time limit.
In [ ]:
# constraint: polishing time limit
ct_polishing = m.add_constraint( 0.6 * desk + 0.6 * cell + 0.3 * hybrid <= 290, "polishing_limit")
In [ ]:
msol = m.solve()
if msol is None:
print("model can't solve")
The model is now infeasible. We need to handle it and dig into the infeasibilities.
You can now use the Relaxer object. You can control the way it will relax the constraints or you can use 1 of the various automatic modes:
We will use the 'match' mode. Polishing constraint is mandatory. Painting constraint is a nice to have. Assembly constraint has low priority.
In [ ]:
ct_polishing.name = "high_"+ct_polishing.name
ct_assembly.name = "low_"+ct_assembly.name
ct_painting.name = "medium_"+ct_painting.name
In [ ]:
# if a name contains "low", it has priority LOW
# if a ct name contains "medium" it has priority MEDIUM
# same for HIGH
# if a constraint has no name or does not match any, it is not relaxable.
from docplex.mp.relaxer import Relaxer
relaxer = Relaxer(prioritizer='match', verbose=True)
relaxed_sol = relaxer.relax(m)
relaxed_ok = relaxed_sol is not None
assert relaxed_ok, "relaxation failed"
relaxer.print_information()
In [ ]:
m.print_solution()
In [ ]:
ct_polishing_relax = relaxer.get_relaxation(ct_polishing)
print("* found slack of {0} for polish ct".format(ct_polishing_relax))
ct_polishing.rhs+= ct_polishing_relax
m.solve()
m.report()
m.print_solution()
Copyright © 2017-2019 IBM. Sample Materials.
In [ ]: