def make_final_exam_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ nodes = [] # TODO: finish this function N_node = BayesNode(0, 2, name = 'netflix') E_node = BayesNode(1, 2, name = 'exercise') A_node = BayesNode(2, 2, name = 'assignment') C_node = BayesNode(3, 2, name = 'club') S_node = BayesNode(4, 2, name = 'social') G_node = BayesNode(5, 2, name='graduate') nodes.extend([N_node, E_node, A_node, C_node, S_node, G_node]) A_node.add_parent(N_node) N_node.add_child(A_node) A_node.add_parent(E_node) E_node.add_child(A_node) S_node.add_parent(C_node) C_node.add_child(S_node) G_node.add_parent(A_node) A_node.add_child(G_node) G_node.add_parent(S_node) S_node.add_child(G_node) return BayesNet(nodes)
def make_cargo_flight_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ nodes = [] # TODO: finish this function paint_node = BayesNode(0, 2, name = 'paint') weather_node = BayesNode(1, 2, name = 'weather') oring_node = BayesNode(2, 2, name = 'oring') shuttle_node = BayesNode(3, 2, name = 'shuttle') booster_node = BayesNode(4, 2, name = 'booster') mission_node = BayesNode(5, 2, name='mission') nodes.extend([paint_node, weather_node, oring_node, shuttle_node, booster_node, mission_node]) shuttle_node.add_parent(paint_node) paint_node.add_child(shuttle_node) shuttle_node.add_parent(weather_node) weather_node.add_child(shuttle_node) booster_node.add_parent(weather_node) weather_node.add_child(booster_node) booster_node.add_parent(oring_node) oring_node.add_child(booster_node) mission_node.add_parent(shuttle_node) shuttle_node.add_child(mission_node) mission_node.add_parent(booster_node) booster_node.add_child(mission_node) return BayesNet(nodes)
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ G_node = BayesNode(0, 2, name='gauge') FG_node = BayesNode(1, 2, name='faulty gauge') T_node = BayesNode(2, 2, name='temperature') A_node = BayesNode(3, 2, name='alarm') FA_node = BayesNode(4, 2, name='faulty alarm') T_node.add_child(G_node) T_node.add_child(FG_node) G_node.add_parent(T_node) FG_node.add_parent(T_node) FG_node.add_child(G_node) G_node.add_parent(FG_node) G_node.add_child(A_node) A_node.add_parent(G_node) FA_node.add_child(A_node) A_node.add_parent(FA_node) nodes = [G_node, FG_node, T_node, FA_node, A_node] # TODO: finish this function return BayesNet(nodes)
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ nodes = [] A_node = BayesNode(0, 2, name='alarm') Fa_node = BayesNode(1, 2, name='faulty alarm') G_node = BayesNode(2, 2, name='gauge') Fg_node = BayesNode(3, 2, name='faulty gauge') T_node = BayesNode(4, 2, name='temperature') # Faulty alarm and gauge reading affects the alarm A_node.add_parent(Fa_node) Fa_node.add_child(A_node) A_node.add_parent(G_node) G_node.add_child(A_node) # Faulty gauge and temperature affects the gauge G_node.add_parent(Fg_node) Fg_node.add_child(G_node) G_node.add_parent(T_node) T_node.add_child(G_node) # High temperature can cause faulty gauge Fg_node.add_parent(T_node) T_node.add_child(Fg_node) nodes = [A_node, Fa_node, G_node, Fg_node, T_node] return BayesNet(nodes)
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ nodes = [] A_node = BayesNode(0, 2, name = "alarm") FA_node = BayesNode(1, 2, name = "faulty alarm") G_node = BayesNode(2, 2, name = "gauge") FG_node = BayesNode(3, 2, name = "faulty gauge") T_node = BayesNode(4, 2, name = "temperature") T_node.add_child(FG_node) T_node.add_child(G_node) FG_node.add_child(G_node) G_node.add_child(A_node) FA_node.add_child(A_node) G_node.add_parent(T_node) G_node.add_parent(FG_node) FG_node.add_parent(T_node) A_node.add_parent(G_node) A_node.add_parent(FA_node) nodes = [A_node, FA_node, G_node, FG_node, T_node] return BayesNet(nodes) raise NotImplementedError
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ nodenames = [ "temperature", "faulty gauge", "gauge", "faulty alarm", "alarm" ] T_node = BayesNode(0, 2, name=nodenames[0]) Fg_node = BayesNode(1, 2, name=nodenames[1]) G_node = BayesNode(2, 2, name=nodenames[2]) Fa_node = BayesNode(3, 2, name=nodenames[3]) A_node = BayesNode(4, 2, name=nodenames[4]) T_node.add_child(Fg_node) T_node.add_child(G_node) Fg_node.add_parent(T_node) Fg_node.add_child(G_node) G_node.add_parent(Fg_node) G_node.add_parent(T_node) G_node.add_child(A_node) Fa_node.add_child(A_node) A_node.add_parent(G_node) A_node.add_parent(Fa_node) nodes = [T_node, Fg_node, G_node, Fa_node, A_node] return BayesNet(nodes) # TODO: finish this function raise NotImplementedError
def make_power_plant_net(): #testing basic bayes net class implementation numberOfNodes = 5 #name the nodes alarm = 0 faulty_alarm = 1 gauge = 2 faulty_gauge = 3 temperature = 4 #create nodes ---> refer Node.py alarm_node = BayesNode(0, 2, name="alarm") faulty_alarm_node = BayesNode(1, 2, name="faulty alarm") gauge_node = BayesNode(2, 2, name="gauge") faulty_gauge_node = BayesNode(3, 2, name="faulty gauge") temperature_node = BayesNode(4, 2, name="temperature") temperature_node.add_child(faulty_gauge_node) temperature_node.add_child(gauge_node) faulty_alarm_node.add_child(alarm_node) faulty_gauge_node.add_parent(temperature_node) faulty_gauge_node.add_child(gauge_node) gauge_node.add_parent(faulty_gauge_node) gauge_node.add_parent(temperature_node) gauge_node.add_child(alarm_node) alarm_node.add_parent(faulty_alarm_node) alarm_node.add_parent(gauge_node) nodes = [alarm_node, faulty_alarm_node, gauge_node,faulty_gauge_node,temperature_node] return BayesNet(nodes)
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem.""" nodes = [] A_node = BayesNode(0, 2, name='alarm') FA_node = BayesNode(1, 2, name='faulty alarm') G_node = BayesNode(2, 2, name='gauge') FG_node = BayesNode(3, 2, name='faulty gauge') T_node = BayesNode(4, 2, name='temperature') T_node.add_child(FG_node) FG_node.add_parent(T_node) T_node.add_child(G_node) G_node.add_parent(T_node) FG_node.add_child(G_node) G_node.add_parent(FG_node) G_node.add_child(A_node) A_node.add_parent(G_node) FA_node.add_child(A_node) A_node.add_parent(FA_node) nodes.append(A_node) nodes.append(FA_node) nodes.append(G_node) nodes.append(FG_node) nodes.append(T_node) return BayesNet(nodes)
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ nodes = [] # TODO: finish this function #raise NotImplementedError # create nodes A_node = BayesNode(0, 2, name='alarm') F_A_node= BayesNode(1, 2, name='faulty alarm') G_node=BayesNode(2, 2, name='gauge') F_G_node = BayesNode(3, 2, name='faulty gauge') T_node = BayesNode(4, 2, name='temperature') #connect nodes T_node.add_child(G_node) G_node.add_parent(T_node) T_node.add_child(F_G_node) F_G_node.add_parent(T_node) F_G_node.add_child(G_node) G_node.add_parent(F_G_node) G_node.add_child(A_node) A_node.add_parent(G_node) F_A_node.add_child(A_node) A_node.add_parent(F_A_node) nodes=[A_node,F_A_node,G_node,F_G_node,T_node] return BayesNet(nodes)
def set_probability(bayes_net): """Set probability distribution for each node in the power plant system.""" A_node = bayes_net.get_node_by_name("alarm") F_A_node = bayes_net.get_node_by_name("faulty alarm") G_node = bayes_net.get_node_by_name("gauge") F_G_node = bayes_net.get_node_by_name("faulty gauge") T_node = bayes_net.get_node_by_name("temperature") nodes = [A_node, F_A_node, G_node, F_G_node, T_node] T_distribution = DiscreteDistribution(T_node) index = T_distribution.generate_index([], []) T_distribution[index] = [0.80, 0.20] T_node.set_dist(T_distribution) dist = zeros([T_node.size(), F_G_node.size()], dtype=float32) dist[0, :] = [0.95, 0.05] dist[1, :] = [0.20, 0.80] F_G_distribution = ConditionalDiscreteDistribution( nodes=[T_node, F_G_node], table=dist) F_G_node.set_dist(F_G_distribution) dist = zeros([T_node.size(), F_G_node.size(), G_node.size()], dtype=float32) dist[0, 0, :] = [0.95, 0.05] dist[0, 1, :] = [0.20, 0.80] dist[1, 0, :] = [0.05, 0.95] dist[1, 1, :] = [0.80, 0.20] G_distribution = ConditionalDiscreteDistribution( nodes=[T_node, F_G_node, G_node], table=dist) G_node.set_dist(G_distribution) F_A_distribution = DiscreteDistribution(F_A_node) index = F_A_distribution.generate_index([], []) F_A_distribution[index] = [0.85, 0.15] F_A_node.set_dist(F_A_distribution) dist = zeros([G_node.size(), F_A_node.size(), A_node.size()], dtype=float32) dist[0, 0, :] = [0.90, 0.10] dist[0, 1, :] = [0.55, 0.45] dist[1, 0, :] = [0.10, 0.90] dist[1, 1, :] = [0.45, 0.55] A_distribution = ConditionalDiscreteDistribution( nodes=[G_node, F_A_node, A_node], table=dist) A_node.set_dist(A_distribution) bayes_net = BayesNet(nodes) return bayes_net # TODO: set the probability distribution for each node raise NotImplementedError
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ A_node = BayesNode(0, 4, name='A') B_node = BayesNode(1, 4, name='B') C_node = BayesNode(2, 4, name='C') AvB_node = BayesNode(3, 3, name='AvB') BvC_node = BayesNode(4, 3, name='BvC') CvA_node = BayesNode(5, 3, name='CvA') A_node.add_child(AvB_node) A_node.add_child(CvA_node) B_node.add_child(AvB_node) B_node.add_child(BvC_node) C_node.add_child(BvC_node) C_node.add_child(CvA_node) AvB_node.add_parent(A_node) AvB_node.add_parent(B_node) BvC_node.add_parent(B_node) BvC_node.add_parent(C_node) CvA_node.add_parent(A_node) CvA_node.add_parent(C_node) A_node = set_skill_distribution(A_node) B_node = set_skill_distribution(B_node) C_node = set_skill_distribution(C_node) AvB_node = set_skill_diff_distribution(AvB_node, [A_node, B_node, AvB_node]) BvC_node = set_skill_diff_distribution(BvC_node, [B_node, C_node, BvC_node]) CvA_node = set_skill_diff_distribution(CvA_node, [C_node, A_node, CvA_node]) nodes = [A_node, B_node, C_node, AvB_node, BvC_node, CvA_node] return BayesNet(nodes)
def make_power_plant_net(): # intial setup nodes = [] numberOfNodes = 5 temperature = 0 gauge = 1 faulty_gauge = 2 alarm = 3 faulty_alarm = 4 # create the nodes T_node = BayesNode(0, 2, name='temperature') G_node = BayesNode(1, 2, name='gauge') F_G_node = BayesNode(2, 2, name='faulty gauge') A_node = BayesNode(3, 2, name='alarm') F_A_node = BayesNode(4, 2, name='faulty alarm') # temperature T_node.add_child(G_node) T_node.add_child(F_G_node) # faulty gauge F_G_node.add_child(G_node) F_G_node.add_parent(T_node) # gauge G_node.add_parent(T_node) G_node.add_parent(F_G_node) G_node.add_child(A_node) # faulty alarm F_A_node.add_child(A_node) # alarm A_node.add_parent(G_node) A_node.add_parent(F_A_node) # add the nodes for setting up example network nodes = [A_node, F_A_node, G_node, F_G_node, T_node] return BayesNet(nodes)
def make_game_net(): nodes = [] # TODO: fill this out # raise NotImplementedError A_node = BayesNode(0, 4, name='A') B_node = BayesNode(1, 4, name='B') C_node = BayesNode(2, 4, name='C') AvB_node = BayesNode(3, 3, name='AvB') BvC_node = BayesNode(4, 3, name='BvC') CvA_node = BayesNode(5, 3, name='CvA') # A A_node.add_child(AvB_node) A_node.add_child(CvA_node) # B B_node.add_child(BvC_node) B_node.add_child(AvB_node) # C C_node.add_child(BvC_node) C_node.add_child(CvA_node) # AvB AvB_node.add_parent(A_node) AvB_node.add_parent(B_node) # BvC BvC_node.add_parent(B_node) BvC_node.add_parent(C_node) # CvA CvA_node.add_parent(C_node) CvA_node.add_parent(A_node) # Append nodes to node list nodes.append(A_node) nodes.append(B_node) nodes.append(C_node) nodes.append(AvB_node) nodes.append(BvC_node) nodes.append(CvA_node) return BayesNet(nodes)
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem. Name the nodes as "alarm","faulty alarm", "gauge","faulty gauge", "temperature". """ nodes = [] # TODO: finish this function #create nodes ---> refer node.py A = BayesNode(0, 2, name="alarm") F_A = BayesNode(1, 2, name="faulty alarm") G_node = BayesNode(2, 2, name="gauge") F_G_node = BayesNode(3, 2, name="faulty gauge") T_node = BayesNode(4, 2, name="temperature") #alarm A.add_parent(G_node) A.add_parent(F_A) #faulty alarm F_A.add_child(A) #gauge G_node.add_parent(T_node) G_node.add_parent(F_G_node) G_node.add_child(A) #faulty gauge F_G_node.add_parent(T_node) F_G_node.add_child(G_node) #temperature T_node.add_child(G_node) T_node.add_child(F_G_node) nodes = [A, F_A, G_node, F_G_node, T_node] return BayesNet(nodes)
def make_power_plant_net(): """Create a Bayes Net representation of the above power plant problem. Use the following as the name attribute: "alarm","faulty alarm", "gauge","faulty gauge", "temperature". (for the tests to work.) """ nodes = [] # TODO: finish this function A_Node = BayesNode(0, 2, name="alarm") FA_Node = BayesNode(2, 2, name="faulty alarm") G_Node = BayesNode(3, 2, name="gauge") FG_Node = BayesNode(4, 2, name="faulty gauge") T_Node = BayesNode(5, 2, name="temperature") # T -> FG T_Node.add_child(FG_Node) FG_Node.add_parent(T_Node) # FG -> G FG_Node.add_child(G_Node) G_Node.add_parent(FG_Node) # T -> G T_Node.add_child(G_Node) G_Node.add_parent(T_Node) # G -> A G_Node.add_child(A_Node) A_Node.add_parent(G_Node) # FA -> A FA_Node.add_child(A_Node) A_Node.add_parent(FA_Node) nodes.append(A_Node) nodes.append(FA_Node) nodes.append(G_Node) nodes.append(FG_Node) nodes.append(T_Node) return BayesNet(nodes)
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ nodes = [] A_node = BayesNode(0, 4, name='A') B_node = BayesNode(1, 4, name='B') C_node = BayesNode(2, 4, name='C') AvB_node = BayesNode(3, 3, name='AvB') BvC_node = BayesNode(4, 3, name='BvC') CvA_node = BayesNode(5, 3, name='CvA') # Skill level of A and B affects AvB AvB_node.add_parent(A_node) AvB_node.add_parent(B_node) A_node.add_child(AvB_node) B_node.add_child(AvB_node) # Skill level of B and C affects BvC BvC_node.add_parent(B_node) BvC_node.add_parent(C_node) B_node.add_child(BvC_node) C_node.add_child(BvC_node) # Skill level of C and A affects BvC CvA_node.add_parent(A_node) CvA_node.add_parent(C_node) A_node.add_child(CvA_node) C_node.add_child(CvA_node) A_dist = DiscreteDistribution(A_node) index = A_dist.generate_index([], []) A_dist[index] = [0.15, 0.45, 0.30, 0.10] A_node.set_dist(A_dist) B_dist = DiscreteDistribution(B_node) index = B_dist.generate_index([], []) B_dist[index] = [0.15, 0.45, 0.30, 0.10] B_node.set_dist(B_dist) C_dist = DiscreteDistribution(C_node) index = C_dist.generate_index([], []) C_dist[index] = [0.15, 0.45, 0.30, 0.10] C_node.set_dist(C_dist) dist = zeros([A_node.size(), B_node.size(), AvB_node.size()], dtype=float32) dist[0, 0, :] = [0.1, 0.1, 0.8] dist[0, 1, :] = [0.2, 0.6, 0.2] dist[0, 2, :] = [0.15, 0.75, 0.1] dist[0, 3, :] = [0.05, 0.9, 0.05] dist[1, 0, :] = [0.6, 0.2, 0.2] dist[1, 1, :] = [0.1, 0.1, 0.8] dist[1, 2, :] = [0.2, 0.6, 0.2] dist[1, 3, :] = [0.15, 0.75, 0.1] dist[2, 0, :] = [0.75, 0.15, 0.1] dist[2, 1, :] = [0.6, 0.2, 0.2] dist[2, 2, :] = [0.1, 0.1, 0.8] dist[2, 3, :] = [0.2, 0.6, 0.2] dist[3, 0, :] = [0.9, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.1] dist[3, 2, :] = [0.6, 0.2, 0.2] dist[3, 3, :] = [0.1, 0.1, 0.8] AvB_dist = ConditionalDiscreteDistribution( nodes=[A_node, B_node, AvB_node], table=dist) AvB_node.set_dist(AvB_dist) # The same distribution AvB applies for BvC and CvA BvC_dist = ConditionalDiscreteDistribution( nodes=[B_node, C_node, BvC_node], table=dist) BvC_node.set_dist(BvC_dist) CvA_dist = ConditionalDiscreteDistribution( nodes=[C_node, A_node, CvA_node], table=dist) CvA_node.set_dist(CvA_dist) nodes = [A_node, B_node, C_node, AvB_node, BvC_node, CvA_node] return BayesNet(nodes)
def make_final_net(): A = BayesNode(0, 2, name='A') B = BayesNode(1, 2, name='B') C = BayesNode(2, 2, name='C') D = BayesNode(3, 2, name='D') E = BayesNode(4, 2, name='E') A.add_child(C) C.add_parent(A) B.add_child(C) C.add_parent(B) B.add_child(D) D.add_parent(B) C.add_child(E) E.add_parent(C) D.add_child(E) E.add_parent(D) nodes = [A, B, C, D, E] net = BayesNet(nodes) A = net.get_node_by_name('A') B = net.get_node_by_name('B') C = net.get_node_by_name('C') D = net.get_node_by_name('D') E = net.get_node_by_name('E') A_distribution = DiscreteDistribution(A) index = A_distribution.generate_index([], []) A_distribution[index] = [0.4, 0.6] A.set_dist(A_distribution) B_distribution = DiscreteDistribution(B) index = B_distribution.generate_index([], []) B_distribution[index] = [0.8, 0.2] B.set_dist(B_distribution) print B, D dist = zeros([B.size(), D.size()], dtype=float32) dist[0, :] = [0.87, 0.13] dist[1, :] = [0.24, 0.76] D_distribution = ConditionalDiscreteDistribution(nodes=[B, D], table=dist) D.set_dist(D_distribution) dist = zeros([A.size(), B.size(), C.size()], dtype=float32) dist[0, 0, :] = [0.85, 0.15] dist[0, 1, :] = [0.68, 0.32] dist[1, 0, :] = [0.16, 0.84] dist[1, 1, :] = [0.05, 0.95] C_distribution = ConditionalDiscreteDistribution(nodes=[A, B, C], table=dist) C.set_dist(C_distribution) dist = zeros([C.size(), D.size(), E.size()], dtype=float32) dist[0, 0, :] = [0.8, 0.2] dist[0, 1, :] = [0.37, 0.63] dist[1, 0, :] = [0.08, 0.92] dist[1, 1, :] = [0.07, 0.93] E_distribution = ConditionalDiscreteDistribution(nodes=[C, D, E], table=dist) E.set_dist(E_distribution) engine = EnumerationEngine(net) # engine1.evidence[wear] = False # engine1.evidence[weap] = True engine.evidence[A] = True engine.evidence[C] = True Q = engine.marginal(E)[0] index = Q.generate_index([True], range(Q.nDims)) prob = Q[index] print prob
def make_exam_net(): """Create a Bayes Net representation of the above power plant problem. Name the nodes as "alarm","faulty alarm", "gauge","faulty gauge", "temperature". """ nodes = [] # TODO: finish this function #create nodes ---> refer node.py MA = BayesNode(0, 2, name="main alarm") A1 = BayesNode(1, 2, name="alarm 2") A2 = BayesNode(2, 2, name="alarm 1") A3 = BayesNode(3, 2, name="alarm 3") G = BayesNode(4, 2, name="ghost") B = BayesNode(5, 2, name="burglar") MA.add_parent(G) MA.add_parent(B) dist = zeros([G.size(), B.size(), MA.size()], dtype=float32) dist[0,0,:] = [0.99, 0.01] dist[0,1,:] = [0.36, 0.64] dist[1,0,:] = [0.75, 0.25] dist[1,1,:] = [0.02, 0.98] MA_distribution = ConditionalDiscreteDistribution(nodes=[G, B, MA], table=dist) MA.set_dist(MA_distribution) G.add_child(MA) G.add_child(A2) G.add_child(A1) G_distribution = DiscreteDistribution(G) index = G_distribution.generate_index([],[]) G_distribution[index] = [0.6,0.4] G.set_dist(G_distribution) B.add_child(MA) B.add_child(A2) B.add_child(A3) B_distribution = DiscreteDistribution(B) index = B_distribution.generate_index([],[]) B_distribution[index] = [0.68,0.32] B.set_dist(B_distribution) A1.add_parent(G) A1_distribution = DiscreteDistribution(A1) dist = zeros([G.size(), A1.size()], dtype=float32) #Note the order of G_node, A_node dist[0,:] = [0.91, 0.09] dist[1,:] = [0.18, 0.82] A1_distribution = ConditionalDiscreteDistribution(nodes=[G,A1], table=dist) A1.set_dist(A1_distribution) A2.add_parent(G) A2.add_parent(B) dist = zeros([G.size(), B.size(), A2.size()], dtype=float32) dist[0,0,:] = [0.95, 0.05] dist[0,1,:] = [0.37, 0.63] dist[1,0,:] = [0.69, 0.31] dist[1,1,:] = [0.13, 0.87] A2_distribution = ConditionalDiscreteDistribution(nodes=[G, B, A2], table=dist) A2.set_dist(A2_distribution) A3.add_parent(B) A3_distribution = DiscreteDistribution(A3) dist = zeros([B.size(), A3.size()], dtype=float32) #Note the order of G_node, A_node dist[0,:] = [0.28, 0.72] dist[1,:] = [0.81, 0.19] A3_distribution = ConditionalDiscreteDistribution(nodes=[B,A3], table=dist) A3.set_dist(A3_distribution) nodes = [MA,A1,A2,A3,G,B] return BayesNet(nodes)
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ nodes = [] # TODO: fill this out A_node = BayesNode(0, 4, name='A') B_node = BayesNode(1, 4, name='B') C_node = BayesNode(2, 4, name='C') AvB_node = BayesNode(3, 3, name='AvB') BvC_node = BayesNode(4, 3, name='BvC') CvA_node = BayesNode(5, 3, name='CvA') # Match A v B AvB_node.add_parent(A_node) AvB_node.add_parent(B_node) A_node.add_child(AvB_node) B_node.add_child(AvB_node) # Match B v C BvC_node.add_parent(B_node) BvC_node.add_parent(C_node) B_node.add_child(BvC_node) C_node.add_child(BvC_node) # Match C v A CvA_node.add_parent(C_node) CvA_node.add_parent(A_node) C_node.add_child(CvA_node) A_node.add_child(CvA_node) nodes = [A_node, B_node, C_node, AvB_node, BvC_node, CvA_node] prior_skill_dist = [0.15, 0.45, 0.3, 0.1] A_skill_dist = DiscreteDistribution(A_node) index = A_skill_dist.generate_index([], []) A_skill_dist[index] = prior_skill_dist A_node.set_dist(A_skill_dist) B_skill_dist = DiscreteDistribution(B_node) index = B_skill_dist.generate_index([], []) B_skill_dist[index] = prior_skill_dist B_node.set_dist(B_skill_dist) C_skill_dist = DiscreteDistribution(C_node) index = C_skill_dist.generate_index([], []) C_skill_dist[index] = prior_skill_dist C_node.set_dist(C_skill_dist) # Match Prob Distribution # P(T1vT2 | T1, T2) # T1 | T2 | T1 | T2 | Tie # 0 | 0 | 0.1 | 0.1 | 0.8 # 0 | 1 | 0.2 | 0.6 | 0.2 # 0 | 2 | 0.15 | 0.75 | 0.1 # 0 | 3 | 0.05 | 0.9 | 0.05 # 1 | 0 | 0.6 | 0.2 | 0.2 # 1 | 1 | 0.1 | 0.1 | 0.8 # 1 | 2 | 0.2 | 0.6 | 0.2 # 1 | 3 | 0.15 | 0.75 | 0.1 # 2 | 0 | 0.75 | 0.15 | 0.1 # 2 | 1 | 0.6 | 0.2 | 0.2 # 2 | 2 | 0.1 | 0.1 | 0.8 # 2 | 3 | 0.2 | 0.6 | 0.2 # 3 | 0 | 0.9 | 0.05 | 0.05 # 3 | 1 | 0.75 | 0.15 | 0.1 # 3 | 2 | 0.6 | 0.2 | 0.2 # 3 | 3 | 0.1 | 0.1 | 0.8 match_dist = zeros([A_node.size(), B_node.size(), AvB_node.size()], dtype=float32) match_dist[0, 0,:] = [0.1, 0.1, 0.8] match_dist[0, 1,:] = [0.2, 0.6, 0.2] match_dist[0, 2,:] = [0.15, 0.75, 0.1] match_dist[0, 3,:] = [0.05, 0.9, 0.05] match_dist[1, 0,:] = [0.6, 0.2, 0.2] match_dist[1, 1,:] = [0.1, 0.1, 0.8] match_dist[1, 2,:] = [0.2, 0.6, 0.2] match_dist[1, 3,:] = [0.15, 0.75, 0.1] match_dist[2, 0,:] = [0.75, 0.15, 0.1] match_dist[2, 1,:] = [0.6, 0.2, 0.2] match_dist[2, 2,:] = [0.1, 0.1, 0.8] match_dist[2, 3,:] = [0.2, 0.6, 0.2] match_dist[3, 0,:] = [0.9, 0.05, 0.05] match_dist[3, 1,:] = [0.75, 0.15, 0.1] match_dist[3, 2,:] = [0.6, 0.2, 0.2] match_dist[3, 3,:] = [0.1, 0.1, 0.8] AvB_distribution = ConditionalDiscreteDistribution(nodes=[A_node, B_node, AvB_node], table=match_dist) AvB_node.set_dist(AvB_distribution) BvC_distribution = ConditionalDiscreteDistribution(nodes=[B_node, C_node, BvC_node], table=match_dist) BvC_node.set_dist(BvC_distribution) CvA_distribution = ConditionalDiscreteDistribution(nodes=[C_node, A_node, CvA_node], table=match_dist) CvA_node.set_dist(CvA_distribution) return BayesNet(nodes)
def get_game_network(): """Create a Bayes Net representation of the game problem.""" nodes = [] # TODO: fill this out A_node = BayesNode(0, 4, name='A') B_node = BayesNode(1, 4, name='B') C_node = BayesNode(2, 4, name='C') AvB_node = BayesNode(3, 3, name='AvB') BvC_node = BayesNode(4, 3, name='BvC') CvA_node = BayesNode(5, 3, name='CvA') A_node.add_child(AvB_node) AvB_node.add_parent(A_node) B_node.add_child(AvB_node) AvB_node.add_parent(B_node) B_node.add_child(BvC_node) BvC_node.add_parent(B_node) C_node.add_child(BvC_node) BvC_node.add_parent(C_node) C_node.add_child(CvA_node) CvA_node.add_parent(C_node) A_node.add_child(CvA_node) CvA_node.add_parent(A_node) nodes.append(A_node) nodes.append(B_node) nodes.append(C_node) nodes.append(AvB_node) nodes.append(BvC_node) nodes.append(CvA_node) A_distribution = DiscreteDistribution(A_node) index = A_distribution.generate_index([], []) A_distribution[index] = [0.15, 0.45, 0.3, 0.1] A_node.set_dist(A_distribution) B_distribution = DiscreteDistribution(B_node) index = B_distribution.generate_index([], []) B_distribution[index] = [0.15, 0.45, 0.3, 0.1] B_node.set_dist(B_distribution) C_distribution = DiscreteDistribution(C_node) index = C_distribution.generate_index([], []) C_distribution[index] = [0.15, 0.45, 0.3, 0.1] C_node.set_dist(C_distribution) dist = zeros([A_node.size(), B_node.size(), AvB_node.size()], dtype=float32) dist[0, 0, :] = [0.1, 0.1, 0.8] dist[0, 1, :] = [0.2, 0.6, 0.2] dist[0, 2, :] = [0.15, 0.75, 0.1] dist[0, 3, :] = [0.05, 0.9, 0.05] dist[1, 0, :] = [0.6, 0.2, 0.2] dist[1, 1, :] = [0.1, 0.1, 0.8] dist[1, 2, :] = [0.2, 0.6, 0.2] dist[1, 3, :] = [0.15, 0.75, 0.1] dist[2, 0, :] = [0.75, 0.15, 0.1] dist[2, 1, :] = [0.6, 0.2, 0.2] dist[2, 2, :] = [0.1, 0.1, 0.8] dist[2, 3, :] = [0.2, 0.6, 0.2] dist[3, 0, :] = [0.9, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.1] dist[3, 2, :] = [0.6, 0.2, 0.2] dist[3, 3, :] = [0.1, 0.1, 0.8] AvB_distribution = ConditionalDiscreteDistribution( nodes=[A_node, B_node, AvB_node], table=dist) AvB_node.set_dist(AvB_distribution) BvC_distribution = ConditionalDiscreteDistribution( nodes=[B_node, C_node, BvC_node], table=dist) BvC_node.set_dist(BvC_distribution) CvA_distribution = ConditionalDiscreteDistribution( nodes=[C_node, A_node, CvA_node], table=dist) CvA_node.set_dist(CvA_distribution) print "Printing table" print type(AvB_node.dist.table) for i in range(3): print AvB_node.dist.table[0][0][i] return BayesNet(nodes)
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ nodes = [] # TODO: fill this out # Create Nodes A = BayesNode(0, 4, name="A") B_node = BayesNode(1, 4, name="B") C_node = BayesNode(2, 4, name="C") AvB = BayesNode(3, 3, name="AvB") BvC_node = BayesNode(4, 3, name="BvC") CvA = BayesNode(5, 3, name="CvA") # A skill A.add_child(AvB) A.add_child(CvA) # B skill B_node.add_child(AvB) B_node.add_child(BvC_node) # C skill C_node.add_child(BvC_node) C_node.add_child(CvA) # AvB outcome AvB.add_parent(A) AvB.add_parent(B_node) # BvC outcome BvC_node.add_parent(B_node) BvC_node.add_parent(C_node) # CvA outcome CvA.add_parent(C_node) CvA.add_parent(A) # Set Probability Distributions # A, B, C team skill levels A_distribution = DiscreteDistribution(A) index = A_distribution.generate_index([], []) A_distribution[index] = [0.15, 0.45, 0.3, 0.1] A.set_dist(A_distribution) B_distribution = DiscreteDistribution(B_node) index = B_distribution.generate_index([], []) B_distribution[index] = [0.15, 0.45, 0.3, 0.1] B_node.set_dist(B_distribution) C_distribution = DiscreteDistribution(C_node) index = C_distribution.generate_index([], []) C_distribution[index] = [0.15, 0.45, 0.3, 0.1] C_node.set_dist(C_distribution) # AvB, BvC, CvA game outcomes dist = zeros([A.size(), B_node.size(), AvB.size()], dtype=float32) dist[0, 0, :] = [0.1, 0.1, 0.8] dist[0, 1, :] = [0.2, 0.6, 0.2] dist[0, 2, :] = [0.15, 0.75, 0.1] dist[0, 3, :] = [0.05, 0.9, 0.05] dist[1, 0, :] = [0.6, 0.2, 0.2] dist[1, 1, :] = [0.1, 0.1, 0.8] dist[1, 2, :] = [0.2, 0.6, 0.2] dist[1, 3, :] = [0.15, 0.75, 0.1] dist[2, 0, :] = [0.75, 0.15, 0.1] dist[2, 1, :] = [0.6, 0.2, 0.2] dist[2, 2, :] = [0.1, 0.1, 0.8] dist[2, 3, :] = [0.2, 0.6, 0.2] dist[3, 0, :] = [0.9, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.1] dist[3, 2, :] = [0.6, 0.2, 0.2] dist[3, 3, :] = [0.1, 0.1, 0.8] AvB_distribution = ConditionalDiscreteDistribution(nodes=[A, B_node, AvB], table=dist) AvB.set_dist(AvB_distribution) BvC_distribution = ConditionalDiscreteDistribution( nodes=[B_node, C_node, BvC_node], table=dist) BvC_node.set_dist(BvC_distribution) CvA_distribution = ConditionalDiscreteDistribution(nodes=[C_node, A, CvA], table=dist) CvA.set_dist(CvA_distribution) nodes = [A, B_node, C_node, AvB, BvC_node, CvA] return BayesNet(nodes)
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ teama_node = BayesNode(0,4,name='A') teamb_node = BayesNode(1,4,name='B') teamc_node = BayesNode(2,4,name='C') matchAvB_node = BayesNode(3,3,name='AvB') matchBvC_node = BayesNode(4,3,name='BvC') matchCvA_node = BayesNode(5,3,name='CvA') teama_node.add_child(matchAvB_node) matchAvB_node.add_parent(teama_node) teama_node.add_child(matchCvA_node) matchCvA_node.add_parent(teama_node) teamb_node.add_child(matchAvB_node) matchAvB_node.add_parent(teamb_node) teamb_node.add_child(matchBvC_node) matchBvC_node.add_parent(teamb_node) teamc_node.add_child(matchBvC_node) matchBvC_node.add_parent(teamc_node) teamc_node.add_child(matchCvA_node) matchCvA_node.add_parent(teamc_node) skill_dist = [0.15,0.45,0.30,0.10] teama_distribution = DiscreteDistribution(teama_node) index = teama_distribution.generate_index([],[]) teama_distribution[index] = skill_dist teama_node.set_dist(teama_distribution) teamb_distribution = DiscreteDistribution(teamb_node) index = teamb_distribution.generate_index([],[]) teamb_distribution[index] = skill_dist teamb_node.set_dist(teamb_distribution) teamc_distribution = DiscreteDistribution(teamc_node) index = teamc_distribution.generate_index([],[]) teamc_distribution[index] = skill_dist teamc_node.set_dist(teamc_distribution) match_dist = zeros([teama_node.size(), teamb_node.size(), matchAvB_node.size()], dtype=float32) match_dist[0,0,:] = [0.10, 0.10, 0.80] match_dist[0,1,:] = [0.20, 0.60, 0.20] match_dist[0,2,:] = [0.15, 0.75, 0.10] match_dist[0,3,:] = [0.05, 0.90, 0.05] match_dist[1,0,:] = [0.60, 0.20, 0.20] match_dist[1,1,:] = [0.10, 0.10, 0.80] match_dist[1,2,:] = [0.20, 0.60, 0.20] match_dist[1,3,:] = [0.15, 0.75, 0.10] match_dist[2,0,:] = [0.75, 0.15, 0.10] match_dist[2,1,:] = [0.60, 0.20, 0.20] match_dist[2,2,:] = [0.10, 0.10, 0.80] match_dist[2,3,:] = [0.20, 0.60, 0.20] match_dist[3,0,:] = [0.90, 0.05, 0.05] match_dist[3,1,:] = [0.75, 0.15, 0.10] match_dist[3,2,:] = [0.60, 0.20, 0.20] match_dist[3,3,:] = [0.10, 0.10, 0.80] matchAvB_distribution = ConditionalDiscreteDistribution(nodes=[teama_node, teamb_node, matchAvB_node], table=match_dist) matchBvC_distribution = ConditionalDiscreteDistribution(nodes=[teamb_node, teamc_node, matchBvC_node], table=match_dist) matchCvA_distribution = ConditionalDiscreteDistribution(nodes=[teamc_node, teama_node, matchCvA_node], table=match_dist) matchAvB_node.set_dist(matchAvB_distribution) matchBvC_node.set_dist(matchBvC_distribution) matchCvA_node.set_dist(matchCvA_distribution) nodes = [teama_node,teamb_node,teamc_node,matchAvB_node,matchBvC_node,matchCvA_node] return BayesNet(nodes)
def make_exam_net(): H = BayesNode(0, 2, name='H') G = BayesNode(1, 2, name='G') B = BayesNode(2, 2, name='B') O = BayesNode(3, 2, name='O') D = BayesNode(4, 2, name='D') C = BayesNode(5, 2, name='C') H.add_child(B) B.add_parent(H) B.add_parent(G) G.add_child(B) G.add_child(O) O.add_parent(G) O.add_child(D) O.add_child(C) D.add_parent(O) C.add_parent(O) B.add_child(D) D.add_parent(B) nodes = [B, C, D, G, H, O] net = BayesNet(nodes) sci = BayesNode(0, 2, name='sci') inf = BayesNode(1, 2, name='inf') weap = BayesNode(2, 2, name='weap') car = BayesNode(3, 2, name='car') wear = BayesNode(4, 2, name='wear') sci.add_child(weap) weap.add_parent(sci) inf.add_child(weap) weap.add_parent(inf) weap.add_child(car) car.add_parent(weap) weap.add_child(wear) wear.add_parent(weap) nodes1 = [sci, weap, wear, inf, car] net1 = BayesNet(nodes1) B = net.get_node_by_name('B') C = net.get_node_by_name('C') D = net.get_node_by_name('D') G = net.get_node_by_name('G') H = net.get_node_by_name('H') O = net.get_node_by_name('O') H_distribution = DiscreteDistribution(H) index = H_distribution.generate_index([], []) H_distribution[index] = [0.6, 0.4] H.set_dist(H_distribution) G_distribution = DiscreteDistribution(G) index = G_distribution.generate_index([], []) G_distribution[index] = [0.75, 0.25] G.set_dist(G_distribution) dist = zeros([O.size(), C.size()], dtype=float32) dist[0, :] = [0.55, 0.45] dist[1, :] = [0.75, 0.25] C_distribution = ConditionalDiscreteDistribution(nodes=[O, C], table=dist) C.set_dist(C_distribution) dist = zeros([G.size(), O.size()], dtype=float32) dist[0, :] = [0.55, 0.45] dist[1, :] = [0.45, 0.55] O_distribution = ConditionalDiscreteDistribution(nodes=[G, O], table=dist) O.set_dist(O_distribution) dist = zeros([B.size(), O.size(), D.size()], dtype=float32) dist[0, 0, :] = [0.72, 0.28] dist[0, 1, :] = [0.38, 0.62] dist[1, 0, :] = [0.85, 0.15] dist[1, 1, :] = [0.65, 0.35] D_distribution = ConditionalDiscreteDistribution(nodes=[B, O, D], table=dist) D.set_dist(D_distribution) dist = zeros([H.size(), G.size(), B.size()], dtype=float32) dist[0, 0, :] = [0.92, 0.08] dist[0, 1, :] = [0.75, 0.25] dist[1, 0, :] = [0.55, 0.45] dist[1, 1, :] = [0.35, 0.65] B_distribution = ConditionalDiscreteDistribution(nodes=[H, G, B], table=dist) B.set_dist(B_distribution) sci = net1.get_node_by_name('sci') weap = net1.get_node_by_name('weap') wear = net1.get_node_by_name('wear') inf = net1.get_node_by_name('inf') car = net1.get_node_by_name('car') sci_distribution = DiscreteDistribution(sci) index = sci_distribution.generate_index([], []) sci_distribution[index] = [0.2, 0.8] sci.set_dist(sci_distribution) inf_distribution = DiscreteDistribution(inf) index = inf_distribution.generate_index([], []) inf_distribution[index] = [0.4, 0.6] inf.set_dist(inf_distribution) dist = zeros([sci.size(), inf.size(), weap.size()], dtype=float32) dist[0, 0, :] = [0.6, 0.4] dist[0, 1, :] = [0.8, 0.2] dist[1, 0, :] = [0.1, 0.9] dist[1, 1, :] = [0.3, 0.7] weap_distribution = ConditionalDiscreteDistribution(nodes=[sci, inf, weap], table=dist) weap.set_dist(weap_distribution) dist = zeros([weap.size(), wear.size()], dtype=float32) dist[0, :] = [0.88, 0.12] dist[1, :] = [0.15, 0.85] wear_distribution = ConditionalDiscreteDistribution(nodes=[weap, wear], table=dist) wear.set_dist(wear_distribution) dist = zeros([weap.size(), car.size()], dtype=float32) dist[0, :] = [0.75, 0.25] dist[1, :] = [0.55, 0.45] car_distribution = ConditionalDiscreteDistribution(nodes=[weap, car], table=dist) car.set_dist(car_distribution) ## engine = JunctionTreeEngine(net) # engine = EnumerationEngine(net) ## engine.evidence[B] = True # Q = engine.marginal(C)[0] # index = Q.generate_index([True], range(Q.nDims)) # prob = Q[index] # print "Thr ptob of O = T given B = T is ", prob engine1 = EnumerationEngine(net1) engine1.evidence[wear] = False engine1.evidence[weap] = True # engine1.evidence[sci] = False Q = engine1.marginal(car)[0] index = Q.generate_index([True], range(Q.nDims)) prob = Q[index] print prob
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ nodenames = ["A", "B", "C", "AvB", "BvC", "CvA"] A_node = BayesNode(0, 4, name=nodenames[0]) B_node = BayesNode(1, 4, name=nodenames[1]) C_node = BayesNode(2, 4, name=nodenames[2]) AvB_node = BayesNode(3, 3, name=nodenames[3]) BvC_node = BayesNode(4, 3, name=nodenames[4]) CvA_node = BayesNode(5, 3, name=nodenames[5]) A_node.add_child(AvB_node) A_node.add_child(CvA_node) B_node.add_child(AvB_node) B_node.add_child(BvC_node) C_node.add_child(BvC_node) C_node.add_child(CvA_node) AvB_node.add_parent(A_node) AvB_node.add_parent(B_node) BvC_node.add_parent(B_node) BvC_node.add_parent(C_node) CvA_node.add_parent(C_node) CvA_node.add_parent(A_node) A_distribution = DiscreteDistribution(A_node) index = A_distribution.generate_index([], []) A_distribution[index] = [0.15, 0.45, 0.30, 0.10] A_node.set_dist(A_distribution) B_distribution = DiscreteDistribution(B_node) index = B_distribution.generate_index([], []) B_distribution[index] = [0.15, 0.45, 0.30, 0.10] B_node.set_dist(B_distribution) C_distribution = DiscreteDistribution(C_node) index = C_distribution.generate_index([], []) C_distribution[index] = [0.15, 0.45, 0.30, 0.10] C_node.set_dist(C_distribution) dist = zeros([A_node.size(), B_node.size(), AvB_node.size()], dtype=float32) dist[0, 0, :] = [0.10, 0.10, 0.80] dist[0, 1, :] = [0.20, 0.60, 0.20] dist[0, 2, :] = [0.15, 0.75, 0.10] dist[0, 3, :] = [0.05, 0.90, 0.05] dist[1, 0, :] = [0.60, 0.20, 0.20] dist[1, 1, :] = [0.10, 0.10, 0.80] dist[1, 2, :] = [0.20, 0.60, 0.20] dist[1, 3, :] = [0.15, 0.75, 0.10] dist[2, 0, :] = [0.75, 0.15, 0.10] dist[2, 1, :] = [0.60, 0.20, 0.20] dist[2, 2, :] = [0.10, 0.10, 0.80] dist[2, 3, :] = [0.20, 0.60, 0.20] dist[3, 0, :] = [0.90, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.10] dist[3, 2, :] = [0.60, 0.20, 0.20] dist[3, 3, :] = [0.10, 0.10, 0.80] AvB_distribution = ConditionalDiscreteDistribution( nodes=[A_node, B_node, AvB_node], table=dist) AvB_node.set_dist(AvB_distribution) dist = zeros([B_node.size(), C_node.size(), BvC_node.size()], dtype=float32) dist[0, 0, :] = [0.10, 0.10, 0.80] dist[0, 1, :] = [0.20, 0.60, 0.20] dist[0, 2, :] = [0.15, 0.75, 0.10] dist[0, 3, :] = [0.05, 0.90, 0.05] dist[1, 0, :] = [0.60, 0.20, 0.20] dist[1, 1, :] = [0.10, 0.10, 0.80] dist[1, 2, :] = [0.20, 0.60, 0.20] dist[1, 3, :] = [0.15, 0.75, 0.10] dist[2, 0, :] = [0.75, 0.15, 0.10] dist[2, 1, :] = [0.60, 0.20, 0.20] dist[2, 2, :] = [0.10, 0.10, 0.80] dist[2, 3, :] = [0.20, 0.60, 0.20] dist[3, 0, :] = [0.90, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.10] dist[3, 2, :] = [0.60, 0.20, 0.20] dist[3, 3, :] = [0.10, 0.10, 0.80] BvC_distribution = ConditionalDiscreteDistribution( nodes=[B_node, C_node, BvC_node], table=dist) BvC_node.set_dist(BvC_distribution) dist = zeros([C_node.size(), A_node.size(), CvA_node.size()], dtype=float32) dist[0, 0, :] = [0.10, 0.10, 0.80] dist[0, 1, :] = [0.20, 0.60, 0.20] dist[0, 2, :] = [0.15, 0.75, 0.10] dist[0, 3, :] = [0.05, 0.90, 0.05] dist[1, 0, :] = [0.60, 0.20, 0.20] dist[1, 1, :] = [0.10, 0.10, 0.80] dist[1, 2, :] = [0.20, 0.60, 0.20] dist[1, 3, :] = [0.15, 0.75, 0.10] dist[2, 0, :] = [0.75, 0.15, 0.10] dist[2, 1, :] = [0.60, 0.20, 0.20] dist[2, 2, :] = [0.10, 0.10, 0.80] dist[2, 3, :] = [0.20, 0.60, 0.20] dist[3, 0, :] = [0.90, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.10] dist[3, 2, :] = [0.60, 0.20, 0.20] dist[3, 3, :] = [0.10, 0.10, 0.80] CvA_distribution = ConditionalDiscreteDistribution( nodes=[C_node, A_node, CvA_node], table=dist) CvA_node.set_dist(CvA_distribution) nodes = [A_node, B_node, C_node, AvB_node, BvC_node, CvA_node] return BayesNet(nodes) # TODO: fill this out raise NotImplementedError
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ nodes = [] A = BayesNode(0, 4, name="A") B = BayesNode(1, 4, name="B") C = BayesNode(2, 4, name="C") AvB = BayesNode(3, 3, name="AvB") BvC = BayesNode(4, 3, name="BvC") CvA = BayesNode(5, 3, name="CvA") # A -> AvB / A -> CvA A.add_child(AvB) A.add_child(CvA) AvB.add_parent(A) CvA.add_parent(A) # B -> AvB / B -> BvC B.add_child(AvB) B.add_child(BvC) AvB.add_parent(B) BvC.add_parent(B) # C -> BvC / C -> CvA C.add_child(BvC) C.add_child(CvA) BvC.add_parent(C) CvA.add_parent(C) N = [A, B, C] for n in N: dist = DiscreteDistribution(n) idx = dist.generate_index([], []) dist[idx] = [0.15, 0.45, 0.3, 0.1] n.set_dist(dist) N = [AvB, BvC, CvA] NN = [[A, B], [B, C], [C, A]] c = 0 for n in N: dist = zeros([NN[c][0].size(), NN[c][1].size(), n.size()], dtype=float32) for i in range(0, 4): for j in range(0, 4): if ((j - i) == 0): dist[i, j, :] = [0.1, 0.1, 0.8] elif ((j - i) == 1): dist[i, j, :] = [0.2, 0.6, 0.2] elif ((j - i) == 2): dist[i, j, :] = [0.15, 0.75, 0.1] elif ((j - i) == 3): dist[i, j, :] = [0.05, 0.9, 0.05] elif ((j - i) == -1): dist[i, j, :] = [0.6, 0.2, 0.2] elif ((j - i) == -2): dist[i, j, :] = [0.75, 0.15, 0.1] elif ((j - i) == -3): dist[i, j, :] = [0.9, 0.05, 0.05] tmp = ConditionalDiscreteDistribution(nodes=[NN[c][0], NN[c][1], n], table=dist) n.set_dist(tmp) c += 1 nodes = [A, B, C, AvB, BvC, CvA] return BayesNet(nodes)
def get_game_network(): # intial setup for problem described above nodes = [] numberOfNodes = 6 A = 0 B = 1 C = 2 AvB = 3 BvC = 4 CvA = 5 # create the nodes A_node = BayesNode(0, 4, name='A') B_node = BayesNode(1, 4, name='B') C_node = BayesNode(2, 4, name='C') AvB_node = BayesNode(3, 3, name='AvB') BvC_node = BayesNode(4, 3, name='BvC') CvA_node = BayesNode(5, 3, name='CvA') # setup A Node prior distribution A_distribution = DiscreteDistribution(A_node) index = A_distribution.generate_index([], []) A_distribution[index] = [0.15, 0.45, 0.3, 0.1] A_node.set_dist(A_distribution) # setup B Node prior distribution B_distribution = DiscreteDistribution(B_node) index = B_distribution.generate_index([], []) B_distribution[index] = [0.15, 0.45, 0.3, 0.1] B_node.set_dist(B_distribution) # setup C Node prior distribution C_distribution = DiscreteDistribution(C_node) index = C_distribution.generate_index([], []) C_distribution[index] = [0.15, 0.45, 0.3, 0.1] C_node.set_dist(C_distribution) # Probabilty Table for Matchup of Two Teams based on Skill Difference # | skill difference (T2 - T1) | T1 wins | T2 wins | Tie | # |0 |0.10 |0.10 |0.80| # |1 |0.20 |0.60 |0.20| # |2 |0.15 |0.75 |0.10| # |3 |0.05 |0.90 |0.05| # |-1 |0.60 |0.20 |0.20| # |-2 |0.75 |0.15 |0.10| # |-3 |0.90 |0.05 |0.05| # setup AvB Node distribution AvB_distribution = DiscreteDistribution(AvB_node) dist = zeros([A_node.size(), B_node.size(), AvB_node.size()], dtype=float32) for a in range(A_node.size()): for b in range(B_node.size()): if (b - a) == -3: dist[a, b, :] = [0.90, 0.05, 0.05] elif (b - a) == -2: dist[a, b, :] = [0.75, 0.15, 0.10] elif (b - a) == -1: dist[a, b, :] = [0.60, 0.20, 0.20] elif (b - a) == 0: dist[a, b, :] = [0.10, 0.10, 0.80] elif (b - a) == 1: dist[a, b, :] = [0.20, 0.60, 0.20] elif (b - a) == 2: dist[a, b, :] = [0.15, 0.75, 0.10] elif (b - a) == 3: dist[a, b, :] = [0.05, 0.90, 0.05] else: print "ERROR in AvB node setup" AvB_distribution = ConditionalDiscreteDistribution( nodes=[A_node, B_node, AvB_node], table=dist) AvB_node.set_dist(AvB_distribution) # setup BvC Node distribution BvC_distribution = DiscreteDistribution(BvC_node) dist = zeros([B_node.size(), C_node.size(), BvC_node.size()], dtype=float32) for b in range(B_node.size()): for c in range(C_node.size()): if (c - b) == -3: dist[b, c, :] = [0.90, 0.05, 0.05] elif (c - b) == -2: dist[b, c, :] = [0.75, 0.15, 0.10] elif (c - b) == -1: dist[b, c, :] = [0.60, 0.20, 0.20] elif (c - b) == 0: dist[b, c, :] = [0.10, 0.10, 0.80] elif (c - b) == 1: dist[b, c, :] = [0.20, 0.60, 0.20] elif (c - b) == 2: dist[b, c, :] = [0.15, 0.75, 0.10] elif (c - b) == 3: dist[b, c, :] = [0.05, 0.90, 0.05] else: print "ERROR in BvC node setup" BvC_distribution = ConditionalDiscreteDistribution( nodes=[B_node, C_node, BvC_node], table=dist) BvC_node.set_dist(BvC_distribution) # setup CvA Node distribution CvA_distribution = DiscreteDistribution(CvA_node) dist = zeros([C_node.size(), A_node.size(), CvA_node.size()], dtype=float32) for c in range(C_node.size()): for a in range(A_node.size()): if (a - c) == -3: dist[c, a, :] = [0.90, 0.05, 0.05] elif (a - c) == -2: dist[c, a, :] = [0.75, 0.15, 0.10] elif (a - c) == -1: dist[c, a, :] = [0.60, 0.20, 0.20] elif (a - c) == 0: dist[c, a, :] = [0.10, 0.10, 0.80] elif (a - c) == 1: dist[c, a, :] = [0.20, 0.60, 0.20] elif (a - c) == 2: dist[c, a, :] = [0.15, 0.75, 0.10] elif (a - c) == 3: dist[c, a, :] = [0.05, 0.90, 0.05] else: print "ERROR in CvA node setup" CvA_distribution = ConditionalDiscreteDistribution( nodes=[C_node, A_node, CvA_node], table=dist) CvA_node.set_dist(CvA_distribution) # Setup Network (Parents & Children) # A A_node.add_child(AvB_node) A_node.add_child(CvA_node) # B B_node.add_child(AvB_node) B_node.add_child(BvC_node) # C C_node.add_child(BvC_node) C_node.add_child(CvA_node) # AvB AvB_node.add_parent(A_node) AvB_node.add_parent(B_node) # BvC BvC_node.add_parent(B_node) BvC_node.add_parent(C_node) # CvA CvA_node.add_parent(C_node) CvA_node.add_parent(A_node) # add the nodes for setting up network nodes = [A_node, B_node, C_node, AvB_node, BvC_node, CvA_node] return BayesNet(nodes)
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ # TODO: fill this out A = BayesNode(0, 4, name='A') B = BayesNode(1, 4, name='B') C = BayesNode(2, 4, name='C') AB = BayesNode(3, 3, name='AvB') BC = BayesNode(4, 3, name='BvC') CA = BayesNode(5, 3, name='CvA') A.add_child(AB) A.add_child(CA) B.add_child(AB) B.add_child(BC) C.add_child(BC) C.add_child(CA) AB.add_parent(A) AB.add_parent(B) BC.add_parent(B) BC.add_parent(C) CA.add_parent(A) CA.add_parent(C) # Set A distribution A_distribution = DiscreteDistribution(A) index = A_distribution.generate_index([], []) A_distribution[index] = [0.15, 0.45, 0.30, 0.10] A.set_dist(A_distribution) # Set B distribution B_distribution = DiscreteDistribution(B) index = B_distribution.generate_index([], []) B_distribution[index] = [0.15, 0.45, 0.30, 0.10] B.set_dist(B_distribution) # Set C distribution C_distribution = DiscreteDistribution(C) index = C_distribution.generate_index([], []) C_distribution[index] = [0.15, 0.45, 0.30, 0.10] C.set_dist(C_distribution) # Set distribution of AvB given A and B dist = zeros([A.size(), B.size(), AB.size()], dtype=float32) dist[0, 0, :] = [0.10, 0.10, 0.80] dist[0, 1, :] = [0.20, 0.60, 0.20] dist[0, 2, :] = [0.15, 0.75, 0.10] dist[0, 3, :] = [0.05, 0.90, 0.05] dist[1, 0, :] = [0.60, 0.20, 0.20] dist[1, 1, :] = [0.10, 0.10, 0.80] dist[1, 2, :] = [0.20, 0.60, 0.20] dist[1, 3, :] = [0.15, 0.75, 0.10] dist[2, 0, :] = [0.75, 0.15, 0.10] dist[2, 1, :] = [0.60, 0.20, 0.20] dist[2, 2, :] = [0.10, 0.10, 0.80] dist[2, 3, :] = [0.20, 0.60, 0.20] dist[3, 0, :] = [0.90, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.10] dist[3, 2, :] = [0.60, 0.20, 0.20] dist[3, 3, :] = [0.10, 0.10, 0.80] AB_distribution = ConditionalDiscreteDistribution(nodes=[A, B, AB], table=dist) AB.set_dist(AB_distribution) # Set distribution of BvC given B and C dist = zeros([B.size(), C.size(), BC.size()], dtype=float32) dist[0, 0, :] = [0.10, 0.10, 0.80] dist[0, 1, :] = [0.20, 0.60, 0.20] dist[0, 2, :] = [0.15, 0.75, 0.10] dist[0, 3, :] = [0.05, 0.90, 0.05] dist[1, 0, :] = [0.60, 0.20, 0.20] dist[1, 1, :] = [0.10, 0.10, 0.80] dist[1, 2, :] = [0.20, 0.60, 0.20] dist[1, 3, :] = [0.15, 0.75, 0.10] dist[2, 0, :] = [0.75, 0.15, 0.10] dist[2, 1, :] = [0.60, 0.20, 0.20] dist[2, 2, :] = [0.10, 0.10, 0.80] dist[2, 3, :] = [0.20, 0.60, 0.20] dist[3, 0, :] = [0.90, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.10] dist[3, 2, :] = [0.60, 0.20, 0.20] dist[3, 3, :] = [0.10, 0.10, 0.80] BC_distribution = ConditionalDiscreteDistribution(nodes=[B, C, BC], table=dist) BC.set_dist(BC_distribution) # Set distribution of CA giveen C and A dist = zeros([C.size(), A.size(), CA.size()], dtype=float32) dist[0, 0, :] = [0.10, 0.10, 0.80] dist[0, 1, :] = [0.20, 0.60, 0.20] dist[0, 2, :] = [0.15, 0.75, 0.10] dist[0, 3, :] = [0.05, 0.90, 0.05] dist[1, 0, :] = [0.60, 0.20, 0.20] dist[1, 1, :] = [0.10, 0.10, 0.80] dist[1, 2, :] = [0.20, 0.60, 0.20] dist[1, 3, :] = [0.15, 0.75, 0.10] dist[2, 0, :] = [0.75, 0.15, 0.10] dist[2, 1, :] = [0.60, 0.20, 0.20] dist[2, 2, :] = [0.10, 0.10, 0.80] dist[2, 3, :] = [0.20, 0.60, 0.20] dist[3, 0, :] = [0.90, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.10] dist[3, 2, :] = [0.60, 0.20, 0.20] dist[3, 3, :] = [0.10, 0.10, 0.80] CA_distribution = ConditionalDiscreteDistribution(nodes=[C, A, CA], table=dist) CA.set_dist(CA_distribution) nodes = [A, B, C, AB, BC, CA] return BayesNet(nodes)
def get_game_network(): """Create a Bayes Net representation of the game problem. Name the nodes as "A","B","C","AvB","BvC" and "CvA". """ nodes = [] # TODO: fill this out #raise NotImplementedError #Skill level Nodes: each has 0-3 four levels A_node=BayesNode(0, 4, name='A') B_node = BayesNode(1, 4, name='B') C_node = BayesNode(2, 4, name='C') # match nodes: each has win, lose or tie AvB_node=BayesNode(3, 3, name='AvB') BvC_node = BayesNode(4, 3, name='BvC') CvA_node=BayesNode(5, 3, name='CvA') nodes = [A_node, B_node, C_node, AvB_node, BvC_node, CvA_node] #create network A_node.add_child(AvB_node) AvB_node.add_parent(A_node) A_node.add_child(CvA_node) CvA_node.add_parent(A_node) B_node.add_child(AvB_node) AvB_node.add_parent(B_node) B_node.add_child(BvC_node) BvC_node.add_parent(B_node) C_node.add_child(CvA_node) CvA_node.add_parent(C_node) C_node.add_child(BvC_node) BvC_node.add_parent(C_node) #set Probability # each team has 4 level of skills with probability 0.15, 0.45, 0.3 0.1 A_distribution = DiscreteDistribution(A_node) index = A_distribution.generate_index([], []) A_distribution[index] = [0.15, 0.45, 0.30, 0.10] A_node.set_dist(A_distribution) B_distribution = DiscreteDistribution(B_node) index = B_distribution.generate_index([], []) B_distribution[index] = [0.15, 0.45, 0.30, 0.10] B_node.set_dist(B_distribution) C_distribution = DiscreteDistribution(C_node) index = C_distribution.generate_index([], []) C_distribution[index] = [0.15, 0.45, 0.30, 0.10] C_node.set_dist(C_distribution) #Probability of matches #AvB: given skill level A, B, P(AvB|A,B) dist = zeros([A_node.size(), B_node.size(), AvB_node.size()], dtype=float32) dist[0, 0, :] = [0.1, 0.1, 0.8] dist[0, 1, :] = [0.2, 0.6, 0.2] dist[0, 2, :] = [0.15, 0.75, 0.1] dist[0, 3, :] = [0.05, 0.9, 0.05] dist[1, 0, :] = [0.6, 0.2, 0.2] dist[1, 1, :] = [0.1, 0.1, 0.8] dist[1, 2, :] = [0.2, 0.6, 0.2] dist[1, 3, :] = [0.15, 0.75, 0.1] dist[2, 0, :] = [0.75, 0.15, 0.1] dist[2, 1, :] = [0.6, 0.2, 0.2] dist[2, 2, :] = [0.1, 0.1, 0.8] dist[2, 3, :] = [0.2, 0.6, 0.2] dist[3, 0, :] = [0.9, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.1] dist[3, 2, :] = [0.6, 0.2, 0.2] dist[3, 3, :] = [0.1, 0.1, 0.8] AvB_distribution = ConditionalDiscreteDistribution(nodes=[A_node, B_node, AvB_node], table=dist) AvB_node.set_dist(AvB_distribution) # BvC: given skill level B, C, P(BvC|B,C) dist = zeros([B_node.size(), C_node.size(), BvC_node.size()], dtype=float32) dist[0, 0, :] = [0.1, 0.1, 0.8] dist[0, 1, :] = [0.2, 0.6, 0.2] dist[0, 2, :] = [0.15, 0.75, 0.1] dist[0, 3, :] = [0.05, 0.9, 0.05] dist[1, 0, :] = [0.6, 0.2, 0.2] dist[1, 1, :] = [0.1, 0.1, 0.8] dist[1, 2, :] = [0.2, 0.6, 0.2] dist[1, 3, :] = [0.15, 0.75, 0.1] dist[2, 0, :] = [0.75, 0.15, 0.1] dist[2, 1, :] = [0.6, 0.2, 0.2] dist[2, 2, :] = [0.1, 0.1, 0.8] dist[2, 3, :] = [0.2, 0.6, 0.2] dist[3, 0, :] = [0.9, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.1] dist[3, 2, :] = [0.6, 0.2, 0.2] dist[3, 3, :] = [0.1, 0.1, 0.8] BvC_distribution = ConditionalDiscreteDistribution(nodes=[B_node, C_node, BvC_node], table=dist) BvC_node.set_dist(BvC_distribution) # CvA: given skill level C, A, P(CvA|C,A) dist = zeros([C_node.size(), A_node.size(), CvA_node.size()], dtype=float32) dist[0, 0, :] = [0.1, 0.1, 0.8] dist[0, 1, :] = [0.2, 0.6, 0.2] dist[0, 2, :] = [0.15, 0.75, 0.1] dist[0, 3, :] = [0.05, 0.9, 0.05] dist[1, 0, :] = [0.6, 0.2, 0.2] dist[1, 1, :] = [0.1, 0.1, 0.8] dist[1, 2, :] = [0.2, 0.6, 0.2] dist[1, 3, :] = [0.15, 0.75, 0.1] dist[2, 0, :] = [0.75, 0.15, 0.1] dist[2, 1, :] = [0.6, 0.2, 0.2] dist[2, 2, :] = [0.1, 0.1, 0.8] dist[2, 3, :] = [0.2, 0.6, 0.2] dist[3, 0, :] = [0.9, 0.05, 0.05] dist[3, 1, :] = [0.75, 0.15, 0.1] dist[3, 2, :] = [0.6, 0.2, 0.2] dist[3, 3, :] = [0.1, 0.1, 0.8] CvA_distribution = ConditionalDiscreteDistribution(nodes=[C_node, A_node, CvA_node], table=dist) CvA_node.set_dist(CvA_distribution) return BayesNet(nodes)