pgmpy / pgmpy

Python Library for learning (Structure and Parameter), inference (Probabilistic and Causal), and simulations in Bayesian Networks.

Home Page:https://pgmpy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to caculate a cases with loop structure such as feedback in motor drive system

yuxin7020 opened this issue · comments

Subject of the issue

When I use the Bayesian model and belief propagation algorithm to calculate the probability of the motor drive system, if there is a feedback structure, then prompt
"raise nx.NetworkXError("Input is not a valid edge list") from err networkx.exception.NetworkXError: Input is not a valid edge list”.
If I remove the feedback then it works well.

Maybe Bayesian network and its corresponding algorithms are not appropriate choices for feedback structures. So which models and algorithms should I use to solve the modeling and caculating to dealwith with feedback structures?

Thanks a lot for any help!:)

Your environment

  • pgmpy 0.1.23 py311_0 ankurankan
  • python 3.11.5 he1021f5_0 defaults
  • Windows 11 Home 22621.2428

Steps to reproduce

from pgmpy.models import BayesianModel
from pgmpy.factors.discrete import TabularCPD
from pgmpy.inference import VariableElimination
import numpy as np

robort = BayesianModel([('Battery', 'Regulator'),
('LED', 'Photodiode'),
('Photodiode', 'Amplifier'),
('Amplifier', 'ADC'),
('ADC', 'MCU'),
('Regulator', 'MCU'),
('MCU', 'Camera'),
('MCU', 'Drive'),
('Drive', 'Motor'),
('Motor','Photodiode')])

cpd_Battery = TabularCPD('Battery', 3, values=[
[0.95],
[0.04],
[0.01]])

cpd_Regulator = TabularCPD('Regulator', 3, values=[
[0.87, 0.65, 0.09],
[0.11, 0.22, 0.10],
[0.02, 0.13, 0.81]],
evidence=['Battery'],
evidence_card=[3],
)

cpd_LED = TabularCPD('LED', 3, values=[[0.87],
[0.11],
[0.02]])

cpd_Photodiode = TabularCPD('Photodiode', 3, values=[
[0.87, 0.65, 0.09, 0.87, 0.65, 0.09, 0.87, 0.65, 0.09],
[0.11, 0.22, 0.10, 0.11, 0.22, 0.10, 0.11, 0.22, 0.10],
[0.02, 0.13, 0.81, 0.02, 0.13, 0.81, 0.02, 0.13, 0.81]],
evidence=['LED'],
evidence_card=[3],
)

cpd_Amplifier = TabularCPD('Amplifier', 3, values=[
[0.87, 0.65, 0.09],
[0.11, 0.22, 0.10],
[0.02, 0.13, 0.81]],
evidence=['Photodiode'],
evidence_card=[3],
)

cpd_ADC = TabularCPD('ADC', 3, values=[
[0.87, 0.65, 0.09],
[0.11, 0.22, 0.10],
[0.02, 0.13, 0.81]],
evidence=['Amplifier'],
evidence_card=[3],
)

cpd_MCU = TabularCPD('MCU', 3, values=[
[0.87, 0.65, 0.09, 0.87, 0.65, 0.09, 0.87, 0.65, 0.09],
[0.11, 0.22, 0.10, 0.11, 0.22, 0.10, 0.11, 0.22, 0.10],
[0.02, 0.13, 0.81, 0.02, 0.13, 0.81, 0.02, 0.13, 0.81]],
evidence=['Regulator','ADC'],
evidence_card=[3, 3],
)

cpd_Camera = TabularCPD('Camera', 3, values=[
[0.87, 0.65, 0.09],
[0.11, 0.22, 0.10],
[0.02, 0.13, 0.81]],
evidence=['MCU'],
evidence_card=[3],
)
cpd_Drive = TabularCPD('Drive', 3, values=[
[0.90, 0.65, 0.09],
[0.08, 0.22, 0.10],
[0.02, 0.13, 0.81]],
evidence=['MCU'],
evidence_card=[3],
)

cpd_Motor = TabularCPD('Motor', 3, values=[
[0.87, 0.65, 0.09],
[0.11, 0.22, 0.10],
[0.02, 0.13, 0.81]],
evidence=['Drive'],
evidence_card=[3],
)

robort.add_cpds(cpd_Battery,
cpd_Regulator,
cpd_LED,
cpd_Photodiode,
cpd_Amplifier,
cpd_ADC,
cpd_MCU,
cpd_Camera,
cpd_Drive,
cpd_Motor)

robort.check_model()

solver = BeliefPropagation(robort)

result = solver.query(variables=['ADC'])
print("ADC good probability=", result.values[0])
print("ADC degraded probability=", result.values[1])
print("ADC failed probability=", result.values[2])