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
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