📄 miningalgorithm.java
字号:
paramTypes[0] = int.class;
arguments[0] = new Integer( in );
}
else
if( type.equals( "long" ) )
{
long l = Long.parseLong( value );
paramTypes[0] = long.class;
arguments[0] = new Long( l );
}
else
if( type.equals( "char" ) )
{
char in = (char)Integer.parseInt( value );
paramTypes[0] = char.class;
arguments[0] = new Character( in );
}
else
if( type.equals( "boolean" ) )
{
Boolean b = new Boolean( value );
boolean in = b.booleanValue();
paramTypes[0] = boolean.class;
arguments[0] = new Boolean( in );
}
else
if( type.equals( "float" ) )
{
float in = Float.parseFloat( value );
paramTypes[0] = float.class;
arguments[0] = new Float( in );
}
else
if( type.equals( "double" ) )
{
double in = Double.parseDouble( value );
paramTypes[0] = double.class;
arguments[0] = new Double( in );
}
else
if( type.equals("java.lang.String") )
{
String s = value;
paramTypes[0] = java.lang.String.class;
arguments[0] = s;
}
Method instanceMethod = algorithmClass.getMethod( method, paramTypes);
instanceMethod.invoke( this, arguments );
}
catch( NoSuchMethodException ex)
{
throw new IllegalArgumentException( "No such method: " + ex.toString() );
}
catch( IllegalAccessException ex)
{
throw new IllegalArgumentException( "Illegal access exception." );
}
catch( InvocationTargetException ex)
{
throw new IllegalArgumentException( "Invocation target exception." );
}
catch( NumberFormatException ex)
{
throw new IllegalArgumentException( "Parameter has a wrong type." );
}
}
/**
* Returns list of event listeners.
*
* @return list of event listeners
*/
public EventListenerList getListenerList()
{
return listenerList;
}
/**
* Returns application input specification.
*
* @return application input specification
*/
public ApplicationInputSpecification getApplicationInputSpecification()
{
return applicationInputSpecification;
}
/**
* Sets application input specification for algorithm.
*
* @param applicationInputSpecification new application input specification
* @throws IllegalArgumentException if arguments are incorrects
*/
public void setApplicationInputSpecification( ApplicationInputSpecification applicationInputSpecification ) throws IllegalArgumentException
{
this.applicationInputSpecification = applicationInputSpecification;
}
/**
* Returns mining model.
*
* @return mining model
*/
public MiningModel getMiningModel()
{
return miningModel;
}
/**
* Returns mining automation assignemnt.
*
* @return mining automation assignment
*/
public MiningAutomationAssignment getAutomationAssignment()
{
return miningAutomationAssignment;
}
/**
* Sets new mining automation assignment.
*
* @param miningAutomationAssignment new mining automation assignment
*/
public void setMiningAutomationAssignment(MiningAutomationAssignment miningAutomationAssignment)
{
this.miningAutomationAssignment = miningAutomationAssignment;
}
/**
* Returns time required to build the minig model.
*
* @return time for building the mining model
*/
public double getTimeSpentToBuildModel()
{
return timeSpentToBuildModel;
}
// -----------------------------------------------------------------------
// Verification of all algorithm parameters
// -----------------------------------------------------------------------
/**
* Checks mining algorithm for completeness, otherwise an exception is thrown.
* This means that mining input stream, meta data, mining settings, mining
* algorithm specification and application input specification must be specified.
* In addition, the completeness of the mining settings is checked.
*
* @throws IllegalArgumentException if some algorithm attributes are incorrect
*/
public void verify() throws IllegalArgumentException
{
if( miningInputStream == null )
{
throw new IllegalArgumentException( "Algorithm's mining input stream can't be null." );
}
if (miningInputStream != null ){ //can not process empty dataset. TWang. Apri 4, 2005.
try {
if( miningInputStream.getVectorsNumber() == 0 )
{
throw new IllegalArgumentException( "Algorithm's mining input stream can't be empty." );
}
} catch (MiningException e) {
e.printStackTrace();
}
}//end. TWang.
if( metaData == null )
{
throw new IllegalArgumentException( "Algorithm's metadata data can't be null." );
}
if( miningSettings == null )
{
throw new IllegalArgumentException( "Algorithm's mining settings can't be null." );
}
if( miningAlgorithmSpecification == null )
{
throw new IllegalArgumentException( "Algorithm's mining algorithm specification can't be null." );
}
if( applicationInputSpecification == null )
{
throw new IllegalArgumentException( "Algorithm's application input specification can't be null." );
}
miningSettings.verifySettings();
}
// -----------------------------------------------------------------------
// Run mining algorithm and build mining model
// -----------------------------------------------------------------------
/**
* Runs the mining algorithm.
*
* @throws MiningException if there are some errors in the algorithm
*/
protected abstract void runAlgorithm() throws MiningException;
/**
* Builds mining model by running the algorithm internally.
* (I.e. the runAlgorithm method is usally applied internally.)
*
* @return mining model generated by the algorithm
* @throws MiningException if there are some errors when builds model
*/
public abstract MiningModel buildModel() throws MiningException;
/**
* Builds mining mining model with automatic parameter tuning.
* Intenally the buildModel method is used (usually some times)
* in connection with the automation classes.
*
* @return miningmodel builded with automation strategy
* @throws MiningException if there are some errors when builds model
*
* @see MiningAutomationAssignment
* @see MiningModelAssessment
* @see MiningAutomationCallback
*/
public MiningModel buildModelWithAutomation() throws MiningException
{
// No automation assignment defined => return:
if( miningAutomationAssignment == null )
{
throw new MiningException( "Init MiningAutomationAssignment to build model automatically" );
};
// Start time:
long start = ( new java.util.Date() ).getTime();
// Get assessment and callback objects:
MiningModelAssessment miningModelAssessment = miningAutomationAssignment.getMiningModelAssessment();
MiningAutomationCallback automationCallback = miningAutomationAssignment.getMiningAutomationCallback();
automationCallback.setMiningAlgorithm(this);
// Get target values:
double min = miningAutomationAssignment.getMinAssessment();
double max = miningAutomationAssignment.getMaxAssessment();
int maxIter = miningAutomationAssignment.getMaxIterationNumber();
// Initial settings if retrospective callback object:
if (automationCallback instanceof RetrospectiveCallback) {
((RetrospectiveCallback)automationCallback).setInitialMiningSettings( miningSettings );
((RetrospectiveCallback)automationCallback).setInitialMiningAlgorithmSpecification( miningAlgorithmSpecification );
};
// Initial run of algorithm and assessment:
miningModel = buildModel();
miningModelAssessment.setMiningModel( miningModel );
double assessment = miningModelAssessment.calculateAssessment();
System.out.println("Assessment: " + assessment);
// Iterate until target conditions are satisfied or maximum iterations reached:
int nIter = 1;
while( (assessment < min || assessment > max) && (nIter < maxIter) )
{
// Set current parameters to automation callback:
automationCallback.setCurrentAssessment( assessment );
if (automationCallback instanceof SlidingCallback) {
((SlidingCallback)automationCallback).setCurrentMiningSettings( miningSettings );
((SlidingCallback)automationCallback).setCurrentMiningAlgorithmSpecification( miningAlgorithmSpecification );
};
// Calculate new parameters:
int status = automationCallback.calculateNewMiningParameters();
// No further improvement possible => stop:
if (status == MiningAutomationCallback.CALCULATION_STATUS_NO_IMPROVEMENT)
break;
// Copy new parameters from automation callback and set to algorithm:
miningSettings = automationCallback.getNewMiningSettings();
miningAlgorithmSpecification = automationCallback.getNewMiningAlgorithmSpecification();
setMiningSettings(miningSettings);
setMiningAlgorithmSpecification(miningAlgorithmSpecification);
// Run algorithm with new parameters:
miningModel = buildModel();
// Calculate mining model assesment:
miningModelAssessment.setMiningModel( miningModel );
assessment = miningModelAssessment.calculateAssessment();
nIter = nIter + 1;
};
long end = ( new java.util.Date() ).getTime();
timeSpentToBuildModel = ( end - start ) / 1000.0;
if(!(assessment < min || assessment > max) && (nIter >= maxIter))
fireMiningEvent(new AlgorithmStatusMessage(getAlgorithmLevel(), "AutomationMaximumIterationReached", null));
return miningModel;
}
// -----------------------------------------------------------------------
// Methods of mining listeners
// -----------------------------------------------------------------------
/**
* Adds a listener for MiningEvent events.
*
* @param miningListener a MiningListener that will be notified
* when a miningEvent node will be generated.
*/
public void addMiningListener( MiningListener miningListener )
{
listenerList.add( MiningListener.class, miningListener );
}
/**
* Removes listener of MiningEvent events.
*
* @param miningListener listener to be removed
*/
public void removeMiningListener( MiningListener miningListener )
{
listenerList.remove( MiningListener.class, miningListener );
}
/**
* Notify all listeners that have registered interest for
* notification on this event type. The event instance
* is lazily created using the parameters passed into
* the fire method.
*
* @param description any object to be transmited to listeners
* @see EventListenerList
* @throws MiningException if can't transmit event for listeners
*/
public void fireMiningEvent( Object description ) throws MiningException
{
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
MiningEvent e = null;
// Process the listeners last to first, notifying
// those that are interested in this event
for( int i = listeners.length - 2; i >= 0; i -= 2 )
{
if( listeners[i] == MiningListener.class )
{
// Lazily create the event:
if( e == null )
{
e = new MiningEvent( this, description );
}
((MiningListener)listeners[i+1]).processMiningEvent(e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -