def set_probability_cargo_flight_goodbooster(bayes_net): """Set probability distribution for each node in the power plant system.""" paint_node = bayes_net.get_node_by_name("paint") weather_node = bayes_net.get_node_by_name("weather") oring_node = bayes_net.get_node_by_name("oring") shuttle_node = bayes_net.get_node_by_name("shuttle") booster_node = bayes_net.get_node_by_name("booster") mission_node = bayes_net.get_node_by_name("mission") nodes = [paint_node, weather_node, oring_node, shuttle_node, booster_node, mission_node] # TODO: set the probability distribution for each node #raise NotImplementedError weather_distribution = DiscreteDistribution(weather_node) index = weather_distribution.generate_index([], []) weather_distribution[index] = [0.1, 0.9] weather_node.set_dist(weather_distribution) paint_distribution = DiscreteDistribution(paint_node) index = paint_distribution.generate_index([], []) paint_distribution[index] = [0.01, 0.99] paint_node.set_dist(paint_distribution) # Before new arrival #oring_distribution = DiscreteDistribution(oring_node) #index = oring_distribution.generate_index([], []) #oring_distribution[index] = [0.75, 0.25] #oring_node.set_dist(oring_distribution) # After new arrival oring_distribution = DiscreteDistribution(oring_node) index = oring_distribution.generate_index([], []) oring_distribution[index] = [0.15, 0.85] oring_node.set_dist(oring_distribution) dist = zeros([paint_node.size(), weather_node.size(), shuttle_node.size()], dtype=float32) dist[0, 0, :] = [0.4, 0.6] dist[0, 1, :] = [0.4, 0.6] dist[1, 0, :] = [0.85, 0.15] dist[1, 1, :] = [0.02, 0.98] shuttle_distribution = ConditionalDiscreteDistribution(nodes = [paint_node, weather_node, shuttle_node], table=dist) shuttle_node.set_dist(shuttle_distribution) dist = zeros([weather_node.size(), oring_node.size(), booster_node.size()], dtype=float32) dist[0, 0, :] = [0.95, 0.05] dist[0, 1, :] = [0.15, 0.85] dist[1, 0, :] = [0.95, 0.05] dist[1, 1, :] = [0.05, 0.95] booster_distribution = ConditionalDiscreteDistribution(nodes = [weather_node, oring_node, booster_node], table=dist) booster_node.set_dist(booster_distribution) dist = zeros([shuttle_node.size(), booster_node.size(), mission_node.size()], dtype=float32) dist[0, 0, :] = [0.55, 0.45] dist[0, 1, :] = [0.2, 0.8] dist[1, 0, :] = [0.2, 0.8] dist[1, 1, :] = [0.05, 0.95] mission_distribution = ConditionalDiscreteDistribution(nodes = [shuttle_node, booster_node, mission_node], table=dist) mission_node.set_dist(mission_distribution) return bayes_net
def set_game_probability(bayes_net): A_node = bayes_net.get_node_by_name('A') B_node = bayes_net.get_node_by_name('B') C_node = bayes_net.get_node_by_name('C') AvB_node = bayes_net.get_node_by_name('AvB') BvC_node = bayes_net.get_node_by_name('BvC') CvA_node = bayes_net.get_node_by_name('CvA') # All AvB, BvC, CvA can use the dist below dist = zeros([4, 4, 3], 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] # A 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 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 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) # AvB AvB_node = bayes_net.get_node_by_name('AvB') AvB_dist = ConditionalDiscreteDistribution( nodes=[A_node, B_node, AvB_node], table=dist) AvB_node.set_dist(AvB_dist) # BvC BvC_node = bayes_net.get_node_by_name('BvC') BvC_dist = ConditionalDiscreteDistribution( nodes=[B_node, C_node, BvC_node], table=dist) BvC_node.set_dist(BvC_dist) # CvA CvA_node = bayes_net.get_node_by_name('CvA') CvA_dist = ConditionalDiscreteDistribution( nodes=[C_node, A_node, CvA_node], table=dist) CvA_node.set_dist(CvA_dist) return bayes_net
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") # P(T) = 20% and P(~T) = 80% T_dist = DiscreteDistribution(T_node) index = T_dist.generate_index([], []) T_dist[index] = [0.8, 0.2] T_node.set_dist(T_dist) # P(Fa) = 15% and P(~Fa) = 85% F_A_dist = DiscreteDistribution(F_A_node) index = F_A_dist.generate_index([], []) F_A_dist[index] = [0.85, 0.15] F_A_node.set_dist(F_A_dist) # P(Fg|T) = 80% and P(Fg|~T) 5% dist = zeros([T_node.size(), F_G_node.size()], dtype=float32) dist[0, :] = [0.95, 0.05] dist[1, :] = [0.2, 0.8] F_G_dist = ConditionalDiscreteDistribution(nodes=[T_node, F_G_node], table=dist) F_G_node.set_dist(F_G_dist) # P(G|Fg,T) = 20% and P(G|~Fg,T) = 95% # P(G|Fg,~T) = 80% and P(G|~Fg,~T) = 5% dist = zeros([F_G_node.size(), T_node.size(), G_node.size()], dtype=float32) dist[0, 0, :] = [0.95, 0.05] dist[0, 1, :] = [0.05, 0.95] dist[1, 0, :] = [0.2, 0.8] dist[1, 1, :] = [0.8, 0.2] G_dist = ConditionalDiscreteDistribution(nodes=[F_G_node, T_node, G_node], table=dist) G_node.set_dist(G_dist) # P(A|Fa,G) = 55% and P(A|~Fa,G) = 90% # P(A|Fa,~G) = 45% and P(A|~Fa,~G) = 10% dist = zeros([F_A_node.size(), G_node.size(), A_node.size()], dtype=float32) dist[0, 0, :] = [0.9, 0.1] dist[0, 1, :] = [0.1, 0.9] dist[1, 0, :] = [0.55, 0.45] dist[1, 1, :] = [0.45, 0.55] A_dist = ConditionalDiscreteDistribution(nodes=[F_A_node, G_node, A_node], table=dist) A_node.set_dist(A_dist) nodes = [A_node, F_A_node, G_node, F_G_node, T_node] return bayes_net
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] # TODO: set the probability distribution for each node #Set distribution of node FA FA_distribution = DiscreteDistribution(F_A_node) index = FA_distribution.generate_index([], []) FA_distribution[index] = [0.85, 0.15] F_A_node.set_dist(FA_distribution) #Set distribution of node T T_distribution = DiscreteDistribution(T_node) index = T_distribution.generate_index([], []) T_distribution[index] = [0.8, 0.2] T_node.set_dist(T_distribution) #Set distribution of FG given T dist = zeros([T_node.size(), F_G_node.size()], dtype=float32) dist[0, :] = [0.95, 0.05] dist[1, :] = [0.2, 0.8] FG_distribution = ConditionalDiscreteDistribution(nodes=[T_node, F_G_node], table=dist) F_G_node.set_dist(FG_distribution) #set distribution of A given FA and G dist = zeros([F_A_node.size(), G_node.size(), A_node.size()], dtype=float32) dist[0, 0, :] = [0.9, 0.1] dist[0, 1, :] = [0.1, 0.9] dist[1, 0, :] = [0.55, 0.45] dist[1, 1, :] = [0.45, 0.55] A_distribution = ConditionalDiscreteDistribution( nodes=[F_A_node, G_node, A_node], table=dist) A_node.set_dist(A_distribution) #Set distribution of G given FG and T dist = zeros([F_G_node.size(), T_node.size(), G_node.size()], dtype=float32) dist[0, 0, :] = [0.95, 0.05] dist[0, 1, :] = [0.05, 0.95] dist[1, 0, :] = [0.2, 0.8] dist[1, 1, :] = [0.8, 0.2] G_distribution = ConditionalDiscreteDistribution( nodes=[F_G_node, T_node, G_node], table=dist) G_node.set_dist(G_distribution) return bayes_net
def set_probability(bayes_net): """Set probability distribution for each node in the power plant system.""" A = bayes_net.get_node_by_name("alarm") F_A = 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, F_A, G_node, F_G_node, T_node] # TODO: set the probability distribution for each node # Gauge reads the correct temperature with 95% probability when it is not faulty and 20% probability when it is faulty dist = zeros([T_node.size(), F_G_node.size(), G_node.size()], dtype=float32) dist[1, 1, :] = [0.8, 0.2] dist[1, 0, :] = [0.05, 0.95] dist[0, 1, :] = [0.2, 0.8] dist[0, 0, :] = [0.95, 0.05] G_distribution = ConditionalDiscreteDistribution( nodes=[T_node, F_G_node, G_node], table=dist) G_node.set_dist(G_distribution) # Alarm is faulty 15% of the time F_A_distribution = DiscreteDistribution(F_A) index = F_A_distribution.generate_index([], []) F_A_distribution[index] = [0.85, 0.15] F_A.set_dist(F_A_distribution) # Temperature is hot (call this "true") 20% of the time T_distribution = DiscreteDistribution(T_node) index = T_distribution.generate_index([], []) T_distribution[index] = [0.8, 0.2] T_node.set_dist(T_distribution) # When temp is hot, the gauge is faulty 80% of the time. Otherwise, the gauge is faulty 5% of the time dist = zeros([T_node.size(), F_G_node.size()], dtype=float32) dist[0, :] = [0.95, 0.05] dist[1, :] = [0.2, 0.8] F_G_distribution = ConditionalDiscreteDistribution( nodes=[T_node, F_G_node], table=dist) F_G_node.set_dist(F_G_distribution) # Alarm responds correctly to the gauge 55% of the time when the alarm is faulty, # and it responds correctly to the gauge 90% of the time when the alarm is not faulty. dist = zeros([G_node.size(), F_A.size(), A.size()], dtype=float32) dist[1, 1, :] = [0.45, 0.55] dist[1, 0, :] = [0.1, 0.9] dist[0, 1, :] = [0.55, 0.45] dist[0, 0, :] = [0.9, 0.1] A_distribution = ConditionalDiscreteDistribution(nodes=[G_node, F_A, A], table=dist) A.set_dist(A_distribution) return bayes_net
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] # 1 dist = np.zeros( [F_G_node.size(), T_node.size(), G_node.size()], dtype=np.float32) dist[0, 0, :] = [0.05, 0.95] dist[0, 1, :] = [0.05, 0.95] dist[1, 0, :] = [0.8, 0.2] dist[1, 1, :] = [0.8, 0.2] G_distribution = ConditionalDiscreteDistribution( nodes=[F_G_node, T_node, G_node], table=dist) G_node.set_dist(G_distribution) # 2 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) # 3 T_distribution = DiscreteDistribution(T_node) index = T_distribution.generate_index([], []) T_distribution[index] = [0.8, 0.2] T_node.set_dist(T_distribution) # 4 dist = np.zeros([T_node.size(), F_G_node.size()], dtype=np.float32) dist[0, :] = [0.95, 0.05] dist[1, :] = [0.2, 0.8] F_G_distribution = ConditionalDiscreteDistribution( nodes=[T_node, F_G_node], table=dist) F_G_node.set_dist(F_G_distribution) # 5 dist = np.zeros( [F_A_node.size(), G_node.size(), A_node.size()], dtype=np.float32) dist[0, 0, :] = [0.1, 0.9] dist[0, 1, :] = [0.1, 0.9] dist[1, 0, :] = [0.45, 0.55] dist[1, 1, :] = [0.45, 0.55] A_distribution = ConditionalDiscreteDistribution( nodes=[F_A_node, G_node, A_node], table=dist) A_node.set_dist(A_distribution) return bayes_net
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 set_probability_final_exam(bayes_net): """Set probability distribution for each node in the power plant system.""" netflix_node = bayes_net.get_node_by_name("netflix") exercise_node = bayes_net.get_node_by_name("exercise") assignment_node = bayes_net.get_node_by_name("assignment") club_node = bayes_net.get_node_by_name("club") social_node = bayes_net.get_node_by_name("social") graduate_node = bayes_net.get_node_by_name("graduate") nodes = [netflix_node, exercise_node, assignment_node, club_node, social_node, graduate_node] # TODO: set the probability distribution for each node #raise NotImplementedError netflix_distribution = DiscreteDistribution(netflix_node) index = netflix_distribution.generate_index([], []) netflix_distribution[index] = [0.27, 0.73] netflix_node.set_dist(netflix_distribution) exercise_distribution = DiscreteDistribution(exercise_node) index = exercise_distribution.generate_index([], []) exercise_distribution[index] = [0.8, 0.2] exercise_node.set_dist(exercise_distribution) # Before new arrival club_distribution = DiscreteDistribution(club_node) index = club_distribution.generate_index([], []) club_distribution[index] = [0.64, 0.36] club_node.set_dist(club_distribution) dist = zeros([club_node.size(), social_node.size()], dtype=float32) # Note the order of G_node, A_node dist[0, :] = [0.31, 0.69] # probabilities for A when G is FALSE dist[1, :] = [0.22, 0.78] # probabilities for A given G is TRUE socal_distribution = ConditionalDiscreteDistribution(nodes=[club_node, social_node], table=dist) social_node.set_dist(socal_distribution) dist = zeros([netflix_node.size(), exercise_node.size(), assignment_node.size()], dtype=float32) dist[0, 0, :] = [0.07, 0.93] dist[0, 1, :] = [0.11, 0.89] dist[1, 0, :] = [0.82, 0.18] dist[1, 1, :] = [0.56, 0.44] assignment_distribution = ConditionalDiscreteDistribution(nodes = [netflix_node, exercise_node, assignment_node], table=dist) assignment_node.set_dist(assignment_distribution) dist = zeros([assignment_node.size(), social_node.size(), graduate_node.size()], dtype=float32) dist[0, 0, :] = [0.7, 0.3] dist[0, 1, :] = [0.91, 0.09] dist[1, 0, :] = [0.23, 0.77] dist[1, 1, :] = [0.48, 0.52] graduate_distribution = ConditionalDiscreteDistribution(nodes = [assignment_node, social_node, graduate_node], table=dist) graduate_node.set_dist(graduate_distribution) return bayes_net
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") # temperature distribution temperature_distribution = DiscreteDistribution(T_node) index = temperature_distribution.generate_index([],[]) temperature_distribution[index] = [0.80,0.20] T_node.set_dist(temperature_distribution) # faulty alarm distribution faulty_alarm_distribution = DiscreteDistribution(F_A_node) index = faulty_alarm_distribution.generate_index([],[]) faulty_alarm_distribution[index] = [0.85,0.15] F_A_node.set_dist(faulty_alarm_distribution) # faulty gauge distribution dist = zeros([T_node.size(), F_G_node.size()], dtype=float32) #Note the order of temp, Fg dist[0,:] = [0.95, 0.05] dist[1,:] = [0.20, 0.80] faulty_gauge_distribution = ConditionalDiscreteDistribution(nodes=[T_node,F_G_node], table=dist) F_G_node.set_dist(faulty_gauge_distribution) # guage 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.2, 0.8] dist[1,0,:] = [0.05, 0.95] dist[1,1,:] = [0.80, 0.20] gauge_node_distribution = ConditionalDiscreteDistribution(nodes=[T_node, F_G_node, G_node], table=dist) G_node.set_dist(gauge_node_distribution) # alarm 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] alarm_node_distribution = ConditionalDiscreteDistribution(nodes=[G_node, F_A_node, A_node], table=dist) A_node.set_dist(alarm_node_distribution) nodes = [A_node, F_A_node, G_node, F_G_node, T_node] return bayes_net
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") FA_node = bayes_net.get_node_by_name("faulty alarm") G_node = bayes_net.get_node_by_name("gauge") FG_node = bayes_net.get_node_by_name("faulty gauge") T_node = bayes_net.get_node_by_name("temperature") #Temparature T_distribution = DiscreteDistribution(T_node) index = T_distribution.generate_index([],[]) T_distribution[index] = [0.8, 0.2] T_node.set_dist(T_distribution) #Faulty Alarm FA_distribution = DiscreteDistribution(FA_node) index = FA_distribution.generate_index([],[]) FA_distribution[index] = [0.85, 0.15] FA_node.set_dist(FA_distribution) #Alarm dist = zeros([G_node.size(), FA_node.size() ,A_node.size()], dtype=float32) #Note the order of G_node, A_node 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, FA_node, A_node], table=dist) A_node.set_dist(A_distribution) #Faulty Gauge dist = zeros([T_node.size(), FG_node.size()], dtype=float32) #Note the order of temp, Fg dist[0,:] = [0.95, 0.05] dist[1,:] = [0.20, 0.80] FG_distribution = ConditionalDiscreteDistribution(nodes=[T_node,FG_node], table=dist) FG_node.set_dist(FG_distribution) #Gauge dist = zeros([T_node.size(), FG_node.size() ,G_node.size()], dtype=float32) #Note the order of G_node, A_node 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, FG_node, G_node], table=dist) G_node.set_dist(G_distribution) return bayes_net raise NotImplementedError
def set_skill_diff_distribution(node, dist_nodes): dist = np.zeros([dist_node.size() for dist_node in dist_nodes], dtype=np.float32) # skill diff = 0 dist[0, 0] = dist[1, 1] = dist[2, 2] = dist[3, 3] = [0.1, 0.1, 0.8] # skill diff = 1 dist[0, 1] = dist[1, 2] = dist[2, 3] = [0.2, 0.6, 0.2] dist[1, 0] = dist[2, 1] = dist[3, 2] = [0.6, 0.2, 0.2] # skill diff = 2 dist[0, 2] = dist[1, 3] = [0.15, 0.75, 0.1] dist[2, 0] = dist[3, 1] = [0.75, 0.15, 0.1] # skill diff = 3 dist[0, 3] = [0.05, 0.9, 0.05] dist[3, 0] = [0.9, 0.05, 0.05] distribution = ConditionalDiscreteDistribution(nodes=dist_nodes, table=dist) node.set_dist(distribution) return node
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". """ 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 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(): # 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.""" 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 set_probability(bayes_net): 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] # Use the following Boolean variables in your implementation: # A = alarm sounds # F_A = alarm is faulty # G = gauge reading (high = True, normal = False) # F_G = gauge is faulty # T = actual temperature (high = True, normal = False) # temperature node distribution # 0 Index = False Prob # 1 Index = True Prob # True = Hot Temp = 20% # False = Normal Temp = 80% T_distribution = DiscreteDistribution(T_node) index = T_distribution.generate_index([], []) T_distribution[index] = [0.8, 0.2] T_node.set_dist(T_distribution) # faulty alarm node distribution # 0 Index = False Prob # 1 Index = True Prob # True = alarm is faulty = 15% # False = alarm is not faulty = 85% 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) # faulty gauge node distribution # 0 column -- when temp is normal (T = False) # 1 Index -- when temp is hot (T = True) # True = faulty alarm, False = not faulty alarm # when Temp is normal (T=F), # F_G = false = .95 normal alarm with normal temp # F_G = true = .05 faulty alarm with normal temp # when Temp is hot (T=T), # F_G = false = .20 normal alarm with hot temp # F_G = true = .80 faulty alarm with hot temp 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) # gauge node distribution #Temp: hot= T normal = F #F_G: faulty= T not faulty/normal = F #G: hot = T normal = F # Temp F_G P(G=true|Temp,F_G) # T T 0.20 # T F 0.95 # F T 0.80 # F F 0.05 # Temp = Hot = True # F_G = Faulty = True # True, True, True = .20 dist = zeros([T_node.size(), F_G_node.size(), G_node.size()], dtype=float32) dist[1, 1, :] = [0.80, 0.20] dist[1, 0, :] = [0.05, 0.95] dist[0, 1, :] = [0.20, 0.80] dist[0, 0, :] = [0.95, 0.05] G_distribution = ConditionalDiscreteDistribution( nodes=[T_node, F_G_node, G_node], table=dist) G_node.set_dist(G_distribution) # alarm node distribution #Gauge: hot= T normal = F #F_A: faulty= T not faulty/normal = F #A: sounds = T alarm doesnt sound = F # Gauge F_A P(A=true|G,F_A) # T T 0.55 # T F 0.90 # F T 0.45 # F F 0.10 # Gauge = Hot = True # F_A = Faulty = True # True, True, True = .55 dist = zeros([G_node.size(), F_A_node.size(), A_node.size()], dtype=float32) dist[1, 1, :] = [0.45, 0.55] dist[1, 0, :] = [0.10, 0.90] dist[0, 1, :] = [0.55, 0.45] dist[0, 0, :] = [0.90, 0.10] A_distribution = ConditionalDiscreteDistribution( nodes=[G_node, F_A_node, A_node], table=dist) A_node.set_dist(A_distribution) return bayes_net
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 = [] # 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 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 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 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] # TODO: set the probability distribution for each node # 1. Gauge Probability Distribution # T | Fg | P(G | T, Fg) # F | F | 0.05 # F | T | 0.8 # T | F | 0.95 # T | T | 0.2 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.2, 0.8] dist[1,0,:] = [0.05, 0.95] dist[1,1,:] = [0.8, 0.2] G_distribution = ConditionalDiscreteDistribution(nodes=[T_node, F_G_node, G_node], table=dist) G_node.set_dist(G_distribution) # 2. Faulty Alarm Probability Distribution # P(Fa) = 0.15 Fa_distribution = DiscreteDistribution(F_A_node) index = Fa_distribution.generate_index([], []) Fa_distribution[index] = [0.85, 0.15] F_A_node.set_dist(Fa_distribution) # 3. Actual Temperature Probability Distribution # P(T) = 0.2 T_distribution = DiscreteDistribution(T_node) index = T_distribution.generate_index([],[]) T_distribution[index] = [0.8, 0.2] T_node.set_dist(T_distribution) # 4. Faulty Gauge Probability Distribution # T | P(Fg | T) # F | 0.05 # T | 0.8 dist = zeros([T_node.size(), F_G_node.size()], dtype=float32) dist[0, :] = [.95,0.05] dist[1, :] = [0.2,0.8] Fg_distribution = ConditionalDiscreteDistribution(nodes=[T_node, F_G_node], table=dist) F_G_node.set_dist(Fg_distribution) # 5. Alarm Probability Distribution # Fa | G | P(A | Fa, G) # F | F | 0.1 # F | T | 0.9 # T | F | 0.45 # T | T | 0.55 dist = zeros([F_A_node.size(), G_node.size(), A_node.size()], dtype=float32) dist[0,0,:] = [0.9, 0.1] dist[0,1,:] = [0.1, 0.9] dist[1,0,:] = [0.55, 0.45] dist[1,1,:] = [0.45, 0.55] A_distribution = ConditionalDiscreteDistribution(nodes=[F_A_node, G_node, A_node], table=dist) A_node.set_dist(A_distribution) return bayes_net
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(): """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(): """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 set_probability(bayes_net): """Set probability distribution for each node in the power plant system.""" ''' Probability Tables ====================================== P(FA=true) = 0.15 P(T=true) = 0.2 FG | T | P(G=true given FG, T) --------------------------------- T | T | 0.2 T | F | 0.8 F | T | 0.95 F | F | 0.15 T | P(FG=true given T) -------------------------- T | 0.8 F | 0.05 FA | G | P(A=true given FA, G) -------------------------------- T | T | 0.55 T | F | 0.45 F | T | 0.9 F | F | 0.1 ''' 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] # P(FA) fa_dist = DiscreteDistribution(F_A_node) idx = fa_dist.generate_index([], []) fa_dist[idx] = [0.85, 0.15] F_A_node.set_dist(fa_dist) # P(T) t_dist = DiscreteDistribution(T_node) idx = t_dist.generate_index([], []) t_dist[idx] = [0.8, 0.2] T_node.set_dist(t_dist) # P(FG | T) fg_dist = zeros([T_node.size(), F_G_node.size()], dtype=float32) fg_dist[0, :] = [0.95, 0.05] fg_dist[1, :] = [0.2, 0.8] fg = ConditionalDiscreteDistribution(nodes=[T_node, F_G_node], table=fg_dist) F_G_node.set_dist(fg) # P(G | FG,T) g_dist = zeros( [F_G_node.size(), T_node.size(), G_node.size()], dtype=float32) g_dist[0, 0, :] = [0.95, 0.05] g_dist[0, 1, :] = [0.05, 0.95] g_dist[1, 0, :] = [0.2, 0.8] g_dist[1, 1, :] = [0.8, 0.2] g = ConditionalDiscreteDistribution(nodes=[F_G_node, T_node, G_node], table=g_dist) G_node.set_dist(g) # P(A | FA,G) a_dist = zeros( [F_A_node.size(), G_node.size(), A_node.size()], dtype=float32) a_dist[0, 0, :] = [0.9, 0.1] a_dist[0, 1, :] = [0.1, 0.9] a_dist[1, 0, :] = [0.55, 0.45] a_dist[1, 1, :] = [0.45, 0.55] a = ConditionalDiscreteDistribution(nodes=[F_A_node, G_node, A_node], table=a_dist) A_node.set_dist(a) return bayes_net
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)