def search(root): queue = [] queue.append(root) while len(queue) != 0: node = queue.pop(0) print(node.getId()) print(node.getStates()) children = nodeRepository.loadNodes(node.getChildrenId()) for child in children: queue.append(child)
def createStrategies(root): queue = [] mapping = {} queue.append(root) mapping[str(root.getId())] = "" wholeChoicesA = [] wholeChoicesB = [] while len(queue) > 0: node = queue.pop(0) nodeChoicesA = [] nodeChoicesB = [] for ce in node.getOutEdges(): choiceA, choiceB = ce.getAllChoices() nodeRepository.addChoice(choiceA) nodeRepository.addChoice(choiceB) # if settings.DEBUG: # print("ChoiceA.toString(): ",choiceA.toString()) # print("ChoiceB.toString(): ",choiceB.toString()) if choiceA.isEmpty() == False: choiceA.setOutId(ce.getChildId()) nodeChoicesA.append(choiceA) if choiceB.isEmpty() == False: choiceB.setOutId(ce.getChildId()) nodeChoicesB.append(choiceB) nodeChoicesA = Choice.removeDupChoices(nodeChoicesA) nodeChoicesB = Choice.removeDupChoices(nodeChoicesB) if len(nodeChoicesA) != 0: wholeChoicesA.append(nodeChoicesA) if len(nodeChoicesB) != 0: wholeChoicesB.append(nodeChoicesB) children = nodeRepository.loadNodes(node.getChildrenId()) for child in children: if str(child.getId()) not in mapping: mapping[str(child.getId())] = "" queue.append(child) straSetA = Strategy.buildStrategies("A", wholeChoicesA) straSetB = Strategy.buildStrategies("B", wholeChoicesB) ChoicesA = Strategy.toChoicesCombination(straSetA) ChoicesB = Strategy.toChoicesCombination(straSetB) # if settings.DEBUG: # print("strategies of player A:") # for stra in straSetA: # print(stra.toString()) # print ("strategies of player B:") # for stra in straSetB: # print(stra.toString()) if settings.DEBUG: print("A的策略数: ", len(straSetA)) print("B的策略数: ", len(straSetB)) return straSetA, straSetB, ChoicesA, ChoicesB
def childNodes(startNode): queue = [] mapping = {} queue.append(startNode) mapping[str(startNode.getId())] = "" while len(queue) > 0: node = queue.pop(0) for child in nodeRepository.loadNodes(node.getChildrenId()): if str(child.getId()) not in mapping: mapping[str(child.getId())] = "" queue.append(child) return mapping
def getAncestor(node, ancestor): parents = nodeRepository.loadNodes(node.getParentsId()) # 直接取得是策略 ancestors = [] if len(parents) == 0: # 如果是叶子节点,则直接返回 @path ancestors.append(ancestor) return ancestors else: for parent in parents: # 如果不是叶子节点 则在原来的策略上加上一条边 newancestor = copy.deepcopy(ancestor) parentId = parent.getId() newancestor.append(parentId) ancestors.extend(getAncestor(parent, newancestor)) return ancestors
def getTransfer(root): trans = [] queue = [] Mapping = {} queue.append(root) Mapping[str(root.getId())] = "" while len(queue) != 0: node = queue.pop(0) trans.extend(buildNodeTransfer(node)) children = nodeRepository.loadNodes(node.getChildrenId()) for child in children: if str(child.getId()) not in Mapping: queue.append(child) Mapping[str(child.getId())] = "" return trans
def getAllPaths(subtreeRoot, path, leavesUtil): children = nodeRepository.loadNodes(subtreeRoot.getChildrenId()) # 直接取得是策略 rlt = [] if len(children) == 0: # 如果是叶子节点,则直接返回 @path utils = getUtility(subtreeRoot, leavesUtil) print("zhuangtai :", subtreeRoot.getStates(), utils) path.setUtility(utils) path.toString() rlt.append(path) return rlt else: for child in children: # 如果不是叶子节点 则在原来的策略上加上一条边 outEdge = subtreeRoot.getOutEdge(child.getId()) newPath = copy.deepcopy(path) newPath.appendEdge(outEdge) rlt.extend(getAllPaths(child, newPath, leavesUtil)) return rlt
def childNodesMapping(root): Queue = [] mapping = {} Queue.append(root) mapping[str(root.getId())] = "" childNodesMapping = {} starttime = time.time() T = 0 while len(Queue) > 0: node = Queue.pop(0) T = T + 1 if T % 10 == 0: endtime = time.time() # print("遍历10个的时间: " ,endtime - starttime) starttime = endtime childNodesMapping[str(node.getId())] = childNodes(node) for child in nodeRepository.loadNodes(node.getChildrenId()): if str(child.getId()) not in mapping: mapping[str(child.getId())] = "" Queue.append(child) return childNodesMapping
def getAllnodeAncestor(root): queue = [] mapping = {} queue.append(root) mapping[str(root.getId())] = "" ancestorMapping = {} starttime = time.time() T = 0 while len(queue) > 0: node = queue.pop(0) T = T + 1 if T % 10 == 0: endtime = time.time() # print("遍历10个的时间: ", endtime - starttime) starttime = endtime ancestorMapping[str(node.getId())] = getAncestor(node, [node.getId()]) for child in nodeRepository.loadNodes(node.getChildrenId()): if str(child.getId()) not in mapping: mapping[str(child.getId())] = "" queue.append(child) return ancestorMapping
def generateDGA(self): counter = 0 # transfer = [] queue = [] queue.append(self._root) # 根节点入队 starttime = time.time() while len(queue) > 0: # 当队列不为空时 if settings.DEBUG == True: counter = counter + 1 if counter % 1000 == 0: endtime = time.time() print("已经处理的节点数:", counter) print("前一批节点总共耗时:", endtime - starttime) starttime = endtime node = queue.pop(0) if node.isLeafNode() == True: self._LeafIdList.append(node.getId()) nodeRepository.addnode(node) continue changeList = node.getAllChanges() # 计算所有独立的变化 序号变id {[[Term1,2],[Term2,3]],...} # print(changeList) for combinedChange in changeList: # 每个变化的组合对应一条边 chd, action = node.createChild(combinedChange) # 根据父节点和复合边,生成子节点。生成过程中,更新子节点各承诺的premise chd.addParentId(node.getId()) # 将其父亲节点加入 queue.append(chd) # trans = [node.getStates(), action, chd.getStates()] # transfer.append(trans) nodeRepository.addnode(node) nodeRepository.saveLeafIdList(self.getLeafList(), GNode.Id) # TODO check transfer = getTransfer(self._root) leafIdList, _ = nodeRepository.getLeafIdList() leaves = nodeRepository.loadNodes(leafIdList) leavesUtil = [] for leaf in leaves: ua = random.randint(1, 50) ub = random.randint(1, 50) item = [leaf, ua, ub] leavesUtil.append(item) return (self._root.getStates(), transfer, self._root,leavesUtil)
def buildNodeTransfer(node): trans = [] children = nodeRepository.loadNodes(node.getChildrenId()) for child in children: trans.append([node.getStates(), node.getOutEdge(child.getId()).toString(), child.getStates()]) return trans
def reduceStrategies(root): ExistPath = {} #存储两个节点之间是否存在路径 Iskeyancestor = {} #存储是否是关键祖先节点 childNodeMapping, ancestorMapping = getData(root) #得到孩子节点的集合与祖先节点的集合 ChoiceCombination.ancestorMapping = ancestorMapping ChoiceCombination.childNodesMapping = childNodeMapping queue = [] mapping = {} queue.append(root) mapping[str(root.getId())] = "" ChoicesA = [] #存储所有已生成的策略的组合 ChoicesAFlag = {} #标记字典用于更新ChoicesA ChoicesB = [] ChoicesBFlag = {} TT = 0 starttime = time.time() while len(queue) > 0: node = queue.pop(0) print("当前节点的状态:", node.getStates()) TT = TT + 1 print("处理过的节点个数: ", TT) endtime = time.time() print("处理这个结点耗时:", endtime - starttime) starttime = endtime newChoicesA = [] newChoicesB = [] nodeChoicesA = [] nodeChoicesB = [] for ce in node.getOutEdges(): choiceA, choiceB = ce.getAllChoices() nodeRepository.addChoice(choiceA) nodeRepository.addChoice(choiceB) if choiceA.isEmpty() == False: choiceA.setOutId(ce.getChildId()) nodeChoicesA.append(choiceA) if choiceB.isEmpty() == False: choiceB.setOutId(ce.getChildId()) nodeChoicesB.append(choiceB) nodeChoicesA = Choice.removeDupChoices(nodeChoicesA) nodeChoicesB = Choice.removeDupChoices(nodeChoicesB) newChoicesA, Iskeyancestor, ExistPath = updatenewChoices( newChoicesA, ChoicesA, nodeChoicesA, ChoicesAFlag, ExistPath, Iskeyancestor) newChoicesB, Iskeyancestor, ExistPath = updatenewChoices( newChoicesB, ChoicesB, nodeChoicesB, ChoicesBFlag, ExistPath, Iskeyancestor) ChoicesA, ChoicesAFlag = updateChoices(ChoicesA, newChoicesA, ChoicesAFlag) ChoicesB, ChoicesBFlag = updateChoices(ChoicesB, newChoicesB, ChoicesBFlag) if settings.DEBUG: print("A当前的策略组合: ") for cComb in ChoicesA: print(cComb.toString()) print("B当前的策略组合: ") for cComb in ChoicesB: print(cComb.toString()) print("ChoicesA的长度", len(ChoicesA)) print("ChoicesB的长度", len(ChoicesB)) children = nodeRepository.loadNodes(node.getChildrenId()) for child in children: if str(child.getId()) not in mapping: mapping[str(child.getId())] = "" queue.append(child) straSetA = Strategy.buildreducedStrategies("A", ChoicesA) straSetB = Strategy.buildreducedStrategies("B", ChoicesB) return straSetA, straSetB, ChoicesA, ChoicesB