コード例 #1
0
ファイル: main.py プロジェクト: worldofnick/Machine-Learning
def ID3(X, Y, attrs, currentDepth, maxDepth=7):

	currentDepth += 1
	# If all examples are positive, Return the single-node tree Root, with label = +.
 	# If all examples are negative, Return the single-node tree Root, with label = -.
	if len(set(Y)) == 1:
		return Leaf(Y[0])

	# If the number of predicting attributes is empty, then return a single node tree with
	# Label = most common value of the target attribute in the examples
	if len(attrs) == 0 or currentDepth == maxDepth:
		return Leaf(majorityElement(Y))

	# A = The Attribute that best classifies examples.
	A = findBestFeatureIndex(X, Y, attrs)
	newAttrs = [attr for attr in attrs if attr != A]
	# Decision Tree attribute for Root = A.
	root = Node(A)

	# For each possible value, vi, of A
	for possible_value in [0, 1]:
		# Let Examples(vi) be the subset of examples that have the value vi for A
		X_s, Y_s = splitData(X, Y, A, possible_value)

		if len(X_s) == 0 or len(Y_s) == 0: 
			# Then below this new branch add a leaf node with label = most common target value in the examples
			root.addChild(possible_value, Leaf(majorityElement(Y)))
		else:
			# Else below this node add a subtree ID3(Examples_vi, TargetAttr, Attrs - {A})
			root.addChild(possible_value, ID3(X_s, Y_s, newAttrs, currentDepth, maxDepth))

	return root
コード例 #2
0
def wf_ICALL(node, type, scope):
    newNode = Node("ICALL")
    newNode.addAttribute(node.attributes[0])
    item = lookup(node.attributes[0], scope)
    if item != None:
        (NA, varname, arguments, returnType, offset, scopefound) = item
    else:
        print("Function " + node.attributes[0] + " Not found")
        sys.exit()
    arguments = flatten(arguments)
    if 2 * (len(node.children)) == len(arguments):
        value = 1
        for child in node.children:
            if arguments[value] == "M_int":
                newNode.addChild(wf_I_expr(child, "Int", scope))
            elif (arguments[value] == "M_bool"):
                newNode.addChild(wf_I_expr(child, "Bool", scope))
            else:
                print("Invalid Argument Type")
                sys.exit()
            value += 2
        return newNode
    else:
        print("Improper number of arguments in function call")

    if (returnType == "M_int" and (type == "Int" or type == "Print")):
        return newNode
    elif (returnType == "M_bool" and (type == "Bool" or type == "Print")):
        return newNode
    else:
        print("Function call return type does not match type of calling code")
        sys.exit()
コード例 #3
0
class App(object):
    def __init__(self):
        print("Creando nodos: ", end='')
        self.i = Node("I", 7)
        self.a = Node("A", 6)
        self.b = Node("B", 2)
        self.c = Node("C", 1)
        self.f = Node("F", 0)
        self.i.addChild(self.a, 1)
        self.i.addChild(self.b, 4)
        print("I ", end='')
        self.a.addChild(self.b, 2)
        self.a.addChild(self.c, 5)
        self.a.addChild(self.f, 12)
        print("A ", end='')
        self.b.addChild(self.c, 2)
        print("B ", end='')
        self.c.addChild(self.f, 3)
        print("C ", end='')
        print("F ", end='')

    def searchAStarAlgorithm(self):
        print("\nBuscando ruta optima con Algoritmo A*")
        astar = AStarAlgorithm(self.i, self.f)
        print("Nodo Inicial: %s -----> Nodo Final %s" %
              (self.i.name, self.f.name))
        astar.run()
コード例 #4
0
def wf_I_Int(node, scope):
    if (node.name == "M_mul"):
        newNode = Node("IMUL")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_add"):
        newNode = Node("IADD")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_sub"):
        newNode = Node("ISUB")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_div"):
        newNode = Node("IDIV")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_neg"):
        newNode = Node("INEG")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    else:
        print("Expected Integer expression but got " + node.name + " Instead")
        sys.exit()
    return newNode
コード例 #5
0
ファイル: gprog.py プロジェクト: eddief/genetic-generator
	def generateTree(self, indsize=3):
		default = "def namedfunc(x):\n\t"
		rootNode = Node(default)
		returnNode = Node("\n\treturn ")
		self.pickChild(rootNode, rootNode)
		rootNode.addChild(returnNode)
		self.pickChild(returnNode, rootNode)	
		return rootNode
コード例 #6
0
    def startTree(carState):
        """
        This function starts the creation of the tree of paths.

        carState: the initial state of the car before starting the search for
        an optimal path
        """
        root = Node(0, [])
        root.addChild(generateChildren(root, carState, 0, []))
コード例 #7
0
ファイル: Graph.py プロジェクト: CatharticMonkey/Snippets
	def getNode(self, k, gDict = None):
		if k not in self._nodeMap:
			newNode = Node(k)
			self._nodeMap[k] = newNode

			for v in gDict[k]:
				newNode.addChild(self.getNode(v, gDict))

		return self._nodeMap[k]
コード例 #8
0
def wf_iBLOCK(node, scope):
    newNode = Node("iBlock")
    tally = 0
    for child in node.children:
        if child.name == "M_fun":
            newNode.addChild(wf_I_fbody(child, scope))
        elif child.name == "M_var":
            tally += 1
        else:
            newNode.addChild(wf_I_stmt(child, scope))
    newNode.addAttribute(tally)
    return newNode
コード例 #9
0
def wf_I_prog(node):
    if node.name == "M_prog":
        newNode = Node("IPROG")
        table = getTable("Prog")
        newNode.addAttribute(table.numberOfLocal)

        for child in node.children:
            if (child.name == "M_fun"):
                newNode.addChild(wf_I_fbody(child, 0))
        for child in node.children:
            if (child.name != "M_var" and child.name != "M_fun"):
                newNode.addChild(wf_I_stmt(child, 0))
        return newNode
コード例 #10
0
def wf_I_opn(node, type, scope):
    if (node.name == "M_fn"):
        newNode = wf_ICALL(node, type, scope)
    elif (node.name == "M_app"):
        newNode = Node("IAPP")
        newNode.addChild(wf_I_opn(node.children[0], type, scope))
    elif (type == "Int"):
        newNode = wf_I_Int(node, scope)
    elif (type == "Bool"):
        newNode = wf_I_Bool(node, scope)
    else:
        print("incompatible type and expression")
        sys.exit()
    return newNode
コード例 #11
0
def wf_IASS(node, scope):
    newNode = Node("IASS")
    item = lookup(node.attributes[0], scope)
    if (item == None):
        print("Variable " + node.attributes[0] + " not found for assignment")
        sys.exit()
    else:
        (type, varname, varType, offset, scopefound) = item
        newNode.addAttribute(node.attributes[0])
        newNode.addAttribute(offset)
        if (varType == "M_int"):
            newNode.addChild(wf_I_expr(node.children[0], "Int", scope))
        elif (varType == "M_bool"):
            newNode.addChild(wf_I_expr(node.children[0], "Bool", scope))
    return newNode
コード例 #12
0
def wf_iRETURN(node, name, scope):
    item = lookup(name, scope)
    if item == None:
        print("Something wrong with return statements")
        sys.exit()
    else:
        newNode = Node("iRETURN")
        (type, varname, arguments, returnType, offset, scopefound) = item
        if (returnType == "M_int"):
            newNode.addChild(wf_I_expr(node.children[0], "Int", scope))
        elif (returnType == "M_bool"):
            newNode.addChild(wf_I_expr(node.children[0], "Bool", scope))
        else:
            print("Unsupported return type: " + returnType)
            sys.exit()
        return newNode
コード例 #13
0
    def testContainsGrandChildNode(self):
        root = self.tree.getRoot()

        child1 = Node("child1")
        child2 = Node("child2")
        grandChild1 = Node("grandChild1")
        grandChild2 = Node("grandChild2")
        grandChild3 = Node("grandChild3")

        root.addChild(child1)
        root.addChild(child2)
        child1.addChild(grandChild1)
        child2.addChild(grandChild2)

        self.assertTrue(self.tree.contains(grandChild1))
        self.assertTrue(self.tree.contains(grandChild2))
        self.assertFalse(self.tree.contains(grandChild3))
コード例 #14
0
	def testContainsGrandChildNode(self):
		root = self.tree.getRoot()

		child1 = Node("child1")
		child2 = Node("child2")
		grandChild1 = Node("grandChild1")
		grandChild2 = Node("grandChild2")
		grandChild3 = Node("grandChild3")
		
		root.addChild(child1)
		root.addChild(child2)
		child1.addChild(grandChild1)
		child2.addChild(grandChild2)

		self.assertTrue(self.tree.contains(grandChild1))
		self.assertTrue(self.tree.contains(grandChild2))
		self.assertFalse(self.tree.contains(grandChild3))
コード例 #15
0
ファイル: parseryacc.py プロジェクト: Awattling/compiler
def p_fun_body(p):
    '''fun_body : BEGIN prog_stmts RETURN expr SEMICOLON END
				| prog_stmts RETURN expr SEMICOLON'''
    if len(p) == 7:
        ret = Node("M_return")
        ret.addChild(p[4])
        if p[2] is not None:
            p[0] = p[2], ret
        else:
            p[0] = ret
    else:
        ret = Node("M_return")
        ret.addChild(p[3])
        if p[1] is not None:
            p[0] = p[1], ret
        else:
            p[0] = ret
コード例 #16
0
    def build(self):
        with open(self.data) as f:
            mvn_result = f.read()
            nodeList = mvn_result.split('\n')
        parent = Node(nodeList[0])

        self.tree = Tree(parent)
        for rawNode in nodeList[1:]:
            level = self.computeLevel(rawNode)
            child = Node(rawNode)

            while (parent.getLevel() >= level):
                parent = parent.getParent()

            parent.addChild(child)
            parent = child

        return self.tree
コード例 #17
0
    def testFindGrandChildNode(self):
        root = self.tree.getRoot()

        child1 = Node("child1")
        child2 = Node("child2")
        grandChild1 = Node("grandChild1")
        grandChild2 = Node("grandChild2")
        grandChild3 = Node("grandChild3")

        root.addChild(child1)
        root.addChild(child2)
        child1.addChild(grandChild1)
        child2.addChild(grandChild2)

        foundGC1 = self.tree.find(grandChild1)
        foundGC2 = self.tree.find(grandChild2)

        self.assertEquals("grandChild1", foundGC1.getData())
        self.assertEquals("grandChild2", foundGC2.getData())
        self.assertEquals(None, self.tree.find(grandChild3))
コード例 #18
0
	def testFindGrandChildNode(self):
		root = self.tree.getRoot()

		child1 = Node("child1")
		child2 = Node("child2")
		grandChild1 = Node("grandChild1")
		grandChild2 = Node("grandChild2")
		grandChild3 = Node("grandChild3")
		
		root.addChild(child1)
		root.addChild(child2)
		child1.addChild(grandChild1)
		child2.addChild(grandChild2)
		
		foundGC1 = self.tree.find(grandChild1)
		foundGC2 = self.tree.find(grandChild2)

		self.assertEquals("grandChild1", foundGC1.getData())
		self.assertEquals("grandChild2", foundGC2.getData())
		self.assertEquals(None, self.tree.find(grandChild3))
コード例 #19
0
ファイル: DTAlgorithm.py プロジェクト: SabaAlex/AI
    def __generateDT(self, dataPartition, attributeList):
        node = Node()

        attributeListFromDataSet = list(map(lambda measurement: measurement.getPrediction(), dataPartition))
        attributeDistribution = {i: attributeListFromDataSet.count(i) for i in set(attributeListFromDataSet)}

        if len(set(map(lambda measurement: measurement.getPrediction(), dataPartition))) == 1:
            node.setLeaf()
            node.setLabel(dataPartition[0].getPrediction())
            return node

        else:
            if not attributeList:
                node.setLeaf()
                node.setLabel(max(attributeDistribution.items(), key=lambda x: x[1])[0])
                return node

            else:
                separationAttribute = self.__AttributeSelection(dataPartition, attributeList)
                node.setLabel(self.__attributeDictionary[separationAttribute])

                for value in self.__giniOutcome:
                    newDataPartition = \
                        list(filter(lambda measurement:
                               (measurement.getMeasurements()[separationAttribute] >=
                                self.__giniValues[separationAttribute]) == value,
                               dataPartition))

                    if not newDataPartition:
                        newChild = Node()
                        newChild.setLeaf()
                        newChild.setLabel(max(attributeDistribution.items(), key=lambda x: x[1])[0])
                        node.addChild(newChild)
                        node.addValueBranch(value)

                    else:
                        node.addChild(self.__generateDT(newDataPartition,
                                                        list(set(attributeList) - {separationAttribute})))
                        node.addValueBranch(value)

                return node
コード例 #20
0
def wf_I_fbody(current, scope):
    for child in current.children:

        if child.name == "M_block":
            scope = scope + 1
            newNode = Node("IFUN")
            table = getTable(current.attributes[0])
            if (table == None):
                print("function " + current.attributes[0] +
                      " not defined in current scope")
                sys.exit()

            newNode.addAttribute(current.attributes[0])
            newNode.addAttribute(table.numberOfLocal)
            newNode.addAttribute(table.numberOfArguments)

            for tiny in child.children:
                if (tiny.name == "M_return"):
                    newNode.addChild(
                        wf_iRETURN(tiny, current.attributes[0], scope))
                elif (tiny.name == "M_fun"):
                    newNode.addChild(wf_I_fbody(tiny, scope))
                elif (tiny.name != "M_var" and tiny.name != "M_fun"):
                    newNode.addChild(wf_I_stmt(tiny, scope))
    return newNode
コード例 #21
0
ファイル: UI.py プロジェクト: NeniscaMaria/AI
 def generateTree(self, data, attributes):
     node = Node()
     if self.__checkSameClass(data):
         node.setLabel(data[0].getResult())
         return node
     else:
         if attributes == []:
             label = self.__getMajority(data)
             node.setLabel(label)
             return node
         else:
             sepparationAttribute = self.__attributeSelection(
                 data, attributes)
             node.setLabel(sepparationAttribute)
             for v in [1, 2, 3, 4, 5]:
                 listData = []
                 for d in data:
                     if sepparationAttribute == "lw" and d.getLW() == v:
                         listData.append(d)
                     if sepparationAttribute == "ld" and d.getLD() == v:
                         listData.append(d)
                     if sepparationAttribute == "rw" and d.getRW() == v:
                         listData.append(d)
                     if sepparationAttribute == "rd" and d.getRD() == v:
                         listData.append(d)
                 #pre pruning
                 if listData == [] or len(listData) == len(data) or len(
                         listData) == 2 * len(data) or len(listData) < 5:
                     newNode = Node()
                     label = self.__getMajority(data)
                     newNode.setLabel(label)
                     node.addChild(newNode)
                     return node
                 else:
                     copyA = copy.deepcopy(attributes)
                     copyA.remove(sepparationAttribute)
                     newNode = self.generateTree(listData, copyA)
                     node.addChild(newNode)
             return node
コード例 #22
0
 def makeTree(self, curPlayer, move, depth):
     root = Node()
     moves = self.getVaildMoves(curPlayer)
     if (depth == 0 or len(moves) == 0):
         v = self.evaluate()
         if curPlayer == 2:
             v *= -1
         root.setVal(v)
         #print('v:' + str(v), end = " ")
     root.setMove(move)
     #if move is not None:
     #print(move.toString(), end = " ")
     if depth > 0:
         for m in moves:
             self.move(m)
             if curPlayer == 1:
                 curPlayer = 2
             else:
                 curPlayer = 1
             root.addChild(self.makeTree(curPlayer, m, depth - 1))
             self.unmove(m)
     return root
コード例 #23
0
def wf_I_APP(node, type, scope):
    newNode = Node("IAPP")
    if (node.name == "M_fn"):
        newNode.addChild(wf_ICALL(node, type, scope))
        return newNode
    elif (node.name == "M_app"):
        newNode.addChild(wf_I_opn(node.children[0], type, scope))
        return newNode
    else:
        newNode.addChild(wf_I_opn(node, type, scope))
        return newNode
コード例 #24
0
ファイル: BFS.py プロジェクト: AssaultKoder95/Google
 def setUp(self):
     self.bfs=BFS()
     tree=Tree()
     node1=Node()
     node1.setValue(1)
     node2=Node()
     node2.setValue(2)
     node3=Node()
     node3.setValue(3)
     node4=Node()
     node4.setValue(4)
     node5=Node()
     node5.setValue(5)
     node1.addChild(node2)
     node1.addChild(node3)
     node2.addChild(node4)
     node3.addChild(node5)
     node4.addChild(node2)
     tree.setRoot(node1)
     self.bfs.setTree(tree)
コード例 #25
0
 def setUp(self):
     self.bfs = BFS()
     tree = Tree()
     node1 = Node()
     node1.setValue(1)
     node2 = Node()
     node2.setValue(2)
     node3 = Node()
     node3.setValue(3)
     node4 = Node()
     node4.setValue(4)
     node5 = Node()
     node5.setValue(5)
     node1.addChild(node2)
     node1.addChild(node3)
     node2.addChild(node4)
     node3.addChild(node5)
     node4.addChild(node2)
     tree.setRoot(node1)
     self.bfs.setTree(tree)
コード例 #26
0
 def __makeCallGraph(self,lines):
     for index in range(len(lines)):
         if lines[index].startswith('===>'):
             classname ,methodname = self.__patternMethod(lines[index][4:])
             # print classname
             # print methodname
             parentNode = Node(classname,methodname)
             if parentNode.getHash() not in self.__nodes:
                 self.__nodes[parentNode.getHash()] = parentNode
                 # for key in self.__nodes.keys():
                 #     print self.__nodes[key].getClassName()
                 #     print self.__nodes[key].getMethodName()
                 #     print self.__nodes[key].getChildList()
             else:
                 parentNode = self.__nodes[parentNode.getHash()]
             childstate = lines[index+1]
             if 'invoke' not in childstate:#没有invoke认为赋值语句
                 if 'return' in childstate:#如果是return 说明return的上一句是return下一句的父节点
                     # print childstate
                     childchildstate = lines[index + 2]
                     classname, methodname = self.__patternMethod(childchildstate[4:])
                     gradepaNode = Node(classname, methodname)
                     if gradepaNode.getHash() not in self.__nodes:
                         self.__nodes[gradepaNode.getHash()] = gradepaNode
                         gradepaNode.addChild(parentNode)
                     else:
                         gradepaNode = self.__nodes[gradepaNode.getHash()]
                         gradepaNode.addChild(parentNode)
             else:
                 temp = self.__patternNode(childstate[7:])
                 if temp is None:
                     continue
                 classname ,methodname = self.__splitNodestr(temp)
                 # print classname
                 # print methodname
                 childNode = Node(classname,methodname)
                 self.__nodes[childNode.getHash()] = childNode
                 parentNode.addChild(childNode)
         if lines[index].startswith('------>'):
             pass
コード例 #27
0
def wf_I_Bool(node, scope):
    if (node.name == "M_eq"):
        newNode = Node("IEQ")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_ge"):
        newNode = Node("IGE")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_gt"):
        newNode = Node("IGT")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_le"):
        newNode = Node("ILE")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_lt"):
        newNode = Node("ILT")
        for child in node.children:
            newNode.addChild(wf_I_expr(child, "Int", scope))
    elif (node.name == "M_and"):
        newNode = Node("IAND")
        for child in node.children:
            newNode.addChild(wf_I_opn(child, "Bool", scope))
    elif (node.name == "M_not" and type == "Bool"):
        newNode = Node("INOT")
        for child in node.children:
            newNode.addChild(wf_I_opn(child, "Bool", scope))
    elif (node.name == "M_or" and type == "Bool"):
        newNode = Node("IOR")
        for child in node.children:
            newNode.addChild(wf_I_opn(child, "Bool", scope))
    else:
        print("Expected boolean expression but got " + node.name + " Instead")
        sys.exit()
    return newNode
コード例 #28
0
    def testFoundNodeBuildWithChildren(self):
        root = self.tree.getRoot()

        child1 = Node("child1")
        child2 = Node("child2")
        child3 = Node("child3")
        grandChild1 = Node("grandChild1")
        grandChild11 = Node("grandChild11")
        grandChild2 = Node("grandChild2")
        grandChild22 = Node("grandChild22")
        descendant1 = Node("descendant1")
        descendant11 = Node("descendant11")
        descendant2 = Node("descendant2")
        descendant22 = Node("descendant22")
        descendant3 = Node("descendant3")

        root.addChild(child1)
        root.addChild(child2)
        root.addChild(child3)
        child1.addChild(grandChild1)
        child2.addChild(grandChild2)
        grandChild1.addChild(descendant1)
        grandChild1.addChild(descendant11)
        grandChild2.addChild(descendant2)
        grandChild2.addChild(descendant22)

        foundNode = self.tree.find(Node("child2"))

        expectedString = ("1   child2\n"
                          "2     grandChild2\n"
                          "3       descendant2\n"
                          "3       descendant22\n")

        self.assertEquals(expectedString, foundNode.buildWithChildren(True))
コード例 #29
0
    def testContainsDescendantNodes(self):
        root = self.tree.getRoot()

        child1 = Node("child1")
        child2 = Node("child2")
        child3 = Node("child3")
        grandChild1 = Node("grandChild1")
        grandChild11 = Node("grandChild11")
        grandChild2 = Node("grandChild2")
        grandChild22 = Node("grandChild22")
        descendant1 = Node("descendant1")
        descendant11 = Node("descendant11")
        descendant2 = Node("descendant2")
        descendant22 = Node("descendant22")
        descendant3 = Node("descendant3")

        root.addChild(child1)
        root.addChild(child2)
        root.addChild(child3)
        child1.addChild(grandChild1)
        child2.addChild(grandChild2)
        grandChild1.addChild(descendant1)
        grandChild1.addChild(descendant11)
        grandChild2.addChild(descendant2)
        grandChild2.addChild(descendant22)

        self.assertTrue(self.tree.contains(Node("descendant1")))
        self.assertTrue(self.tree.contains(Node("descendant11")))
        self.assertTrue(self.tree.contains(Node("descendant2")))
        self.assertTrue(self.tree.contains(Node("descendant22")))
        self.assertFalse(self.tree.contains(descendant3))
コード例 #30
0
def wf_print(current, scope):
    newNode = Node("IPRINT")
    newNode.addChild(wf_I_expr(current.children[0], "Print", scope))
    return newNode
コード例 #31
0
def wf_ICOND(node, scope):
    newNode = Node("ICOND")
    newNode.addChild(wf_I_expr(node.children[0], "Bool", scope))
    newNode.addChild(wf_I_stmt(node.children[1], scope))
    newNode.addChild(wf_I_stmt(node.children[2], scope))
    return newNode
コード例 #32
0
	def testTreeToString(self):
		root = self.tree.getRoot()

		child1 = Node("child1")
		child2 = Node("child2")
		child3 = Node("child3")
		grandChild1 = Node("grandChild1")
		grandChild11 = Node("grandChild11")
		grandChild2 = Node("grandChild2")
		grandChild22 = Node("grandChild22")
		descendant1 = Node("descendant1")
		descendant11 = Node("descendant11")
		descendant2 = Node("descendant2")
		descendant22 = Node("descendant22")
		descendant3 = Node("descendant3")
		
		root.addChild(child1)
		root.addChild(child2)
		root.addChild(child3)
		child1.addChild(grandChild1)
		child2.addChild(grandChild2)
		grandChild1.addChild(descendant1)
		grandChild1.addChild(descendant11)
		grandChild2.addChild(descendant2)
		grandChild2.addChild(descendant22)

		expectedString = (	"0 hello world\n"
							"1   child1\n"
							"2     grandChild1\n"
							"3       descendant1\n"
							"3       descendant11\n"
							"1   child2\n"
							"2     grandChild2\n"
							"3       descendant2\n"
							"3       descendant22\n"
							"1   child3\n")

		self.assertEquals(expectedString, self.tree.toString(True))
コード例 #33
0
	def testContainsDescendantNodes(self):
		root = self.tree.getRoot()

		child1 = Node("child1")
		child2 = Node("child2")
		child3 = Node("child3")
		grandChild1 = Node("grandChild1")
		grandChild11 = Node("grandChild11")
		grandChild2 = Node("grandChild2")
		grandChild22 = Node("grandChild22")
		descendant1 = Node("descendant1")
		descendant11 = Node("descendant11")
		descendant2 = Node("descendant2")
		descendant22 = Node("descendant22")
		descendant3 = Node("descendant3")
		
		root.addChild(child1)
		root.addChild(child2)
		root.addChild(child3)
		child1.addChild(grandChild1)
		child2.addChild(grandChild2)
		grandChild1.addChild(descendant1)
		grandChild1.addChild(descendant11)
		grandChild2.addChild(descendant2)
		grandChild2.addChild(descendant22)
		


		self.assertTrue(self.tree.contains(Node("descendant1")))
		self.assertTrue(self.tree.contains(Node("descendant11")))
		self.assertTrue(self.tree.contains(Node("descendant2")))
		self.assertTrue(self.tree.contains(Node("descendant22")))
		self.assertFalse(self.tree.contains(descendant3))
コード例 #34
0
	def testFoundNodeBuildWithChildren(self):
		root = self.tree.getRoot()

		child1 = Node("child1")
		child2 = Node("child2")
		child3 = Node("child3")
		grandChild1 = Node("grandChild1")
		grandChild11 = Node("grandChild11")
		grandChild2 = Node("grandChild2")
		grandChild22 = Node("grandChild22")
		descendant1 = Node("descendant1")
		descendant11 = Node("descendant11")
		descendant2 = Node("descendant2")
		descendant22 = Node("descendant22")
		descendant3 = Node("descendant3")

		
		root.addChild(child1)
		root.addChild(child2)
		root.addChild(child3)
		child1.addChild(grandChild1)
		child2.addChild(grandChild2)
		grandChild1.addChild(descendant1)
		grandChild1.addChild(descendant11)
		grandChild2.addChild(descendant2)
		grandChild2.addChild(descendant22)

		foundNode = self.tree.find(Node("child2"))

		expectedString = (	"1   child2\n"
							"2     grandChild2\n"
							"3       descendant2\n"
							"3       descendant22\n")

		self.assertEquals(expectedString, foundNode.buildWithChildren(True))
コード例 #35
0
    def testTreeToString(self):
        root = self.tree.getRoot()

        child1 = Node("child1")
        child2 = Node("child2")
        child3 = Node("child3")
        grandChild1 = Node("grandChild1")
        grandChild11 = Node("grandChild11")
        grandChild2 = Node("grandChild2")
        grandChild22 = Node("grandChild22")
        descendant1 = Node("descendant1")
        descendant11 = Node("descendant11")
        descendant2 = Node("descendant2")
        descendant22 = Node("descendant22")
        descendant3 = Node("descendant3")

        root.addChild(child1)
        root.addChild(child2)
        root.addChild(child3)
        child1.addChild(grandChild1)
        child2.addChild(grandChild2)
        grandChild1.addChild(descendant1)
        grandChild1.addChild(descendant11)
        grandChild2.addChild(descendant2)
        grandChild2.addChild(descendant22)

        expectedString = ("0 hello world\n"
                          "1   child1\n"
                          "2     grandChild1\n"
                          "3       descendant1\n"
                          "3       descendant11\n"
                          "1   child2\n"
                          "2     grandChild2\n"
                          "3       descendant2\n"
                          "3       descendant22\n"
                          "1   child3\n")

        self.assertEquals(expectedString, self.tree.toString(True))
コード例 #36
0
    def testFindDescendantNodes(self):
        root = self.tree.getRoot()

        child1 = Node("child1")
        child2 = Node("child2")
        child3 = Node("child3")
        grandChild1 = Node("grandChild1")
        grandChild11 = Node("grandChild11")
        grandChild2 = Node("grandChild2")
        grandChild22 = Node("grandChild22")
        descendant1 = Node("descendant1")
        descendant11 = Node("descendant11")
        descendant2 = Node("descendant2")
        descendant22 = Node("descendant22")
        descendant3 = Node("descendant3")

        root.addChild(child1)
        root.addChild(child2)
        root.addChild(child3)
        child1.addChild(grandChild1)
        child2.addChild(grandChild2)
        grandChild1.addChild(descendant1)
        grandChild1.addChild(descendant11)
        grandChild2.addChild(descendant2)
        grandChild2.addChild(descendant22)

        foundD1 = self.tree.find(Node("descendant1"))
        foundD11 = self.tree.find(Node("descendant11"))
        foundD2 = self.tree.find(Node("descendant2"))
        foundD22 = self.tree.find(Node("descendant22"))

        self.assertEquals(descendant1.getData(), foundD1.getData())
        self.assertEquals(descendant11.getData(), foundD11.getData())

        self.assertEquals(descendant2.getData(), foundD2.getData())
        self.assertEquals(descendant22.getData(), foundD22.getData())

        self.assertEquals(None, self.tree.find(descendant3))
コード例 #37
0
	def testFindDescendantNodes(self):
		root = self.tree.getRoot()

		child1 = Node("child1")
		child2 = Node("child2")
		child3 = Node("child3")
		grandChild1 = Node("grandChild1")
		grandChild11 = Node("grandChild11")
		grandChild2 = Node("grandChild2")
		grandChild22 = Node("grandChild22")
		descendant1 = Node("descendant1")
		descendant11 = Node("descendant11")
		descendant2 = Node("descendant2")
		descendant22 = Node("descendant22")
		descendant3 = Node("descendant3")
		
		root.addChild(child1)
		root.addChild(child2)
		root.addChild(child3)
		child1.addChild(grandChild1)
		child2.addChild(grandChild2)
		grandChild1.addChild(descendant1)
		grandChild1.addChild(descendant11)
		grandChild2.addChild(descendant2)
		grandChild2.addChild(descendant22)

		foundD1 = self.tree.find(Node("descendant1"))
		foundD11 = self.tree.find(Node("descendant11"))
		foundD2 = self.tree.find(Node("descendant2"))
		foundD22 = self.tree.find(Node("descendant22"))

		self.assertEquals(descendant1.getData(), foundD1.getData())
		self.assertEquals(descendant11.getData(), foundD11.getData())

		self.assertEquals(descendant2.getData(), foundD2.getData())
		self.assertEquals(descendant22.getData(), foundD22.getData())

		self.assertEquals(None, self.tree.find(descendant3))
コード例 #38
0
def wf_IWHILE(node, scope):
    newNode = Node("IWHILE")
    newNode.addChild(wf_I_expr(node.children[0], "Bool", scope))
    newNode.addChild(wf_I_stmt(node.children[1], scope))
    return newNode
コード例 #39
0
ファイル: NodeTest.py プロジェクト: gabrielmahia/tafutAI
'''
@author: Devangini Patel
'''

from Node import Node
from State import State

initialState = State()
root = Node(initialState)

childStates = initialState.successorFunction()
for childState in childStates:
    childNode = Node(State(childState))
    root.addChild(childNode)

root.printTree()