44Authors: Antony Phillips, Dr Stuart Mitchell 2008
55"""
66
7+ from typing import Any , Dict , List , Optional , Tuple , Union
8+
79# Import PuLP modeler functions
810from pulp import *
11+ from pulp .pulp import LpConstraintVar , LpProblem
912
1013
1114class Pattern :
@@ -19,21 +22,21 @@ class Pattern:
1922 lenOpts = ["5" , "7" , "9" ]
2023 numPatterns = 0
2124
22- def __init__ (self , name , lengths = None ) :
25+ def __init__ (self , name : str , lengths : List [ int ]) -> None :
2326 self .name = name
2427 self .lengthsdict = dict (zip (self .lenOpts , lengths ))
2528 Pattern .numPatterns += 1
2629
2730 def __str__ (self ):
2831 return self .name
2932
30- def trim (self ):
33+ def trim (self ) -> int :
3134 return Pattern .totalRollLength - sum (
3235 [int (i ) * int (self .lengthsdict [i ]) for i in self .lengthsdict ]
3336 )
3437
3538
36- def createMaster ():
39+ def createMaster () -> Tuple [ LpProblem , LpConstraintVar , Dict [ str , LpConstraintVar ]] :
3740 rollData = { # Length Demand SalePrice
3841 "5" : [150 , 0.25 ],
3942 "7" : [200 , 0.33 ],
@@ -71,7 +74,11 @@ def createMaster():
7174 return prob , obj , constraints
7275
7376
74- def addPatterns (obj , constraints , newPatterns ):
77+ def addPatterns (
78+ obj : LpConstraintVar ,
79+ constraints : Dict [str , LpConstraintVar ],
80+ newPatterns : List [List [int ]],
81+ ) -> None :
7582 # A list called Patterns is created to contain all the Pattern class
7683 # objects created in this function call
7784 Patterns = []
@@ -82,7 +89,7 @@ def addPatterns(obj, constraints, newPatterns):
8289 for j , k in zip (i , Pattern .lenOpts ):
8390 lsum += j * int (k )
8491 if lsum > Pattern .totalRollLength :
85- raise ("Length Options too large for Roll" )
92+ raise Exception ("Length Options too large for Roll" )
8693
8794 # The number of rolls of each length in each new pattern is printed
8895 print ("P" + str (Pattern .numPatterns ), "=" , i )
@@ -92,20 +99,22 @@ def addPatterns(obj, constraints, newPatterns):
9299
93100 # The pattern variables are created
94101 pattVars = []
95- for i in Patterns :
102+ for p in Patterns :
96103 pattVars += [
97104 LpVariable (
98- "Pattern " + i .name ,
105+ "Pattern " + p .name ,
99106 0 ,
100107 None ,
101108 LpContinuous ,
102- (i .cost - Pattern .trimValue * i .trim ()) * obj
103- + lpSum ([constraints [l ] * i .lengthsdict [l ] for l in Pattern .lenOpts ]),
109+ (p .cost - Pattern .trimValue * p .trim ()) * obj
110+ + lpSum ([constraints [l ] * p .lengthsdict [l ] for l in Pattern .lenOpts ]),
104111 )
105112 ]
106113
107114
108- def masterSolve (prob , relax = True ):
115+ def masterSolve (
116+ prob : LpProblem , relax : bool = True
117+ ) -> Union [Tuple [float , Dict [str , int ]], Dict [str , float ]]:
109118 # Unrelaxes the Integer Constraint
110119 if not relax :
111120 for v in prob .variables ():
@@ -130,7 +139,7 @@ def masterSolve(prob, relax=True):
130139 return value (prob .objective ), varsdict
131140
132141
133- def subSolve (duals ) :
142+ def subSolve (duals : Dict [ str , float ]) -> List [ Union [ Any , List [ int ]]] :
134143 # The variable 'prob' is created
135144 prob = LpProblem ("SubProb" , LpMinimize )
136145
0 commit comments