aiub, aiedges) # node inspection node_insp = Node("Inspection" + str(ia + 1), parents=None, rvname='discrete') node_insp.set_node_kind(DECISION_NODE) node_insp.set_node_state_name(['no', 'insp1', 'insp2', 'insp3']) # node mi node_mi = create_node_mi("M" + str(ia + 1), [node_insp, node_ai], minum, ailb, aiub, aiedges) # node ui node_ui = Node("Util_Insp" + str(ia + 1), parents=[node_insp], rvname='deterministic') node_ui.set_node_kind(UTILITY_NODE) node_ui.assign_func(inspection_utility) # node repair node_repair = Node("Repair" + str(ia + 1), parents=[node_insp], rvname='discrete') node_repair.set_node_kind(DECISION_NODE) node_repair.set_node_state_name(['no', 'repair']) # node ur node_ur = Node("Util_Repair" + str(ia + 1), parents=[node_repair, node_ai], rvname='deterministic') node_ur.set_node_kind(UTILITY_NODE) def repair_utility(pstate, node_ai=node_ai): repairstate, aistate = pstate acrstate = np.searchsorted(node_ai.bins, acrit + 1e-10) - 1
node_ai = create_node_a("A"+str(ia+1), [aarray[-1], node_k, node_m], ainum, ailb, aiub, aiedges, node_repair=rarray[-1], asmp0=asmp0) aarray.append(node_ai) elif inspyear[ia]: # node ai node_ai = create_node_a("A"+str(ia+1), [aarray[-1], node_k, node_m], ainum, ailb, aiub, aiedges) # node inspection node_insp = Node("Inspection"+str(ia+1), parents=None, rvname='discrete') node_insp.set_node_kind(DECISION_NODE) node_insp.set_node_state_name(['no', 'insp1', 'insp2', 'insp3']) # node mi node_mi = create_node_mi("M"+str(ia+1), [node_insp, node_ai], minum, ailb, aiub, aiedges) # node ui node_ui = Node("Util_Insp"+str(ia+1), parents=[node_insp], rvname='deterministic') node_ui.set_node_kind(UTILITY_NODE) node_ui.assign_func(inspection_utility) # node repair node_repair = Node("Repair"+str(ia+1), parents=[node_insp], rvname='discrete') node_repair.set_node_kind(DECISION_NODE) node_repair.set_node_state_name(['no', 'repair']) # node ur node_ur = Node("Util_Repair"+str(ia+1), parents=[node_repair, node_ai], rvname='deterministic') node_ur.set_node_kind(UTILITY_NODE) def repair_utility(pstate, node_ai=node_ai): repairstate, aistate = pstate acrstate = np.searchsorted(node_ai.bins, acrit)-1 truncrv_ai = node_ai.truncate_rv(aistate, lmd=trunclmd) if aistate<acrstate and repairstate == 0: utilr = 0. elif aistate<acrstate and repairstate == 1: utilr = -Cinit
forecastcpt = np.array([[0.7, 0.2, 0.1], [0.15, 0.25, 0.6]]) forecast.assign_cpt(forecastcpt, statenames=['sunny', 'cloudy', 'rainy']) # node umbrella umbrella.set_node_state_name(['take_umbrella', 'dont_take_umbrella']) # node satisfy def calculate_util(pstate): forecaststate,umbrellastate = pstate if forecaststate==0 and umbrellastate==0: return 20. elif forecaststate==0 and umbrellastate==1: return 100. elif forecaststate==1 and umbrellastate==0: return 70. elif forecaststate==1 and umbrellastate==1: return 0. satisfy.assign_func(calculate_util) # create new network dbnet = Network("BNexample1") # add nodes to network dbnet.add_nodes([weather, forecast, umbrella, satisfy]) # add link: must before define nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network
# node satisfy def calculate_util(pstate): forecaststate, umbrellastate = pstate if forecaststate == 0 and umbrellastate == 0: return 20. elif forecaststate == 0 and umbrellastate == 1: return 100. elif forecaststate == 1 and umbrellastate == 0: return 70. elif forecaststate == 1 and umbrellastate == 1: return 0. satisfy.assign_func(calculate_util) # create new network dbnet = Network("BNexample1") # add nodes to network dbnet.add_nodes([weather, forecast, umbrella, satisfy]) # add link: must before define nodes dbnet.add_link() # define nodes dbnet.define_nodes() # compile the net dbnet.compile_net() # enable autoupdate dbnet.set_autoupdate() # save the network