畢設需要研究 Hugin Expert——一款商業貝葉斯網絡軟體,支援 C/C++/Java/.NET API,支援離散/連續節點、表達式與高斯分佈。Java API 文檔缺乏範例,靈機一動查看 .NET API 發現豐富範例,遂將其移植至 Java。
getNodes() 獲取所有節點ContinuousChanceNode:連續節點DiscreteNode:離散節點(含 DiscreteChanceNode、DiscreteDecisionNode)UtilityNode:效用節點InstanceNode:實例節點(暫未使用)new Domain("path.net", new DefaultClassParseListener())Domain 無參建構子package com.ssdut.wangyou;
import COM.hugin.HAPI.*;
public class LoadAndRun {
public static void main(String[] args) throws ExceptionHugin {
// 載入範例網絡
Domain domain = new Domain("C:\\Program Files\\Hugin Expert\\Hugin Lite 7.0\\Samples\\fire.net",
new DefaultClassParseListener());
domain.openLogFile("mylog.log");
domain.compile(); // 編譯網絡
domain.closeLogFile();
// 設定證據並傳播推理
domain.propagate(Domain.H_EQUILIBRIUM_SUM, Domain.H_EVIDENCE_MODE_NORMAL);
// 將 Fire 節點設為狀態 1(發生火災)
DiscreteChanceNode node = (DiscreteChanceNode)domain.getNodeByName("Fire");
node.selectState(1);
// 列印所有節點邊際概率
PrintNodeMarginals(domain);
}
public static void PrintNodeMarginals(Domain domain) throws ExceptionHugin {
NodeList nlist = domain.getNodes();
for (int i = 0; i < nlist.size(); i++) {
DiscreteChanceNode node = (DiscreteChanceNode)nlist.get(i);
int nStates = node.getNumberOfStates();
System.out.println(node.getLabel());
for (int j = 0; j < nStates; j++) {
System.out.println("- " + node.getStateLabel(j) + " " + node.getBelief(j));
}
}
}
}
compile():編譯網絡結構,建立連接關係propagate():執行推理傳播,參數指定平衡模式與證據模式selectState():設定觀測證據(硬證據)getBelief():獲取節點在當前證據下的後驗機率分佈若有研究 Hugin Expert 的同好歡迎留言交流探討。