请大家帮忙看看俺的程序。。
要读取的文件文件名为example-bn.txt
内容:
3
2 2 2
0.5 .05
0.4 0.6
0 1
0.1 0.9
0.2 0.8
0.25 0.75
0.35 0.65其实原本内容为
3
2 2 2.5 .5.4 .6
0 1
.1 .9
.2 .8
.25 .75
.35 .65
为了找出问题,我把那些没有内容的行给删掉了。至于那些小说。我怕读取的时候有问题。暂时改成了正规的形式。按理说.4这种形式也可以的吧。程序本身是个Python程序。请朋友帮忙转换了一下。。发现不能运行。。更改了N多的错误。结果最后还是有Exception in thread "main" java.lang.NullPointerException
at BN.loadCPD(BN.java 108)
at BN.loadCPD(BN.java 117)
at BN.loadCPD(BN.java 117)
at BN.loadBN(BN.java 79)
at BN.main(BN.java 203)
在下面的程序中已经作了标示程序并不长,就是注释太多。原本的Python程序全部换成了注释。
请高手们帮忙看一下,俺实在是心力绞碎了。请高手帮忙看一下问题所在。。相同的帖子:
(http://community.csdn.net/Expert/topic/4685/4685229.xml?temp=.176861)
要读取的文件文件名为example-bn.txt
内容:
3
2 2 2
0.5 .05
0.4 0.6
0 1
0.1 0.9
0.2 0.8
0.25 0.75
0.35 0.65其实原本内容为
3
2 2 2.5 .5.4 .6
0 1
.1 .9
.2 .8
.25 .75
.35 .65
为了找出问题,我把那些没有内容的行给删掉了。至于那些小说。我怕读取的时候有问题。暂时改成了正规的形式。按理说.4这种形式也可以的吧。程序本身是个Python程序。请朋友帮忙转换了一下。。发现不能运行。。更改了N多的错误。结果最后还是有Exception in thread "main" java.lang.NullPointerException
at BN.loadCPD(BN.java 108)
at BN.loadCPD(BN.java 117)
at BN.loadCPD(BN.java 117)
at BN.loadBN(BN.java 79)
at BN.main(BN.java 203)
在下面的程序中已经作了标示程序并不长,就是注释太多。原本的Python程序全部换成了注释。
请高手们帮忙看一下,俺实在是心力绞碎了。请高手帮忙看一下问题所在。。相同的帖子:
(http://community.csdn.net/Expert/topic/4685/4685229.xml?temp=.176861)
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;//
//import math
//from math import *
//from random import *
//
//class BN:
public class BN{
// nNodes = 0
// nValuesPerNode = []
// parents = []
// CondPrNode = []
public int nNodes =0;
int[] nValuesPerNode;
List[] parents;
List[] condPrNode;
//
// def _init_(self):
// nNodes = 0
// nValuesPerNode = []
// parents = []
// CondPrNode = []
//
public BN(){
//nNodes = 0;
//nValuesPerNode = null;
// parents = null;
//condPrNode = null;
}
// # function loading a BN from a file
// def loadBN(self, filename):
public void loadBN(String fileName)throws Exception{
// print "Loading BN from file "+filename
System.out.println("Loading BN from file "+fileName);
// f = file(filename, 'r')
BufferedReader f = new BufferedReader(new FileReader(fileName));
// # 1- Get number of nodes
// self.nNodes = int(f.readline())
this.nNodes = Integer.parseInt( f.readLine() );
// # 2- Get number of possible values for each node
// tmp = f.readline().split()
String[] tmp = f.readLine().split(" ");
nValuesPerNode = new int[tmp.length];
// for i in tmp:
// self.nValuesPerNode.append(int(i))
for(int i=0;i<tmp.length ;i++){
nValuesPerNode[i]=Integer.parseInt(tmp[i]);
}
// # 3- For each node, get:
// for node in range(self.nNodes):
parents = new List[nNodes];
condPrNode = new List[nNodes];
for(int node=0;node<nNodes;node++){
// self.parents.append([])
parents[node]=new ArrayList();
// # 3-a the list of parents and
// tmp = f.readline().split()
tmp = f.readLine().split(" ");
// for i in tmp:
// self.parents[node].append(int(i))
for(int i=0;i<tmp.length ;i++){
parents[node].add(Float.valueOf(tmp[i]));
}
// # 3-b the conditional probability distribution
// #print "node's "+str(node)+" parents: "+str(self.parents)
// self.CondPrNode.append([])
// self.loadCPD(f,node,self.parents[node])
List tls = new ArrayList(parents[node]); //79行在这里-----------------
loadCPD(f,node,tls);
}
// #print "self.CondPrNode="+str(self.CondPrNode)
//
// f.close()
f.close() ;
// print "BN loaded"
System.out.println("BN loaded");
// print
//
}
// # function loading the conditional probability distribution of a node
// # from the next line in file f
// def loadCPD(self, f, node, remainingParents):
private void loadCPD(BufferedReader f,int node,List remainingParents )throws Exception{
// if (len(remainingParents) == 0):
if(remainingParents.size() ==0){
// tmp = f.readline().split()
String[] tmp = f.readLine().split(" ");
// tmp2 = []
List tmp2 = new ArrayList();
// for p in tmp:
// tmp2.append(float(p))
for(int p = 0;p<tmp.length ;p++){
tmp2.add(Float.valueOf(tmp[p]));
}
// self.CondPrNode[node].append(tmp2)
for(int i=0; i<tmp2.size();i++){
condPrNode[node].add(tmp2.get(i));//108行在这里-----------------------
}
}
// else:
else{
// for i in range(self.nValuesPerNode[remainingParents[0]]):
// self.loadCPD(f, node, remainingParents[1:])
for(int i=0;i<remainingParents.size();i++){
remainingParents.remove( 0);
loadCPD(f,node,remainingParents);//117行在这里-----------------------------
}
}
}
//
// # function giving the index in the CPD table of the distribution
// # conditioned on the parents values (in vector pValues)
// def indexCPD(self, node, e):
private int indexCPD(int node,int e){
// pValues = []
List pValues = new ArrayList();
// for p in self.parents[node]:
// pValues.append(e[p])
for(int i=0;i<parents[node].size();i++){
pValues.add(parents[node].get( i));
}
// return self.indexCPDrec(self.parents[node], pValues)
return indexCPDRec(parents[node],pValues);
}
//
// def indexCPDrec(self, remainingParents, pValues):
private int indexCPDRec(List remainingParents,List pValues ){
// last = len(remainingParents)-1
int last = remainingParents.size()-1;
// if (last == -1):
if(last==-1){
return 0;
}
// return 0
// elif (last == 0):
// return pValues[last]
if(last==0){
return ((Integer)pValues.get( last)).intValue() ;
}
// else:
// return pValues[last] + self.nValuesPerNode[last] * self.indexCPDrec(remainingParents[0:last], pValues[0:last])
remainingParents.remove(last);
pValues.remove( last);
return ((Integer)pValues.get(last)).intValue() +nValuesPerNode[last]*indexCPDRec(remainingParents,pValues);
}
// # Sampling algorithm.
// # Returns a vector of values sampled according to the distribution
// # defined by the Bayesian network.
// def Sample(self):
private List sample(){
// S = []
// return S
return new ArrayList();
}
//
// # Enumeration algorithm to infer X given evidence e.
// def EnumerationAsk(self,X,e):
private Object enumerationAsk(Object X,Object e){
// Q = []
List Q = new ArrayList();
//
// return Normalize(Q)
return normalize(Q);
}
//
// def EnumerateAll(self, vars, e):
//
private int enumerateAll(Object vars,Object e){
// return 0.
return 0;
}
//
//
//
//def Normalize(Q):
private Object normalize(Object Q){
// return Q
return Q;
}
//
public static void main(String[] args)throws Exception {
//# 1- Reading the file
//print "Enter name of BN-file to read> ",
System.out.println("Enter name of BN-file to read> ") ;
//filename = str(raw_input())
BufferedReader raw_input = new BufferedReader(new InputStreamReader(System.in));
String fileName = raw_input.readLine() ;
//bn = BN()
BN bn = new BN();
//bn.loadBN(filename)
bn.loadBN(fileName); //203行在这里-----------------------------
//
//# 2- "Interactive command line"
//while (1):
while(true){
// print "> ",
System.out.println(">");
// command = str(raw_input())
String command = raw_input.readLine() ;
// if (command == "quit"):
// break
if(command.equals( "quit"))break;
//
// elif (command == "sample"):
if(command.equals("sample")){
// print "How many samples do you need ?"
System.out.println("How many samples do you need ?");
// n = int(raw_input())
int n = Integer.parseInt( raw_input.readLine());
// for i in range(n):
// print "\t"+str(bn.Sample())
for(int i=0;i<n;i++){
System.out.println("\t");
print(bn.sample() );
}
}
//
// elif (command == "infer"):
if(command.equals( "infer")){
// print "Give a list of evidence variables (nodes) and their values:"
System.out.println("Give a list of evidence variables (nodes) and their values:");
// print "nEvidence"
System.out.println("nEvidence");
// print "e1 value"
System.out.println("e1 value");
// print "e2 value"
System.out.println("e2 value");
// print "..."
System.out.println("...");
// print "en value"
System.out.println("en value");
// e = []
List e = new ArrayList();
// for i in range(int(raw_input())):
// tmp = raw_input().split()
// e.append([int(tmp[0]), int(tmp[1])])
for(int i=0;i<Integer.parseInt( raw_input.readLine());i++){
String [] tmp = raw_input.readLine().split("");
e.add(new int[]{Integer.parseInt(tmp[0]),Integer.parseInt(tmp[1])});
}
// e.sort()
Collections.sort(e);
// print "\te="+str(e)
print(e);
// e2 = []
List e2 = new ArrayList();
// for i in range(bn.nNodes):
// e2.append(-1)
for(int i=0;i<bn.nNodes ;i++){
e.add( new Integer(-1));
}
// for i in e:
// e2[i[0]] = i[1]
for(int i=0;i<e.size() ;i++){
e2.set(((int[])e.get(i))[0],new Integer(((int[])e.get(i))[1]));
}
// print "Give the query variable:",
System.out.println("Give the query variable:" );
// X = int(raw_input())
int x = Integer.parseInt( raw_input.readLine());
// print "\tX="+str(X)
System.out.println("\t x="+x);
//
// print "\t"+str(bn.EnumerationAsk(X,e2))
System.out.println(bn.enumerationAsk(new Integer(x),e2));
}
//
// else:
// print "[Help] available commands:"
System.out.println("[Help] available commands:");
// print " - sample"
System.out.println(" - sample");
// print " - infer"
System.out.println(" - infer");
// print " - quit"
System.out.println("- quit");
// print
}
//
//print "bye"
System.out.println("bye");
}
public static void print(List list){
if(list==null)return ;
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
condPrNode = new List[nNodes];这两句改为:
parents = new ArrayList[nNodes];
condPrNode = new ArrayList[nNodes];
本来以为是这里可能还有问题。。把这里改了还是不行。。不再显示这行有问题这行有问题转而显示
82行有问题 for(int i=0; i<tmp2.size();i++){
condPrNode[node].add(tmp2.get(i));//这行有问题
}
}还有
for(int i=0;i<remainingParents.size();i++){
remainingParents.remove( 0);
loadCPD(f,node,remainingParents);//这行有问题
}
}
}
简单的说调用loadCPD()方法的语句都有问题把所有的List都改成Arraylist。还是那个错误。。这个问题到底在什么地方啊。。我都要晕死了。
请高手们帮忙看一下:)