def test_markov(self):
        g = Linking.Graph()
        g.add_vertex('rainy')
        g.add_vertex('sunny')
        g.add_edge('rainy', 'sunny', 0.5)
        g.add_edge('rainy', 'rainy', 0.5)
        g.add_edge('sunny', 'rainy', 0.1)
        g.add_edge('sunny', 'sunny', 0.9)
        g.get_vertex('rainy').probability = 1  #initial probability
        g.get_vertex('sunny').probability = 0  #initial probability
        model = g.vert_dict

        gg = Linking.Graph()
        gg.add_vertex(0)
        gg.add_vertex(1)
        gg.add_vertex(2)
        gg.add_vertex(3)
        gg.add_edge(0, 1)
        gg.add_edge(1, 2)
        gg.add_edge(2, 3)
        al = Algorithms.algorithms(gg.vert_dict)
        expected = {
            0: {
                'rainy': 1,
                'sunny': 0
            },
            1: {
                'rainy': 0.5,
                'sunny': 0.5
            },
            2: {
                'rainy': 0.3,
                'sunny': 0.7
            },
            3: {
                'rainy': 0.21999999999999997,
                'sunny': 0.78
            }
        }
        self.assertDictEqual(al.markov(model, 0), expected)

        expected = {
            1: {
                'rainy': 1,
                'sunny': 0
            },
            2: {
                'rainy': 0.5,
                'sunny': 0.5
            },
            3: {
                'rainy': 0.3,
                'sunny': 0.7
            }
        }
        self.assertDictEqual(al.markov(model, 1), expected)
    def test_refreshP_simulating(self):
        g = Linking.Graph()
        for i in range(5):
            g.add_vertex(i)
        #    0    1
        #       \ /  \
        #        2   3
        #       /
        #      4
        g.add_edge(0, 2)
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 4)
        al = Algorithms.algorithms(g.vert_dict)
        al.generateProbabilityTable()
        al.setProbabilityTable(0, 'T', 0.1)
        al.setProbabilityTable(1, 'T', 0.5)
        al.setProbabilityTable(2, 'TT', 1)
        al.setProbabilityTable(2, 'TF', 1)
        al.setProbabilityTable(2, 'FT', 0.5)
        al.setProbabilityTable(2, 'FF', 0.01)
        al.setProbabilityTable(3, 'T', 0.8)
        al.setProbabilityTable(3, 'F', 0.01)
        al.setProbabilityTable(4, 'T', 0.1)
        al.setProbabilityTable(4, 'F', 0.01)
        al.simulateData(10000)

        obs = {3: 'T', 4: 'T'}
        self.assertTrue(math.fabs(al.query(obs, 0) - 0.17) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 1) - 1) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 2) - 0.92) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 3) - 1) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 4) - 1) <= 0.05)
Beispiel #3
0
 def test_g_add_objEdge_vert_connection(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     g.add_objEdge('a0')
     g.add_objEdge_vert_connection('a0', 0, 0.5)
     self.assertEqual(g.vert_dict['a0'].neighbor[0], 0.5)
 def test_setKnownPT(self):
     g = Linking.Graph()
     for i in range(5):
         g.add_vertex(i)
     #    0    1
     #       \ /  \
     #        2   3
     #       /
     #      4
     g.add_edge(0, 2)
     g.add_edge(1, 2)
     g.add_edge(1, 3)
     g.add_edge(2, 4)
     al = Algorithms.algorithms(g.vert_dict)
     al.generateProbabilityTable()
     al.setKnownPT(0, 'T', 0.1)
     al.setKnownPT(1, 'T', 0.5)
     al.setKnownPT(2, 'TT', 1)
     al.setKnownPT(2, 'TF', 1)
     al.setKnownPT(2, 'FT', 0.5)
     al.setKnownPT(2, 'FF', 0.01)
     al.setKnownPT(3, 'T', 0.8)
     al.setKnownPT(3, 'F', 0.01)
     al.setKnownPT(4, 'T', 0.1)
     al.setKnownPT(4, 'F', 0.01)
     v = g.get_vertex(2).probabilityTable['TT'][0]  #node 2
     self.assertTrue(v == 1)  #expect v =1
     v = g.get_vertex(3).probabilityTable['T'][0]  #node 3
     self.assertTrue(v == 0.8)  #expect v = 0.8
     v = g.get_vertex(4).probabilityTable['T'][0]  #node 4
     self.assertTrue(v == 0.1)  #expect v = 0.1
     v = g.get_vertex(0).probabilityTable['T'][0]  #node 0
     self.assertTrue(v == 0.1)  #expect v = 0.1
     v = g.get_vertex(1).probabilityTable['T'][0]  #node 1
     self.assertTrue(v == 0.5)  #expect v = 0.5
 def test_generatePriorProbability(self):
     g = Linking.Graph()
     for i in range(5):
         g.add_vertex(i)
     #    0    1
     #       \ /  \
     #        2   3
     #       /
     #      4
     g.add_edge(0, 2)
     g.add_edge(1, 2)
     g.add_edge(1, 3)
     g.add_edge(2, 4)
     al = Algorithms.algorithms(g.vert_dict)
     al.generateProbabilityTable()
     al.setKnownPT(0, 'T', 0.1)
     al.setKnownPT(1, 'T', 0.5)
     al.setKnownPT(2, 'TT', 1)
     al.setKnownPT(2, 'TF', 1)
     al.setKnownPT(2, 'FT', 0.5)
     al.setKnownPT(2, 'FF', 0.01)
     al.setKnownPT(3, 'T', 0.8)
     al.setKnownPT(3, 'F', 0.01)
     al.setKnownPT(4, 'T', 0.1)
     al.setKnownPT(4, 'F', 0.01)
     al.generatePriorProbability()
     self.assertTrue(g.get_vertex(0).probability == 0.1)
     self.assertTrue(g.get_vertex(1).probability == 0.5)
     self.assertTrue(g.get_vertex(2).probability == 0.3295)
     self.assertTrue(g.get_vertex(3).probability == 0.405)
     self.assertTrue(g.get_vertex(4).probability == 0.039655)
Beispiel #6
0
 def test_g_add_vertex(self):
     g = Linking.Graph()
     g.add_vertex(0)  #add vertex
     g.add_vertex(1)
     self.assertTrue(str(
         g.vert_dict[0].get_id()) == str(0))  #see if the vertex exist
     self.assertTrue(str(g.vert_dict[1].get_id()) == str(1))
Beispiel #7
0
 def test_g_get_vertices(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     self.assertListEqual(g.get_vertices(), [0, 1])
     g.add_vertex(2)
     self.assertListEqual(g.get_vertices(), [0, 1, 2])
Beispiel #8
0
 def test_g_add_edge(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     g.add_edge(0, 1, 1)
     self.assertTrue(1 in g.vert_dict[0].adjacent.keys())
     self.assertFalse(2 in g.vert_dict[0].adjacent.keys())
Beispiel #9
0
 def test_g_add_objEdge2(
         self
 ):  #for adding connection from node(as parent) to action(as chile)
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_objEdge('a0')
     g.add_edge(0, 'a0')
     self.assertDictEqual(g.vert_dict[0].adjacent, {'a0': 1})
Beispiel #10
0
 def test_g_resetAllHeuristic(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     g.get_vertex(0).heuristic = 1
     g.get_vertex(1).heuristic = 2
     g.resetAllHeuristic()
     self.assertTrue(g.vert_dict[0].heuristic == 0)
     self.assertTrue(g.vert_dict[1].heuristic == 0)
Beispiel #11
0
 def test_g_resetAllUtility(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     g.get_vertex(0).utility = 2
     g.get_vertex(1).utility = 3
     g.resetAllUtility()
     self.assertEqual(g.get_vertex(0).utility, 0)
     self.assertEqual(g.get_vertex(1).utility, 0)
Beispiel #12
0
 def test_g_delete_objEdge_vert_connection(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     g.add_objEdge('a0')
     g.add_objEdge_vert_connection('a0', 0, 0.8)
     g.add_edge(0, 1, 3)
     g.delete_objEdge_vert_connection('a0', 0)
     self.assertDictEqual(g.vert_dict['a0'].neighbor, {})  #  empty
     self.assertDictEqual(g.vert_dict[0].adjacent, {1: 3})
Beispiel #13
0
 def test_g_check_edge_existed(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     g.add_vertex(2)
     g.add_edge(0, 1, 1)
     g.add_edge(0, 2, 1)
     self.assertTrue(g.check_edge_existed(0, 1))
     self.assertTrue(g.check_edge_existed(0, 2))
     g.delete_edge(0, 2)
     self.assertTrue(g.check_edge_existed(0, 1))
     self.assertFalse(g.check_edge_existed(0, 2))
Beispiel #14
0
 def test_g_delete_vertex(self):
     g = Linking.Graph()
     g.add_vertex(0)
     g.add_vertex(1)
     g.add_vertex(2)
     g.add_edge(0, 1, 1)  #0 connect to 1
     g.add_edge(0, 2, 1)  #0 connect to 2
     g.delete_vertex(2)  #delete 2
     self.assertTrue(
         1 in g.vert_dict[0].adjacent.keys())  #0 is connected to 1
     self.assertFalse(
         2 in g.vert_dict[0].adjacent.keys())  #0 is not connected to 2
     self.assertTrue((0 in g.vert_dict.keys()))  #vertex 0 exist
     self.assertTrue((1 in g.vert_dict.keys()))  #vertex 1 exist
     self.assertFalse((2 in g.vert_dict.keys()))  #vertex 2 not exist
 def test_ucsAStar_ucs_dif_Cost(self):
     g = Linking.Graph()
     for i in range(7):
         g.add_vertex(i)
     g.add_edge(0, 2, 2)
     g.add_edge(0, 1, 1)
     g.add_edge(1, 4, 4)
     g.add_edge(1, 3, 1)
     g.add_edge(2, 6, 1)
     g.add_edge(2, 5, 1)
     g.add_edge(2, 4, 2)
     al = Algorithms.algorithms(g.vert_dict)
     self.assertListEqual([0, 2, 4], al.ucsAStar(0, 4, 'UCS'))
     expect = [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 5],
               [0, 1, 2, 3, 5, 6], [0, 1, 2, 3, 5, 6, 4]]
     self.assertListEqual(expect, al.getVisitedLog())
    def randomGraph(self,branch,layer,lowerBound,upperBound):
        LK=Linking.Graph()
        LK.add_vertex(0)#root
        queue=[LK.get_vertex(0).id]
        nodeId=1
        layerDict={0:0}

        while queue:#queue is not empty
            parent=queue.pop(0)#pop from head
            if layer>layerDict[parent]:
                for i in range(branch):
                    LK.add_vertex(nodeId)
                    LK.add_edge(parent,nodeId)
                    layerDict[nodeId]=layerDict[parent]+1
                    queue.append(nodeId)
                    nodeId+=1

        for n in LK.get_vertices():
            if not LK.get_vertex(n).get_connections():
                LK.get_vertex(n).utility=random.randint(lowerBound,upperBound)
        return LK


# g=randomGenerator().randomGraph(2,2,-2,2)
# g.delete_edge(2,5)
# g.delete_edge(2,6)
# g.add_objEdge('a0')
# g.add_edge(2,'a0')
# g.add_objEdge_vert_connection('a0',5)
# g.add_objEdge_vert_connection('a0',6)
# g.get_vertex(5).probability=0.2
# g.get_vertex(6).probability=0.8
# a=Algorithms.algorithms(g.vert_dict).miniMaxAlphaBeta(0,2,'exMiniMax')
# for n in g:
#     if type(n) is Linking.Vertex:
#         if not n.get_connections():
#             print(str(n.id)+'\'s utility is '+str(n.utility))
# print()
# for e in a:
#     print(e)
 def test_simulateData(self):
     g = Linking.Graph()
     for i in range(5):
         g.add_vertex(i)
     #    0    1
     #       \ /  \
     #        2   3
     #       /
     #      4
     g.add_edge(0, 2)
     g.add_edge(1, 2)
     g.add_edge(1, 3)
     g.add_edge(2, 4)
     al = Algorithms.algorithms(g.vert_dict)
     al.generateProbabilityTable()
     al.setProbabilityTable(0, 'T', 0.1)
     al.setProbabilityTable(1, 'T', 0.5)
     al.setProbabilityTable(2, 'TT', 1)
     al.setProbabilityTable(2, 'TF', 1)
     al.setProbabilityTable(2, 'FT', 0.5)
     al.setProbabilityTable(2, 'FF', 0.01)
     al.setProbabilityTable(3, 'T', 0.8)
     al.setProbabilityTable(3, 'F', 0.01)
     al.setProbabilityTable(4, 'T', 0.1)
     al.setProbabilityTable(4, 'F', 0.01)
     al.simulateData(10000)
     v = g.get_vertex(0).probabilityTable['T'][0]  #node 0
     self.assertAlmostEqual(v, 0.1, 1)  #expect v =0.1
     v = g.get_vertex(1).probabilityTable['T'][0]  #node 1
     self.assertAlmostEqual(v, 0.5, 1)  #expect v = 0.5
     v = g.get_vertex(2).probabilityTable['TT'][0]  #node 2
     self.assertAlmostEqual(v, 1, 1)  #expect v =1
     v = g.get_vertex(3).probabilityTable['T'][0]  #node 3
     self.assertAlmostEqual(v, 0.8, 1)  #expect v = 0.8
     v = g.get_vertex(4).probabilityTable['T'][0]  #node 4
     self.assertAlmostEqual(0.1, v, 1)  #expect v = 0.1
Beispiel #18
0
class al_data:
    g=Linking.Graph()
    for i in range(9):
        g.add_vertex(i)
    g.add_edge(0,1)
    g.add_edge(1,0)
    g.add_edge(0,2)
    g.add_edge(2,0)
    g.add_edge(0,6)
    g.add_edge(6,0)
    g.add_edge(1,3)
    g.add_edge(3,1)
    g.add_edge(1,4)
    g.add_edge(4,1)
    g.add_edge(2,5)
    g.add_edge(5,2)
    g.add_edge(2,6)
    g.add_edge(6,2)
    g.add_edge(6,4)
    g.add_edge(4,6)
    g.add_edge(6,7)
    g.add_edge(7,6)
    g.add_edge(7,8)
    g.add_edge(8,7)
    g.add_edge(8,3)
    g.add_edge(3,8)

    h=Linking.Graph()
    for i in range(9):
        h.add_vertex(i)
    h.add_edge(0,1)
    h.add_edge(1,0)
    h.add_edge(0,2,2)
    h.add_edge(2,0,2)
    h.add_edge(0,6,2.2)
    h.add_edge(6,0,2.2)
    h.add_edge(1,3,2.2)
    h.add_edge(3,1,2.2)
    h.add_edge(1,4,1.4)
    h.add_edge(4,1,1.4)
    h.add_edge(2,5)
    h.add_edge(5,2)
    h.add_edge(2,6)
    h.add_edge(6,2)
    h.add_edge(6,4,1.4)
    h.add_edge(4,6,1.4)
    h.add_edge(6,7)
    h.add_edge(7,6)
    h.add_edge(7,8,2)
    h.add_edge(8,7,2)
    h.add_edge(8,3,2)
    h.add_edge(3,8,2)


    def run(self,goal):
        l=[['BFS',0], ['DFS',0], ['UCS',0], ['aStar',0], ['BFS',1], ['DFS',1], ['UCS',1], ['aStar',1]]
        for i in l:
            if i[1]==0:
                print('**'+str(i[0]))
            else:
                print('**iter '+str(i[0]))

            if i[0] in ('BFS','DFS'):
                if i[1]==0:
                    self.al.bdfs(0,goal,i[0])
                else:
                    self.al.iterative(0,goal,i[0],2)
            else:
                if i[1]==0:
                    self.al.ucsAStar(0,goal,i[0])
                else:
                    self.al.iterative(0,goal,i[0],2)
            count=0
            # print(self.al.getQsLog())
            for item in self.al.getQsLog():
                count+=len(item[1])
            avg=count/len(self.al.getQsLog())
            print('Steps: '+str(len(self.al.getQsLog())))
            print('Avg:                       '+str(avg))

    def CGH(self):
        print('CGH')
        #Costs are the same
        #Goal is found
        #With Heuristic
        G=self.g
        G.get_vertex(0).heuristic=3
        G.get_vertex(1).heuristic=2
        G.get_vertex(2).heuristic=3
        G.get_vertex(3).heuristic=1
        G.get_vertex(4).heuristic=0
        G.get_vertex(5).heuristic=4
        G.get_vertex(6).heuristic=2
        G.get_vertex(7).heuristic=3
        G.get_vertex(8).heuristic=3
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(4)

    def CGh(self):
        print('CGh')
        #Costs are the same
        #Goal is found
        #Without Heuristic
        G=self.g
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(4)

    def CgH(self):
        print('CgH')
        #Costs are the same
        #Goal is not found
        #With Heuristic
        G=self.g
        G.get_vertex(0).heuristic=3
        G.get_vertex(1).heuristic=2
        G.get_vertex(2).heuristic=3
        G.get_vertex(3).heuristic=1
        G.get_vertex(4).heuristic=0
        G.get_vertex(5).heuristic=4
        G.get_vertex(6).heuristic=2
        G.get_vertex(7).heuristic=3
        G.get_vertex(8).heuristic=3
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(9)

    def Cgh(self):
        print('Cgh')
        #Costs are the same
        #Goal is not found
        #Without Heuristic
        G=self.g
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(9)

    def cGH(self):
        print('cGH')
        #Costs are the same
        #Goal is found
        #With Heuristic
        G=self.h
        G.get_vertex(0).heuristic=3
        G.get_vertex(1).heuristic=2
        G.get_vertex(2).heuristic=3
        G.get_vertex(3).heuristic=1
        G.get_vertex(4).heuristic=0
        G.get_vertex(5).heuristic=4
        G.get_vertex(6).heuristic=2
        G.get_vertex(7).heuristic=3
        G.get_vertex(8).heuristic=3
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(4)

    def cGh(self):
        print('cGh')
        #Costs are the same
        #Goal is found
        #Without Heuristic
        G=self.h
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(4)

    def cgH(self):
        print('cgH')
        #Costs are the same
        #Goal is not found
        #With Heuristic
        G=self.h
        G.get_vertex(0).heuristic=3
        G.get_vertex(1).heuristic=2
        G.get_vertex(2).heuristic=3
        G.get_vertex(3).heuristic=1
        G.get_vertex(4).heuristic=0
        G.get_vertex(5).heuristic=4
        G.get_vertex(6).heuristic=2
        G.get_vertex(7).heuristic=3
        G.get_vertex(8).heuristic=3
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(9)

    def cgh(self):
        print('cgh')
        #Costs are the same
        #Goal is not found
        #Without Heuristic
        G=self.h
        self.al=Algorithms.algorithms(G.vert_dict)
        self.run(9)
Beispiel #19
0
 def test_g_get_vertex(self):
     g = Linking.Graph()
     v = Linking.Vertex(0)  #create a vertex
     g.vert_dict[0] = v  #store the vertex into vert_dict
     self.assertEqual(g.get_vertex(0), v)
class Test_algorithm(unittest.TestCase):
    g = Linking.Graph()
    gd = Linking.Grid(3, 3)  #3*3 grid

    # 3 layer absolutely complete perfect tree
    for i in range(7):
        g.add_vertex(i)

    g.add_edge(1, 4, 1)
    g.add_edge(1, 3, 1)
    g.add_edge(2, 6, 1)
    g.add_edge(2, 5, 1)
    g.add_edge(0, 2, 1)
    g.add_edge(0, 1, 1)

    AL = Algorithms.algorithms(g.vert_dict)
    AL_gd = Algorithms.algorithms(gd.grid_dict)

    #For Graph data structure
    def test_bdfs_bfs(self):
        #BFS
        self.assertEqual([0, 1, 4], self.AL.bdfs(0, 4,
                                                 'BFS'))  #Test BFS final path
        qsExpect = [[0, [1, 2]], [1, [2, 3, 4]], [2, [3, 4, 5, 6]],
                    [3, [4, 5, 6]], [4, [5, 6]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test BFS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test BFS visitedLog

    def test_bdfs_dfs(self):
        #DFS
        self.assertEqual([0, 1, 4], self.AL.bdfs(0, 4,
                                                 'DFS'))  #Test DFS final path
        qsExpect = [[0, [1, 2]], [2, [1, 5, 6]], [6, [1, 5]], [5, [1]],
                    [1, [3, 4]], [4, [3]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test DFS qsLog
        visitedExpect = [[0], [0, 2], [0, 2, 6], [0, 2, 6, 5], [0, 2, 6, 5, 1],
                         [0, 2, 6, 5, 1, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test DFS visitedLog

    def test_ucsAStar_ucs_dif_Cost(self):
        g = Linking.Graph()
        for i in range(7):
            g.add_vertex(i)
        g.add_edge(0, 2, 2)
        g.add_edge(0, 1, 1)
        g.add_edge(1, 4, 4)
        g.add_edge(1, 3, 1)
        g.add_edge(2, 6, 1)
        g.add_edge(2, 5, 1)
        g.add_edge(2, 4, 2)
        al = Algorithms.algorithms(g.vert_dict)
        self.assertListEqual([0, 2, 4], al.ucsAStar(0, 4, 'UCS'))
        expect = [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 5],
                  [0, 1, 2, 3, 5, 6], [0, 1, 2, 3, 5, 6, 4]]
        self.assertListEqual(expect, al.getVisitedLog())

    def test_ucsAStar_ucs(self):
        #UCS
        self.assertEqual([0, 1, 4],
                         self.AL.ucsAStar(0, 4, 'UCS'))  #Test UCS final path
        qsExpect = [[0, [1, 2], [1, 1]], [1, [2, 3, 4], [1, 2, 2]],
                    [2, [3, 4, 5, 6], [2, 2, 2, 2]], [3, [4, 5, 6], [2, 2, 2]],
                    [4, [5, 6], [2, 2]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test UCS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test UCS visitedLog

    def test_ucsAStar_aStar(self):
        #A* (manual heuristic)
        self.g.get_vertex(1).heuristic = 1
        self.g.get_vertex(3).heuristic = 1
        self.assertEqual([0, 1, 4],
                         self.AL.ucsAStar(0, 4, 'aStar'))  #Test A* final path
        qsExpect = [[0, [2, 1], [1, 2]], [2, [1, 5, 6], [2, 2, 2]],
                    [1, [4, 5, 6, 3], [2, 2, 2, 3]], [4, [5, 3, 6], [2, 3, 2]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test A* qsLog
        visitedExpect = [[0], [0, 2], [0, 2, 1], [0, 2, 1, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test A* visitedLog
        self.g.resetAllHeuristic()
        #A* (default heuristic=0)
        self.assertEqual([0, 1, 4],
                         self.AL.ucsAStar(0, 4, 'aStar'))  #Test A* final path
        qsExpect = [[0, [1, 2], [1, 1]], [1, [2, 3, 4], [1, 2, 2]],
                    [2, [3, 4, 5, 6], [2, 2, 2, 2]], [3, [4, 5, 6], [2, 2, 2]],
                    [4, [5, 6], [2, 2]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test A* qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test A* visitedLog

    def test_bdfs_bfs_iter(self):
        #BFS
        self.assertEqual([0, 1, 4], self.AL.iterative(0, 4, 'BFS',
                                                      3))  #Test BFS final path
        qsExpect = [[0, [1, 2]], [1, [2]], [2, []], [0, [1, 2]],
                    [1, [2, 3, 4]], [2, [3, 4, 5, 6]], [3, [4, 5, 6]],
                    [4, [5, 6]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test BFS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 2], [0], [0, 1], [0, 1, 2],
                         [0, 1, 2, 3], [0, 1, 2, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test BFS visitedLog

    def test_bdfs_dfs_iter(self):
        #DFS
        self.assertEqual([0, 2, 5], self.AL.iterative(0, 5, 'DFS',
                                                      3))  #Test DFS final path
        qsExpect = [[0, [1, 2]], [2, [1]], [1, []], [0, [1, 2]],
                    [2, [1, 5, 6]], [6, [1, 5]], [5, [1]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test DFS qsLog
        visitedExpect = [[0], [0, 2], [0, 2, 1], [0], [0, 2], [0, 2, 6],
                         [0, 2, 6, 5]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test DFS visitedLog

    def test_ucsAStar_ucs_iter(self):
        #UCS
        self.assertEqual([0, 1, 4], self.AL.iterative(0, 4, 'UCS',
                                                      3))  #Test UCS final path
        qsExpect = [[0, [1, 2], [1, 1]], [1, [2], [1]], [2, [], []],
                    [0, [1, 2], [1, 1]], [1, [2, 3, 4], [1, 2, 2]],
                    [2, [3, 4, 5, 6], [2, 2, 2, 2]], [3, [4, 5, 6], [2, 2, 2]],
                    [4, [5, 6], [2, 2]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test UCS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 2], [0], [0, 1], [0, 1, 2],
                         [0, 1, 2, 3], [0, 1, 2, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test UCS visitedLog

    def test_ucsAStar_aStar_iter(self):
        #A* (manual heuristic)
        self.g.get_vertex(1).heuristic = 1
        self.g.get_vertex(3).heuristic = 1
        self.assertEqual([0, 1, 4], self.AL.iterative(0, 4, 'aStar',
                                                      3))  #Test A* final path
        qsExpect = [[0, [2, 1], [1, 2]], [2, [1], [2]], [1, [], []],
                    [0, [2, 1], [1, 2]], [2, [1, 5, 6], [2, 2, 2]],
                    [1, [4, 5, 6, 3], [2, 2, 2, 3]], [4, [5, 3, 6], [2, 3, 2]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test A* qsLog
        visitedExpect = [[0], [0, 2], [0, 2, 1], [0], [0, 2], [0, 2, 1],
                         [0, 2, 1, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test A* visitedLog
        self.g.resetAllHeuristic()
        #A* (default heuristic=0)
        self.assertEqual([0, 1, 4], self.AL.iterative(0, 4, 'aStar',
                                                      3))  #Test A* final path
        qsExpect = [[0, [1, 2], [1, 1]], [1, [2], [1]], [2, [], []],
                    [0, [1, 2], [1, 1]], [1, [2, 3, 4], [1, 2, 2]],
                    [2, [3, 4, 5, 6], [2, 2, 2, 2]], [3, [4, 5, 6], [2, 2, 2]],
                    [4, [5, 6], [2, 2]]]
        self.assertEqual(qsExpect, self.AL.getQsLog())  #Test A* qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 2], [0], [0, 1], [0, 1, 2],
                         [0, 1, 2, 3], [0, 1, 2, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL.getVisitedLog())  #Test A* visitedLog

#For Grid data structure

    def test_bdfs_bfs_grid(self):
        #BFS
        self.assertEqual([0, 1, 4],
                         self.AL_gd.bdfs(0, 4, 'BFS'))  #Test BFS final path
        qsExpect = [[0, [1, 3]], [1, [3, 2, 4]], [3, [2, 4, 6]],
                    [2, [4, 6, 5]], [4, [6, 5]]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test BFS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 3], [0, 1, 3, 2], [0, 1, 3, 2, 4]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test BFS visitedLog

    def test_bdfs_dfs_grid(self):
        #DFS
        self.assertEqual([0, 3, 4],
                         self.AL_gd.bdfs(0, 4, 'DFS'))  #Test DFS final path
        qsExpect = [[0, [1, 3]], [3, [1, 4, 6]], [6, [1, 4,
                                                      7]], [7, [1, 4, 8]],
                    [8, [1, 4, 5]], [5, [1, 4, 2]], [2, [1, 4]], [4, [1]]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test DFS qsLog
        visitedExpect = [[0], [0, 3], [0, 3, 6], [0, 3, 6, 7], [0, 3, 6, 7, 8],
                         [0, 3, 6, 7, 8, 5], [0, 3, 6, 7, 8, 5, 2],
                         [0, 3, 6, 7, 8, 5, 2, 4]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test DFS visitedLog

    def test_ucsAStar_ucs_grid(self):
        #UCS
        self.assertEqual([0, 1, 4],
                         self.AL_gd.ucsAStar(0, 4,
                                             'UCS'))  #Test UCS final path
        qsExpect = [[0, [1, 3], [1, 1]], [1, [3, 2, 4], [1, 2, 2]],
                    [3, [2, 4, 6], [2, 2, 2]], [2, [4, 6, 5], [2, 2, 3]],
                    [4, [6, 5], [2, 3]]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test UCS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 3], [0, 1, 3, 2], [0, 1, 3, 2, 4]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test UCS visitedLog

    def test_ucsAStar_aStar_grid(self):
        #A* (getManhattanDist)
        self.gd.setManhattanDist(4)
        self.assertEqual([0, 1, 4],
                         self.AL_gd.ucsAStar(0, 4,
                                             'aStar'))  #Test A* final path
        qsExpect = [[0, [1, 3], [2, 2]], [1, [3, 4, 2], [2, 2, 4]],
                    [3, [4, 2, 6], [2, 4, 4]], [4, [2, 6], [4, 4]]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test A* qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 3], [0, 1, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test A* visitedLog

    def test_bdfs_bfs_iter_grid(self):
        #BFS
        self.assertEqual([0, 3, 6],
                         self.AL_gd.iterative(0, 6, 'BFS',
                                              it=3))  #Test BFS final path
        qsExpect = [[0, [1, 3]], [1, [3]], [3, []], [0, [1, 3]],
                    [1, [3, 2, 4]], [3, [2, 4, 6]], [2, [4, 6]], [4, [6]],
                    [6, []]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test BFS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 3], [0], [0, 1], [0, 1, 3],
                         [0, 1, 3, 2], [0, 1, 3, 2, 4], [0, 1, 3, 2, 4, 6]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test BFS visitedLog

    def test_bdfs_dfs_iter_grid(self):
        #DFS
        self.assertEqual([0, 3, 6],
                         self.AL_gd.iterative(0, 6, 'DFS',
                                              it=3))  #Test DFS final path
        qsExpect = [[0, [1, 3]], [3, [1]], [1, []], [0, [1, 3]],
                    [3, [1, 4, 6]], [6, [1, 4]]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test DFS qsLog
        visitedExpect = [[0], [0, 3], [0, 3, 1], [0], [0, 3], [0, 3, 6]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test DFS visitedLog

    def test_ucsAStar_ucs_iter_grid(self):
        #UCS
        self.assertEqual([0, 3, 6],
                         self.AL_gd.iterative(0, 6, 'UCS',
                                              it=3))  #Test UCS final path
        qsExpect = [[0, [1, 3], [1, 1]], [1, [3], [1]], [3, [], []],
                    [0, [1, 3], [1, 1]], [1, [3, 2, 4], [1, 2, 2]],
                    [3, [2, 4, 6], [2, 2, 2]], [2, [4, 6], [2, 2]],
                    [4, [6], [2]], [6, [], []]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test UCS qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 3], [0], [0, 1], [0, 1, 3],
                         [0, 1, 3, 2], [0, 1, 3, 2, 4], [0, 1, 3, 2, 4, 6]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test UCS visitedLog

    def test_ucsAStar_aStar_iter_grid(self):
        #A* (getManhattanDist)
        self.gd.setManhattanDist(4)
        self.assertEqual([0, 1, 4],
                         self.AL_gd.iterative(0, 4, 'aStar',
                                              it=3))  #Test A* final path
        qsExpect = [[0, [1, 3], [2, 2]], [1, [3], [2]], [3, [], []],
                    [0, [1, 3], [2, 2]], [1, [3, 4, 2], [2, 2, 4]],
                    [3, [4, 2, 6], [2, 4, 4]], [4, [2, 6], [4, 4]]]
        self.assertEqual(qsExpect, self.AL_gd.getQsLog())  #Test A* qsLog
        visitedExpect = [[0], [0, 1], [0, 1, 3], [0], [0, 1], [0, 1, 3],
                         [0, 1, 3, 4]]
        self.assertEqual(visitedExpect,
                         self.AL_gd.getVisitedLog())  #Test A* visitedLog

    def test_miniMaxAlphaBeta_miniMax(self):
        self.g.resetAllUtility()
        self.g.get_vertex(3).utility = 20
        self.g.get_vertex(4).utility = 10
        self.g.get_vertex(5).utility = 5
        self.g.get_vertex(6).utility = 15
        expect = [[0, -math.inf, math.inf], [1, -math.inf, math.inf],
                  [3, 20, 20], [1, -math.inf, 20], [4, 10, 10], [1, 10, 10],
                  [0, 10, math.inf], [2, -math.inf, math.inf], [5, 5, 5],
                  [2, -math.inf, 5], [6, 15, 15], [2, 5, 5], [0, 10, 10]]
        self.assertListEqual(self.AL.miniMaxAlphaBeta(0, 3, 'miniMax'), expect)
        self.assertListEqual(self.AL.finalPath, [0, 1, 4])

    def test_miniMaxAlphaBeta_alphaBeta(self):
        self.g.resetAllUtility()
        self.g.get_vertex(3).utility = 20
        self.g.get_vertex(4).utility = 1
        self.g.get_vertex(5).utility = 5
        self.g.get_vertex(6).utility = 30
        #without prune
        expect = [[0, -math.inf, math.inf], [1, -math.inf, math.inf],
                  [3, 20, 20], [1, -math.inf, 20], [4, 1, 1], [1, 1, 1],
                  [0, 1, math.inf], [2, -math.inf, math.inf], [5, 5, 5],
                  [2, -math.inf, 5], [6, 30, 30], [2, 5, 5], [0, 5, 5]]

        self.assertListEqual(self.AL.miniMaxAlphaBeta(0, 3, 'alphaBeta'),
                             expect)
        self.assertListEqual(self.AL.finalPath, [0, 2, 5])
        # #with prune
        self.g.resetAllUtility()
        self.g.get_vertex(3).utility = 20
        self.g.get_vertex(4).utility = 10
        self.g.get_vertex(5).utility = 5
        self.g.get_vertex(6).utility = 15
        expect = [[0, -math.inf, math.inf], [1, -math.inf, math.inf],
                  [3, 20, 20], [1, -math.inf, 20], [4, 10, 10], [1, 10, 10],
                  [0, 10, math.inf], [2, -math.inf, math.inf], [5, 5, 5],
                  [2, -math.inf, 5], [0, 10, 10]]
        self.assertListEqual(self.AL.miniMaxAlphaBeta(0, 3, 'alphaBeta'),
                             expect)  #node 6 is pruned
        self.assertListEqual(self.AL.finalPath, [0, 1, 4])

    def test_miniMaxAlphaBeta_exMiniMax(self):
        self.g.resetAllUtility()
        self.g.get_vertex(3).utility = 20
        self.g.get_vertex(4).utility = 1
        self.g.get_vertex(5).utility = 10
        self.g.get_vertex(6).utility = 4
        self.g.add_objEdge('a0')
        self.g.add_edge(2, 'a0')
        self.g.delete_edge(2, 5)
        self.g.delete_edge(2, 6)
        self.g.add_objEdge_vert_connection(
            'a0', 5, 0.3)  #utility of node 5 =10, 10*0.3=3
        self.g.add_objEdge_vert_connection('a0', 6,
                                           0.5)  #utility of node 6 =4, 4*0.5=2
        #so now the total utility of action0 = 5
        expect = [[0, -math.inf, math.inf], [1, -math.inf, math.inf],
                  [3, 20, 20], [1, -math.inf, 20], [4, 1, 1], [1, 1, 1],
                  [0, 1, math.inf], [2, -math.inf, math.inf], [5, 10, 10],
                  [6, 4, 4], [2, 5.0, 5.0], [0, 5.0, 5.0]]
        self.assertListEqual(self.AL.miniMaxAlphaBeta(0, 3, 'exMiniMax'),
                             expect)
        self.assertListEqual(self.AL.finalPath, [0, 2])
        self.g.add_edge(2, 5)
        self.g.add_edge(2, 6)
        self.g.delete_objEdge('a0')

    def test_generateProbabilityTable(self):
        self.g.add_edge(1, 2)
        self.g.add_edge(1, 5)
        self.g.add_edge(4, 5)
        self.AL.generateProbabilityTable()
        expect = {
            2: [0, 1],
            'TT': [0, 0, 0, 0],
            'TF': [0, 0, 0, 0],
            'FT': [0, 0, 0, 0],
            'FF': [0, 0, 0, 0]
        }
        self.assertDictEqual(self.g.get_vertex(2).probabilityTable, expect)
        expect = {
            5: [1, 2, 4],
            'TTT': [0, 0, 0, 0],
            'TTF': [0, 0, 0, 0],
            'TFT': [0, 0, 0, 0],
            'TFF': [0, 0, 0, 0],
            'FTT': [0, 0, 0, 0],
            'FTF': [0, 0, 0, 0],
            'FFT': [0, 0, 0, 0],
            'FFF': [0, 0, 0, 0]
        }
        self.assertDictEqual(self.g.get_vertex(5).probabilityTable, expect)
        self.g.delete_edge(1, 2)
        self.g.delete_edge(1, 5)
        self.g.delete_edge(4, 5)

    def test_simulateData(self):
        g = Linking.Graph()
        for i in range(5):
            g.add_vertex(i)
        #    0    1
        #       \ /  \
        #        2   3
        #       /
        #      4
        g.add_edge(0, 2)
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 4)
        al = Algorithms.algorithms(g.vert_dict)
        al.generateProbabilityTable()
        al.setProbabilityTable(0, 'T', 0.1)
        al.setProbabilityTable(1, 'T', 0.5)
        al.setProbabilityTable(2, 'TT', 1)
        al.setProbabilityTable(2, 'TF', 1)
        al.setProbabilityTable(2, 'FT', 0.5)
        al.setProbabilityTable(2, 'FF', 0.01)
        al.setProbabilityTable(3, 'T', 0.8)
        al.setProbabilityTable(3, 'F', 0.01)
        al.setProbabilityTable(4, 'T', 0.1)
        al.setProbabilityTable(4, 'F', 0.01)
        al.simulateData(10000)
        v = g.get_vertex(0).probabilityTable['T'][0]  #node 0
        self.assertAlmostEqual(v, 0.1, 1)  #expect v =0.1
        v = g.get_vertex(1).probabilityTable['T'][0]  #node 1
        self.assertAlmostEqual(v, 0.5, 1)  #expect v = 0.5
        v = g.get_vertex(2).probabilityTable['TT'][0]  #node 2
        self.assertAlmostEqual(v, 1, 1)  #expect v =1
        v = g.get_vertex(3).probabilityTable['T'][0]  #node 3
        self.assertAlmostEqual(v, 0.8, 1)  #expect v = 0.8
        v = g.get_vertex(4).probabilityTable['T'][0]  #node 4
        self.assertAlmostEqual(0.1, v, 1)  #expect v = 0.1

    def test_setProbabilityTable(self):
        g = Linking.Graph()
        for i in range(5):
            g.add_vertex(i)
        #    0    1
        #       \ /  \
        #        2   3
        #       /
        #      4
        g.add_edge(0, 2)
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 4)
        al = Algorithms.algorithms(g.vert_dict)
        al.generateProbabilityTable()
        al.setProbabilityTable(0, 'T', 0.1)
        al.setProbabilityTable(1, 'T', 0.5)
        al.setProbabilityTable(2, 'TT', 1)
        al.setProbabilityTable(2, 'TF', 1)
        al.setProbabilityTable(2, 'FT', 0.5)
        al.setProbabilityTable(2, 'FF', 0.01)
        al.setProbabilityTable(3, 'T', 0.8)
        al.setProbabilityTable(3, 'F', 0.01)
        al.setProbabilityTable(4, 'T', 0.1)
        al.setProbabilityTable(4, 'F', 0.01)
        v = g.get_vertex(2).probabilityTable['TT'][3]  #node 2
        self.assertTrue(v == 1)  #expect v =1
        v = g.get_vertex(3).probabilityTable['T'][3]  #node 3
        self.assertTrue(v == 0.8)  #expect v = 0.8
        v = g.get_vertex(4).probabilityTable['T'][3]  #node 4
        self.assertTrue(v == 0.1)  #expect v = 0.1
        v = g.get_vertex(0).probabilityTable['T'][3]  #node 0
        self.assertTrue(v == 0.1)  #expect v = 0.1
        v = g.get_vertex(1).probabilityTable['T'][3]  #node 1
        self.assertTrue(v == 0.5)  #expect v = 0.5

    def test_setKnownPT(self):
        g = Linking.Graph()
        for i in range(5):
            g.add_vertex(i)
        #    0    1
        #       \ /  \
        #        2   3
        #       /
        #      4
        g.add_edge(0, 2)
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 4)
        al = Algorithms.algorithms(g.vert_dict)
        al.generateProbabilityTable()
        al.setKnownPT(0, 'T', 0.1)
        al.setKnownPT(1, 'T', 0.5)
        al.setKnownPT(2, 'TT', 1)
        al.setKnownPT(2, 'TF', 1)
        al.setKnownPT(2, 'FT', 0.5)
        al.setKnownPT(2, 'FF', 0.01)
        al.setKnownPT(3, 'T', 0.8)
        al.setKnownPT(3, 'F', 0.01)
        al.setKnownPT(4, 'T', 0.1)
        al.setKnownPT(4, 'F', 0.01)
        v = g.get_vertex(2).probabilityTable['TT'][0]  #node 2
        self.assertTrue(v == 1)  #expect v =1
        v = g.get_vertex(3).probabilityTable['T'][0]  #node 3
        self.assertTrue(v == 0.8)  #expect v = 0.8
        v = g.get_vertex(4).probabilityTable['T'][0]  #node 4
        self.assertTrue(v == 0.1)  #expect v = 0.1
        v = g.get_vertex(0).probabilityTable['T'][0]  #node 0
        self.assertTrue(v == 0.1)  #expect v = 0.1
        v = g.get_vertex(1).probabilityTable['T'][0]  #node 1
        self.assertTrue(v == 0.5)  #expect v = 0.5

    def test_generatePriorProbability(self):
        g = Linking.Graph()
        for i in range(5):
            g.add_vertex(i)
        #    0    1
        #       \ /  \
        #        2   3
        #       /
        #      4
        g.add_edge(0, 2)
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 4)
        al = Algorithms.algorithms(g.vert_dict)
        al.generateProbabilityTable()
        al.setKnownPT(0, 'T', 0.1)
        al.setKnownPT(1, 'T', 0.5)
        al.setKnownPT(2, 'TT', 1)
        al.setKnownPT(2, 'TF', 1)
        al.setKnownPT(2, 'FT', 0.5)
        al.setKnownPT(2, 'FF', 0.01)
        al.setKnownPT(3, 'T', 0.8)
        al.setKnownPT(3, 'F', 0.01)
        al.setKnownPT(4, 'T', 0.1)
        al.setKnownPT(4, 'F', 0.01)
        al.generatePriorProbability()
        self.assertTrue(g.get_vertex(0).probability == 0.1)
        self.assertTrue(g.get_vertex(1).probability == 0.5)
        self.assertTrue(g.get_vertex(2).probability == 0.3295)
        self.assertTrue(g.get_vertex(3).probability == 0.405)
        self.assertTrue(g.get_vertex(4).probability == 0.039655)

    def test_refreshP_simulating(self):
        g = Linking.Graph()
        for i in range(5):
            g.add_vertex(i)
        #    0    1
        #       \ /  \
        #        2   3
        #       /
        #      4
        g.add_edge(0, 2)
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 4)
        al = Algorithms.algorithms(g.vert_dict)
        al.generateProbabilityTable()
        al.setProbabilityTable(0, 'T', 0.1)
        al.setProbabilityTable(1, 'T', 0.5)
        al.setProbabilityTable(2, 'TT', 1)
        al.setProbabilityTable(2, 'TF', 1)
        al.setProbabilityTable(2, 'FT', 0.5)
        al.setProbabilityTable(2, 'FF', 0.01)
        al.setProbabilityTable(3, 'T', 0.8)
        al.setProbabilityTable(3, 'F', 0.01)
        al.setProbabilityTable(4, 'T', 0.1)
        al.setProbabilityTable(4, 'F', 0.01)
        al.simulateData(10000)

        obs = {3: 'T', 4: 'T'}
        self.assertTrue(math.fabs(al.query(obs, 0) - 0.17) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 1) - 1) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 2) - 0.92) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 3) - 1) <= 0.05)
        self.assertTrue(math.fabs(al.query(obs, 4) - 1) <= 0.05)
        # print(al.refreshP(obs))
    def test_refreshP_known(self):
        g = Linking.Graph()
        for i in range(5):
            g.add_vertex(i)
        #    0    1
        #       \ /  \
        #        2   3
        #       /
        #      4
        g.add_edge(0, 2)
        g.add_edge(1, 2)
        g.add_edge(1, 3)
        g.add_edge(2, 4)
        al = Algorithms.algorithms(g.vert_dict)
        al.generateProbabilityTable()
        al.setKnownPT(0, 'T', 0.1)
        al.setKnownPT(1, 'T', 0.5)
        al.setKnownPT(2, 'TT', 1)
        al.setKnownPT(2, 'TF', 1)
        al.setKnownPT(2, 'FT', 0.5)
        al.setKnownPT(2, 'FF', 0.01)
        al.setKnownPT(3, 'T', 0.8)
        al.setKnownPT(3, 'F', 0.01)
        al.setKnownPT(4, 'T', 0.1)
        al.setKnownPT(4, 'F', 0.01)
        al.generatePriorProbability()
        obs = {3: 'T', 4: 'T'}
        expected = {
            0: 0.1694628029147602,
            1: 0.9958554837953809,
            2: 0.9228191078724887,
            3: 1,
            4: 1
        }
        self.assertDictEqual(al.refreshP(obs), expected)
        obs = {3: 'T'}
        expected = {
            0: 0.1,
            1: 0.9876543209876543,
            2: 0.5445555555555555,
            3: 1,
            4: 0.05900999999999999
        }
        self.assertDictEqual(al.refreshP(obs), expected)
        obs = {3: 'F', 4: 'F'}
        expected = {
            0: 0.09244807952710092,
            1: 0.16236679513583266,
            2: 0.1692887479810971,
            3: 0,
            4: 0
        }
        self.assertDictEqual(al.refreshP(obs), expected)
        obs = {3: 'T', 4: 'F'}
        expected = {
            0: 0.09564394945748625,
            1: 0.9871400215612162,
            2: 0.520834440323489,
            3: 1,
            4: 0
        }
        self.assertDictEqual(al.refreshP(obs), expected)
        obs = {1: 'T', 3: 'T', 4: 'F'}  #3(true) is conditional independant
        expected = {
            0: 0.09665052244976859,
            1: 1,
            2: 0.5263157894736843,
            3: 1,
            4: 0
        }
        self.assertDictEqual(al.refreshP(obs), expected)
        obs = {1: 'T', 4: 'F'}  #3(not given) is conditional independant
        expected = {
            0: 0.1597316508266113,
            1: 1,
            2: 0.5263157894736843,
            3: 0.8,
            4: 0
        }
        self.assertDictEqual(al.refreshP(obs), expected)
        obs = {1: 'T', 3: 'T', 4: 'F'}  #3(False) is conditional independant
        expected = {
            0: 0.09665052244976859,
            1: 1,
            2: 0.5263157894736843,
            3: 1,
            4: 0
        }
        self.assertDictEqual(al.refreshP(obs), expected)
        obs = {}  #empty
        expected = {0: 0.1, 1: 0.5, 2: 0.3295, 3: 0.405, 4: 0.039655}
        self.assertDictEqual(al.refreshP(obs), expected)

    def test_markov(self):
        g = Linking.Graph()
        g.add_vertex('rainy')
        g.add_vertex('sunny')
        g.add_edge('rainy', 'sunny', 0.5)
        g.add_edge('rainy', 'rainy', 0.5)
        g.add_edge('sunny', 'rainy', 0.1)
        g.add_edge('sunny', 'sunny', 0.9)
        g.get_vertex('rainy').probability = 1  #initial probability
        g.get_vertex('sunny').probability = 0  #initial probability
        model = g.vert_dict

        gg = Linking.Graph()
        gg.add_vertex(0)
        gg.add_vertex(1)
        gg.add_vertex(2)
        gg.add_vertex(3)
        gg.add_edge(0, 1)
        gg.add_edge(1, 2)
        gg.add_edge(2, 3)
        al = Algorithms.algorithms(gg.vert_dict)
        expected = {
            0: {
                'rainy': 1,
                'sunny': 0
            },
            1: {
                'rainy': 0.5,
                'sunny': 0.5
            },
            2: {
                'rainy': 0.3,
                'sunny': 0.7
            },
            3: {
                'rainy': 0.21999999999999997,
                'sunny': 0.78
            }
        }
        self.assertDictEqual(al.markov(model, 0), expected)

        expected = {
            1: {
                'rainy': 1,
                'sunny': 0
            },
            2: {
                'rainy': 0.5,
                'sunny': 0.5
            },
            3: {
                'rainy': 0.3,
                'sunny': 0.7
            }
        }
        self.assertDictEqual(al.markov(model, 1), expected)

    def test_hiddenMarkov(self):
        g = Linking.Graph()
        g.add_vertex('rainy')
        g.add_vertex('sunny')
        g.add_vertex('hear')
        g.add_vertex('not')
        #transition probability
        g.add_edge('rainy', 'sunny', 0.5)
        g.add_edge('rainy', 'rainy', 0.5)
        g.add_edge('sunny', 'rainy', 0.1)
        g.add_edge('sunny', 'sunny', 0.9)
        #emission probability
        g.add_edge('rainy', 'hear', 0.7)
        g.add_edge('rainy', 'not', 0.3)
        g.add_edge('sunny', 'hear', 0.2)
        g.add_edge('sunny', 'not', 0.8)
        #initial probability
        g.get_vertex('rainy').probability = 0.5
        g.get_vertex('sunny').probability = 0.5
        model = g.vert_dict

        gg = Linking.Graph()
        #node
        gg.add_vertex(0)
        gg.add_vertex(1)
        gg.add_vertex(2)
        gg.add_vertex(3)
        #obs
        gg.add_vertex(4)
        gg.add_vertex(5)
        gg.add_vertex(6)

        gg.add_edge(0, 1)
        gg.add_edge(1, 2)
        gg.add_edge(2, 3)
        gg.add_edge(0, 4)  #obs
        gg.add_edge(1, 5)
        gg.add_edge(2, 6)
        al = Algorithms.algorithms(gg.vert_dict)

        obs = {4: 'hear', 5: 'hear', 6: 'not'}
        states = ['rainy', 'sunny']

        expected = {
            0: {
                'rainy': 0.7777777777777778,
                'sunny': 0.22222222222222227
            },
            1: {
                'rainy': 0.7095890410958903,
                'sunny': 0.2904109589041096
            },
            2: {
                'rainy': 0.18936697454381615,
                'sunny': 0.8106330254561839
            },
            3: {
                'rainy': 0.17574678981752645,
                'sunny': 0.8242532101824737
            }
        }
        # print(al.hiddenMarkov(model,0,obs,states))
        self.assertDictEqual(al.hiddenMarkov(model, 0, obs, states), expected)

    def test_valueIteration(self):
        g = Linking.Grid(2, 2)
        g.grid_dict[0].reward = -4
        g.grid_dict[1].utility = 100
        g.grid_dict[1].reward = 100
        g.grid_dict[2].utility = -100
        g.grid_dict[2].reward = -100
        g.setObstacle(3)
        al = Algorithms.algorithms(g.grid_dict)

        action = {'forward': 0.8, 'left': 0.1, 'right': 0.1}
        discount = 0.9

        expexted = {0: 64.83478199000001, 1: 100, 2: -100, 3: 0}
        # print(al.valueIteration(discount,action,g))
        self.assertDictEqual(al.valueIteration(discount, action, g), expexted)
        #another test
        g = Linking.Grid(4, 3)
        for i in g.grid_dict:
            if i != 5:
                g.grid_dict[i].reward = -4
        g.grid_dict[3].utility = 100
        g.grid_dict[3].reward = 100
        g.grid_dict[7].utility = -100
        g.grid_dict[7].reward = -100
        g.setObstacle(5)
        al = Algorithms.algorithms(g.grid_dict)

        action = {'forward': 0.8, 'left': 0.1, 'right': 0.1}
        discount = 0.9

        expexted = {
            0: 50.94115412930875,
            1: 64.95855729696827,
            2: 79.5362031683467,
            3: 100,
            4: 39.85041277059019,
            5: 0,
            6: 48.6439148544741,
            7: -100,
            8: 29.64382885103273,
            9: 25.391792612055916,
            10: 34.476553020311734,
            11: 12.99228937944113
        }
        # print(al.valueIteration(discount,action,g))
        self.assertDictEqual(al.valueIteration(discount, action, g), expexted)
 def test_refreshP_known(self):
     g = Linking.Graph()
     for i in range(5):
         g.add_vertex(i)
     #    0    1
     #       \ /  \
     #        2   3
     #       /
     #      4
     g.add_edge(0, 2)
     g.add_edge(1, 2)
     g.add_edge(1, 3)
     g.add_edge(2, 4)
     al = Algorithms.algorithms(g.vert_dict)
     al.generateProbabilityTable()
     al.setKnownPT(0, 'T', 0.1)
     al.setKnownPT(1, 'T', 0.5)
     al.setKnownPT(2, 'TT', 1)
     al.setKnownPT(2, 'TF', 1)
     al.setKnownPT(2, 'FT', 0.5)
     al.setKnownPT(2, 'FF', 0.01)
     al.setKnownPT(3, 'T', 0.8)
     al.setKnownPT(3, 'F', 0.01)
     al.setKnownPT(4, 'T', 0.1)
     al.setKnownPT(4, 'F', 0.01)
     al.generatePriorProbability()
     obs = {3: 'T', 4: 'T'}
     expected = {
         0: 0.1694628029147602,
         1: 0.9958554837953809,
         2: 0.9228191078724887,
         3: 1,
         4: 1
     }
     self.assertDictEqual(al.refreshP(obs), expected)
     obs = {3: 'T'}
     expected = {
         0: 0.1,
         1: 0.9876543209876543,
         2: 0.5445555555555555,
         3: 1,
         4: 0.05900999999999999
     }
     self.assertDictEqual(al.refreshP(obs), expected)
     obs = {3: 'F', 4: 'F'}
     expected = {
         0: 0.09244807952710092,
         1: 0.16236679513583266,
         2: 0.1692887479810971,
         3: 0,
         4: 0
     }
     self.assertDictEqual(al.refreshP(obs), expected)
     obs = {3: 'T', 4: 'F'}
     expected = {
         0: 0.09564394945748625,
         1: 0.9871400215612162,
         2: 0.520834440323489,
         3: 1,
         4: 0
     }
     self.assertDictEqual(al.refreshP(obs), expected)
     obs = {1: 'T', 3: 'T', 4: 'F'}  #3(true) is conditional independant
     expected = {
         0: 0.09665052244976859,
         1: 1,
         2: 0.5263157894736843,
         3: 1,
         4: 0
     }
     self.assertDictEqual(al.refreshP(obs), expected)
     obs = {1: 'T', 4: 'F'}  #3(not given) is conditional independant
     expected = {
         0: 0.1597316508266113,
         1: 1,
         2: 0.5263157894736843,
         3: 0.8,
         4: 0
     }
     self.assertDictEqual(al.refreshP(obs), expected)
     obs = {1: 'T', 3: 'T', 4: 'F'}  #3(False) is conditional independant
     expected = {
         0: 0.09665052244976859,
         1: 1,
         2: 0.5263157894736843,
         3: 1,
         4: 0
     }
     self.assertDictEqual(al.refreshP(obs), expected)
     obs = {}  #empty
     expected = {0: 0.1, 1: 0.5, 2: 0.3295, 3: 0.405, 4: 0.039655}
     self.assertDictEqual(al.refreshP(obs), expected)
Beispiel #22
0
 def test_g_add_objEdge(self):
     g = Linking.Graph()
     g.add_objEdge('a0')
     self.assertEqual(g.vert_dict['a0'].id, 'a0')
    def test_hiddenMarkov(self):
        g = Linking.Graph()
        g.add_vertex('rainy')
        g.add_vertex('sunny')
        g.add_vertex('hear')
        g.add_vertex('not')
        #transition probability
        g.add_edge('rainy', 'sunny', 0.5)
        g.add_edge('rainy', 'rainy', 0.5)
        g.add_edge('sunny', 'rainy', 0.1)
        g.add_edge('sunny', 'sunny', 0.9)
        #emission probability
        g.add_edge('rainy', 'hear', 0.7)
        g.add_edge('rainy', 'not', 0.3)
        g.add_edge('sunny', 'hear', 0.2)
        g.add_edge('sunny', 'not', 0.8)
        #initial probability
        g.get_vertex('rainy').probability = 0.5
        g.get_vertex('sunny').probability = 0.5
        model = g.vert_dict

        gg = Linking.Graph()
        #node
        gg.add_vertex(0)
        gg.add_vertex(1)
        gg.add_vertex(2)
        gg.add_vertex(3)
        #obs
        gg.add_vertex(4)
        gg.add_vertex(5)
        gg.add_vertex(6)

        gg.add_edge(0, 1)
        gg.add_edge(1, 2)
        gg.add_edge(2, 3)
        gg.add_edge(0, 4)  #obs
        gg.add_edge(1, 5)
        gg.add_edge(2, 6)
        al = Algorithms.algorithms(gg.vert_dict)

        obs = {4: 'hear', 5: 'hear', 6: 'not'}
        states = ['rainy', 'sunny']

        expected = {
            0: {
                'rainy': 0.7777777777777778,
                'sunny': 0.22222222222222227
            },
            1: {
                'rainy': 0.7095890410958903,
                'sunny': 0.2904109589041096
            },
            2: {
                'rainy': 0.18936697454381615,
                'sunny': 0.8106330254561839
            },
            3: {
                'rainy': 0.17574678981752645,
                'sunny': 0.8242532101824737
            }
        }
        # print(al.hiddenMarkov(model,0,obs,states))
        self.assertDictEqual(al.hiddenMarkov(model, 0, obs, states), expected)
Beispiel #24
0
##This refers to the second frame which the second row of buttons can be viewed on.
topFrame2 = Frame(root)
topFrame2.pack(fill=X)
##This referst to the bottom frame produced to display the results.
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
##This is the canvas on which all the visualisations are drawn.
#The background is set to a different colour to distinguish it from the other frames.
canvas = Canvas(root, width=800,height=400,bg="old lace")
canvas.pack(expand=1,fill=BOTH)

##These variables are to be used as a reference, when calling methods or objects
# from these classes.
GA=GuiArray.guiArray(canvas)
MN=ManageNode.manageNode()
LK=Linking.Graph()

##This creates a drop down menu list
myMenu = Menu(root)
root.config(menu=myMenu)

##This method is used to save the graphs produced by the user as a .pkl file to be
# loaded when the user returns to the interface at a later point.
def saveFile():
    fileFormats = [('Pickle', "*.pkl")]
    ##This produces a dialog box allowing the user to choose where to save the file on their system.
    filename = tkinter.filedialog.asksaveasfilename(filetypes=fileFormats)

    ##Everything the user has placed onto the canvas is saved into the GA.nodeList dictionary.
    #Everything in this dictionary is called into a new dictionary and this new dictionary is used
    #by pickle.dump and saved into a .pkl file.