📄 sparsegridsalgorithm.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Michael Thess
* @version 1.1
*/
package com.prudsys.pdm.Models.Regression.SparseGrids;
import com.prudsys.pdm.Core.ApplicationAttribute;
import com.prudsys.pdm.Core.MiningException;
import com.prudsys.pdm.Core.MiningModel;
import com.prudsys.pdm.Core.MiningSettings;
import com.prudsys.pdm.Input.MiningArrayStream;
import com.prudsys.pdm.Models.Regression.RegressionAlgorithm;
import com.prudsys.pdm.Models.Supervised.Classifier;
import com.prudsys.pdm.Transform.MiningTransformationActivity;
import com.prudsys.pdm.Transform.Special.ReplaceMissingValueStream;
import com.prudsys.pdm.Transform.Special.TreatOutlierValueStream;
/**
* A class representing a Sparse Grids algorithm. Each implementation
* should extend this class and override only the methods:
* {@link #runAlgorithm() runAlgorithm()},
* {@link #getClassifier()() getClassifier()}
*/
public abstract class SparseGridsAlgorithm extends RegressionAlgorithm
{
// -----------------------------------------------------------------------
// Variables declarations
// -----------------------------------------------------------------------
/** Defines the type of SG (tensor product, simplicial). */
protected int sgType = SparseGridsSettings.SG_TENSOR_PRODUCT_BASIS_TYPE;
/** Defines the polynomial degree of SG basis functions. */
protected int basisDegree = 1;
/** Is wavelet basis orthogonal? Otherwise, it is just biorthogonal. */
protected boolean waveletBasis = false;
/** Include coarse level 0 into calculations? */
protected boolean coarseGrid = false;
/** Discretization level. */
protected int level = 1;
/** Array of discretization levels if anisotropic grid is used. */
protected int[] attributeLevels;
/** Array of all Sparse Grids of the model. */
protected SparseGrid[] sparseGrids;
/** Regularization parameter. */
protected double lambda = 1.0;
// -----------------------------------------------------------------------
// Constructor
// -----------------------------------------------------------------------
/**
* Empty constructor.
*/
public SparseGridsAlgorithm()
{
}
// -----------------------------------------------------------------------
// Getter and setter methods
// -----------------------------------------------------------------------
/**
* Creates an instance of the sparse grids settings class that is required
* to run the algorithm. The mining settings are assigned through the
* setMiningSettings method.
*
* @return new instance of the sparse grids settings class of the algorithm
*/
public MiningSettings createMiningSettings() {
return new SparseGridsSettings();
}
/**
* Set SG settings.
*
* @param miningSettings instance of SparseGridsSettings
* @exception IllegalArgumentException mining settings are not of sparse grids type
*/
public final void setMiningSettings( MiningSettings miningSettings ) throws IllegalArgumentException
{
if ( miningSettings instanceof SparseGridsSettings )
{
super.setMiningSettings( miningSettings );
SparseGridsSettings sgs = (SparseGridsSettings) miningSettings;
this.sgType = sgs.getSgType();
this.basisDegree = sgs.getBasisDegree();
this.waveletBasis = sgs.isWaveletBasis();
this.coarseGrid = sgs.isCoarseGrid();
this.level = sgs.getLevel();
this.attributeLevels = sgs.getAttributeLevels();
this.lambda = sgs.getLambda();
}
else
{
throw new IllegalArgumentException( "MiningSettings have to be SparseGridsSettings." );
};
}
/**
* Returns SG classifier.
*
* @return SG classifier
*/
public abstract Classifier getClassifier();
// -----------------------------------------------------------------------
// Run SG algorithm and build mining model
// -----------------------------------------------------------------------
/**
* Runs SG algorithm.
*
* @exception MiningException could not run algorithm
*/
protected abstract void runAlgorithm() throws MiningException;
/**
* Builds mining model by running the SG algorithm internally.
* Before starting the algorithm, missing values are replaced.
*
* @return sparse grids mining model generated by the algorithm
* @exception MiningException could not build model
*/
public MiningModel buildModel() throws MiningException
{
long start = ( new java.util.Date() ).getTime();
// Outlier treatment and missing value replacement:
TreatOutlierValueStream tro = new TreatOutlierValueStream(miningInputStream);
tro.setNumOutliers( ApplicationAttribute.OUTLIER_TREATMENT_METHOD_asExtremeValues );
tro.createTreatOutlierValueTransformationStep();
ReplaceMissingValueStream rep = new ReplaceMissingValueStream(miningInputStream);
miningInputStream = new MiningArrayStream( rep.createReplaceMissingValueStream() );
// Run SG algorithm:
runAlgorithm();
// Build SG model:
SparseGridsMiningModel model = new SparseGridsMiningModel();
model.setMiningSettings( miningSettings );
model.setInputSpec( applicationInputSpecification );
model.setTarget( applicationInputSpecification.getTargetApplicationAttribute() );
// Outlier treatment and missing value in application input specification:
// Create inner transformation object:
MiningTransformationActivity mta = new MiningTransformationActivity();
mta.addTransformationStep( tro.getMts() );
mta.addTransformationStep( rep.getMts() );
model.setMiningTransform( mta );
// Outliers and missing values in application input specification:
applicationInputSpecification.setInputSpecFromInnerTrafo(metaData, tro, rep);
// Set SG parameter:
model.setSgType(sgType);
model.setBasisDegree(basisDegree);
model.setWaveletBasis(waveletBasis);
model.setCoarseGrid(coarseGrid);
model.setLevel(level);
model.setAttributeLevels(attributeLevels);
// Set classifier:
model.setClassifier( getClassifier() );
this.miningModel = model;
long end = ( new java.util.Date() ).getTime();
timeSpentToBuildModel = ( end - start ) / 1000.0;
return model;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -