bayesnetgenerator.java
来自「Weka」· Java 代码 · 共 605 行 · 第 1/2 页
JAVA
605 行
for (int iParent = 0; iParent < m_ParentSets[iAttribute].getCardinalityOfParents(); iParent++) {
// fill array with random nr's
for (int iValue = 1; iValue < nValues; iValue++) {
nPs[iValue] = random.nextInt(1000);
}
// sort
for (int iValue = 1; iValue < nValues; iValue++) {
for (int iValue2 = iValue + 1; iValue2 < nValues; iValue2++) {
if (nPs[iValue2] < nPs[iValue]) {
int h = nPs[iValue2]; nPs[iValue2] = nPs[iValue]; nPs[iValue] = h;
}
}
}
// assign to probability tables
DiscreteEstimatorBayes d = new DiscreteEstimatorBayes(nValues, getEstimator().getAlpha());
for (int iValue = 0; iValue < nValues; iValue++) {
d.addValue(iValue, nPs[iValue + 1] - nPs[iValue]);
}
m_Distributions[iAttribute][iParent] = d;
}
}
} // GenerateRandomDistributions
/**
* GenerateInstances generates random instances sampling from the
* distribution represented by the Bayes network structure. It assumes
* a Bayes network structure has been initialized
*
* @throws Exception if something goes wrong
*/
public void generateInstances () throws Exception {
int [] order = getOrder();
for (int iInstance = 0; iInstance < m_nNrOfInstances; iInstance++) {
int nNrOfAtts = m_Instances.numAttributes();
Instance instance = new Instance(nNrOfAtts);
instance.setDataset(m_Instances);
for (int iAtt2 = 0; iAtt2 < nNrOfAtts; iAtt2++) {
int iAtt = order[iAtt2];
double iCPT = 0;
for (int iParent = 0; iParent < m_ParentSets[iAtt].getNrOfParents(); iParent++) {
int nParent = m_ParentSets[iAtt].getParent(iParent);
iCPT = iCPT * m_Instances.attribute(nParent).numValues() + instance.value(nParent);
}
double fRandom = random.nextInt(1000) / 1000.0f;
int iValue = 0;
while (fRandom > m_Distributions[iAtt][(int) iCPT].getProbability(iValue)) {
fRandom = fRandom - m_Distributions[iAtt][(int) iCPT].getProbability(iValue);
iValue++ ;
}
instance.setValue(iAtt, iValue);
}
m_Instances.add(instance);
}
} // GenerateInstances
/**
* @throws Exception if there's a cycle in the graph
*/
int [] getOrder() throws Exception {
int nNrOfAtts = m_Instances.numAttributes();
int [] order = new int[nNrOfAtts];
boolean [] bDone = new boolean[nNrOfAtts];
for (int iAtt = 0; iAtt < nNrOfAtts; iAtt++) {
int iAtt2 = 0;
boolean allParentsDone = false;
while (!allParentsDone && iAtt2 < nNrOfAtts) {
if (!bDone[iAtt2]) {
allParentsDone = true;
int iParent = 0;
while (allParentsDone && iParent < m_ParentSets[iAtt2].getNrOfParents()) {
allParentsDone = bDone[m_ParentSets[iAtt].getParent(iParent++)];
}
if (allParentsDone && iParent == m_ParentSets[iAtt2].getNrOfParents()) {
order[iAtt] = iAtt2;
bDone[iAtt2] = true;
} else {
iAtt2++;
}
} else {
iAtt2++;
}
}
if (!allParentsDone && iAtt2 == nNrOfAtts) {
throw new Exception("There appears to be a cycle in the graph");
}
}
return order;
} // getOrder
/**
* Returns either the net (if BIF format) or the generated instances
*
* @return either the net or the generated instances
*/
public String toString() {
if (m_bGenerateNet) {
return toXMLBIF03();
}
return m_Instances.toString();
} // toString
boolean m_bGenerateNet = false;
int m_nNrOfNodes = 10;
int m_nNrOfArcs = 10;
int m_nNrOfInstances = 10;
int m_nCardinality = 2;
String m_sBIFFile = "";
void setNrOfNodes(int nNrOfNodes) {m_nNrOfNodes = nNrOfNodes;}
void setNrOfArcs(int nNrOfArcs) {m_nNrOfArcs = nNrOfArcs;}
void setNrOfInstances(int nNrOfInstances) {m_nNrOfInstances = nNrOfInstances;}
void setCardinality(int nCardinality) {m_nCardinality = nCardinality;}
void setSeed(int nSeed) {m_nSeed = nSeed;}
/**
* Returns an enumeration describing the available options
*
* @return an enumeration of all the available options
*/
public Enumeration listOptions() {
Vector newVector = new Vector(6);
newVector.addElement(new Option("\tGenerate network (instead of instances)\n", "B", 0, "-B"));
newVector.addElement(new Option("\tNr of nodes\n", "N", 1, "-N <integer>"));
newVector.addElement(new Option("\tNr of arcs\n", "A", 1, "-A <integer>"));
newVector.addElement(new Option("\tNr of instances\n", "M", 1, "-M <integer>"));
newVector.addElement(new Option("\tCardinality of the variables\n", "C", 1, "-C <integer>"));
newVector.addElement(new Option("\tSeed for random number generator\n", "S", 1, "-S <integer>"));
newVector.addElement(new Option("\tThe BIF file to obtain the structure from.\n", "F", 1, "-F <file>"));
return newVector.elements();
} // listOptions
/**
* Parses a given list of options. <p/>
*
<!-- options-start -->
* Valid options are: <p/>
*
* <pre> -B
* Generate network (instead of instances)
* </pre>
*
* <pre> -N <integer>
* Nr of nodes
* </pre>
*
* <pre> -A <integer>
* Nr of arcs
* </pre>
*
* <pre> -M <integer>
* Nr of instances
* </pre>
*
* <pre> -C <integer>
* Cardinality of the variables
* </pre>
*
* <pre> -S <integer>
* Seed for random number generator
* </pre>
*
* <pre> -F <file>
* The BIF file to obtain the structure from.
* </pre>
*
<!-- options-end -->
*
* @param options the list of options as an array of strings
* @exception Exception if an option is not supported
*/
public void setOptions(String[] options) throws Exception {
m_bGenerateNet = Utils.getFlag('B', options);
String sNrOfNodes = Utils.getOption('N', options);
if (sNrOfNodes.length() != 0) {
setNrOfNodes(Integer.parseInt(sNrOfNodes));
} else {
setNrOfNodes(10);
}
String sNrOfArcs = Utils.getOption('A', options);
if (sNrOfArcs.length() != 0) {
setNrOfArcs(Integer.parseInt(sNrOfArcs));
} else {
setNrOfArcs(10);
}
String sNrOfInstances = Utils.getOption('M', options);
if (sNrOfInstances.length() != 0) {
setNrOfInstances(Integer.parseInt(sNrOfInstances));
} else {
setNrOfInstances(10);
}
String sCardinality = Utils.getOption('C', options);
if (sCardinality.length() != 0) {
setCardinality(Integer.parseInt(sCardinality));
} else {
setCardinality(2);
}
String sSeed = Utils.getOption('S', options);
if (sSeed.length() != 0) {
setSeed(Integer.parseInt(sSeed));
} else {
setSeed(1);
}
String sBIFFile = Utils.getOption('F', options);
if ((sBIFFile != null) && (sBIFFile != "")) {
setBIFFile(sBIFFile);
}
} // setOptions
/**
* Gets the current settings of the classifier.
*
* @return an array of strings suitable for passing to setOptions
*/
public String[] getOptions() {
String[] options = new String[13];
int current = 0;
if (m_bGenerateNet) {
options[current++] = "-B";
}
options[current++] = "-N";
options[current++] = "" + m_nNrOfNodes;
options[current++] = "-A";
options[current++] = "" + m_nNrOfArcs;
options[current++] = "-M";
options[current++] = "" + m_nNrOfInstances;
options[current++] = "-C";
options[current++] = "" + m_nCardinality;
options[current++] = "-S";
options[current++] = "" + m_nSeed;
if (m_sBIFFile.length() != 0) {
options[current++] = "-F";
options[current++] = "" + m_sBIFFile;
}
// Fill up rest with empty strings, not nulls!
while (current < options.length) {
options[current++] = "";
}
return options;
} // getOptions
/**
* prints all the options to stdout
*/
protected static void printOptions(OptionHandler o) {
Enumeration enm = o.listOptions();
System.out.println("Options for " + o.getClass().getName() + ":\n");
while (enm.hasMoreElements()) {
Option option = (Option) enm.nextElement();
System.out.println(option.synopsis());
System.out.println(option.description());
}
}
/**
* Main method
*
* @param args the commandline parameters
*/
static public void main(String [] args) {
BayesNetGenerator b = new BayesNetGenerator();
try {
if ( (args.length == 0) || (Utils.getFlag('h', args)) ) {
printOptions(b);
return;
}
b.setOptions(args);
b.generateRandomNetwork();
if (!b.m_bGenerateNet) { // skip if not required
b.generateInstances();
}
System.out.println(b.toString());
} catch (Exception e) {
e.printStackTrace();
printOptions(b);
}
} // main
} // class BayesNetGenerator
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?