📄 searchinducer.java
字号:
package fss;import shared.*;import shared.Error;import java.lang.*;
import java.util.*;
/***************************************************************************
Wrapper inducer for search-based induction methods.
Complexity : Training is the number of states searched times the
estimation time per state.
@author Dan Sommerfield (5/21/95) Initial revision (.h,.c)
@author James Louis (8/9/2001) Ported to Java.
***************************************************************************/
abstract public class SearchInducer extends Inducer
{
/** **/
private Random randNumGen;
/** **/
private String dotFileName;
/** **/
protected int[] finalStateInfo;
//protected SearchMethod serachMethod;
/** **/
protected Categorizer categorizer;
/** **/
protected BaseInducer baseInducer;
/** **/
public PerfEstInfo globalInfo;// Global search information. Must be
//set in derived class constructor
/** **/
private String COMPLEX_PENALTY_HELP = "This option specifies a multiplier "
+"which determines how much complexity of a state hurts its fitness.";
/** **/
private String USE_COMPOUND_HELP = "This option specifies whether or not to "
+"combine information about generated states in the search in an attempt "
+"to generate a better state more quickly.";
/** **/
private String DOT_FILE_NAME_HELP = "This option specifies the file that will "
+"receive output for the graphical representation of the search, which "
+"can be displayed by dotty.";
//SearchDispatch<Array<int>, PerfEstInfo> searchDispatch; originally- JL
/** **/
SearchDispatch searchDispatch = new SearchDispatch();
/***************************************************************************
Constructor.
@param description A description of this SearchInducer object.
***************************************************************************/
public SearchInducer(String description)
{
super(description);
baseInducer = null;
dotFileName = description + ".dot";
categorizer = null;
globalInfo = null;
finalStateInfo = null;
}
/***************************************************************************
Constructor.
@param description A description of this SearchInducer object.
@param ind The inducer to be wrapped in this SearchInducer
object.
***************************************************************************/
public SearchInducer(String description, BaseInducer ind)
{
super(description);
baseInducer = ind;
dotFileName = description + ".dot";
categorizer = null;
globalInfo = null;
finalStateInfo = null;
}
/***************************************************************************
Verify that the global info was correctly created by the constructor.
@return TRUE if the global info has been set, FALSE otherwise.
***************************************************************************/
public boolean has_global_info()
{
return has_global_info(false);
}
/***************************************************************************
Verify that the global info was correctly created by the constructor.
@return TRUE if the global info has been set, FALSE otherwise.
@param fatalOnFalse Set to TRUE if an error message display is
requested. FALSE otherwise.
***************************************************************************/
public boolean has_global_info(boolean fatalOnFalse)
{
if(fatalOnFalse && (globalInfo == null))
Error.fatalErr("SearchInducer::has_global_info: global_info should have "
+ "been set by constructor");
return (globalInfo != null);
}
/***************************************************************************
Returns true if this class has a valid Categorizer object. Displays an
error message if there is no categorizer.
@return TRUE if there is a Categorizer, FALSE otherwise.
***************************************************************************
public boolean was_trained(){return was_trained(true);}
/***************************************************************************
Returns true if this class has a valid Categorizer object.
@param fatalOnFalse Set to TRUE if an error message is requested,
FALSE otherwise.
@return TRUE if there is a Categorizer, FALSE otherwise.
***************************************************************************/
public boolean was_trained(boolean fatalOnFalse){
if((fatalOnFalse)&&(categorizer == null))
Error.fatalErr("SearchInducer::was_trained: No categorizer, "
+"Call train() to create categorizer");
return (categorizer != null);
}
/***************************************************************************
Returns the categorizer that the inducer has generated.
@return The Categorizer this Inducer produces.
****************************************************************************/
public Categorizer get_categorizer(){
was_trained(true);
return categorizer;
}
/***************************************************************************
Gives ownership of the generated categorizer to the caller, reverting
the Inducer to untrained state.
@return The Categorizer this Inducer produces.
***************************************************************************/
public Categorizer release_categorizer(){
was_trained(true);
Categorizer retCat = categorizer;
categorizer = null;
return retCat;
}
abstract public PerfEstInfo create_global_info();
abstract public int[] create_initial_info(InstanceList trainingSet);
abstract public PerfEstState create_initial_state(int[] initialInfo, PerfEstInfo gI);
/***************************************************************************
Sets the seed for the random number generator to the specified value.
@param newSeed The specified value for the new seed.
***************************************************************************/
public void set_seed(int newSeed){
// DBG(has_global_info());
globalInfo.seed = newSeed;
}
/***************************************************************************
Returns the seed number for the random number generator.
@return The seed number for the random number generator.
***************************************************************************/
public int get_seed(){
// DBG(has_global_info());
return globalInfo.seed;
}
/***************************************************************************
***************************************************************************/
public void set_use_compound(boolean newCompound){
// DBG(has_global_info());
globalInfo.useCompound = newCompound;
}
/***************************************************************************
***************************************************************************/
public boolean get_use_compound(){
// DBG(has_global_info());
return globalInfo.useCompound;
}
/***************************************************************************
***************************************************************************/
public void set_cmplx_penalty(double penalty){
// DBG(has_global_info());
globalInfo.complexityPenalty = penalty;
}
/***************************************************************************
***************************************************************************/
public double get_cmplx_penalty(){
// DBG(has_global_info());
return globalInfo.complexityPenalty;
}
/***************************************************************************
Returns the final state info. Displays an error message if there is no
final state info.
@return The final state info.
***************************************************************************/
public int[] get_final_state_info(){
if(finalStateInfo == null)
Error.err("SearchInducer::get_final_state_info: finalStateInfo is NULL)");
return finalStateInfo;
}
/***************************************************************************
Default state_to_categorizer function aborts.
***************************************************************************/
public Categorizer state_to_categorizer(int[] stateInfo){
Error.fatalErr("SearchInducer::state_to_categorizer: should never be called");
return null;
}
/***************************************************************************
Returns TRUE if this Inducer can support full testing. The SearchInducer
can support full testing only if the wrapped inducer can.
@return TRUE if this Inducer supports full testing, FALSE otherwise.
***************************************************************************/
public boolean supports_full_testing(){
has_global_info();
return baseInducer.can_cast_to_inducer();
}
/***************************************************************************
***************************************************************************/
public PerfEstDispatch perf_est_dispatch()
{
// DBG(has_global_info());
return globalInfo.perfEst;
}
/***************************************************************************
Read all options from the user.
@param prefix
***************************************************************************/
public Random rand_num_gen() {return randNumGen;}
/***************************************************************************
Read all options from the user.
@param prefix
***************************************************************************/
public void init_rand_num_gen(long seed) {randNumGen.setSeed(seed);}
/***************************************************************************
Find best attributes and create categorizer.
***************************************************************************/
public void train()
{
has_data();
// DBG(OK());
has_global_info();
// give a nice error message if attempting to use test-set as an
// performance estimator
if(globalInfo.perfEst.get_perf_estimator() == PerfEstDispatch.testSet)
Error.fatalErr("SearchInducer::train: performance estimation method may not "
+"be test-set if using Inducer::train()");
// set the inducer
globalInfo.inducer = baseInducer;
if(globalInfo.inducer == null)
Error.fatalErr("SearchInducer::train: must call set_user_options prior to "
+"train if inducer is not set");
if (globalInfo.inducer.can_cast_to_inducer() == false)
Error.fatalErr("SearchInducer::train: wrapped inducer must be derived from "
+"Inducer to use train()");
// test data not available so force the SHOW_TEST_SET_PERF option to
// never for this search.
byte oldOption = searchDispatch.get_show_test_set_perf();
searchDispatch.set_show_test_set_perf(ShowTestSetPerf.showNever);
// use the final state of the search to build a categorizer
search(TS);
categorizer = null;
categorizer = state_to_categorizer(finalStateInfo);
// restore the previous value of SHOW_TEST_SET_PERF
searchDispatch.set_show_test_set_perf(oldOption);
}
/*
protected SearchDispatch<Array<int>, PerfEstInfo> searchDispatch;
/***************************************************************************
Read all options from the user.
@param prefix
***************************************************************************
public void set_user_options(String prefix){
// make sure the global info exists
GetEnv GE = new GetEnv();
has_global_info();
// if no inducer, use an option
if(baseInducer == null)
baseInducer = env_inducer(prefix);
// create a modified prefix without the final underscore (if there is one)
String modPrefix;
if((prefix.length()!= 0)&&(prefix.endsWith("_")))
modPrefix = prefix.substring(0, prefix.length()-2);
else
modPrefix = prefix;
// read dot file name here using modified-prefix.dot as the
// default name of the file.
dotFileName =
GE.get_option_string(prefix + "DOT_FILE", modPrefix + ".dot",
DOT_FILE_NAME_HELP, false);
// set search options
searchDispatch.set_user_options(prefix);
// set performance estimation options
perf_est_dispatch().set_user_options(prefix);
// set extra search-global options
globalInfo.useCompound =
GE.get_option_bool(prefix + "USE_COMPOUND", globalInfo.useCompound,
USE_COMPOUND_HELP, true);
globalInfo.complexityPenalty =
GE.get_option_real(prefix + "CMPLX_PENALTY",
globalInfo.complexityPenalty, COMPLEX_PENALTY_HELP,
true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -