def createTree(dataSet, minSup=1): headerTable = {} for trans in dataSet: for item in trans: headerTable[item] = headerTable.get(item, 0) + dataSet[trans] for k in list(headerTable): if headerTable[k] < minSup: del (headerTable[k]) freqItemSet = set(headerTable.keys()) if len(freqItemSet) == 0: return None, None for k in headerTable: headerTable[k] = [headerTable[k], None] retTree = treeNode('Null Set', 1, None) for tranSet, count in dataSet.items(): localD = {} for item in tranSet: if item in freqItemSet: localD[item] = headerTable[item][0] if len(localD) > 0: orderedItems = [v[0] for v in sorted(localD.items(), key=lambda p: p[1], reverse=True)] updateTree(orderedItems, retTree, \ headerTable, count) return retTree, headerTable
def updateTree(items, inTree, headerTable, count): if items[0] in inTree.children: inTree.children[items[0]].inc(count) else: inTree.children[items[0]] = treeNode(items[0], count, inTree) if headerTable[items[0]][1] == None: headerTable[items[0]][1] = inTree.children[items[0]] else: updateHeader(headerTable[items[0]][1], inTree.children[items[0]]) if len(items) > 1: updateTree(items[1::], inTree.children[items[0]], headerTable, count)
def __init__(self, coalitionsByTasksList, screenDump): self.coalitionsByTasksList = coalitionsByTasksList self.root = treeNode("root", [], []) # name, coalition, onPath self.orderOfTasks = [] self.bestRevenue = 0 self.bestPartition = None self.chosenTasks = [] self.nodecount = 0 self.dummyNodeCount = 0 self.regularNodeCount = 0 self.dummyTime = 0.0 self.regularTime = 0.0 # enable/disable print self.screenDump = screenDump
def __init__(self, coalitionsByTasksList, screenDump): self.coalitionsByTasksList = coalitionsByTasksList self.root = treeNode("root", [], []) # name, coalition, onPath self.root.blocked = False self.orderOfTasks = [] self.bestRevenue = 0 self.bestPartition = None self.chosenTasks = [] # purely A-star DSs self.nodeHeuristics = [] # used for collecting all the heuristics for each node for a particular task level self.leaves = [] # used for collecting all the leaf nodes self.winnerNode = None self.unexpandedWinnerNodesPresent = False # declarations below used for statistical data collection self.nodecount = 0 self.dummyNodeCount = 0 self.regularNodeCount = 0 self.dummyTime = 0.0 self.regularTime = 0.0 # enable/disable print self.screenDump = screenDump
def addNodes(self, ptr, cNodes, taskName, addDummy, nodesOnPath = []): if ((ptr.name == "root") and (len(ptr.children) == 0)): # add the set of coalitions to the root node self.printit(" Adding Nodes to root....") for cn in cNodes: coalition = cn.getCoalition() start = time.time() # start time tN = treeNode(str(coalition), coalition, coalition) # name, coalition, robotsOnPath tN.parent = ptr tN.coalitionsOnPath.append(coalition) tN.totalRevenue = int(cn.getBid()) tN.tasksOnPath.append(taskName) tN.memberOfTask = taskName tN.allTasksOnPath.append(taskName) tN.blocked = False tN.hasWinnerOnPath = True #--------------------------------------------------------------------------------------------- # determine the heuristic - used exclusively for A* tN.heuristicVal, tN.futureNodes = self.estimateHeuristic(tN, tN.coalitionsOnPath, tN.allTasksOnPath, tN.totalRevenue) ## self.nodeHeuristics.append(tN.heuristicVal) self.leaves.append(tN) #--------------------------------------------------------------------------------------------- self.root.children.append(tN) end = time.time() # end time self.trackWinner(tN.totalRevenue, tN.coalitionsOnPath, tN.tasksOnPath) self.regularTime += (end - start) self.regularNodeCount += 1 self.nodecount += 1 # add dummy node self.printit(" Adding dummy node to node '" + str(ptr.name) + "'....") start = time.time() # start time tN = treeNode("dummy", [], []) # name, coalition, robotsOnPath tN.parent = ptr tN.memberOfTask = taskName tN.allTasksOnPath.append(taskName) #--------------------------------------------------------------------------------------------- # determine the heuristic - used exclusively for A* tN.heuristicVal, tN.futureNodes = self.estimateHeuristic(tN, tN.coalitionsOnPath, tN.allTasksOnPath, tN.totalRevenue) self.leaves.append(tN) #--------------------------------------------------------------------------------------------- self.root.children.append(tN) end = time.time() # end time self.dummyTime += (end - start) self.dummyNodeCount += 1 self.nodecount += 1 return else: if (len(ptr.children) > 0): self.printit(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") self.printit(" Traversing children of '" + str(ptr.name) + "'....") # only expand nodes that have not been blocked for child in ptr.children: if ((child.blocked == False) and (child.winnerOnPath == True)): self.printit(" Path to " + str(child.name) + " not blocked and has winner on path...") self.addNodes(child, cNodes, taskName, addDummy) # recursive call else: self.printit(" !! Path to " + str(child.name) + " is blocked or no winner(s) on path, not proceeding !!") else: self.printit(" For node " + str(ptr.name) + ", adding the current coalitions....") # if the leaf node is the winnerNode, then add children # we validate this by checking if the winnerNode's parent is the current ptr node's parent if (self.IsWinnerNode(ptr)): coalition = [] for cn in cNodes: coalition = cn.getCoalition() self.printit(" Adding node " + str(coalition) + "....") if (self.hasMember(ptr.robotsOnPath, coalition) == False): self.printit(" -- current nodes not repeated in path") self.printit(" -- items on path " + str(ptr.robotsOnPath)) start = time.time() # start time tN = treeNode(str(coalition), coalition, ptr.robotsOnPath + coalition) tN.parent = ptr tN.coalitionsOnPath = ptr.coalitionsOnPath[:] # deep copy the entire list tN.coalitionsOnPath.append(coalition) # --> extra operation than DUMMY #- - - - - - - - - - - - - - tN.totalRevenue = int(ptr.totalRevenue) + int(cn.getBid()) #- - - - - - - - - - - - - - tN.tasksOnPath = ptr.tasksOnPath[:] # deep copy the entire list tN.tasksOnPath.append(taskName) # --> extra operation than DUMMY #- - - - - - - - - - - - - - tN.memberOfTask = taskName #- - - - - - - - - - - - - - tN.allTasksOnPath = ptr.allTasksOnPath[:] tN.allTasksOnPath.append(taskName) #- - - - - - - - - - - - - - #--------------------------------------------------------------------------------------------- # determine the heuristic - used exclusively for A* tN.heuristicVal, tN.futureNodes = self.estimateHeuristic(tN, tN.coalitionsOnPath, tN.allTasksOnPath, tN.totalRevenue) #--------------------------------------------------------------------------------------------- self.printit(" -- tasks on path " + str(tN.tasksOnPath)) ptr.children.append(tN) end = time.time() # end time self.trackWinner(tN.totalRevenue, tN.coalitionsOnPath, tN.tasksOnPath) self.regularTime += (end - start) self.regularNodeCount += 1 self.nodecount += 1 else: self.printit(" -- current nodes repeated -> NOT ADDING") self.printit(" -- items on path " + str(ptr.robotsOnPath)) coalition = [] if (addDummy): # add dummy node self.printit(" ** Adding dummy node to node '" + str(ptr.name) + "'....") start = time.time() # start time tN = treeNode("dummy", [], ptr.robotsOnPath) tN.parent = ptr tN.coalitionsOnPath = ptr.coalitionsOnPath[:] tN.totalRevenue = ptr.totalRevenue tN.tasksOnPath = ptr.tasksOnPath tN.memberOfTask = taskName tN.allTasksOnPath = ptr.allTasksOnPath[:] tN.allTasksOnPath.append(taskName) #--------------------------------------------------------------------------------------------- # determine the heuristic - used exclusively for A* tN.heuristicVal, tN.futureNodes = self.estimateHeuristic(tN, tN.coalitionsOnPath, tN.allTasksOnPath, tN.totalRevenue) #--------------------------------------------------------------------------------------------- ptr.children.append(tN) end = time.time() # end time self.dummyTime += (end - start) self.dummyNodeCount += 1 self.nodecount += 1 else: # dont add dummy node self.printit(" ** Skipping adding dummy node to node '" + str(ptr.name) + "'....") self.printit(" . . . . . . . . . . . . . . . . . . . . . . . . . . . . .") else: self.printit(" ^^ Cannot add children to this node. It has the same g+h but these children are for " + str(self.winnerNode.name)) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # if no children could be added to current node, then that node is a leaf # add it to list of leaves and record revenue for A* self.printit("Children Added: --->>>>> " + str(len(ptr.children))) if (len(ptr.children) == 0): #self.leaves.append(ptr) self.printit(" No children added for " + str(ptr.name)) else: # remove ptr from self.leaves and add its children as the new leaves for leaf in self.leaves: if ((leaf.parent == ptr.parent) and (leaf.name == ptr.name) and (leaf.totalRevenue == ptr.totalRevenue) and (leaf.memberOfTask == ptr.memberOfTask)): self.leaves.pop(self.leaves.index(leaf)) for child in ptr.children: self.leaves.append(child) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - return
def addNodes(self, ptr, cNodes, taskName, addDummy, nodesOnPath=[]): if ((ptr.name == "root") and (len(ptr.children) == 0)): # add the coalitions to the root node self.printit(" Adding Nodes to root....") for cn in cNodes: coalition = cn.getCoalition() start = time.time() # start time tN = treeNode(str(coalition), coalition, coalition) tN.coalitionsOnPath.append(coalition) tN.totalRevenue = int(cn.getBid()) tN.tasksOnPath.append(taskName) self.root.children.append(tN) end = time.time() # end time self.trackWinner(tN.totalRevenue, tN.coalitionsOnPath, tN.tasksOnPath) self.regularTime += (end - start) self.regularNodeCount += 1 self.nodecount += 1 # add dummy node self.printit(" Adding dummy node to node '" + str(ptr.name) + "'....") start = time.time() # start time self.root.children.append(treeNode("dummy", [], [])) end = time.time() # end time self.dummyTime += (end - start) self.dummyNodeCount += 1 self.nodecount += 1 return else: if (len(ptr.children) > 0): self.printit( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" ) self.printit(" Traversing children of '" + str(ptr.name) + "'....") for child in ptr.children: self.addNodes(child, cNodes, taskName, addDummy) else: self.printit(" For node " + str(ptr.name) + ", adding the current coalitions....") coalition = [] for cn in cNodes: coalition = cn.getCoalition() self.printit(" Adding node " + str(coalition) + "....") if (self.hasMember(ptr.hasOnPath, coalition) == False): self.printit( " -- current nodes not repeated in path") self.printit(" -- items on path " + str(ptr.hasOnPath)) start = time.time() # start time tN = treeNode(str(coalition), coalition, ptr.hasOnPath + coalition) tN.coalitionsOnPath = ptr.coalitionsOnPath[:] tN.coalitionsOnPath.append( coalition) # --> extra operation than DUMMY tN.totalRevenue = int(ptr.totalRevenue) + int( cn.getBid()) tN.tasksOnPath = ptr.tasksOnPath[:] tN.tasksOnPath.append( taskName) # --> extra operation than DUMMY self.printit(" -- tasks on path " + str(tN.tasksOnPath)) ptr.children.append(tN) end = time.time() # end time self.trackWinner(tN.totalRevenue, tN.coalitionsOnPath, tN.tasksOnPath) self.regularTime += (end - start) self.regularNodeCount += 1 self.nodecount += 1 else: self.printit( " -- current nodes repeated -> NOT ADDING") self.printit(" -- items on path " + str(ptr.hasOnPath)) coalition = [] if (addDummy): # add dummy node self.printit(" ** Adding dummy node to node '" + str(ptr.name) + "'....") start = time.time() # start time tN = treeNode("dummy", [], ptr.hasOnPath) tN.coalitionsOnPath = ptr.coalitionsOnPath[:] tN.totalRevenue = ptr.totalRevenue tN.tasksOnPath = ptr.tasksOnPath ptr.children.append(tN) end = time.time() # end time self.dummyTime += (end - start) self.dummyNodeCount += 1 self.nodecount += 1 else: # dont add dummy node self.printit( " ** Skipping adding dummy node to node '" + str(ptr.name) + "'....") self.printit( " . . . . . . . . . . . . . . . . . . . . . . . . . . . . ." ) return
def addNodes(self, ptr, cNodes, taskName, addDummy, nodesOnPath = []): if ((ptr.name == "root") and (len(ptr.children) == 0)): # add the coalitions to the root node self.printit(" Adding Nodes to root....") for cn in cNodes: coalition = cn.getCoalition() start = time.time() # start time tN = treeNode(str(coalition), coalition, coalition) tN.coalitionsOnPath.append(coalition) tN.totalRevenue = int(cn.getBid()) tN.tasksOnPath.append(taskName) self.root.children.append(tN) end = time.time() # end time self.trackWinner(tN.totalRevenue, tN.coalitionsOnPath, tN.tasksOnPath) self.regularTime += (end - start) self.regularNodeCount += 1 self.nodecount += 1 # add dummy node self.printit(" Adding dummy node to node '" + str(ptr.name) + "'....") start = time.time() # start time self.root.children.append(treeNode("dummy", [], [])) end = time.time() # end time self.dummyTime += (end - start) self.dummyNodeCount += 1 self.nodecount += 1 return else: if (len(ptr.children) > 0): self.printit(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - -") self.printit(" Traversing children of '" + str(ptr.name) + "'....") for child in ptr.children: self.addNodes(child, cNodes, taskName, addDummy) else: self.printit(" For node " + str(ptr.name) + ", adding the current coalitions....") coalition = [] for cn in cNodes: coalition = cn.getCoalition() self.printit(" Adding node " + str(coalition) + "....") if (self.hasMember(ptr.hasOnPath, coalition) == False): self.printit(" -- current nodes not repeated in path") self.printit(" -- items on path " + str(ptr.hasOnPath)) start = time.time() # start time tN = treeNode(str(coalition), coalition, ptr.hasOnPath + coalition) tN.coalitionsOnPath = ptr.coalitionsOnPath[:] tN.coalitionsOnPath.append(coalition) # --> extra operation than DUMMY tN.totalRevenue = int(ptr.totalRevenue) + int(cn.getBid()) tN.tasksOnPath = ptr.tasksOnPath[:] tN.tasksOnPath.append(taskName) # --> extra operation than DUMMY self.printit(" -- tasks on path " + str(tN.tasksOnPath)) ptr.children.append(tN) end = time.time() # end time self.trackWinner(tN.totalRevenue, tN.coalitionsOnPath, tN.tasksOnPath) self.regularTime += (end - start) self.regularNodeCount += 1 self.nodecount += 1 else: self.printit(" -- current nodes repeated -> NOT ADDING") self.printit(" -- items on path " + str(ptr.hasOnPath)) coalition = [] if (addDummy): # add dummy node self.printit(" ** Adding dummy node to node '" + str(ptr.name) + "'....") start = time.time() # start time tN = treeNode("dummy", [], ptr.hasOnPath) tN.coalitionsOnPath = ptr.coalitionsOnPath[:] tN.totalRevenue = ptr.totalRevenue tN.tasksOnPath = ptr.tasksOnPath ptr.children.append(tN) end = time.time() # end time self.dummyTime += (end - start) self.dummyNodeCount += 1 self.nodecount += 1 else: # dont add dummy node self.printit(" ** Skipping adding dummy node to node '" + str(ptr.name) + "'....") self.printit(" . . . . . . . . . . . . . . . . . . . . . . . . . . . . .") return