demand_source
Module¶
Overview¶
This module contains the DemandSource
class. A DemandSource
object represents external demand observed by a node.
The demand can be random or deterministic. Attributes specify the type of demand
distribution and its parameters. The object can generate demands from the specified distribution.
Note
The notation and references (equations, sections, examples, etc.) used below refer to Snyder and Shen, Fundamentals of Supply Chain Theory (FoSCT), 2nd edition (2019).
Example: Create a DemandSource
object representing demand that has a normal
distribution with a mean of 50 and a standard deviation of 10. Generate a random demand from the distribution.
>>> ds = DemandSource(type='N', mean=50, standard_deviation=10) >>> ds.generate_demand() 46.75370030596123 >>> # Tell object to round demands to integers. >>> ds.round_to_int = True >>> ds.generate_demand() 63
API Reference¶
- class DemandSource(**kwargs)[source]¶
A
DemandSource
object represents external demand observed by a node. The demand can be random or deterministic. Attributes specify the type of demand distribution and its parameters. The object can generate demands from the specified distribution.- Parameters
**kwargs – Keyword arguments specifying values of one or more attributes of the
DemandSource
, e.g.,type='N'
.
- type¶
The demand type, as a string. Currently supported strings are:
None
‘N’ (normal)
‘P’ (Poisson)
‘UD’ (uniform discrete)
‘UC’ (uniform continuous)
‘D’ (deterministic)
‘CD’ (custom discrete)
- Type
str
- round_to_int¶
Round demand to nearest integer?
- Type
bool
- mean¶
Mean of demand per period. Required if
type
== ‘N’ or ‘P’. [\(\mu\)]- Type
float, optional
- standard_deviation¶
Standard deviation of demand per period. Required if
type
== ‘N’. [\(\sigma\)]- Type
float, optional
- demand_list¶
List of demands, one per period (for deterministic demand types), or list of possible demand values (for custom discrete demand types). For deterministic demand types, if demand is required in a period beyond the length of the list, the list is restarted at the beginning. This also allows
demand_list
to be a singleton, in which case it is used in every period. Required iftype
== ‘D’ or ‘CD’. [\(d\)]- Type
list, optional
- probabilities¶
List of probabilities of each demand value (for custom discrete demand types). Required if
type
== ‘CD’.- Type
list, optional
- lo¶
Low value of demand range (for uniform demand types). Required if
type
== ‘UD’ or ‘UC’.- Type
float, optional
- hi¶
High value of demand range (for uniform demand types). Required if
type
== ‘UD’ or ‘UC’.- Type
float, optional
- property demand_distribution¶
Demand distribution, as a
scipy.stats.rv_continuous
orscipy.stats.rv_discrete
object. Read only.
- validate_parameters()[source]¶
Check that appropriate parameters have been provided for the given demand type. Raise an exception if not.
- to_dict()[source]¶
Convert the
DemandSource
object to a dict. List attributes (demand_list
,probabilities
) are deep-copied so changes to the original object do not get propagated to the dict.- Returns
The dict representation of the object.
- Return type
dict
- classmethod from_dict(the_dict)[source]¶
Return a new
DemandSource
object with attributes copied from the values inthe_dict
. List attributes (demand_list
,probabilities
) are deep-copied so changes to the original dict do not get propagated to the object. Any missing attributes are set to their default values.- Parameters
the_dict (dict) – Dict representation of a
DemandSource
, typically created usingto_dict()
.- Returns
The object converted from the dict.
- Return type
- generate_demand(period=None)[source]¶
Generate a demand value using the demand type specified in
type
. Iftype
isNone
, returnsNone
.- Parameters
period (int, optional) – The period to generate a demand value for. If
type
= ‘D’ (deterministic), this is required ifdemand_list
is a list of demands, one per period. If omitted, will return first (or only) demand in list.- Returns
demand – The demand value.
- Return type
float
- cdf(x)[source]¶
Cumulative distribution function of demand distribution.
In some cases, this is just a wrapper around
cdf()
function ofscipy.stats.rv_continuous
orscipy.stats.rv_discrete
object.- Parameters
x (float) – Value to calculate cdf for.
- Returns
F – cdf of
x
.- Return type
float
- lead_time_demand_distribution(lead_time)[source]¶
Return lead-time demand distribution, as a
scipy.stats.rv_continuous
orscipy.stats.rv_discrete
object.Note
For ‘UC’, ‘UD’, and ‘CD’ demands, this method calculates the lead-time demand distribution as the sum of
lead_time
independent random variables. Therefore, the method requireslead_time
to be an integer for these distributions. If it is not, it raises aValueError
.- Parameters
lead_time (float or int) – The lead time. [\(L\)]
- Returns
distribution – The lead-time demand distribution object.
- Return type
rv_continuous or rv_discrete
- Raises
ValueError – If
type
is ‘UC’, ‘UD’, or ‘CD’ andlead_time
is not an integer.