class TaskGen: def __init__(self, datalayer): #1 for job self.taskClient = datalayer.getJobRedis() self.graphCache = datalayer.getGraphCache() self.IDQueue = Queue() self.localJobQueue = [] self.taskID = 0 self.recentProcessedIDs = set() def addNewTask(self, nodeID): self.IDQueue.put(nodeID) def nextTask(self): id = -1 while True: if len(self.localJobQueue) > 0 and random() < 0.7: id = self.localJobQueue.pop() elif len(self.IDQueue) == 0: self.IDQueue.append( id=self.taskClient.getRedis(self.taskID).spop(100)) if len(self.IDQueue) != 0: id = self.IDQueue.get() if not (id in self.recentProcessedIDs): break else: id = -1 if id == -1: return [id, Graph()] return [id, self.graphCache.egoNetwork(id)] """ status is true: this node success to compute and store the results """ def taskFinish(self, nodeID, status): self.recentProcessedIDs.add(nodeID) if len(self.recentProcessedIDs) > 10000: self.recentProcessedIDs.pop() if status: self.localJobQueue.extend(self.graphCache.neighbors(nodeID)) while len(self.localJobQueue) > 10000: self.localJobQueue.pop() else: #remove some nodes from local queue as they may have been computed size = randrange(1, 100) size = min([size, len(self.localJobQueue)]) del self.localJobQueue[0:size]
class TaskGen: def __init__(self, datalayer): #1 for job self.taskClient = datalayer.getJobRedis() self.graphCache = datalayer.getGraphCache() self.IDQueue = Queue() self.localJobQueue = [] self.taskID = 0 self.recentProcessedIDs = set() def addNewTask(self, nodeID): self.IDQueue.put(nodeID) def nextTask(self): id = -1 while True: if len(self.localJobQueue) > 0 and random() < 0.7: id = self.localJobQueue.pop() elif len(self.IDQueue) == 0: self.IDQueue.append(id=self.taskClient.getRedis(self.taskID).spop(100)) if len(self.IDQueue) != 0: id = self.IDQueue.get() if not (id in self.recentProcessedIDs): break else: id = -1 if id == -1: return [id, Graph()] return [id, self.graphCache.egoNetwork(id)] """ status is true: this node success to compute and store the results """ def taskFinish(self, nodeID, status): self.recentProcessedIDs.add(nodeID) if len(self.recentProcessedIDs) > 10000: self.recentProcessedIDs.pop() if status: self.localJobQueue.extend(self.graphCache.neighbors(nodeID)) while len(self.localJobQueue) > 10000: self.localJobQueue.pop() else: #remove some nodes from local queue as they may have been computed size = randrange(1, 100) size = min([size, len(self.localJobQueue)]) del self.localJobQueue[0:size]
def addTaskIntoMsgQueue(data): Robot_Task_Cache.put(data) old_queue = None try: file_status = File(Robot_Task_Cache_File).status() if file_status['is_opened']: return except OSError as e: logger.info("Task MSG Queue file not exitsed!") old_queue = Queue() try: if old_queue is None: with open(Robot_Task_Cache_File, 'rb') as f: old_queue = pickle.load(f) while not Robot_Task_Cache.empty(): task_data = Robot_Task_Cache.get_nowait() old_queue.append(task_data) with open(Robot_Task_Cache_File, 'wb') as f: pickle.dump(old_queue, f) except Exception as e: logger.error("Error to insert Task data into MSG queue! " + str(e))
q.put(s) while not q.empty(): cur = q.get() nxt = [] if cur in gra: nxt = gra[cur] for node in nxt: if node not in covered: covered.add(node) q.put(node) #DFS q = [] covered = set([s]) q.append(s) connected = False while q: cur = q.pop() if cur == t: connected = True break nxt = [] if cur in gra: nxt = gra[cur] for node in nxt: if node not in covered: covered.add(node) q.append(node)
class _xclib(CDLL): serialErr = { '0x50': 'ETX', '0x51': 'ETX_SER_TIMEOUT', '0x52': 'ETX_CK_SUM_ERR', '0x53': 'ETX_I2C_ERR', '0x54': 'ETX_UNKNOWN_CMD', '0x55': 'ETX_DONE_LOW' } STREAM = True device_id = 1 DRIVERPARMS = '-XU 1 -DM %d' # allow other applications to share use of imaging boards previously opened for use by the first application FORMAT = 'DEFAULT' isOpen = False isInitSerial = False queue = None stream = None secPerTick = 4E-3 class _streamer(Thread): """a thread class that will stream frames to MDSplus tree""" def __init__(self, node): Thread.__init__(self) self.node = node self.daemon = True def run(self): while True: # run until None is send frameset = CYGNET4K.xclib.queue.get() if frameset is None: break CYGNET4K.xclib.storeFrame(self.node, frameset[0], frameset[1]) CYGNET4K.xclib.queue.task_done() CYGNET4K.xclib.queue.task_done() def __init__(self): try: postfix = uname()[4] name = '_'.join(['xclib'] + [postfix]) + '.so' super(CYGNET4K._xclib, self).__init__(name) self.pxd_mesgErrorCode.restype = c_char_p except OSError: error('xclib: ' + str(exc_info()[1])) def __del__(self): if self.isOpen: self.pxd_PIXCIclose() def printErrorMsg(self, status): error(self.pxd_mesgErrorCode(status)) self.pxd_mesgFault(0xFF) def closeDevice(self): self.pxd_PIXCIclose() CYGNET4K.isOpen = False def openDevice(self, dev_id, formatFile=""): """configFile includes exposure time which seems to take precedence over later serial commands""" if CYGNET4K.isOpen: return self.device_id == int(dev_id) self.device_id = int(dev_id) DRIVERPARMS = self.DRIVERPARMS % (1 << (dev_id - 1)) if Device.debug: print( "Opening EPIX(R) PIXCI(R) Frame Grabber\nDevice parameters: '%s'" % (DRIVERPARMS, )) status = self.pxd_PIXCIopen(c_char_p(DRIVERPARMS), c_char_p(self.FORMAT), c_char_p(formatFile)) if status < 0: self.printErrorMsg(status) CYGNET4K.isOpen = False return False if Device.debug: print("Open OK") CYGNET4K.isOpen = True self.serialUseAck = self.serialUseChk = True status = self.getSystemStateP() self.serialUseAck = status['ack'] self.serialUseChk = status['chksum'] self.nUnits = self.pxd_infoUnits() self.memSize = [ self.pxd_infoMemsize(c_int(1 << i)) for i in range(self.nUnits) ] self.nBuffer = self.pxd_imageZdim() self.PixelsX = self.pxd_imageXdim() self.PixelsY = self.pxd_imageYdim() self.Colors = self.pxd_imageCdim() self.ColorBits = self.pxd_imageBdim() self.PixelsToRead = self.PixelsX * self.PixelsY if Device.debug: print("number of boards: %d" % self.nUnits) print("buffer memory size: %.1f MB" % (self.memSize[0] / 1048576.)) print("frame buffers: %d" % self.nBuffer) print("image resolution: %d x %d" % (self.PixelsX, self.PixelsY)) print("colors: %d" % self.Colors) print("bits per pixel: %d" % (self.Colors * self.ColorBits)) ticku = (c_uint32 * 2)() if self.pxd_infoSysTicksUnits(ticku) == 0: self.secPerTick = ticku[0] / ticku[1] * 1E-6 if Device.debug: print("Microseconds per tick: %.1f" % (self.secPerTick * 1E6)) return True def startVideoCapture(self, node): if self.goneLive: error('Cannot go live again. Re-init first!') raise mdsExceptions.DevERROR_DOING_INIT if CYGNET4K.xclib.STREAM: self.queue = Queue() self.stream = self._streamer(node) self.stream.start() else: self.queue = [] status = self.pxd_goLivePair(1, c_long(1), c_long(2)) if status < 0: self.printErrorMsg(status) raise mdsExceptions.DevERROR_DOING_INIT self.lastCaptured = self.pxd_buffersFieldCount( 1, self.pxd_capturedBuffer(1)) self.currTime = 0 self.Frames = 0 for i in range(10): if self.goneLive: break else: sleep(.3) if self.goneLive: if Device.debug: print("Video capture started.") else: error('Timeout!') raise mdsExceptions.DevERROR_DOING_INIT def _goneLive(self): return not self.pxd_goneLive(1, 0) == 0 goneLive = property(_goneLive) def captureFrame(self, TriggerTime): currBuffer = self.pxd_capturedBuffer(1) currCaptured = self.pxd_buffersFieldCount(1, currBuffer) if currCaptured != self.lastCaptured: # A new frame arrived currTicks = self.pxd_buffersSysTicks( 1, currBuffer) # get internal clock of that buffer if Device.debug > 3: print("%d -> %d @ %d" % (self.lastCaptured, currCaptured, currTicks)) if self.Frames == 0: # first frame self.baseTicks = currTicks currTime = (currTicks - (self.baseTicks)) * self.secPerTick + TriggerTime self.lastCaptured = currCaptured usFrame = (c_ushort * self.PixelsToRead)() # allocate frame PixelsRead = self.pxd_readushort(1, c_long(currBuffer), 0, 0, self.PixelsX, self.PixelsY, byref(usFrame), self.PixelsToRead, c_char_p("Grey")) # get frame if PixelsRead != self.PixelsToRead: error('ERROR READ USHORT') if PixelsRead < 0: self.printErrorMsg(PixelsRead) else: error("pxd_readushort error: %d != %d" % (PixelsRead, self.PixelsToRead)) return False if Device.debug: print("FRAME %d READ AT TIME %f" % (self.Frames, currTime)) if isinstance(self.queue, Queue): self.queue.put((currTime, usFrame)) else: self.queue.append((currTime, usFrame)) self.Frames += 1 return True else: # No new frame return False def storeFrames(self, node): if isinstance(self.queue, Queue): self.stream.join() else: for frameset in self.queue: self.storeFrame(node, frameset[0], frameset[1]) def storeFrame(self, node, dim, frame): dims = Float32Array([dim]).setUnits('s') data = Int16Array( array(frame, 'int16').reshape([1, self.PixelsX, self.PixelsY])) if Device.debug: print('storeFrame', node.minpath, dim, data.shape) node.makeSegment(dim, dim, dims, data) def stopVideoCapture(self): self.pxd_goUnLive(1) if isinstance(self.queue, Queue): self.queue.put(None) # invoke stream closure if Device.debug: print("Video capture stopped.") def serialIO(self, writeBuf, BytesToRead=0): BytesToWrite = len(writeBuf) check = 0 for i in range(BytesToWrite): check ^= ord(writeBuf[i]) writeBuf += chr(check) if not self.isInitSerial: status = self.pxd_serialConfigure(1, 0, c_double(115200.), 8, 0, 1, 0, 0, 0) if status < 0: error("ERROR CONFIGURING SERIAL CAMERALINK PORT") self.printErrorMsg(status) raise mdsExceptions.DevCOMM_ERROR self.isInitSerial = True sleep(0.02) if Device.debug > 3: print( 'serial write: ' + ' '.join(['%02x' % ord(c) for c in writeBuf]), BytesToRead) while self.pxd_serialRead(1, 0, create_string_buffer(1), 1): pass BytesRead = self.pxd_serialWrite(1, 0, c_char_p(writeBuf), BytesToWrite + 1) if BytesRead < 0: error("ERROR IN SERIAL WRITE") self.printErrorMsg(BytesRead) raise mdsExceptions.DevCOMM_ERROR if BytesToRead is None: return # no response e.g. for resetMicro EOC = int(self.serialUseAck) + int( self.serialUseChk) # ETX and optional check sum expected = BytesToRead + EOC cReadBuf = create_string_buffer(expected) timeout = time() + .01 out = [] while timeout > time() and expected > 0: BytesRead = self.pxd_serialRead(1, 0, cReadBuf, expected) if BytesRead < 0: error("ERROR IN SERIAL READ\n") self.printErrorMsg(BytesRead) raise mdsExceptions.DevCOMM_ERROR out += cReadBuf.raw[0:BytesRead] expected -= BytesRead if Device.debug: print("SERIAL READ: %d of %d" % (len(out) - EOC, BytesToRead)) return out[0:BytesToRead] '''Set Commands''' def setValue(self, addrlist, value, useExtReg=False): shift = 8 * len(addrlist) - 8 for addr in addrlist: byte = chr((value >> shift) & 0xFF) shift -= 8 if useExtReg: # set addr of extended register self.serialIO(b'\x53\xE0\x02\xF3' + addr + b'\x50') addr = b'\xF4' self.serialIO(b'\x53\xE0\x02' + addr + byte + b'\x50') return self def resetMicro(self): """Will trap Micro causing watchdog and reset of firmware.""" # The camera will give no response to this command self.serialIO(b'\x55\x99\x66\x11\x50\EB', None) return self def setSystemState(self, byte): self.serialIO(b'\x4F' + chr(byte) + b'\x50') return self def setSystemStateP(self, chksum, ack, FPGAboot, FPGAcom): """ setSystemState(chksum,ack,FPGAreset,FPGAcomms) chksum Bit 6 = 1 to enable check sum mode ack Bit 4 = 1 to enable command ack FPGAboot Bit 1 = 0 to Hold FPGA in RESET; do not boot FPGAcom Bit 0 = 1 to enable comms to FPGA EPROM """ byte = 0 if chksum: byte |= 1 << 6 if ack: byte |= 1 << 4 if FPGAboot: byte |= 1 << 1 if FPGAcom: byte |= 1 << 0 return self.setSystemState(byte) def setFpgaCtrlReg(self, byte): """setFpgaCtrlReg(byte)""" return self.setValue(b'\x00', byte) def setFpgaCtrlRegP(self, enableTEC): """setFpgaCtrlRegP(enableTEC)""" return self.setFpgaCtrlReg(1 if enableTEC else 0) def setFrameRate(self, frameRate): """set the frame rate in Hz""" # 1 count = 1*60MHz period = 16.66ns # 32-bit value, 4 separate commands, # Frame rate updated on LSB write return self.setValue(b'\xDD\xDE\xDF\xE0', int(6E7 / frameRate)) def setExposure(self, exposureMs): """set exposure time in ms""" # 40-bit value, 1count = 1*60 MHz period # Frame rate updated on LSB write return self.setValue(b'\xED\xEE\xEF\xF0\xF1', int(6E4 * exposureMs)) def setTrigMode(self, byte): """ setTrigMode(byte) raising Bit 7 = 1 to enable rising edge, = 0 falling edge Ext trigger (Default=1) ext Bit 6 = 1 to enable External trigger (Default=0) abort Bit 3 = 1 to Abort current exposure, self-clearing bit (Default=0) cont Bit 2 = 1 to start continuous seq'., 0 to stop (Default=1) fixed Bit 1 = 1 to enable Fixed frame rate, 0 for continuous ITR (Default=0) snap Bit 0 = 1 for snapshot, self-clearing bit (Default=0) """ return self.setValue(b'\xD4', byte) def setTrigModeP(self, raising, ext, abort, cont, fixed, snap): """setTrigModeP(raising,ext,abort,cont,fixed,snap)""" byte = 0 if raising: byte |= 1 << 7 if ext: byte |= 1 << 6 if abort: byte |= 1 << 3 if cont: byte |= 1 << 2 if fixed: byte |= 1 << 1 if snap: byte |= 1 << 0 return self.setTrigMode(byte) def setDigitalVideoGain(self, gain): """set digital video gain""" # 16-bit value = gain*512 # Data updated on write to LSBs return self.setValue(b'\xD5\xD6', int(gain * 512)) def setBinning(self, binning): """setBinning(N) for NxN, N in [1,2,4]""" if binning == 1: byte = 0x00 # 1*1 elif binning == 2: byte = 0x11 # 2*2 elif binning == 4: byte = 0x22 # 4*4 else: raise mdsExceptions.DevINV_SETUP return self.setValue(b'\xDB', byte) def setRoiXSize(self, value): """set ROI X size as 12-bit value""" return self.setValue(b'\xD7\xD8', min(0xFFF, value)) def setRoiXOffset(self, value): """set ROI X offset as 12-bit value""" return self.setValue(b'\81\x82', min(0xFFF, value), True) def setRoiYSize(self, value): """set ROI Y size as 12-bit value""" return self.setValue(b'\x83\x84', min(0xFFF, value), True) def setRoiYOffset(self, value): """set ROI Y offset as 12-bit value""" return self.setValue(b'\xD7\xD8', min(0xFFF, value)) '''Query Commands''' def getSystemState(self): """get system state byte""" return ord(self.serialIO(b'\x49\x50', 1)[0]) def getSystemStateP(self): """get system state as dict""" byte = self.getSystemState() return { 'chksum': bool(byte & 1 << 6), 'ack': bool(byte & 1 << 4), 'FPGAboot': bool(byte & 1 << 2), 'FPGAhold': bool(byte & 1 << 1), 'FPGAcomm': bool(byte & 1 << 0) } def getByte(self, n=1): return ord(self.serialIO(b'\x53\xE1\x01\x50', n)[0]) def getValue(self, addrlist, rc=1, useExtReg=False): value = 0 for i, addr in enumerate(addrlist): if useExtReg: self.setValue(addr, 0x00, True) addr = '\x73' self.serialIO(b'\x53\xE0\x01' + addr + b'\x50', rc - 1) byte = self.getByte(rc) if useExtReg: # LSBF value |= byte << (i * 8) else: # MSBF value = value << 8 | byte return value def getFpgaCtrlReg(self): """byte = getFpgaCtrlReg()""" """TEC bit 0 = 1 enabled""" return self.getValue(b'\x00') def getFpgaCtrlRegP(self): """status = getFpgaCtrlRegP()""" return {'TEC': bool(self.getFpgaCtrlReg & 1 << 0)} def getFrameRate(self): """get frameRate in Hz""" return 6E7 / self.getValue(b'\xDD\xDE\xDF\xE0') def getExposure(self): """get exposure in ms""" return self.getValue(b'\xED\xEE\xEF\xF0\xF1') / 6E4 def getDigitalVideoGain(self): """get digital video gain""" return self.getValue(b'\xD5\xD6', 2) / 512. def getPcbTemp(self): """get temperature of pc board in degC""" self.setValue(b'\x70', 0) value = self.getByte() & 0xF self.setValue(b'\x71', 0) value = value << 8 | self.getByte() return value / 16. def getCmosTemp(self): """get temperature of CMOS chip (raw word)""" return self.getValue(b'\x7E\x7F', 1, True) def getMicroVersion(self): """major,minor = getMicroVersion()""" return tuple(map(ord, self.serialIO(b'\x56\x50', 2)[0:2])) def getFpgaVersion(self): """major,minor = getFpgaVersion()""" return self.getValue(b'\x7E'), self.getValue(b'\x7F') def getTrigMode(self): """ get trigger mode byte raising Bit 7 = 1 to enable rising edge, = 0 falling edge Ext trigger (Default=1) ext Bit 6 = 1 to enable External trigger (Default=0) abort Bit 3 = 1 to Abort current exposure, self-clearing bit (Default=0) cont Bit 2 = 1 to start continuous seq'., 0 to stop (Default=1) fixed Bit 1 = 1 to enable Fixed frame rate, 0 for continuous ITR (Default=0) snap Bit 0 = 1 for snapshot, self-clearing bit (Default=0) """ return self.getValue(b'\xD4') def getTrigModeP(self): """get trigger mode as dict""" byte = self.getTrigMode() return { 'raising': bool(byte & 1 << 7), 'ext': bool(byte & 1 << 6), 'abort': bool(byte & 1 << 3), 'cont': bool(byte & 1 << 2), 'fixed': bool(byte & 1 << 1), 'snap': bool(byte & 1 << 0) } def getBinning(self): """n = getBinning(), => binning: nxn""" return 1 << (self.getValue(b'\xDB') & 3) def getRoiXSize(self): """get width of ROI""" return self.getValue(b'\xD7\xD8') def getRoiXOffset(self): """get X offset of ROI""" return self.getValue(b'\xD9\xDA') def getRoiYSize(self): """get height of ROI""" return self.getValue(b'\x01\x02', 1, True) def getRoiYOffset(self): """get Y offset of ROI""" return self.getValue(b'\x03\x04', 1, True) ''' currently unsupported: returns error code 0x53 ETX_I2C_ERR def getUnitSerialNumber(self): """word = getUnitSerialNumber()""" self.serialIO(b'\x53\xAE\x05\x01\x00\x00\x02\x00\x50') word = self.serialIO(b'\x53\xAF\x02\x50',2) return ord(word[0]) | ord(word[1])<<8 def getBuildDate(self): """DD,MM,YY = getBuildDate()""" self.serialIO(b'\x53\xAE\x05\x01\x00\x00\x04\x00\x50') data = self.serialIO(b'\x53\xAF\x03\x50',2) return tuple(map(ord,data[0:3])) def getBuildCode(self): """'ABCDE' = getBuildCode()""" self.serialIO(b'\x53\xAE\x05\x01\x00\x00\x07\x00\x50') return self.serialIO(b'\x53\xAF\x05\x50',2)[0:5] def getAdcCalib(self): """v0degC,v40degC = getAdcCalib()""" self.serialIO(b'\x53\xAE\x05\x01\x00\x00\x0C\x00\x50') data = map(ord,self.serialIO(b'\x53\xAF\x04\x50',2)[0:4]) return data[0]|data[1]<<8,data[2]|data[3]<<8 def getDacCalib(self): """v0degC,v40degC = getDacCalib()""" self.serialIO(b'\x53\xAE\x05\x01\x00\x00\x10\x00\x50') data = map(ord,self.serialIO(b'\x53\xAF\x04\x50',2)[0:4]) return data[0]|data[1]<<8,data[2]|data[3]<<8 ''' def setConfiguration(self, exposure, frameRate, trigMode): self.setExposure(exposure) self.setFrameRate(frameRate) self.setTrigMode(trigMode) def getConfiguration(self): self.binning = self.getBinning() self.roiXSize = self.getRoiXSize() self.roiXOffset = self.getRoiXOffset() self.roiYSize = self.getRoiYSize() self.roiYOffset = self.getRoiYOffset() self.exposure = self.getExposure() self.frameRate = self.getFrameRate()
lock_all_queues.acquire() if mod_link not in crawled and mod_link not in toCrawl and not fnmatch.fnmatch(mod_link, '*http*http*'): toCrawl.append(mod_link) lock_all_queues.release() # Internal Link else: lock_all_queues.acquire() if next_link not in crawled and next_link not in toCrawl: toCrawl.append(next_link) lock_all_queues.release() # print " Thread #" + str(thread_number) + " : Finished" semaphore.release() # Procedure Init if __name__ == "__main__": toCrawl.append(homeurl) i = 0 while 1: semaphore.acquire() # Check for Number of Docs created lock_doc_id.acquire() if doc_id > DOCS_TO_SAVE: print("%d Documents created and exiting.", DOCS_TO_SAVE) break lock_doc_id.release() lock_all_queues.acquire() if toCrawl: link = toCrawl.pop() crawled.append(link)
class Search: def __init__ (self, init_str, arg, cost_flag): self.cost_flag = cost_flag self.tree = Tree() self.arg = arg init_state = State(0,0,init_str) self.length = len(init_str) #length of puzzle used for making moves #data structure for BFS is a Queue import Queue class for L if arg == "BFS": self.L = Queue() self.L.put(init_state) else: if arg not in ["DFS","UCS","A-STAR","GS"]: arg = "DFS" #if not a valid search default to DFS self.L = [init_state] #only BFS uses Queue every other search will use a list self.expanded = [] #list of expanded states self.cur_tree_id = 1 #unique tree id per state added to the tree self.tree.create_node(init_state.string,init_state.tid) #creates the root node of the search tree # returns the needed node from structure L # in GS,UCS,A-STAR it requires a min heap. # use a list but sort the list by the given f-costs # UCS : "cost to of a path(number of moves)" # A-STAR : "path cost + number of tiles misplaced" # GS : "number of tiles misplaced" # since list does not have a remove from front # reverse the sorted list and pop. def get(self): if isinstance(self.L, Queue): state = self.L.get() elif isinstance(self.L, list): if self.arg == "UCS": self.L.sort(key = lambda n: (n.cost, n.tid), reverse=True) elif self.arg == "A-STAR": self.L.sort(key = lambda n: ((n.cost + n.num_misplaced),n.tid), reverse=True) #returns lowest f cost where f(n)=h(n) elif self.arg == "GS": self.L.sort(key = lambda n: (n.num_misplaced,n.tid), reverse=True) state = self.L.pop() return state def put(self,state): if isinstance(self.L, Queue): self.L.put(state) elif isinstance(self.L, list): self.L.append(state) def is_empty(self): if isinstance(self.L, Queue): return self.L.empty() elif isinstance(self.L, list): return not bool(self.L) #bool ([]) returns false # generic search will handle all searches # the node to chosen for the search will depend on the get method def search(self): while not self.is_empty(): node = self.get() if self.is_goal(node): break else: self.expand(node) self.path_to_goal(node) def expand(self,state): if not self.is_in_expanded(state): self.expanded.append(state) # for i in range(self.length): if self.cost_flag: #cost will be the steps for x cost = abs(state.string.index("x") - i) else: cost = state.cost + 1 #total path cost successor = State(self.cur_tree_id,cost, state.string) #create a copy of node to apply move then add to L and tree self.cur_tree_id += 1 if i != state.string.index('x'): #don't move x into itself successor.move(i) self.put(successor) #put state into data structure L self.add_to_tree(successor,state) def is_in_expanded (self, state): # checks expanded if a state as already been exanded return state in self.expanded def is_goal (self, state): # returns true if state as no misplaced tiles return not bool(state.num_misplaced) def add_to_tree (self, state, parent): self.tree.create_node(state.string, state.tid, parent=parent.tid) def path_to_goal (self, goal): node = self.tree[goal.tid] move_list = [node] while not node.is_root(): node = self.tree[node.bpointer] move_list.append(node) #reverse move_list because first node is the goal and you want first node to be root Path(root->goal) move_list = list(reversed(move_list)) print "board movements start at the left index 0 " print "example initial wxb step 1: move 0 xwb" print "step 0: ", move_list[0].tag for i in range(1, len(move_list)): print "step %i: " % i, "move %i"% move_list[i].tag.index("x"), move_list[i].tag
raise ValueError ok = True except ValueError: print("Invalid value!") q = Queue(capacity) while True: cls() print_status(q) choice = dictionary.get(input("> ").lower(), -1) if choice == 1: value = input("value> ") while not len(value) > 0: print("Value cannot be empty.") value = input("value> ") if not q.append(value): print("Queue is full") elif choice == 2: value = q.pop() if not value: print("Queue is empty") elif choice == 3: q.sort() elif choice == 0: break else: print("Invalid command") print_help() q.print() if q.is_empty(): print("The queue is empty")
class TheGridDivTwo(object): seen = set() invalid = set() Q = Queue() bestX = None def find1(self, x, y, k): self.seen = set() self.invalid = set() self.Q = Queue() self.bestX = 0 # hash each invalid position for i in range(len(x)): self.invalid.add((x[i], y[i])) # dbfs for the highest y position reachable self.Q.put( (0, 0, k) ) while not self.Q.empty(): x, y, ttl = self.Q.get_nowait() self.bestX = max(self.bestX, x) if ttl == 0: continue for newPos in ( (x, y-1), (x, y+1), (x-1, y), (x+1, y) ): if newPos in self.seen: continue if newPos in self.invalid: continue else: self.seen.add(newPos) self.Q.put( ( newPos[0], newPos[1], ttl-1 ) ) return self.bestX # same as find1, but does not use python Queue.Queue def find2(self, x, y, k): self.seen = set() self.invalid = set() self.Q = deque() self.bestX = 0 # hash each invalid position for i in range(len(x)): self.invalid.add((x[i], y[i])) self.Q.append( (0, 0, k) ) #appends to right of deque while len(self.Q) != 0: x, y, ttl = self.Q.popleft() self.bestX = max(self.bestX, x) if ttl == 0: continue for newPos in ( (x, y-1), (x, y+1), (x-1, y), (x+1, y) ): if newPos in self.seen or newPos in self.invalid: continue else: self.seen.add(newPos) self.Q.append( ( newPos[0], newPos[1], ttl-1 ) ) return self.bestX # same as find1/2, but uses a huge list instead of a Queue def find3(self, x, y, k): self.seen = set() self.invalid = set() Q = [ 0 ] * (((2*k+1)**2)+1) Qstart = 0 Qend = 0 self.bestX = 0 # hash each invalid position for i in range(len(x)): self.invalid.add((x[i], y[i])) Q[Qend] = ( (0, 0, k) ) Qend += 1 while Qstart < Qend: x, y, ttl = Q[Qstart] Qstart += 1 self.bestX = max(self.bestX, x) if ttl == 0: continue for newPos in ( (x, y-1), (x, y+1), (x-1, y), (x+1, y) ): if newPos in self.seen or newPos in self.invalid: continue else: self.seen.add(newPos) Q[Qend] = ( ( newPos[0], newPos[1], ttl-1 ) ) Qend += 1 return self.bestX def find(self, x, y, k): return self.find2(x, y, k)
from Queue import Queue queue = Queue() option = -1 while(option != 0): print(''' 1 - Add 2 - Delete 3 - Print 4 - Inverted Print 0 - Quit ''') option = input("Choose a option: ") if option == '1': value = input("Enter a value: ") queue.append(value) if option == '2': queue.dequeue() pass if option == '3': queue.print() if option == '4': queue.invertedPrint()