# source:coopr.pysp/trunk/examples/pysp/lotsizing/models/ReferenceModel.py@2411

Last change on this file since 2411 was 2411, checked in by jwatson, 9 years ago

Initial commit of PySP lot sizing example

File size: 3.2 KB
Line
1#
2# A lot-sizing problem, taken from Haugen, Lokketangen, and Woodruff.
3#
4
5from coopr.pyomo import *
6
7#
8# Model
9#
10
11model = Model()
12
13#
14# Parameters
15#
16
17# the number of stages.
18model.NumStages = Param(within=PositiveIntegers)
19
20# derived set.
21def stages_set_rule(model):
22   return set(range(1, model.NumStages()+1))
23model.Stages = Set(rule=stages_set_rule)
24
25# the initial inventory.
26model.InitialInventory = Param(within=NonNegativeReals)
27
28# the production setup cost in each time period.
29model.SetupCost = Param(model.Stages, within=NonNegativeReals)
30
31# the holding cost for inventory in each time period.
32model.HoldingCost = Param(model.Stages, within=NonNegativeReals)
33
34# the shortage cost for inventory in each time period.
35model.ShortageCost = Param(model.Stages, within=NonNegativeReals)
36
37# the demand for inventory in each time period.
38model.Demand = Param(model.Stages, within=NonNegativeReals)
39
40# the big M coefficient used to compute the production binary.
41# production is trivially bounded by the sum of demand across
42# stages minus the initial inventory.
43def compute_m_rule(model):
44   result = sum([model.Demand[t] for t in model.Stages]) - model.InitialInventory
45   return result()
46model.M = Param(within=NonNegativeReals, rule=compute_m_rule)
47
48#
49# Variables
50#
51
52# does production occur in time period t?
53model.Produce = Var(model.Stages, within=Binary)
54
55# TBD - upper bounds can be computed for each amount produced, inventory,
56#       backlog variable - introduce bounds rule.
57
58# the amount of product to be produced in time period t.
59model.AmountProduced = Var(model.Stages, within=NonNegativeReals)
60
61# the excess inventory at the end of time period t.
62model.Inventory = Var(model.Stages, within=NonNegativeReals)
63
64# the inventory backlog at the end of time period t.
65model.Backlog = Var(model.Stages, within=NonNegativeReals)
66
67# the cost variables for the stages.
68model.StageCost = Var(model.Stages)
69
70#
71# Constraints
72#
73
74# can't produce unless production is enabled!
75def limit_production_rule(t, model):
76   return (None, model.AmountProduced[t] - model.M * model.Produce[t], 0.0)
77model.LimitProduction = Constraint(model.Stages, rule=limit_production_rule)
78
79# enforce inventory balance for each time period.
80def inventory_balance_rule(t, model):
81   expr = None
82   if (t == 1):
83      expr = model.AmountProduced[t] + model.InitialInventory + model.Backlog[t] - \
84             model.Inventory[t] - model.Demand[t]
85   else:
86      expr = model.AmountProduced[t] + model.Inventory[t-1] + model.Backlog[t] - \
87             model.Inventory[t] - model.Backlog[t-1] - model.Demand[t]
88   return (expr == 0.0)
89model.InventoryBalance = Constraint(model.Stages, rule=inventory_balance_rule)
90
91#
92# Stage-specific cost computations
93#
94
95def compute_stage_cost_rule(t, model):
96   cost = sum([model.SetupCost[t] * model.Produce[t] + \
97               model.HoldingCost[t] * model.Inventory[t] + \
98               model.ShortageCost[t] * model.Backlog[t]])
99
100   return (model.StageCost[t] - cost) == 0.0
101
102model.ComputeStageCost = Constraint(model.Stages, rule=compute_stage_cost_rule)
103
104#
105# Objective
106#
107
108def total_cost_rule(model):
109    return sum([model.StageCost[t] for t in model.Stages])
110
111model.Total_Cost_Objective = Objective(rule=total_cost_rule, sense=minimize)
Note: See TracBrowser for help on using the repository browser.