def matrix_bfs(initialState): # initialize datastructures knownSet = dict() toBeVisited = Queue() # give the algorithm initial parameters knownSet[initialState.__hash__()] = 1 toBeVisited._put(initialState) while not(toBeVisited.empty()): global count count += 1 #remove a state from the front of the queue curState = toBeVisited.get() global endPos if not(curState.curPosition.__eq__(endPos)): knownSet[curState.__hash__()] = 1 #check to see if it's the final state, if so flag, break, and print if(is_final_state(curState)): global bfs_complete_flag bfs_complete_flag = 1 break #curState.__repr__() #print(curState.curPosition.row, curState.curPosition.column) #print() #get the curState's neighbors, and queue them up if they haven't been visited yet for adj in curState.get_adjacent_states(): if not(adj.__hash__ in knownSet): toBeVisited.put(adj)
def _put(self, xxx_todo_changeme): # Only consider re-evaluation if we are still on the same eval # session. (eval_sess, is_reeval) = xxx_todo_changeme if is_reeval and self._curr_eval_sess is not eval_sess: return replace = True if hasattr(eval_sess, "ctlr") and eval_sess.ctlr and eval_sess.ctlr.keep_existing: # Allow multiple eval sessions; currently used for variable # highlighting (bug 80095), may pick up additional uses. Note that # these sessions can still get wiped out by a single replace=False # caller. replace = False if replace: # We only allow *one* eval session at a time. # - Drop a possible accumulated eval session. if len(self.queue): self.queue.clear() ## - Abort the current eval session. if not is_reeval and self._curr_eval_sess is not None: self._curr_eval_sess.ctlr.abort() # Lazily start the eval thread. if not self.isAlive(): self.start() Queue._put(self, (eval_sess, is_reeval)) if replace: assert len(self.queue) == 1
class Bot(object): def __init__(self, me, job): self.targets = Queue() self.me = me self.position = 1 pushes = job.split() for i in range(len(pushes)): if pushes[i] == me: self.targets._put(pushes[i+1]) def move(self, who): if self.targets.empty(): return False target = int(self.targets.queue[0]) if self.position == target: if self.me == who: self.targets.get() return True elif self.position < target: self.position += 1 else: self.position -= 1 return False
def insert(self, node, data): queue = Queue() queue._put(node) while queue.empty() is not True: current_node = queue.get_nowait() if current_node.left: queue._put(current_node.left) else: current_node.set_left(BinaryNode(data)) break if current_node.right: queue._put(current_node.right) else: current_node.set_right(BinaryNode(data)) break
def _put(self, item): if item not in self.all_items: Queue._put(self, item) self.all_items.add(item) else: print("Queue - Not a new item: {0}".format(item))
def _put(self, item): Queue._put(self, item) self.sort()
def _put(self, item): Queue._put(self, item) self.unfinished_tasks += 1
def _put(self, item): if item not in self.all_items: Queue._put(self, item) self.all_items.add(item)
def _put(self, item): if item not in self._done: Queue._put(self, item) self._done.add(item)
def _put(self, item): Queue._put(self, item) self.added_signal.emit()
def find_parameters(dataset_pathname, options=''): def update_param(c, g, p, mse, best_c, best_g, best_p, best_mse, worker, resumed): if (mse < best_mse): best_mse, best_c, best_g, best_p = mse, c, g, p stdout_str = '[{0}] {1} {2} (best '.format \ (worker, ' '.join(str(x) for x in [c, g, p] if x is not None), mse) output_str = '' if c != None: stdout_str += 'c={0}, '.format(2.0 ** best_c) output_str += 'log2c={0} '.format(c) if g != None: stdout_str += 'g={0}, '.format(2.0 ** best_g) output_str += 'log2g={0} '.format(g) if p != None: stdout_str += 'p={0}, '.format(2.0 ** best_p) output_str += 'log2p={0} '.format(p) stdout_str += 'mse={0})'.format(best_mse) #print(stdout_str) if options.out_pathname and not resumed: output_str += 'mse={0}\n'.format(mse) result_file.write(output_str) result_file.flush() return best_c, best_g, best_p, best_mse options = GridOption(dataset_pathname, options) if options.gnuplot_pathname: gnuplot = Popen(options.gnuplot_pathname, stdin=PIPE, stdout=PIPE, stderr=PIPE).stdin else: gnuplot = None # put jobs in queue jobs, resumed_jobs = calculate_jobs(options) job_queue = Queue(0) result_queue = Queue(0) for (c, g, p) in resumed_jobs: result_queue.put(('resumed', c, g, p, resumed_jobs[(c, g, p)])) for line in jobs: for (c, g, p) in line: if (c, g, p) not in resumed_jobs: job_queue.put((c, g, p)) # hack the queue to become a stack -- # this is important when some thread # failed and re-put a job. It we still # use FIFO, the job will be put # into the end of the queue, and the graph # will only be updated in the end job_queue._put = job_queue.queue.appendleft # fire telnet workers if telnet_workers: nr_telnet_worker = len(telnet_workers) username = getpass.getuser() password = getpass.getpass() for host in telnet_workers: worker = TelnetWorker(host, job_queue, result_queue, host, username, password, options) worker.start() # fire ssh workers if ssh_workers: for host in ssh_workers: worker = SSHWorker(host, job_queue, result_queue, host, options) worker.start() # fire local workers for i in range(nr_local_worker): worker = LocalWorker('local', job_queue, result_queue, options) worker.start() # gather results done_jobs = {} if options.out_pathname: if options.resume_pathname: result_file = open(options.out_pathname, 'a') else: result_file = open(options.out_pathname, 'w') db = [] best_mse = float('+inf') best_c, best_g, best_p = None, None, None for (c, g, p) in resumed_jobs: mse = resumed_jobs[(c, g, p)] best_c, best_g, best_p, best_mse = update_param(c, g, p, mse, best_c, best_g, best_p, best_mse, 'resumed', True) for line in jobs: for (c, g, p) in line: while (c, g, p) not in done_jobs: (worker, c1, g1, p1, mse1) = result_queue.get() done_jobs[(c1, g1, p1)] = mse1 if (c1, g1, p1) not in resumed_jobs: best_c, best_g, best_p, best_mse = update_param(c1, g1, p1, mse1, best_c, best_g, best_p, best_mse, worker, False) db.append((c, g, p, done_jobs[(c, g, p)])) if gnuplot and options.grid_with_c and options.grid_with_g: redraw(db, [best_c, best_g, best_rate], gnuplot, options) redraw(db, [best_c, best_g, best_rate], gnuplot, options, True) if options.out_pathname: result_file.close() job_queue.put((WorkerStopToken, None, None)) best_param, best_cgp = {}, [] if best_c != None: best_param['c'] = 2.0 ** best_c best_cgp += [2.0 ** best_c] if best_g != None: best_param['g'] = 2.0 ** best_g best_cgp += [2.0 ** best_g] if best_p != None: best_param['p'] = 2.0 ** best_p best_cgp += [2.0 ** best_p] print('{0} {1}'.format(' '.join(map(str, best_cgp)), best_mse)) return best_param
class GetUserPwd(metaclass=ABCMeta): def __init__(self,get_count_once=1,q_size=50,min_count=15,max_try_count=5): self.__q_size = q_size self.__min_count = min_count self.__max_try_count = max_try_count self.__get_count_once = get_count_once self.__background_thread_switch = True self.__thLock = threading.Condition() #声明通知锁 self.__user_pwd_pool = Queue(maxsize=self.__q_size) #保存用户名密码的列表 def set_background_thread_switch_close(self): """关闭后台获取用户名密码自动填充的程序""" self.__background_thread_switch = False def autoGetUser(self,fun): """后台获取用户名密码,等待通知,自动获取并填充用户名队列""" self.__thLock.acquire() #枷锁 try: while self.__background_thread_switch: self.__thLock.wait() #进入等待池 print("填充开始....当前Q数量:[%s]"%str(self.__user_pwd_pool.qsize())) while True: try: try: need_count = self.__q_size - self.__user_pwd_pool.qsize() co = int(need_count / self.__get_count_once) except Exception as e: co = 1 result = [] for i in range(co): result+=fun() if result: self.__user_pwd_pool.not_full.acquire() try: for item in result: self.__user_pwd_pool._put(item) self.__user_pwd_pool.unfinished_tasks += 1 finally: self.__user_pwd_pool.not_empty.notify() self.__user_pwd_pool.not_full.release() self.__thLock.notify() print("填充结束,当前Q数量:[%s]"%str(self.__user_pwd_pool.qsize())) else: print("Interface 未获取到内容...") time.sleep(2) continue break except Exception as e: print("Interface 定义错误,err:",e) finally: self.__thLock.release() def getUPfromQ(self): """ 从Q中获取一条用户名密码组合 :return:{"User":"******"} """ self.__thLock.acquire() try: for i in range(self.__max_try_count): try: user_item = self.__user_pwd_pool.get_nowait() if self.__user_pwd_pool.qsize() <= self.__min_count: self.__thLock.notify() return user_item except queue.Empty: self.__thLock.notify() self.__thLock.wait(None) except Exception as e: pass finally: self.__thLock.release() def run(self,fun): threading.Thread(target=self.autoGetUser,args=(fun,)).start() @abstractmethod def getUserPwdInterface(self): """ 必须实现的获取用户名和密码的方法 :return: [{"User":"******"},...] """ return [{"User":"******"},]