def main():
    data, prune_data, test_data = load_data()
    node = create_node(data, feature_list, None, None, 0)

    # import pickle
    # f=open("node.serize","wb")
    # pickle.dump(node,f)
    # f.close()

    # f=open("node.serize","rb")
    # node=pickle.load(f)

    train_lable = nm.array(data[lable])
    _lable = []
    del data[lable]
    for _, i in data.iterrows():
        l, p = node(i, 1, 0)
        _lable.append(l)

    train_accuracy = nm.sum(nm.equal(nm.array(_lable),
                                     train_lable)) / len(train_lable)

    test_lable = nm.array(test_data[lable])
    _lable = []

    del test_data[lable]

    for _, i in test_data.iterrows():
        l, p = node(i, 1, 0)
        _lable.append(l)
    print(_lable)
    test_accuracy = nm.sum(nm.equal(nm.array(_lable),
                                    test_lable)) / len(test_lable)
    print(
        "before prune : train data accuracy:{} | test data accuracy:{}".format(
            train_accuracy, test_accuracy))

    node.prune(prune_data)

    _lable = []

    for _, i in data.iterrows():
        l, p = node(i, 1, 0)
        _lable.append(l)

    train_accuracy = nm.sum(nm.equal(nm.array(_lable),
                                     train_lable)) / len(train_lable)

    _lable = []

    for _, i in test_data.iterrows():
        l, p = node(i, 1, 0)
        _lable.append(l)
    print(_lable)
    test_accuracy = nm.sum(nm.equal(nm.array(_lable),
                                    test_lable)) / len(test_lable)
    print(
        "after prune : train data accuracy:{} | test data accuracy:{}".format(
            train_accuracy, test_accuracy))
Пример #2
0
 def test_example(self):
     (d, s, b) = make_variables("d[1, 0, 15], s[1,0,15], b[1, 0, 15]")
     chan = node("chan", pois, [d, s+b])
     
     (b0, sigma_b) = make_variables("b0[5,0,10], sigma_b[1,0,3]")
     b_constraint = node("b_constraint", gauss, [b0, b, sigma_b])
     
     model = chan*b_constraint
     
     print "model val: ", model()
Пример #3
0
def tonode(g):
    child = []

    for i, c in enumerate(g['children']):
        child.append(node(c['name'], c['children']))
        child[i].val = c['val']

    n = node("root", child)
    n.val = g['val']
    return n
Пример #4
0
def make_tree(filename="sample.123.321.dump.gz"):
    #Print statistics at the end of the program
    printstat = False 

    stat = statistics(1234)
    #Create binary tree root
    root = node(socket.inet_aton("0.0.0.0"), 0, [], None, False, 0)
    
    dump = Parser(filename)
    loop = True

    #Loop over the input file, reading one line at a time.
    while loop:
        try:
            line = dump.next()
        except:
            print "EOF"
            break
        #Parsing dumps
        if line[0] == 'D': 
            for entry in line[3]:
		fromSet = False
                time = entry[-1]
                act = 'A'     #Action: A|W
		revoked = None
		mask = line[2]
		prefix = int2ip(line[1])
		if act == "W":
		    revoked = 'revoked'
		else:
		    revoked = None
		origin = parseASpath(entry, stat)
		new_node = node(socket.inet_aton(prefix), mask, origin, revoked, fromSet, time)
                addAction(act, root, new_node, stat)
        #Parsing updates
	elif line[0] == 'U':
            for entry in line[3]:
                time = entry[-1]
		fromSet = False
		act = entry[0]     #Action: A|W
		revoked = None
		mask = entry[2]
		prefix = int2ip(entry[1])
		if act == "W":
		    revoked = 0
		else:
		    revoked = None
		origin = parseASpath(entry, stat)
		new_node = node(socket.inet_aton(prefix), mask, origin, revoked, fromSet, time)
                addAction(act,root, new_node, stat)
        #Unknown type (neither dump nor update)
        else:
	    print "Unknown message type " + line[0]

    return root
Пример #5
0
    def addEpsilonStates(self, initialNode):

        tempNode = initialNode
        tempNode.init = False
        newInitNode = node("initial", True, False)
        newInitNode.directionsDict['$'] = [tempNode]
        newFinalNode = node("final", False, True)
        for x in self.arr_nodos:
            if x.final:
                x.directionsDict['$'] = [newFinalNode]
                x.final = False
        self.arr_nodos.extend([newInitNode, newFinalNode])
Пример #6
0
    def setUp(self):

        (x0, mu0, sigma0) = make_variables("x0[.2,-6,6], mu0[0,-5,5], sigma0[1,0,3]")
        mass0 = node("mass0", gauss, {'x':x0, 'mu':mu0, 'sigma':sigma0})
    
        (x1, mu1, sigma1) = make_variables("x1[1.2,-6,6], mu1[1,-5,5], sigma1[2,0,3]")
        mass1 = node("mass1", gauss, {'x':x1, 'mu':mu1, 'sigma':sigma1})
    
        inv_mass = node("inv_mass", invariant_mass, {"a": mass0, "b":mass1})

        self.vars = (x0, mu0, sigma0, x1, mu1, sigma1)
        self.nodes = (inv_mass, mass0, mass1)
Пример #7
0
	def construct(self):
		queue = Queue.Queue()
		if self.root is None:
			self.root = node(self.data, self.type)
			queue.put(self.root)

		while queue.empty() is False:
			cur_node = queue.get()
			if cur_node.is_leaf is True:
				continue
			for child in cur_node.children.keys():
				cur_node.connection[child] = node(cur_node.children[child], self.type)
				queue.put(cur_node.connection[child])
Пример #8
0
def to_cnf(root):
    if root.kind in ['^', 'v']:
        if root.children[0] not in ['^', 'v']:
            atom, conn = root.children
        else:
            conn, atom = root.children
        # one child has to be a connective and the other a non-connetive
        if atom.kind not in ['^', 'v'] and conn.kind in ['^', 'v'] and conn.kind != root.kind:
            child1 = node(root.kind, atom, conn.children[0])
            child2 = node(root.kind, atom.clone(), conn.children[1])
            root.children = [child1, child2]
            root.kind = conn.kind

    for c in root.children:
        to_cnf(c)
 def _insert(self, value, cur_node):
     if value < cur_node.value:
         if cur_node.left_child == None:
             cur_node.left_child == node(value)
             cur_node.left_child.parent = cur_node
         else:
             self._insert(value, cur_node.left_child)
     elif value > cur_node.value:
         if cur_node.right_child == None:
             cur_node.right_child = node(value)
             cur_node.right_child.parent = cur_node
         else:
             self._insert(value, cur_node.right_child)
     else:
         print("Value already in tree!")
def Dictionary_into_Node(Node_Dic):
    Root_List = []
    #going backwards it iterates through the dictionary
    for i in reversed(range(0, len(Node_Dic))):
        #If the current Dictionary has no child it will create a node and append it
        if Node_Dic[i]['children'] == []:
            Root_List.append(node(Node_Dic[i]['name']))
            Root_List[len(Root_List) - 1].val = Node_Dic[i]['val']
        else:
            #If it does have a child it will look for its child in the Root list dictionary and append them in a new node
            Root_List.append(
                node(Node_Dic[i]['name'],
                     FatherChildren(Root_List, Node_Dic[i]['children'])))
            Root_List[len(Root_List) - 1].val = Node_Dic[i]['val']
    #Will return the last node in the List which is the root of the tree
    return Root_List[len(Root_List) - 1]
Пример #11
0
    def __init__(self,
                 data,
                 data_type,
                 y,
                 y_type,
                 n_classes,
                 F=1,
                 min_leaf_size=1,
                 n_retry=1,
                 f_num="VR",
                 f_cat="IG"):
        self.data = data
        self.data_type = data_type
        self.y = y
        self.y_type = y_type
        self.n_classes = n_classes
        self.min_leaf_size = min_leaf_size
        self.F = F
        self.options = {
            "VR": self.VR,
            "IG": self.IG,
            "GINI": self.GINI,
            "TEST": self.TEST
        }
        self.f_num = self.options[f_num]
        self.f_cat = self.options[f_cat]
        self.n_retry = n_retry

        split_feature, split_number, data_left_idx, data_right_idx = self.find_split(
            np.array(list(range(len(data)))))

        self.root = node(data_type[split_feature])
        self.root.split_value = split_number
        self.root.split_feature = split_feature
        self.grow_tree(self.root, data_left_idx, data_right_idx)
Пример #12
0
def dictionary_to_object(serialized_graph):
	# initialize root node to None
	root_node = None
	# iterate through all nodes in the dictionary
	for each_node in serialized_graph:
		# create node objects for each node in the dictionary
		# and assign a new dictionary item in the existing
		# dictionary to hold the object
		serialized_graph[each_node]['node_object'] = node(serialized_graph[each_node]['name'])
		# get the value of node from the dictionary
		serialized_graph[each_node]['node_object'].val = serialized_graph[each_node]['value']
		# empty list of childrem
		serialized_graph[each_node]['node_object'].children = []

		# if the level of this node is 0, then assign root node to the current node object
		if serialized_graph[each_node]['level'] == 0:
			root_node = serialized_graph[each_node]['node_object']
	
	# after creating all nodes,
	# iterate through each node to assign the 
	# children node object pointer to the correct parent 
	for each_node in serialized_graph:
		# iterate through all children 
		for each_child in serialized_graph[each_node]['children']:
			serialized_graph[each_node]['node_object'].children.append(serialized_graph[each_child]['node_object'])

	# return the root node
	return root_node
Пример #13
0
	def findLabel(self, treestr, i):
		tlabel = ''
		while (i < len(treestr) and treestr[i] !='(' and treestr[i] != ',' and treestr[i] != ')'):
			tlabel += treestr[i]
			i += 1
		newNode = node(tlabel)
		return newNode, i
Пример #14
0
 def fill_network(
         self, num_nodes):  #quickly fill network and randomly place nodes
     for i in range(num_nodes):  #create and add nodes to network
         ide = str(i)
         node_temp = node(ide)
         node_temp.load_pkl()
         self.add_node(node_temp)
Пример #15
0
 def insertNode(self, n):
     #Add a dummy element to the list becuase list index must start at 1
     #for heap operations to work
     if len(self.nodes) == 0:
         self.nodes.append(node(-1, -1))
     self.nodes.append(n)
     self.index[n.value] = len(self.nodes) - 1
     self.bubbleUp(n)
Пример #16
0
def jsond(data):
    obj = ast.literal_eval(data)
    print(obj)
    # return node("Root")
    children = []
    for child in obj['children']:
        children.append(jsond(child))
    return node(obj['name'], children, obj['val'])
Пример #17
0
    def _skolemize(n, fn_args = [], skolems = {}):
        if n.kind == 'A':
            fn_args.append(n.name)
        elif n.kind == 'E':
            children = [node('var', a) for a in fn_args]
            skolems[n.name] = node('fn', avail.pop(), *children)
        elif n.kind == 'var':
            if n.name in skolems:
                n.consume(skolems[n.name].clone())

        for c in n.children:
            _skolemize(c, fn_args, skolems)

        if n.kind == 'A':
            fn_args.pop()
        elif n.kind == 'E':
            del skolems[n.name]
            n.consume_child()
Пример #18
0
    def fill_network(self):
        files = os.listdir(DataMule_path)
        nodeIDs = [int(self.get_ID(file)) for file in files]
        nodeIDs.sort()

        for i in range(len(nodeIDs)):
            node_ID = nodeIDs[i]
            node_curr = node(node_ID)
            self.add_node(node_curr)
Пример #19
0
 def addNode(self, infoList):
     id, x, y, z = infoList
     id = int(id.split(",")[0].strip())
     x = float(x.split(",")[0].strip())
     y = float(y.split(",")[0].strip())
     z = float(z.split(",")[0].strip())
     newNode = node(id, x, y, z)
     self.nodeMap[id] = newNode
     self.nodes += 1
Пример #20
0
    def buildCFPTree(self, items, routeList):
        #建立头表
        for item in items:
            htnodeTmp = node(item[0], item[1])
            self.headTable.append(htnodeTmp)

        #建树
        for routeNode in routeList:
            self.insertTran(routeNode, self.root)
Пример #21
0
 def insert(self,path,data):
     """"""
     cur_node = self._head
     for step in path:
         if cur.search_children(step)==None:
             return False
         else:
             cur=cur.search_children(step)
     cur.add(node(data))
     return True
Пример #22
0
def buildGraphRecursive(root, children):
    for child in children:
        #print(child) does indeed print out child
        #if (child["children"] != ""):
        #buildGraphRecursive(child, child["children"])
        tempChild = node(child["name"])  #valid node is created out of this
        #root.children = node(child["name"]) #returns reference to object addy???
        #print(child) #dictionary type
        #print(root.name) #no error when grabbing fields
        print(tempChild.name)
Пример #23
0
def JSONToGraph(jsonString):
    jsonArr = jsonString.split('\n')
    name = jsonArr[0].split(':')[1]
    val = jsonArr[1].split(':')[1]
    children = []
    for c in jsonString[jsonString.find("{") + 1:].split(',')[:-1]:
        children.append(JSONToGraph(c))
    graph = node(name, children)
    graph.val = int(val)
    return graph
Пример #24
0
    def run(self):
        while (not self.stop_cond.is_set()):
            packet = self.client.rxpacket()
            if packet != None:
                node1 = node(packet, 0, None)
                self.LList.addNode(node1)
            else:
                print "no packet"

        print "Sniffing Terminated"
        return
Пример #25
0
 def obtainDict(self, DictRoot):  #Creates all necessary steps for dict.
     global treeToUse
     global JSONDecryptedTree
     treeToUse = json.loads(DictRoot)
     for x, y in treeToUse.items(
     ):  #Iterates across tree objects and adds it to a variable.
         temp = json.loads(y)
         treeToUse[x] = node(temp[list(temp.keys())[0]],
                             temp[list(temp.keys())[1]])
     JSONDecryptedTree = parseTree(treeToUse, JSONDecryptedTree)
     JSONDecryptedTree.show(0)
Пример #26
0
    def increment(self, graph):
        print("Received from client:", graph)
        root_children = []  # For the children of the root
        root = list(graph)[0]  # Get the name of the root
        root_val = graph[root][len(graph[root]) -
                               1]  # Get the value of the root
        del graph[root][len(graph[root]) - 1]  # Delete the root value

        for child in graph[root]:  # Traverse every child in the graph
            if not root_children:  # If the graph is empty add the child
                tmp_child = node(child[0], child[1])  # Create the node
                tmp_child.val = child[2]  # Assign its value
                root_children.append(tmp_child)  # Add it to the list
            else:
                in_list = False  # Flag to check if node already on the list
                for index in range(len(root_children)
                                   ):  # Check the list to see i already there
                    if child[0] == root_children[index].name:  # If there
                        in_list = True  # Set flag
                        root_children.append(
                            root_children[index]
                        )  # Append the one already in the list again
                        break
                if not in_list:  # If not in the list
                    tmp_child = node(child[0], child[1])  # Create the node
                    tmp_child.val = child[2]  # Assign its value
                    root_children.append(tmp_child)  # Add it to the list

        root = node(root, root_children)  # Generate the graph
        root.val = root_val  # Assign the value of the root
        increment(root)  # Increment the graph

        graph = {root.name: []}  # Start the dictionary to send to the client

        for child in root.children:  # Populate the dictionary with the children
            graph[root.name].append([child.name, child.children, child.val])

        graph[root.name].append(root.val)  # Append the new value of the root

        print("Sending to client:", graph, "\n")
        return graph  # Send it to the client
Пример #27
0
def unflatten(jFile):
    with open(jFile) as lFile:
        j = json.load(lFile)
    lFile.close()

    jlist = j["children"]

    leaf1 = node(jlist[0]["name"])
    leaf1.val = jlist[0]["val"]

    leaf2 = node(jlist[1]["name"])
    leaf1.val = jlist[1]["val"]

    leaf3 = node(jlist[2]["name"])
    leaf1.val = jlist[2]["val"]

    root = node(j["name"], [leaf1, leaf2, leaf3])
    root.val = j["val"]

    root.show()
    return root
Пример #28
0
 def run(self):
     while(not self.stop_cond.is_set()):
         packet = self.client.rxpacket()
         if packet != None:
             node1 = node(packet,0,None)
             self.LList.addNode(node1)
         else:
         	print "no packet"
     
     print "Sniffing Terminated"
     return
     
Пример #29
0
    def __init__(self, data, label, type_list, list_of_interpreter, max_split,
                 minimal_data_size):
        self.data = data
        self.type_list = type_list
        self.list_of_interpreter = list_of_interpreter
        self.max_split = max_split
        self.label = label

        self.root = node(data, label)
        self.minimal_data_size = minimal_data_size
        self.leaves = [self.root]
        self.nodes = [self.root]
Пример #30
0
 def cpb4(self, htNode):
     routeList = []
     if htNode.children:
         for nodeTmp in htNode.children:
             treeNode = nodeTmp
             childNode = node()
             currentNode = node()
             flag = 0
             while treeNode.parent.count != -1:
                 if flag == 0:
                     currentNode = node(treeNode.parent.item, treeNode.count)
                     treeNode = treeNode.parent
                     flag = 1
                 else:
                     childNode = currentNode
                     currentNode = node(treeNode.parent.item, currentNode.count)
                     childNode.parent = currentNode
                     currentNode.children.append(childNode)
                     #根据路径向上搜索
                     treeNode = treeNode.parent
             routeList.append(currentNode)
     return routeList
Пример #31
0
    def buildFPtree(self, items, transactions):
        #建立头表
        for item in items:
            htnodeTmp = node(item[0], item[1][0])
            self.headTable.append(htnodeTmp)

        #建树
        for tran in transactions:
            count = 0
            tranRoot = node()
            parentNode = node()
            for item in tran:
                if count == 0:
                    nodeTmp = node(item, 1)
                    tranRoot = nodeTmp
                    count = 1
                else:
                    nodeTmp = node(item, 1)
                    nodeTmp.parent = parentNode
                    parentNode.children.append(nodeTmp)
                parentNode = nodeTmp
            self.insertTran(tranRoot, self.root)
Пример #32
0
def buildJson(Str):
    glist = json.loads(Str)
    gkeys = list(glist.keys())
    nodes = {}
    for i in range(len(glist)):
        nodes[gkeys[i]] = node(gkeys[i])
    for i in gkeys:
        nodes[i].val = glist[i]["val"]
        temp = glist[i]["children"].values()
        nodes[i].children = []
        for j in temp:
            nodes[i].children.append(nodes[j])
    return nodes["root"]
Пример #33
0
    def helpInsert(self, x, root):

        # if tree is empty, make node x the root of the tree.
        if root is None:
            root = node(x.getKey(), None, None)
            return x

        # if node's key is smaller than the root's key.
        if x.getKey() < root.getKey():

            # Recurse until node x is the root of
            # the left subtree.
            if self.helpInsert(x, root.getLeft()) == x:

                # if node x has a smaller rank than the
                # root of the subtree, set node x to
                # be it's left child.
                if x.getRank() < root.getRank():
                    root.setLeft(x)

                # if node x has a larger rank than the
                # root of the subtree, set node x as the new root
                # of the subtree, and make the old root x's right
                # child
                else:
                    root.setLeft(x.getRight())
                    x.setRight(root)
                    return x

        # if node's key is larger than or equal to the root's key.
        else:

            # Recurse until node x is the root of
            # the right subtree.
            if self.helpInsert(x, root.getRight()) == x:

                # if node x has a smaller or equal to the
                # rank of the root of the subtree, set node x to
                # be it's right child.
                if x.getRank() <= root.getRank():
                    root.setRight(x)

                # if node x has a larger rank than the
                # root of the subtree, set node x as the new root
                # of the subtree, and make the old root x's left
                # child
                else:
                    root.setRight(x.getLeft())
                    x.setLeft(root)
                    return x
        return root
Пример #34
0
    def updateNode(self, nodes, element):
        myList = []
        for current in nodes:
            if current.getId() in self.nodeTracker:
                self.nodes += 1
                id = current.getId()
                current = node(self.nodes, current.x, current.y, current.z)
                self.nodeMap[self.nodes] = current
                element.updateNode(id, current)
            else:
                self.nodeTracker[current.getId()] = []

            myList.append(current.getId())
        return myList
Пример #35
0
    def test_3d_int(self):
        
        (x0, mu0, sigma0, x1, mu1, sigma1) = self.vars
        (inv_mass, mass0, mass1) = self.nodes

        (x2, mu2, sigma2) = make_variables("x2[2.2,-6,6], mu2[2,-8,8], sigma2[2,0,3]")
        mass2 = node("mass2", gauss, {'x':x2, 'mu':mu2, 'sigma':sigma2})

        prod = mass0*mass1*mass2

        #integral = inv_mass.integral(x0, x1)
        integral = prod.integral(x0, x1, x2)
        self.assertAlmostEqual(integral, 1.0, 1)
        print "Integral over x0, x1, and x2: ", integral
Пример #36
0
def runaStar(length, numColors, gridSpace, startingPoints, endingPoints):

    color = 0
    startingNodes = []
    solutions = []
    stackTrace = []
    while(color < numColors):
        xcoord = startingPoints[color][0]
        ycoord = startingPoints[color][1]
        ID = str(ycoord) + " " + str(xcoord)
        newNode = node(None, None, xcoord, ycoord, gridSpace[xcoord][ycoord], 0, ID)
        startingNodes.append(newNode)
        color += 1
    color = 0
    while(color < numColors):
        xcoord = endingPoints[color][0]
        ycoord = endingPoints[color][1]
        ID = str(ycoord) + " " + str(xcoord)
        newNode = node(None, None, xcoord, ycoord, gridSpace[xcoord][ycoord], 0, ID)
        startingNodes.append(newNode)
        color += 1

    return aStar(startingNodes, gridSpace, length, numColors)
Пример #37
0
  def toGraph(self,dictionary):
    root = node("root",[])

    for key in dictionary:
     if key == 'name':
         root.name= dictionary[key]
         #print "root name is:" ,root.name

     elif key=='val':
         #root.val= dictionary[key]
         print "root value is:", root.val

     elif key=='children':
         #print "Children are:", root.children
         leaf1 = node("leaf1")
         leaf2 = node("leaf2")
         for x in dictionary[key]:
            root.children = leafx=node("x")
            for y in x:
              if x["name"]=='leaf1':
                    leaf1.name = x["name"]
                    leaf1.val = x["val"]
                    leaf1.children=x["children"]
                   # print "leaf 1 name is:" , leaf1.name

              elif x["name"]=='leaf2':
                    leaf2.name = x["name"]
                    leaf2.val = x["val"]
                    leaf2.children=x["children"]
                    #print "leaf 2 name is:" , leaf2.name

         root = node("root",[leaf1,leaf1,leaf2])
         nodeName = []
         increment(root,nodeName)
         dictionary = {"name": None, "val": None, "children": []}
         dictionary = toDictionary(root,dictionary)
         return dictionary
Пример #38
0
 def get_nodes(self):
     startNode = self.repository.repo[self.get_node()]
     queue = []
     queue.append(startNode)
     nodes = {}
     c = 0
     while len(queue) != 0:
         currentNode = queue.pop()
         if not currentNode.hex() in nodes:
             nodes[currentNode.hex()] = currentNode
             if currentNode.__nonzero__():
                 if currentNode.branch() == self.name:
                     for parentNode in currentNode.parents():
                         queue.append(parentNode)
     return [node(nodes[b]) for b in nodes]
Пример #39
0
def createNodes(dict):
    nodeList=[]
    #dfList(pd.DataFrame(dict))
    for ip in dict.keys():
        pkts=pd.DataFrame(dict[ip]['pkts'])

        hop=64-(int(pkts[0:1]["ttl"]))
        pkts = pkts.drop(['ttl'], axis=1)
        pkts=pkts.rename(columns={"pkt":"seq"})
        #print(type(pkts[0:1]["ttl"]))
        #print(pkts[0:1]["ttl"])
        n=node(ip,hop,pkts)

        nodeList.append(n)

    return nodeList
Пример #40
0
    def _instantiate_nodes(self):
        #generate NUMBER_OF_NODES nodes
        for i in range(NUMBER_OF_NODES):
            new_node = node(master_addr=self, [],
                            0.5,
                            self.genesis_block,
                            i + 8000)
            self.nodes.append(new_node)

        #Assign each node some neighbors
        for i, node in enumerate(self.nodes):
            for x in range(random.randint(3, 6)):
                randint = random.randint(0, NUMBER_OF_NODES)
                while randint == i:
                    randint = random.randint(0, NUMBER_OF_NODES)
                node.neighbors.append(self.nodes[randint])
Пример #41
0
def buildFromJson(jStr):
    myDict = json.loads(jStr)
    myKeys = list(myDict.keys())
    # Create Nodes
    nodes = {}
    for i in range(len(myDict)):
        nodes[myKeys[i]] = node(myKeys[i])

    # Set children
    for i in myKeys:
        nodes[i].val = myDict[i]["val"]

        auxKeys = myDict[i]["children"].values()
        nodes[i].children = []
        for auxChild in auxKeys:
            nodes[i].children.append(nodes[auxChild])
    return nodes["root"]
Пример #42
0
def make_tree(input_list, out_dir):
    save_dir = out_dir + "/"
    origin_as = {}
    path_as = {}
    tree = node()

    count = 0
    save_nr = 1
    i = 0

    for filename in input_list:
	print i + 1, filename
	i += 1
	read_file(filename, tree, origin_as, path_as)
	count += 1
	if count == SAVE_INTERVAL:
	    #### SAVING FILES ####
	    filename_tree = "pickled_prefix_" + str(save_nr)
	    filename_path = "pickled_path_" + str(save_nr)
	    filename_origin = "pickled_origin_" + str(save_nr)

            print "saving prefix tree as", save_dir + filename_tree
	    pickle.dump(tree, open(save_dir + filename_tree, "wb"))

	    print "saving AS origins as", save_dir + filename_origin
	    pickle.dump(origin_as, open(save_dir + filename_origin, "wb"))

	    print "saving AS paths as", save_ddir + filename_path
	    pickle.dump(path_as, open(save_dir + filename_path, "wb"))

            count = 0
	    save_nr += 1

    #### SAVING FILES ####
    filename_tree = "PrefixTree.pickle"
    filename_path = "ASpaths.pickle"
    filename_origin = "ASorigins.pickle"

    print "saving prefix tree as", save_dir + filename_tree
    pickle.dump(tree, open(save_dir + filename_tree, "wb"))

    print "saving AS origins as", save_dir + filename_origin
    pickle.dump(origin_as, open(save_dir + filename_origin, "wb"))

    print "saving AS paths as", save_dir + filename_path
    pickle.dump(path_as, open(save_dir + filename_path, "wb"))
Пример #43
0
    def parse(self, lev_file):
        # Read .lev file into an array of lines
        f = open(lev_file)
        lines = f.readlines()
        f.close()

        # find nodes count
        nodecount = int(lines[0]) - 1

        # Make a list of nodes
        nodes = [node() for i in range(nodecount)]

        # Parse nodes
        if len(lines) < nodecount + 2:
            print "Invalid file format. Expecting ", nodecount + 2, " lines."
        else:
            for i in range(nodecount):
                nodes[i] = self.parseLineToNode(lines[i + 2])

        return nodes
Пример #44
0
    def test_vals(self):
        
        # Test a gaussian pdf
        (x, mu, sigma) = make_variables("x[.2,-5,5], mu[0,-5,5], sigma[1,0,3]")
        mass = node("mass", gauss, {'x':x, 'mu':mu, 'sigma':sigma})
        my_pdf = pdf(mass, data=['x'])
        
        # Evaluate the pdf    
        mu.val = 0    
        val = my_pdf()
        print "x=%s, mu=%s, sigma=%s, val=%s" % (x.val, mu.val, sigma.val, val)
        self.assertTrue( abs(val - 0.39104269) < .01 )
        
        mu.val = 2    
        val = my_pdf()
        print "x=%s, mu=%s, sigma=%s, val=%s" % (x.val, mu.val, sigma.val, val)
        self.assertTrue( abs(val - 0.07895016) < .01 )

        x.val = 1    
        val = my_pdf()
        print "x=%s, mu=%s, sigma=%s, val=%s" % (x.val, mu.val, sigma.val, val)
        self.assertTrue( abs(val - 0.24197072) < .01 )
Пример #45
0
def normalize(root, restrict_to = None):
    if restrict_to is not None and root.kind != restrict_to: pass
    # a <=> b :::: a ==> b ^ b ==> a
    elif root.kind == '<=>':
        root.kind = '^'
        root2 = root.clone()
        child1 = node('==>', *root.children)
        child2 = node('==>', *list(reversed(root2.children)))
        root.children = [child1, child2]
    # a ==> b :::: ~a v b
    elif root.kind == '==>':
        root.kind = 'v'
        root.children[0] = node('~', root.children[0])
    # push the nots inwards
    elif root.kind == '~':
        child = root.children[0]
        # ~~a :::: a
        if child.kind == '~':
            child.consume_child()
            root.consume_child()
        # ~(a ^ b) :::: (~a v ~b)
        elif child.kind == '^':
            root.kind = 'v'
            root.children = [node('~', c) for c in child.children]
        # ~(a v b) :::: (~a ^ ~b)
        elif child.kind == 'v':
            root.kind = '^'
            root.children = [node('~', c) for c in child.children]
        # ~Ax[f(x)] :::: Ex[~f(x)]
        elif child.kind == 'A':
            root.kind = 'E'
            root.name = child.name
            root.children = [node('~', c) for c in child.children]
        # ~Ex[f(x)] :::: Ax[~f(x)]
        elif child.kind == 'E':
            root.kind = 'A'
            root.name = child.name
            root.children = [node('~', c) for c in child.children]

    for c in root.children:
        normalize(c, restrict_to)
Пример #46
0
    def test_pdf(self):

        # Make a simple pdf for integration and normalization testing
        (x0, mu0, sigma0) = make_variables("x0[.2,-5,5], mu0[0,-5,5], sigma0[1,0,3]")
        mass0 = node("mass0", gauss, {'x':x0, 'mu':mu0, 'sigma':sigma0})
        my_pdf = pdf(mass0, data=['x0'])

        # Evaluate the pdf
        val = my_pdf()
        print "x=%s, mu=%s, sigma=%s, val=%s" % (x0.val, mu0.val, sigma0.val, val)
        
        val = my_pdf()
        print "x=%s, mu=%s, sigma=%s, val=%s" % (x0.val, mu0.val, sigma0.val, val)
        
        # 1-d Fitting
        my_pdf.logging.setLevel(logging.DEBUG)
        my_pdf.fitTo( data=1, params_to_fit=["mu0"])
        print "Fitted pdf to x=1: mu=%s x=%s" % (mu0.val, x0.val)
        
        # Get a 2-d function and make a pdf
        inv_mass = make_node()
        my_pdf = pdf(inv_mass, data=['x0', 'x1'])

        # By Variable
        x0 = inv_mass.var('x0')
        x1 = inv_mass.var('x1')
        my_pdf = pdf(inv_mass, data=[x0, x1])

        print "Params: ", [param.name for param in my_pdf._params]
        print "Data: ", [param.name for param in my_pdf._data]
        print "x0 in pdf: ", my_pdf.var('x0'), my_pdf.var('x0').val

        # Evaluate the pdf
        val = my_pdf()
        print "x=%s, mu=%s, sigma=%s, val=%s" % (x0.val, mu0.val, sigma0.val, val)

        my_pdf.fitTo( data={x0:1, x1:1.2}, params_to_fit=["mu0"])
Пример #47
0
    def parseLineToNode(self, line):
        params = line.split(" ")
        n = node()
        n.id = int(params[0])
        n.gatetype = int(params[1])
        n.level = int(params[2])
        n.fanins = int(params[3])

        # cc0, cc1 end index
        cc0_end_index = n.fanins + 4
        cc1_end_index = 2 * n.fanins + 4
        n_fanouts_index = cc1_end_index

        # Adding cc0, cc1 lists
        for i in range(4, cc0_end_index):
            n.cc0_input_order.append(int(params[i]))

        for i in range(cc0_end_index, cc1_end_index):
            n.cc1_input_order.append(int(params[i]))

        # fanouts value
        n.fanouts = int(params[n_fanouts_index])

        # co_output index
        co_end_index = n_fanouts_index + 1 + n.fanouts

        # Adding co_output list
        for i in range(n_fanouts_index + 1, co_end_index):
            n.co_output_order.append(int(params[i]))

        # Setting remaining params - co, po, cc0, cc1
        n.co = int(params[co_end_index])
        n.po = params[co_end_index + 1]
        n.cc0 = int(params[co_end_index + 2])
        n.cc1 = int(params[co_end_index + 3])

        return n
Пример #48
0
def main():
	#get 8 puzzle
	print("Enter an 8puzzle")
	r1 = input()
	r2 = input()
	r3 = input()
	#split input into seperate numbers
	r1 = r1.split()
	r2 = r2.split()
	r3 = r3.split()
	r1 = r1 + r2 + r3
	#make all into int
	r1 = list(map(int, r1))
	r1 = node(r1)
	print (""""Enter the type of search
		(0) Uniform cost
		(1) A* with misplaced tile heuristic
		(2) A* with manhattan distance
		""")
	s = input()
	if search(r1, int(s)):
		print("solution found")
	else:
		print("no solution")
Пример #49
0
def nodemaker(n):
    a=[]
    for x in range(n):
        y= node(x,x,x)
        a.append(y)   
    return a
Пример #50
0
            E('y',
                infix(
                    fn('Q', var('y')),
                    '^',
                    fn('R', var('y'), var('x'))))))))

tests.append(
E('x',
    infix(
        fn('P', var('x')),
        '^',
        A('x',
            infix(
                fn('Q', var('x')),
                '==>',
                node('~', fn('P', var('x'))))))))

tests.append(
A('x',
    infix(
        fn('P', var('x')),
        '<=>',
        infix(
            fn('Q', var('x')),
            '^',
            E('y',
                infix(
                    fn('Q', var('y')),
                    '^',
                    fn('R', var('y'), var('x'))))))))
Пример #51
0
	def __init__(self, root_data=None):
		self.root = node(root_data)
		self.end = self.root 
Пример #52
0
# -*- coding: utf-8 -*-
from ecdsa import SigningKey
from transaction import *
from node import *

"""
privatekey = SigningKey.generate()	
publickey = privatekey.get_verifying_key()
message = "Reshma" + "Thomas"
signature = privatekey.sign(message)
#print "Signature is", signature
assert publickey.verify(signature, message)
"""
# checked signing,verification problem
filename = "newtrans.txt"
newnode = node()
# newtrans = filetotrans(filename)
# transtofile(newtrans, "new.txt")
signtrans(newnode, filename)


T = filetotrans("signedtrans.txt")

# verifying the signature(Not sure if it will work!)
transstr = str(T.incount) + str(T.outcount)

for i in range(T.incount):

    inliststr = str(T.inlist[i].hash) + str(T.inlist[i].n) + str(T.inlist[i].pub)
    assert newnode.publickey.verify(T.inlist[i].sign, inliststr)
Пример #53
0
from node import *
from endNode import *
from protocolDecisionTreeConstruct import *

t = node("t")
b = node("b")
h = node("h")
hi = node("hi")
hs= node("hs")
ha=node("ha")
end = endNode()

test = protocolDecisionTreeConstruct()
test.addNewNode(t,"start")
test.addDecisionNode(ha,hs,"t")
node = test.createNode("his")
test.setFieldInfo(False,False,True,False,False,"t","hi")
test1 = test.getFieldInfo(False,False,True,False,False,"t")
test = test.createList()
print test
print node.name
Пример #54
0
	def add(self, data):
		self.end.set_next(node(data))
		self.end = self.end.get_next()
Пример #55
0
 def __init__(self):
     self.root = node('root', -1)
     self.headTable = []
Пример #56
0
import random
from node import *
from m1m3 import *
from SearchSubC import *
from SearchSubS import *
import mr,rp1,rp2,rp3
import cplex
N=50
C=3
S=2
U=S
H=C
for i in xrange(I_size):
	X=node()
	X.index=i
	X.E=random.uniform(0.3,0.5)
	#X.E=cplex.infinity

	X.R=10
	X.x=random.randrange(0,N)
	X.y=random.randrange(0,N)
	I.append(X)
	I[i].k_index=-1
	I[i].j_index=-1
	

for j in xrange(J_size):
	J.append(I[j*3])
	I[j*3].j_index=j
	J[j].j_index=j
 def createNode(self,name):
     newNode = node(name)
     return newNode
Пример #58
0
	def load_graph(self, file):
		
		"""The format of the file must be:
			
			nodeX -- nodeY -- rtt -- bwXY -- bwYX
			nodeX -- nodeZ -- rtt -- bwXZ -- bwZX
			...
		   The "-- weightXY -- weightYX" part is optional.
		   All the lines of the file which aren't in this form are
		   ignored. In other words, you can use Graphviz (.dot) file.
		"""
		
		f=open(file)
		for l in f:
			w=re.split(' *-- *', l.strip())
			if len(w) == 1:
				continue
			
			if len(w)==7: 
			         rtt=float(w[2])
				 bwXY=float(w[3])
				 bwYX=float(w[4])
				 dpx=float(w[5])
				 dpy=float(w[6])
				 
			
			elif len(w) == 2:
				rtt=G.DEFAULT_RTT
				bwXY=G.DEFAULT_BW
				bwYX=G.DEFAULT_BW
				dpx=G.DEFAULT_DP
				dpy=G.DEFAULT_DP
				
				
			else:
				print "ERROR: The format of the file must be: \n nodeX -- nodeY -- rtt -- bwXY -- bwYX -- dpx -- dpy \n nodeX -- nodeZ -- rtt -- bwXZ -- bwZX -- dpx -- dpz\n ..."
				sys.exit()
			
			first_n=int(w[0])
			second_n=int(w[1])
			
			#pdb.set_trace()
			rem_first=rem_t(rtt,bwXY,bwYX,0)	#create a rem per each direction of the link; the dp will assigned later per each node
			rem_second=rem_t(rtt,bwYX,bwXY,0)	#create a rem per each direction of the link; the dp will assigned later per each node

			if G.whole_network.has_key(first_n):	#the nodeX already exist
				G.whole_network[first_n].new_link(second_n,rem_first) #add to nodeX the link for the nodeY
			else:
				nodex = node(first_n, second_n,rem_first) #create an istance of the nodeX 
				
				if G.DP_ENHANCEMENT:	#assign a death probability
					if nodex.dp==0:
						nodex.dp=dpx
				
				G.whole_network[first_n]=nodex
				nodex.hook()
				

		
			if G.whole_network.has_key(second_n):	#the nodeY already exist
				G.whole_network[second_n].new_link(first_n,rem_second) #add to nodeY the link for the nodeX
			else:
				nodey = node(second_n, first_n,rem_second) #create an istance of the nodex 
				
				if G.DP_ENHANCEMENT:	#assign a death probability
					if nodey.dp==0:
						nodey.dp=dpy
				
				G.whole_network[second_n]=nodey
				nodey.hook()
Пример #59
0
def merge_dir(data_dir, out_dir, base_dir=None):
   global TRAVERSED

   ###############################
   print "\n## MERGING TREES ##"
   print "###################\n"
   ###############################

   # create empty tree
   root = node()

   TRAVERSED = 0
   paths = set()

   # Add base pickles directory to build upon
   if base_dir is not None:
       for subdir, dirs, files in os.walk(base_dir):
	  for f in files:
	     paths.add(subdir)


   # Add tmp files for merge
   for subdir, dirs, files in os.walk(data_dir):
      for f in files:
         paths.add(subdir)

   count = 0
   for path in sorted(paths):
      try:
	 TRAVERSED = 0
	 count += 1
	 file_path = os.path.join(path, TREE_NAME)
         update_tree = pickle.load(open(file_path, 'rb'))
	 size = update_tree.size()
	 print "UPDATE tree " + str(count) + "/" + str(len(paths)) + " '" + os.path.join(path, TREE_NAME) + "' loaded (size=" + str(size) + ")."
	 add_tree(root, update_tree, size)
	 print "...new root size=" + str(root.size()) + ".\n"
	 #os.remove(file_path)
      except:
	 print "Could not read file:",file_path

   # save final tree
   os.mkdir(out_dir)
   root_file = os.path.join(out_dir, 'PrefixTree.pickle')
   print "Saving root tree to '" + root_file + "'."
   pickle.dump(root, open(root_file, "wb"))
   print "...ok!"
   root = None
   update_tree = None



   ######################################
   print "\n\n## MERGING AS ORIGINS ##"
   print "########################\n"
   ######################################

   # create empty dict
   asorigins = dict()

   count = 0
   for path in sorted(paths):
      try:
	 count += 1
	 file_path = os.path.join(path, AS_ORIGIN_NAME)
         update_dict = pickle.load(open(file_path, 'rb'))
	 print "adding UPDATE dict " + str(count) + "/" + str(len(paths)) + " from " + os.path.basename(os.path.dirname(path)) + " (size=" + str(len(update_dict)) + ")."
	 asorigins = add_dict(asorigins, update_dict)
	 print "...new dict size=" + str(len(asorigins)) + ".\n"
	 #os.remove(file_path)
      except:
	 print "Could not read file:",file_path

   # save final dict
   dict_file = os.path.join(out_dir, 'ASorigins.pickle')
   print "Saving AS origins dict to '" + dict_file + "'."
   pickle.dump(asorigins, open(dict_file, "wb"))
   print "...ok!"



   ####################################
   print "\n\n## MERGING AS PATHS ##"
   print "######################\n"
   ####################################

   # create empty dict
   aspaths = dict()

   count = 0
   for path in sorted(paths):
      try:
	 count += 1
	 file_path = os.path.join(path, AS_PATH_NAME)
         update_dict = pickle.load(open(file_path, 'rb'))
	 print "adding UPDATE dict " + str(count) + "/" + str(len(paths)) + " from " + os.path.basename(os.path.dirname(path)) + " (size=" + str(len(update_dict)) + ")."
	 aspaths = add_dict(aspaths, update_dict)
	 print "...new dict size=" + str(len(aspaths)) + ".\n"
	 #os.remove(file_path)
      except:
	 print "Could not read file:",file_path

   # save final dict
   dict_file = os.path.join(out_dir, 'ASpaths.pickle')
   print "Saving AS paths dict to '" + dict_file + "'."
   pickle.dump(aspaths, open(dict_file, "wb"))
   print "...ok!\n"