📄 generatingforwardselection.java
字号:
/*
* YALE - Yet Another Learning Environment
* Copyright (C) 2001-2004
* Simon Fischer, Ralf Klinkenberg, Ingo Mierswa,
* Katharina Morik, Oliver Ritthoff
* Artificial Intelligence Unit
* Computer Science Department
* University of Dortmund
* 44221 Dortmund, Germany
* email: yale-team@lists.sourceforge.net
* web: http://yale.cs.uni-dortmund.de/
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package edu.udo.cs.yale.operator.features;
import edu.udo.cs.yale.operator.parameter.*;
import edu.udo.cs.yale.operator.Value;
import edu.udo.cs.yale.operator.OperatorException;
import edu.udo.cs.yale.operator.UserError;
import edu.udo.cs.yale.operator.IOObject;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.AttributeWeightedExampleSet;
import edu.udo.cs.yale.generator.*;
import edu.udo.cs.yale.tools.LogService;
import edu.udo.cs.yale.tools.Tools;
import edu.udo.cs.yale.operator.performance.PerformanceVector;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
/** This operator is a kind of nested forward selection and thus is (in contrast to
* a genetic algorithm) a directed search.
* <ol>
* <li>use forward selection in order to determine the best attributes</li>
* <li>Create a new attribute by multiplying any of the original attributes with any
* of the attributes selected by the forward selection in the last turn</li>
* <li>loop as long as performance increases</li>
* </ol>
*
* @yale.xmlclass GeneratingForwardSelection
* @author simon
* @version $Id: GeneratingForwardSelection.java,v 2.11 2004/09/14 08:39:05 ingomierswa Exp $
*/
public class GeneratingForwardSelection extends FeatureSelectionOperator {
private int degree;
/** List of AttributeReferences. */
private Attribute[] originalAttributes;
private ExampleSet bestIndividual;
private List useGenerators;
private int newAttributeStart;
private int turn;
public GeneratingForwardSelection() {
addValue(new Value("turn", "The number of the current turn.") {
public double getValue() {
return turn;
}
});
}
int getDefaultDirection() {
return FORWARD_SELECTION;
}
public IOObject[] apply() throws OperatorException {
useGenerators = new LinkedList();
if (getParameterAsBoolean("reciprocal_value")) {
FeatureGenerator g = new ReciprocalValueGenerator();
useGenerators.add(g);
}
if (getParameterAsBoolean("function_characteristica")) {
FeatureGenerator g = new FunctionCharacteristicaGenerator();
useGenerators.add(g);
}
if (getParameterAsBoolean("use_plus")) {
FeatureGenerator g = new BasicArithmeticOperationGenerator(0);
useGenerators.add(g);
}
if (getParameterAsBoolean("use_diff")) {
FeatureGenerator g = new BasicArithmeticOperationGenerator(1);
useGenerators.add(g);
}
if (getParameterAsBoolean("use_mult")) {
FeatureGenerator g = new BasicArithmeticOperationGenerator(2);
useGenerators.add(g);
}
if (getParameterAsBoolean("use_div")) {
FeatureGenerator g = new BasicArithmeticOperationGenerator(3);
useGenerators.add(g);
}
if (useGenerators.size()==0) {
LogService.logMessage("No FeatureGenerators specified for " + getName() + ".", LogService.WARNING);
}
return super.apply();
}
public Population createInitialPopulation(AttributeWeightedExampleSet es) {
newAttributeStart = 0;
turn = 0;
// remember the original attributes
originalAttributes = new Attribute[es.getNumberOfAttributes()];
for (int i = 0; i < originalAttributes.length; i++) {
originalAttributes[i] = es.getAttribute(i);
}
return super.createInitialPopulation(es);
}
public boolean solutionGoodEnough(Population pop) throws OperatorException {
if (super.solutionGoodEnough(pop)) {
// The forward selection is finished
AttributeWeightedExampleSet fsBest = (AttributeWeightedExampleSet)pop.lastBest().clone();
// Check whether the performance was improved by this turn
if ((bestIndividual == null) ||
(((PerformanceVector)fsBest.getUserData("performance")).compareTo(((PerformanceVector)bestIndividual.getUserData("performance"))) > 0)) {
turn++;
bestIndividual = (ExampleSet)fsBest.clone();
fsBest = new AttributeWeightedExampleSet(fsBest.createCleanExampleSet());
LogService.logMessage(getName() +": "+Tools.ordinalNumber(turn) + " turn's FS result: " +
fsBest, LogService.TASK);
// and generate all new attributes using the generators
List generators = new LinkedList();
ListIterator i = useGenerators.listIterator();
// for all generator types
while (i.hasNext()) {
FeatureGenerator fg = (FeatureGenerator)i.next();
// for all new arguments
for (int a = newAttributeStart; a < fsBest.getNumberOfAttributes(); a++) {
// for all original attributes
for (int o = 0; o < originalAttributes.length; o++) {
FeatureGenerator g = (FeatureGenerator)fg.newInstance();
g.setArguments(new Attribute[] { originalAttributes[o], fsBest.getAttribute(a) });
generators.add(g);
}
}
}
LogService.logMessage(getName() + ": generating " + generators.size() + " new attributes.",
LogService.OPERATOR);
newAttributeStart = fsBest.getNumberOfAttributes();
// generate the new attributes
try {
List attributes = FeatureGenerator.generateAll(fsBest.getExampleTable(),
generators);
Iterator j = attributes.iterator();
while (j.hasNext()) {
Attribute attr = (Attribute)j.next();
fsBest.addAttribute(attr);
fsBest.setAttributeUsed(attr, false);
}
} catch (GenerationException e) {
throw new UserError(this, e, 108, e.getMessage());
}
// clear the population, add the generated set
pop.clear();
pop.add(fsBest);
return false;
} else {
// otherwise quit
return true;
}
}
// go on with the forward selection
return false;
}
public List getParameterTypes() {
List types = super.getParameterTypes();
types.add(new ParameterTypeBoolean("reciprocal_value", "Generate reciprocal values.", true));
types.add(new ParameterTypeBoolean("function_characteristica", "Generate function characteristica (for C9).", false));
types.add(new ParameterTypeBoolean("use_plus", "Generate sums.", true));
types.add(new ParameterTypeBoolean("use_diff", "Generate differences.", true));
types.add(new ParameterTypeBoolean("use_mult", "Generate products.", true));
types.add(new ParameterTypeBoolean("use_div", "Generate quotients.", true));
return types;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -