def _optimizeLoad( self, load ): outputLoad = Load() packages = [] while load.charter.length() > 0: packages.append( load.charter.pop() ) # Sort packages by distance from arbitrary item in list ''' firstPackage = packages[4] packages.sort( key = lambda x: self._getDistanceBetweenPackages( firstPackage.location, x.location )) for package in packages: outputLoad.addPackage( package ) ''' # Nearest Neighbor sorting # Time Complexity: O(logN) sortedList = SimpleQueue() sortedList.push( packages[0] ) packages.remove( packages[0] ) for i in range( len( packages ) ): neighbor = self._getNearestPackage( sortedList.peek(), packages ) sortedList.push( neighbor ) index = packages.index( neighbor ) packages.remove( packages[index] ) while( sortedList.length() > 0 ): outputLoad.addPackage( sortedList.pop() ) return outputLoad
class Load(object): MIN_PACKAGES = 8 MAX_PACKAGES = 14 # A load is a set of packages to be assigned to a truck # Time Complexity: O(1) def __init__(self): self.charter = SimpleQueue() self.count = 0 self.max = self.MAX_PACKAGES # Add a package to the load: O(1) def addPackage(self, package): if not self._isFull(): self.charter.push(package) self.count += 1 else: return 'Maximimum Load reached' # Remove package from load: O(1) def removePackage(self): self.charter.pop() self.count -= 1 def getCount(self): return self.count def _isFull(self): return self.charter.length() > self.max
def _assignPackage( self, truck ): allPackages = self.packages.getAll() priorityList = SimpleQueue() # Check if package needs a target truck for package in allPackages: if truck is not None: if truck.id == 2 and package.id in self.GROUP_ONE: priorityList.push( package ) allPackages.remove( package ) # Filter all packages for staged packages for package in allPackages: if package.status is Status.STAGED: # Check for packages with time sensitive requirements if self.currentTime >= self.DELAY_TIME: if package.id in self.RUSH_PACKAGES: priorityList.push( package ) allPackages.remove( package ) if package.id in self.DELAYED_PACKAGES: priorityList.push( package ) #allPackages.remove( package ) # Hotfix for incorrect postage if self.currentTime >= self.CORRECTION_TIME: if package.id in self.INCORRECT_PACKAGES: self._updatePackageNine() priorityList.push( package ) allPackages.remove( package ) # Push remaining packages if no heuristic is required if str( package.notes ) == '': priorityList.push( package ) #allPackages.remove( package ) # Pop items of the prioritized list until a suitable package returns # Time Complexity: O(n) for i in range( priorityList.length() ): selection = priorityList.peek() if selection.status is not Status.STAGED: priorityList.pop() elif selection.status: return selection # Return empty result return False