📄 geneticsearch.java
字号:
while (bSelected[iSelected2]) {
iSelected2 = (iSelected2 + 1) % getDescendantPopulationSize();
}
if (descendantPopulation[iSelected2].getScore() > descendantPopulation[iSelected].getScore()) {
iSelected = iSelected2;
}
} else {
// find best scoring network in population
while (bSelected[iSelected]) {
iSelected++;
}
double fScore = descendantPopulation[iSelected].getScore();
for (int j = 0; j < getDescendantPopulationSize(); j++) {
if (!bSelected[j] && descendantPopulation[j].getScore() > fScore) {
fScore = descendantPopulation[j].getScore();
iSelected = j;
}
}
}
population[i] = descendantPopulation[iSelected];
bSelected[iSelected] = true;
}
}
// restore current network to best network
copyParentSets(bayesNet, bestBayesNet);
// free up memory
bestBayesNet = null;
} // search
/** copyParentSets copies parent sets of source to dest BayesNet
* @param dest: destination network
* @param source: source network
*/
void copyParentSets(BayesNet dest, BayesNet source) {
int nNodes = source.getNrOfNodes();
// clear parent set first
for (int iNode = 0; iNode < nNodes; iNode++) {
dest.getParentSet(iNode).copy(source.getParentSet(iNode));
}
} // CopyParentSets
/**
* @return number of runs
*/
public int getRuns() {
return m_nRuns;
} // getRuns
/**
* Sets the number of runs
* @param nRuns The number of runs to set
*/
public void setRuns(int nRuns) {
m_nRuns = nRuns;
} // setRuns
/**
* Returns an enumeration describing the available options.
*
* @return an enumeration of all the available options.
*/
public Enumeration listOptions() {
Vector newVector = new Vector(7);
newVector.addElement(new Option("\tPopulation size\n", "L", 1, "-L <integer>"));
newVector.addElement(new Option("\tDescendant population size\n", "A", 1, "-A <integer>"));
newVector.addElement(new Option("\tNumber of runs\n", "U", 1, "-U <integer>"));
newVector.addElement(new Option("\tUse mutation.\n\t(default true)", "M", 0, "-M"));
newVector.addElement(new Option("\tUse cross-over.\n\t(default true)", "C", 0, "-C"));
newVector.addElement(new Option("\tUse tournament selection (true) or maximum subpopulatin (false).\n\t(default false)", "O", 0, "-O"));
newVector.addElement(new Option("\tRandom number seed\n", "R", 1, "-R <seed>"));
Enumeration em = super.listOptions();
while (em.hasMoreElements()) {
newVector.addElement(em.nextElement());
}
return newVector.elements();
} // listOptions
/**
* Parses a given list of options. Valid options are:<p>
*
* For other options see search algorithm.
*
* @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 {
String sPopulationSize = Utils.getOption('L', options);
if (sPopulationSize.length() != 0) {
setPopulationSize(Integer.parseInt(sPopulationSize));
}
String sDescendantPopulationSize = Utils.getOption('A', options);
if (sDescendantPopulationSize.length() != 0) {
setDescendantPopulationSize(Integer.parseInt(sDescendantPopulationSize));
}
String sRuns = Utils.getOption('U', options);
if (sRuns.length() != 0) {
setRuns(Integer.parseInt(sRuns));
}
String sSeed = Utils.getOption('R', options);
if (sSeed.length() != 0) {
setSeed(Integer.parseInt(sSeed));
}
setUseMutation(Utils.getFlag('M', options));
setUseCrossOver(Utils.getFlag('C', options));
setUseTournamentSelection(Utils.getFlag('O', options));
super.setOptions(options);
} // setOptions
/**
* Gets the current settings of the search algorithm.
*
* @return an array of strings suitable for passing to setOptions
*/
public String[] getOptions() {
String[] superOptions = super.getOptions();
String[] options = new String[11 + superOptions.length];
int current = 0;
options[current++] = "-L";
options[current++] = "" + getPopulationSize();
options[current++] = "-A";
options[current++] = "" + getDescendantPopulationSize();
options[current++] = "-U";
options[current++] = "" + getRuns();
options[current++] = "-R";
options[current++] = "" + getSeed();
if (getUseMutation()) {
options[current++] = "-M";
}
if (getUseCrossOver()) {
options[current++] = "-C";
}
if (getUseTournamentSelection()) {
options[current++] = "-O";
}
// insert options from parent class
for (int iOption = 0; iOption < superOptions.length; iOption++) {
options[current++] = superOptions[iOption];
}
// Fill up rest with empty strings, not nulls!
while (current < options.length) {
options[current++] = "";
}
return options;
} // getOptions
/**
* @return whether cross-over is used
*/
public boolean getUseCrossOver() {
return m_bUseCrossOver;
}
/**
* @return whether mutation is used
*/
public boolean getUseMutation() {
return m_bUseMutation;
}
/**
* @return descendant population size
*/
public int getDescendantPopulationSize() {
return m_nDescendantPopulationSize;
}
/**
* @return population size
*/
public int getPopulationSize() {
return m_nPopulationSize;
}
/**
* @param bUseCrossOver: sets whether cross-over is used
*/
public void setUseCrossOver(boolean bUseCrossOver) {
m_bUseCrossOver = bUseCrossOver;
}
/**
* @param bUseMutation: sets whether mutation is used
*/
public void setUseMutation(boolean bUseMutation) {
m_bUseMutation = bUseMutation;
}
/**
* @return whether Tournament Selection (true) or Maximum Sub-Population (false) should be used
*/
public boolean getUseTournamentSelection() {
return m_bUseTournamentSelection;
}
/**
* @param bUseTournamentSelection: sets whether Tournament Selection or Maximum Sub-Population should be used
*/
public void setUseTournamentSelection(boolean bUseTournamentSelection) {
m_bUseTournamentSelection = bUseTournamentSelection;
}
/**
* @param iDescendantPopulationSize: sets descendant population size
*/
public void setDescendantPopulationSize(int iDescendantPopulationSize) {
m_nDescendantPopulationSize = iDescendantPopulationSize;
}
/**
* @param iPopulationSize: sets population size
*/
public void setPopulationSize(int iPopulationSize) {
m_nPopulationSize = iPopulationSize;
}
/**
* @return random number seed
*/
public int getSeed() {
return m_nSeed;
} // getSeed
/**
* Sets the random number seed
* @param nSeed The number of the seed to set
*/
public void setSeed(int nSeed) {
m_nSeed = nSeed;
} // setSeed
/**
* This will return a string describing the classifier.
* @return The string.
*/
public String globalInfo() {
return "This Bayes Network learning algorithm uses genetic search for finding a well scoring " +
"Bayes network structure. Genetic search works by having a population of Bayes network structures " +
"and allow them to mutate and apply cross over to get offspring. The best network structure " +
"found during the process is returned.";
} // globalInfo
/**
* @return a string to describe the Runs option.
*/
public String runsTipText() {
return "Sets the number of generations of Bayes network structure populations.";
} // runsTipText
/**
* @return a string to describe the Seed option.
*/
public String seedTipText() {
return "Initialization value for random number generator." +
" Setting the seed allows replicability of experiments.";
} // seedTipText
/**
* @return a string to describe the Population Size option.
*/
public String populationSizeTipText() {
return "Sets the size of the population of network structures that is selected each generation.";
} // populationSizeTipText
/**
* @return a string to describe the Descendant Population Size option.
*/
public String descendantPopulationSizeTipText() {
return "Sets the size of the population of descendants that is created each generation.";
} // descendantPopulationSizeTipText
/**
* @return a string to describe the Use Mutation option.
*/
public String useMutationTipText() {
return "Determines whether mutation is allowed. Mutation flips a bit in the bit " +
"representation of the network structure. At least one of mutation or cross-over " +
"should be used.";
} // useMutationTipText
/**
* @return a string to describe the Use Cross-Over option.
*/
public String useCrossOverTipText() {
return "Determines whether cross-over is allowed. Cross over combined the bit " +
"representations of network structure by taking a random first k bits of one" +
"and adding the remainder of the other. At least one of mutation or cross-over " +
"should be used.";
} // useCrossOverTipText
/**
* @return a string to describe the Use Tournament Selection option.
*/
public String useTournamentSelectionTipText() {
return "Determines the method of selecting a population. When set to true, tournament " +
"selection is used (pick two at random and the highest is allowed to continue). " +
"When set to false, the top scoring network structures are selected.";
} // useTournamentSelectionTipText
} // GeneticSearch
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -