Click here to hide/show the list of notebooks.
  pyAgrum on notebooks   pyAgrum jupyter
☰  InfluenceDiagram 
pyAgrum 0.16.3   
Zipped notebooks   
generation: 2019-10-20 09:16  

Creative Commons License
This pyAgrum's notebook is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

In [1]:
%load_ext autoreload
%autoreload 2

import os

%matplotlib inline
from pylab import *
import matplotlib.pyplot as plt
In [2]:
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
In [3]:
diag=gum.loadID(os.path.join("res","diag.bifxml"))
gnb.showInfluenceDiagram(diag)
---------------------------------------------------------------------------
InvocationException                       Traceback (most recent call last)
<ipython-input-3-5d25df0f0497> in <module>
      1 diag=gum.loadID(os.path.join("res","diag.bifxml"))
----> 2 gnb.showInfluenceDiagram(diag)

~/virtualenvs/devAgrum/lib/python3.7/site-packages/pyAgrum/lib/notebook.py in showInfluenceDiagram(diag, size)
    351     size = gum.config["notebook", "default_graph_size"]
    352 
--> 353   return showDot(_infdiag_todot(diag), size)
    354 
    355 

~/virtualenvs/devAgrum/lib/python3.7/site-packages/pyAgrum/lib/notebook.py in showDot(dotstring, size)
    197   if size is None:
    198     size = gum.config["notebook", "default_graph_size"]
--> 199   return showGraph(_from_dotstring(dotstring), size)
    200 
    201 

~/virtualenvs/devAgrum/lib/python3.7/site-packages/pyAgrum/lib/notebook.py in showGraph(gr, size)
    156     size = gum.config["notebook", "default_graph_size"]
    157 
--> 158   return _reprGraph(gr, size, asString=False)
    159 
    160 

~/virtualenvs/devAgrum/lib/python3.7/site-packages/pyAgrum/lib/notebook.py in _reprGraph(gr, size, asString)
    132   gr.set_size(size)
    133   if gum.config["notebook", "graph_format"] == "svg":
--> 134     gsvg = SVG(__insertLinkedSVGs(gr.create_svg().decode('utf-8')))
    135     if asString:
    136       return gsvg.data

~/virtualenvs/devAgrum/lib/python3.7/site-packages/pydotplus/graphviz.py in <lambda>(f, prog)
   1795             self.__setattr__(
   1796                 'create_' + frmt,
-> 1797                 lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1798             )
   1799             f = self.__dict__['create_' + frmt]

~/virtualenvs/devAgrum/lib/python3.7/site-packages/pydotplus/graphviz.py in create(self, prog, format)
   2030             raise InvocationException(
   2031                 'Program terminated with status: %d. stderr follows: %s' % (
-> 2032                     status, stderr_output))
   2033         elif stderr_output:
   2034             print(stderr_output)

InvocationException: Program terminated with status: 1. stderr follows: Format: "svg" not recognized. Use one of:
In [ ]:
F=diag.addChanceNode(gum.LabelizedVariable("F","F",2))
diag.addArc(diag.idFromName("decisionVar1"),F)

U=diag.addUtilityNode(gum.LabelizedVariable("U","U",1))
diag.addArc(diag.idFromName("decisionVar3"),U)
diag.addArc(diag.idFromName("F"),U)
gnb.showInfluenceDiagram(diag)
In [ ]:
 
In [ ]:
diag.cpt(F)[{'decisionVar1':0}]=[0.9,0.1]
diag.cpt(F)[{'decisionVar1':1}]=[0.3,0.7]

diag.utility(U)[{'F':0,'decisionVar3':0}]=2
diag.utility(U)[{'F':0,'decisionVar3':1}]=4
diag.utility(U)[{'F':1}]=[[0],[5]]
In [ ]:
asia=gum.loadID(os.path.join("res","DecAsia.bifxml"))
gnb.showInfluenceDiagram(asia)
In [ ]:
oil=gum.loadID(os.path.join("res","OilWildcater.bifxml"))
gnb.showInfluenceDiagram(oil)
In [ ]:
ie=gum.InfluenceDiagramInference(oil)
In [ ]:
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
In [ ]:
print(oil.variable(3))
In [ ]:
ie.eraseAllEvidence()
ie.setEvidence({3:0})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
In [ ]:
ie.eraseAllEvidence()
ie.setEvidence({3:1})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
In [ ]:
ie.eraseAllEvidence()
ie.setEvidence({3:2})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
In [ ]:
ie.eraseAllEvidence()
ie.setEvidence({3:[1,0.5,0.1]})
ie.makeInference()
print("Best decision for T : {0}".format(ie.getBestDecisionChoice(oil.idFromName("T"))))
print("Best decision for D : {0}".format(ie.getBestDecisionChoice(oil.idFromName("D"))))
print("For a maxEU : {0}".format(ie.getMEU()))
In [ ]: