import functools
import re
import pyeda.inter as pinter
def get_operand(G, F, operand):
name, ind = operand[0], int(operand[1:]) - 1
if name == 'G':
g = G[ind]
if g is None:
raise ValueError("G operand {} is referenced before assignment".format(ind + 1))
return g
return F[ind]
def assign(left, right, operand):
if operand == '+':
return left | right
else:
return left & right
def read_file(path):
with open(path) as inp:
count_G, count_F = (int(x) for x in inp.readline().split())
G = [None for _ in range(count_G)]
F = [pinter.exprvar("F" + str(i)) for i in range(count_F)]
probs = [float(x) for x in inp.readline().split()]
if len(probs) != count_F:
raise ValueError("Wrong number of probabilities!")
for ind, s in enumerate(inp):
s = s.split()
if not re.search(r"^G[\d]+:$", s[0]):
raise TypeError("Not 'G%number%:' expression at string {}".format(ind + 1))
G_ind = int(s[0][1:-1]) - 1
if not 0 <= G_ind < count_G:
raise ValueError("G index {} is out of range".format(G_ind + 1))
if not re.search("^[F,G][\d]+$", s[1]):
raise TypeError("Wrong first operand at string {}".format(ind + 1))
if not re.search("^[F,G][\d]+$", s[3]):
raise TypeError("Wrong second operand at string {}".format(ind + 1))
if s[2] not in ("+", "*"):
raise TypeError("Operator at string {} is not among '+' or '*'".format(ind + 1))
left = get_operand(G, F, s[1])
right = get_operand(G, F, s[3])
G[G_ind] = assign(left, right, s[2])
return F, G, probs
def evaluate_dnf(dnf, values):
conjucts = re.findall("And\(.+?\)", dnf)
conj_indexes = [set(int(x) for x in re.findall('[\d]+', a)) for a in conjucts]
return sum(functools.reduce(lambda x, y: x*y,
[values[i] for i in conj_ind]) for conj_ind in conj_indexes)
if __name__ == "__main__":
path = "input.txt"
F, G, probs = read_file(path)
DNF = G[-1].to_dnf()
print("Final DNF: ", DNF)
print("Probability: ", evaluate_dnf(str(DNF), probs))
事故树.zip_python_事件分析法_事故原因_事故树_故障树
版权申诉
5星 · 超过95%的资源 166 浏览量
2022-07-15
02:20:54
上传
评论 1
收藏 1KB ZIP 举报
alvarocfc
- 粉丝: 105
- 资源: 1万+
最新资源
- 使用 C 语言实现的计算非负整数的阶乘
- 2011-2021最新版本北京大学数字普惠金融指数(PKU-DFIIC).xlsx
- 县域数字乡村指数2018-2020(1).xlsx
- Docker容器配置进阶
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- 李慧琴C语言基础部分.zip
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论3