Skip to content

Commit

Permalink
Added comments for ease of code review
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-stp committed Aug 15, 2022
1 parent 84f7f83 commit 57adc10
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions match_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, name, bucketCap, swingTime, truckSetupTime, truckCycleTime):
def getLoadingTime(self, truckType):
return (math.floor(truckType.capacity/self.bucketCap)+1)*self.swingTime+self.truckSetupTime

#returns the cycle time any truck takes to go from the loader to the destination point and then back
def getCycleTime(self):
return self.truckCycleTime

Expand All @@ -47,6 +48,8 @@ def addTrucks(self, truckType, numTrucks):
def addLoader(self, loader):
self.loaders += [loader]

#this function uses integer programming to solve the match factor problem
#Also calls functions at the end to format and display output
def optimize(self,desiredMF=1):
[A,B,C] = self.makeSolverMatrices(desiredMF)
print("A matrix (rounded) *********************************************************")
Expand Down Expand Up @@ -90,6 +93,7 @@ def optimize(self,desiredMF=1):
#solves IP problem
status = lp.Solve()

#Displays IP solver results. Useful for troubleshooting
if status == pywraplp.Solver.OPTIMAL:
print('Objective value =', lp.Objective().Value())
for j in range(numVar):
Expand All @@ -104,6 +108,7 @@ def optimize(self,desiredMF=1):
self.printMatchFactors(x)
self.printAssignmentMatrix()

#creates an easy to interpret assignemtn matrix from the complicated variable vector
def parseAssignmentMatrix(self,x):
self.assignmentMatrix = np.zeros([self.numTruckTypes(),self.numLoaders()])
i = 0
Expand All @@ -112,6 +117,8 @@ def parseAssignmentMatrix(self,x):
self.assignmentMatrix[r,c] = x[i].solution_value()
i = i + 1

#creates the classic Ax=b, Min/Max xc matrices for a linear program
#Consult the documentation for how the constraints and variables are formulated
def makeSolverMatrices(self,desiredMF=1):
numLoaders = self.numLoaders()
numTruckTypes = self.numTruckTypes()
Expand Down Expand Up @@ -163,12 +170,16 @@ def makeSolverMatrices(self,desiredMF=1):

return [A,B,C]


def printAssignmentMatrix(self):
print("Assignment Matrix *******************************************************")
print(str(["RESULT"]+[l.getName() for l in self.loaders]))
for r in range(self.numTruckTypes()):
print([str(self.truckFleet[r][0].getName())]+list(self.assignmentMatrix[r,:]))

#Displays the result of IP solver in terms of match factor
#THis is not match factor per se, but rather the absolute value of the deviation
#from the desired match factor inputted into the optimize() function. The default MF is 1
def printMatchFactors(self,x):
print("Match Factors *******************************************************")
print(str(["MF DELTAS"]+[l.getName() for l in self.loaders]))
Expand All @@ -179,9 +190,10 @@ def printMatchFactors(self,x):
mfs += [round(x[i].solution_value(),3)]
print(["(Desired MF +/-)"]+mfs)

#helper functions to calculate commonly used values
def numLoaders(self):
return len(self.loaders)

def numTruckTypes(self):
return len(self.truckFleet)

Expand All @@ -191,8 +203,10 @@ def numVar(self):
def numConstraints(self):
return self.numTruckTypes()+2*self.numLoaders()

#keymatrix is a helper matrix to identify which truck and loader each variable is related to.
#since variables refer to a truck-loader pair, when the variables are listed in 1D order,
#it can be difficult to know which truck-loader pair a variable is for. This is an index for that.
def keyMatrix(self):
#keymatrix is a helper matrix to identify which truck and loader each variable is related to
keyMatrix = np.zeros([self.numVar(), 2])
i = 0
for t in range(self.numTruckTypes()):
Expand Down

0 comments on commit 57adc10

Please sign in to comment.