📄 generatingforwardselection.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * 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.example.Attribute;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.example.AttributeSelectionExampleSet;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.4 2003/06/18 17:11:18 fischer 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 void initApply() throws OperatorException { super.initApply(); useGenerators = new LinkedList(); if (getParameterAsBoolean("reciprocal_value")) { FeatureGenerator g = new ReciprocalValueGenerator(true); useGenerators.add(g); } if (getParameterAsBoolean("function_characteristica")) { FeatureGenerator g = new FunctionCharacteristicaGenerator(); useGenerators.add(g); } if (getParameterAsBoolean("use_plus")) { FeatureGenerator g = new BasicArithmeticOperationGenerator(0, true); useGenerators.add(g); } if (getParameterAsBoolean("use_diff")) { FeatureGenerator g = new BasicArithmeticOperationGenerator(1, true); useGenerators.add(g); } if (getParameterAsBoolean("use_mult")) { FeatureGenerator g = new BasicArithmeticOperationGenerator(2, true); useGenerators.add(g); } if (getParameterAsBoolean("use_div")) { FeatureGenerator g = new BasicArithmeticOperationGenerator(3, true); useGenerators.add(g); } if (useGenerators.size()==0) { LogService.logMessage("No FeatureGenerators specified for " + getName() + ".", LogService.WARNING); } } public Population createInitialPopulation(AttributeSelectionExampleSet 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 AttributeSelectionExampleSet fsBest = (AttributeSelectionExampleSet)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 AttributeSelectionExampleSet(fsBest.createExampleSet()); 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_charactersitica", "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 + -