def __init__(self): print("Starting Tutorial4...") net = pysmile.Network() net.read_file("tutorial1.xdsl") s = net.get_node("Success") i = self.create_node(net, pysmile.NodeType.DECISION, "Invest", "Investment decision", ["Invest", "DoNotInvest"], 160, 240) g = self.create_node(net, pysmile.NodeType.UTILITY, "Gain", "Financial gain", None, 60, 200) net.add_arc(i, g) net.add_arc(s, g) gain_definition = [ 10000, # Utility(Invest=I, Success=S) -5000, # Utility(Invest=I, Success=F) 500, # Utility(Invest=D, Success=S) 500 # Utility(Invest=D, Success=F) ] net.set_node_definition(g, gain_definition) net.write_file("tutorial4.xdsl") print("Tutorial4 complete:" + " Influence diagram written to tutorial4.xdsl.")
def __init__(self): print("Starting Tutorial3...") net = pysmile.Network() # load the network created by Tutorial1 net.read_file("tutorial1.xdsl") for h in net.get_all_nodes(): self.print_node_info(net, h) print("Tutorial3 complete.")
def __init__(self, width, height): net = pysmile.Network() # net.read_file("MineSweeper.xdsl") self.width = width self.height = height self.net = net self.create_fields(net) self.create_field_values(net)
def main(): print("vi kjoerer") feverNet = pysmile.Network() print("lagde nettverk") feverNet.read_file("FluModel.xdsl") feverNet.set_evidence("flu", "True") feverNet.update_beliefs() beliefs = feverNet.get_node_value("HighTemp") for i in range(0, len(beliefs)): print(feverNet.get_outcome_id("HighTemp", i) + "=" + str(beliefs[i]))
def hello_smile(): net = pysmile.Network() #Read XDSL file from disk net.read_file("VentureBN.xdsl") #Set the evidence on the 'Forecast' node to 'Moderate' net.set_evidence("Forecast", "Moderate") #Update the network net.update_beliefs() #Get values(Success, Failure) of Success node beliefs = net.get_node_value("Success") for i in range(0, len(beliefs)): print(net.get_outcome_id("Success", i) + "=" + str(beliefs[i]))
def __init__(self): print("Starting Tutorial5...") net = pysmile.Network() net.read_file("tutorial4.xdsl") print("No evidence set.") net.update_beliefs() self.print_financial_gain(net) print("Setting Forecast=Good.") self.change_evidence_and_update(net, "Forecast", "Good") print("Adding Economy=Up") self.change_evidence_and_update(net, "Economy", "Up") print("Tutorial5 complete.")
def __init__(self): net = pysmile.Network() # load the network created in the class CreatingBN net.read_file("Network.xdsl") #Update the probabilities and display them using the method print_all_posteriors() defined below print("Posteriors with no evidence set:") net.update_beliefs() self.print_all_posteriors(net) #Fix the state of each node using its id print("Setting Engage_in_Learning_Goals =Learned.") self.change_evidence_and_update(net, "Engage_in_Learning_Goals", "Learned") print("MonitorEvaluateProgress=InProgress.") self.change_evidence_and_update(net, "MonitorEvaluateProgress", "InProgress") print("Changing MonitorEvaluateProgress to NotLearned, Keeping Engage_in_Learning_Goals=Learned.") self.change_evidence_and_update(net, "MonitorEvaluateProgress", "NotLearned") print("Removing evidence from Engage_in_Learning_Goals , keeping MonitorEvaluateProgress=NotLearned.") self.change_evidence_and_update(net, "Engage_in_Learning_Goals", None) print("Inferences are successfully done")
def __init__(self): print("Starting Tutorial2...") net = pysmile.Network() # load the network created by Tutorial1 net.read_file("tutorial1.xdsl") print("Posteriors with no evidence set:") net.update_beliefs() self.print_all_posteriors(net) print("Setting Forecast=Good.") self.change_evidence_and_update(net, "Forecast", "Good") print("Adding Economy=Up.") self.change_evidence_and_update(net, "Economy", "Up") print("Changing Forecast to Poor, keeping Economy=Up.") self.change_evidence_and_update(net, "Forecast", "Poor") print("Removing evidence from Economy, keeping Forecast=Poor.") self.change_evidence_and_update(net, "Economy", None) print("Tutorial2 complete.")
def __init__(self): net = pysmile.Network() e = self.create_cpt_node(net, "Economy", "State of the economy", ["Up","Flat","Down"], 160, 40) s = self.create_cpt_node(net, "Success", "Success of the venture", ["Success","Failure"], 60, 40) f = self.create_cpt_node(net, "Forecast", "Expert forecast", ["Good","Moderate","Poor"], 110, 140) #Add arcs linking the nodes (two-way) #1. using node variable #2. using node id net.add_arc(e, s); #e->s net.add_arc(s, f); #s->f net.add_arc("Economy", "Forecast"); #e->f #Initialize the conditional probability tables of the node economyDef = [ 0.2, # P(Economy=U) 0.7, # P(Economy=F) 0.1 # P(Economy=D) ] net.set_node_definition(e, economyDef); successDef =[ 0.3, # P(Success=S|Economy=U) 0.7, # P(Success=F|Economy=U) 0.2, # P(Success=S|Economy=F) 0.8, # P(Success=F|Economy=F) 0.1, # P(Success=S|Economy=D) 0.9 # P(Success=F|Economy=D) ] net.set_node_definition(s, successDef); forecastDef = [ 0.70, # P(Forecast=G|Success=S,Economy=U) 0.29, # P(Forecast=M|Success=S,Economy=U) 0.01, # P(Forecast=P|Success=S,Economy=U) 0.65, # P(Forecast=G|Success=S,Economy=F) 0.30, # P(Forecast=M|Success=S,Economy=F) 0.05, # P(Forecast=P|Success=S,Economy=F) 0.60, # P(Forecast=G|Success=S,Economy=D) 0.30, # P(Forecast=M|Success=S,Economy=D) 0.10, # P(Forecast=P|Success=S,Economy=D) 0.15, # P(Forecast=G|Success=F,Economy=U) 0.30, # P(Forecast=M|Success=F,Economy=U) 0.55, # P(Forecast=P|Success=F,Economy=U) 0.10, # P(Forecast=G|Success=F,Economy=F) 0.30, # P(Forecast=M|Success=F,Economy=F) 0.60, # P(Forecast=P|Success=F,Economy=F) 0.05, # P(Forecast=G|Success=F,Economy=D) 0.25, # P(Forecast=G|Success=F,Economy=D) 0.70 # P(Forecast=G|Success=F,Economy=D) ] net.set_node_definition(f, forecastDef); #Write network to the XDSL file net.write_file("tutorial1.xdsl"); print("Tutorial1 complete: Network written to tutorial1.xdsl")
def __init__(self): # Iniziamo dichiarando la variabile Network. I nodi nel network sono creati # in sequenza chiamando l'helper method create_cpt_node net = pysmile.Network() a = self.create_cpt_node(net, "asia", "Asia", ["Yes", "No"], 60, 40) t = self.create_cpt_node(net, "tub", "Tuberculosis", ["Yes", "No"], 110, 140) e = self.create_cpt_node(net, "either", "Either", ["Yes", "No"], 160, 240) x = self.create_cpt_node(net, "xray", "XRay", ["Yes", "No"], 110, 340) l = self.create_cpt_node(net, "lung", "LungCancer", ["Yes", "No"], 210, 140) s = self.create_cpt_node(net, "smoke", "Smoke", ["Yes", "No"], 260, 40) b = self.create_cpt_node(net, "bronc", "Bronchitis", ["Yes", "No"], 260, 140) d = self.create_cpt_node(net, "dysp", "Dyspnea", ["Yes", "No"], 260, 240) #possiamo ora aggiungere gli archi che collegano i nodi net.add_arc(a, t) net.add_arc(s, l) net.add_arc(s, b) net.add_arc(l, e) net.add_arc(t, e) net.add_arc(b, d) net.add_arc(e, x) net.add_arc(e, d) asiaDef = [ 0.01, # P(asia=Y) 0.99 # P(asia=N) ] # il prossimo step e' la creazione delle tabelle di probabilita' condizionata. Per ogni nodo chiamiamo # net.set_node_definition e in questo modo settiamo le probabilita' net.set_node_definition(a, asiaDef) tubDef = [ 0.05, # P(tub=Y|asia=Y) 0.95, # P(tub=N|asia=Y) 0.01, # P(tub=Y|asia=N) 0.99 # P(tub=N|asia=N) ] net.set_node_definition(t, tubDef) smokeDef = [ 0.5, # P(smoke=Y) 0.5 # P(smoke=N) ] net.set_node_definition(s, smokeDef) lungDef = [ 0.1, # P(lung=Y|smoke=Y) 0.9, # P(lung=N|smoke=Y) 0.01, # P(lung=Y|smoke=N) 0.99 # P(lung=N|smoke=N) ] net.set_node_definition(l, lungDef) eitherDef = [ 1, # P(either=Y|lung=Y,tub=Y) 0, # P(either=N|lung=Y,tub=Y) 1, # P(either=Y|lung=Y,tub=N) 0, # P(either=N|lung=Y,tub=N) 1, # P(either=Y|lung=N,tub=Y) 0, # P(either=N|lung=N,tub=Y) 0, # P(either=Y|lung=N,tub=N) 1 # P(either=N|lung=N,tub=N) ] net.set_node_definition(e, eitherDef) xrayDef = [ 0.98, # P(xray=Y|either=Y) 0.02, # P(xray=N|either=Y) 0.05, # P(xray=Y|either=N) 0.95 # P(xray=N|either=N) ] net.set_node_definition(x, xrayDef) broncDef = [ 0.6, # P(bronc=Y|smoke=Y) 0.4, # P(bronc=N|smoke=Y) 0.3, # P(bronc=Y|smoke=N) 0.7 # P(bronc=N|smoke=N) ] net.set_node_definition(b, broncDef) dyspDef = [ 0.9, # P(dysp=Y|bronc=Y,either=Y) 0.1, # P(dysp=N|bronc=Y,either=Y) 0.8, # P(dysp=Y|bronc=Y,either=N) 0.2, # P(dysp=N|bronc=Y,either=N) 0.7, # P(dysp=Y|bronc=N,either=Y) 0.3, # P(dysp=N|bronc=N,either=Y) 0.1, # P(dysp=Y|bronc=N,either=N) 0.9 # P(dysp=N|bronc=N,either=N) ] net.set_node_definition(d, dyspDef) print("Network created!") print("") #Update delle probabilita' e si procede a mostrarle usando l'helper method print_all_posteriors print("Posteriors with no evidence set:") net.update_beliefs() self.print_all_posteriors(net) print("") print("Set or change evidences") print("") print("Setting Either=Yes, Asia=Yes and Dyspnea=No") self.change_evidence_and_update(net, "either", "Yes") self.change_evidence_and_update(net, "asia", "Yes") self.change_evidence_and_update(net, "dysp", "No") print( "Setting Smoke=No, changing Either to No, keeping Asia=Yes and Dyspnea=No" ) self.change_evidence_and_update(net, "smoke", "No") self.change_evidence_and_update(net, "either", "No") print( "Removing evidence from Asia and Dyspnea, setting Bronchitis=Yes, keeping Either=No and Smoke=No" ) self.change_evidence_and_update(net, "asia", None) self.change_evidence_and_update(net, "dysp", None) self.change_evidence_and_update(net, "bronc", "Yes") print("Nodes information") print("") for h in net.get_all_nodes(): self.print_node_info(net, h)
def initialize_network(): network = pysmile.Network() network.read_file(gv.file_string) return update_cases(network)
def __init__(self, numMines, size, fileName="Minesweeper.xdsl"): self.net = pysmile.Network() self.fileName = fileName self.numMines = numMines self.generate_network(size)
def __init__(self, model_path, threshold=0.95): self.net = pysmile.Network() self.net.read_file(model_path) self.threshold = threshold
def net_setup(): net = pysmile.Network() net.read_file("mma.xdsl") return net
def __init__(self): net = pysmile.Network() #Defining of nodes lof = self.create_cpt_node(net,"Level_of_SelfRegulation", "Level of Self-regulation",["Bad","Intermediate","Good"],200, 40) eng = self.create_fcpt_node(net,"Engage_in_Learning_Goals", "Engage in Learning Goals",["NotLearned","InProgress","Learned"],160, 48) eval = self.create_cpt_node(net,"MonitorEvaluateProgress", "Evaluate and Monitor the progress",["NotLearned","InProgress","Learned"],110, 140) under = self.create_cpt_node(net,"Understand_and_describe_Goals_Plan", "Understand and describe Goals ( Plan )",["NotLearned","InProgress","Learned"],111, 160) mang = self.create_cpt_node(net,"Manage_the_obstacles_Challenges", "Manage the obstacles/Challenges",["NotLearned","InProgress","Learned"],98, 170) res = self.create_cpt_node(net,"ResourceManagement", "Manage the Resources",["NotLearned","InProgress","Learned"],141, 180) htd = self.create_cpt_node(net,"HowToDo", "Execute Tasks (How To Do)",["NotLearned","InProgress","Learned"],181, 98) task = self.create_cpt_node(net,"Identify_and_Descibe_tasks", "Identify and Descibe tasks",["NotLearned","InProgress","Learned"],171, 150) react = self.create_cpt_node(net,"React_to_different_situations_Adaptability", "React to different situations / Adaptability",["NotLearned","InProgress","Learned"],45, 180) #Defining of arcs net.add_arc(eng, lof) net.add_arc(eval, lof) net.add_arc(under, eng) net.add_arc(eng, task) net.add_arc(react, task) net.add_arc(task, res) net.add_arc(res, htd) net.add_arc(htd, mang) net.add_arc(mang, eval) #Defining of Conditional Probability Table (CPT) for each node underCPT = [ 0.091856061, # P(under=N) 0.38162879, # P(under=I) 0.52651515 # P(under=L) ] net.set_node_definition(under, underCPT) reactCPT = [ 0.1174242424242424, # P(react=N) 0.4839015151515152, # P(react=I) 0.3986742424242424 # P(react=L) ] net.set_node_definition(react, reactCPT) evalCPT = [ #--------State=NotLearned 0.27693333, # P(eval=N|mang=N) 0.229771138561716, # P(eval=N|mang=I) 0.1561223732085839, # P(eval=N|mang=L) #--------State=InProgress 0.4649333333333333, # P(eval=I|mang=N) 0.5256631236234798, # P(eval=I|mang=I) 0.6753916982252913, # P(eval=I|mang=L) #--------State=Learned 0.2581333333333333, # P(eval=L|mang=N) 0.2445657378148041, # P(eval=L|mang=I) 0.1684859285661246 # P(eval=L|mang=L) ] net.set_node_definition(eval, evalCPT) engCPT = [ #--------State=NotLearned 0.3482304422842233, # P(eng=N|under=N) 0.324035516777932, # P(eng=N|under=I) 0.2131292517006803, # P(eng=N|under=L) #--------State=InProgress 0.3258847788578882, # P(eng=I|under=N) 0.4216625906096459, # P(eng=I|under=I) 0.4341496598639455, # P(eng=I|under=L) #--------State=Learned 0.3258847788578882, # P(eng=L|under=N) 0.2543018926124221, # P(eng=L|under=I) 0.3527210884353741 # P(eng=L|under=L) ] net.set_node_definition(eng, engCPT) mangCPT = [ #--------State=NotLearned 0.2606293333333333, # P(mang=N|htd=N) 0.2230315643426986, # P(mang=N|htd=I) 0.2613190058755145, # P(mang=N|htd=L) #--------State=InProgress 0.3424213333333334, # P(mang=I|htd=N) 0.3057578910856746, # P(mang=I|htd=I) 0.3693404970622428, # P(mang=I|htd=L) #--------State=Learned 0.3969493333333333, # P(mang=L|htd=N) 0.4712105445716268, # P(mang=L|htd=I) 0.3693404970622428 # P(mang=L|htd=L) ] net.set_node_definition(mang, mangCPT) htdCPT = [ #--------State=NotLearned 0.2606293333333333, # P(htd=N|res=N) 0.2230315643426986, # P(htd=N|res=I) 0.2613190058755145, # P(htd=N|res=L) #--------State=InProgress 0.3424213333333334, # P(htd=I|res=N) 0.3057578910856746, # P(htd=I|res=I) 0.3693404970622428, # P(htd=I|res=L) #--------State=Learned 0.3969493333333333, # P(htd=L|res=N) 0.4712105445716268, # P(htd=L|res=I) 0.3693404970622428 # P(htd=L|res=L) ] net.set_node_definition(htd, htdCPT) taskCPT = [ #--------State=NotLearned 0.3333333333333333, # P(task=N|eng=N|react=N) 0.3062973222530009, # P(task=N|eng=N|react=I) 0.3145333333333334, # P(task=N|eng=N|react=L) 0.3406584362139918, # P(task=N|eng=I|react=N) 0.4130527210884354, # P(task=N|eng=I|react=I) 0.2988014714607808, # P(task=N|eng=I|react=L) 0.3626337448559671, # P(task=N|eng=L|react=N) 0.4161735700197238, # P(task=N|eng=L|react=I) 0.3265743305632502, # P(task=N|eng=L|react=L) #--------State=InProgress 0.3333333333333333, # P(task=I|eng=N|react=N) 0.3468513388734995, # P(task=I|eng=N|react=I) 0.3145333333333334, # P(task=I|eng=N|react=L) 0.3406584362139918, # P(task=I|eng=I|react=N) 0.3333333333333333, # P(task=I|eng=I|react=I) 0.4196629880147146, # P(task=I|eng=I|react=L) 0.3186831275720165, # P(task=I|eng=L|react=N) 0.3274161735700197, # P(task=I|eng=L|react=I) 0.3468513388734996, # P(task=I|eng=L|react=L) #--------State=Learned 0.3333333333333333, # P(task=L|eng=N|react=N) 0.3468513388734995, # P(task=L|eng=N|react=I) 0.3709333333333333, # P(task=L|eng=N|react=L) 0.3186831275720165, # P(task=L|eng=I|react=N) 0.2536139455782313, # P(task=L|eng=I|react=I) 0.2815355405245045, # P(task=L|eng=I|react=L) 0.3186831275720165, # P(task=L|eng=L|react=N) 0.2564102564102564, # P(task=L|eng=L|react=I) 0.3265743305632502 # P(task=L|eng=L|react=L) ] net.set_node_definition(task, taskCPT) resCPT = [ #--------State=NotLearned 0.4023970570784384, # P(res=N|task=N) 0.4053476607911521, # P(res=N|task=I) 0.3549490802238055, # P(res=N|task=L) #--------State=InProgress 0.3505992642696095, # P(res=I|task=N) 0.351336915197788, # P(res=I|task=I) 0.3117175864428612, # P(res=I|task=L) #--------State=Learned 0.2470036786519521, # P(res=L|task=N) 0.2433154240110598, # P(res=L|task=I) 0.3333333333333334, # P(res=L|task=L) ] net.set_node_definition(res, resCPT) lofCPT = [ #--------State=Bad 0.1, # P(lof=B|eval=N|eng=N) 0.3, # P(lof=B|eval=N|eng=I) 0.0, # P(lof=B|eval=N|eng=L) 0.1, # P(lof=B|eval=I|eng=N) 0.1, # P(lof=B|eval=I|eng=I) 0.1, # P(lof=B|eval=I|eng=L) 0.1, # P(lof=B|eval=L|eng=N) 0.1, # P(lof=B|eval=L|eng=I) 0.1, # P(lof=B|eval=L|eng=L) #--------State=Intermediate 0.5, # P(lof=I|eval=N|eng=N) 0.4, # P(lof=I|eval=N|eng=I) 0.6, # P(lof=I|eval=N|eng=L) 0.5, # P(lof=I|eval=I|eng=N) 0.5, # P(lof=I|eval=I|eng=I) 0.6, # P(lof=I|eval=I|eng=L) 0.6, # P(lof=I|eval=L|eng=N) 0.6, # P(lof=I|eval=L|eng=I) 0.6, # P(lof=I|eval=L|eng=L) #--------State=Good 0.4, # P(lof=G|eval=N|eng=N) 0.3, # P(lof=G|eval=N|eng=I) 0.4, # P(lof=G|eval=N|eng=L) 0.4, # P(lof=G|eval=I|eng=N) 0.4, # P(lof=G|eval=I|eng=I) 0.3, # P(lof=G|eval=I|eng=L) 0.3, # P(lof=G|eval=L|eng=N) 0.3, # P(lof=G|eval=L|eng=I) 0.3 # P(lof=G|eval=L|eng=L) ] net.set_node_definition(lof, lofCPT) #save the file "Network.xdsl" in the current directory net.write_file("Network.xdsl") print("Network written to Network.xdsl")