📄 attributeselectionexampleset.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.example;import edu.udo.cs.yale.tools.Ontology;import java.util.Iterator;import java.util.Map;import java.util.HashMap;/** An implementation of ExampleSet that allows switching attributes on and off. */public class AttributeSelectionExampleSet extends AttributeWeightedExampleSet { /** Clone constructor. */ public AttributeSelectionExampleSet(AttributeSelectionExampleSet exampleSet) { super((AttributeWeightedExampleSet)exampleSet); } /** Constructs a new AttributeSelectionExampleSet. */ public AttributeSelectionExampleSet(ExampleSet exampleSet) { super(exampleSet); } /** Returns the selection state of the attribute. */ public boolean isAttributeUsed(Attribute attribute) { return getWeight(attribute) != 0.0d; } /** Sets the selection state of the attribute. */ public void setAttributeUsed(Attribute attribute, boolean selected) { setWeight(attribute, (selected ? 1.0d : 0.0d)); clearUserData(); } /** Flips the selection state of the attribute. (Convenience method for evolutionary algorithms). * If a block starts with this attribute the whole block will be switched. */ public void flipAttributeUsed(Attribute attribute) { if (attribute.getBlockNr() != Attribute.UNDEFINED_BLOCK_NR) { if (attribute.isBlockStart()) { int startIndex = attribute.getIndex(); int endIndex = getExampleTable().getBlockEndIndex(startIndex); for (int i = startIndex; i <= endIndex; i++) { Attribute blockAtt = getExampleTable().getAttribute(i); if (!blockAtt.isSeries()) { throw new RuntimeException("Attribute " + blockAtt.getName() + " is a single value and should not have a blocknumber!"); } setAttributeUsed(blockAtt, !isAttributeUsed(blockAtt)); } clearUserData(); } } else { setAttributeUsed(attribute, !isAttributeUsed(attribute)); clearUserData(); } } // -------------------- wrapper methods -------------------- /** Returns the selection state of the attribute with the given index. */ public boolean isAttributeUsed(int i) { return isAttributeUsed(getAttribute(i)); } /** Sets the selection state of the attribute with the given index. */ public void setAttributeUsed(int index, boolean selected) { setAttributeUsed(getAttribute(index), selected); } /** Flips the selection state of the attribute with the given index. (Convenience * method for evolutionary algorithms) */ public void flipAttributeUsed(int index) { flipAttributeUsed(getAttribute(index)); } /** Sets all attributes to selected. */ public void selectAll() { setAll(true); } /** Sets all attributes to selected. */ public void deselectAll() { setAll(false); } /** Sets all flags to the given value. */ private void setAll(boolean on) { for (int i = 0; i < getNumberOfAttributes(); i++) { setAttributeUsed(getAttribute(i), on); } } // -------------------- overridden methods -------------------- public boolean equals(Object o) { if (!super.equals(o)) return false; if (!(o instanceof AttributeSelectionExampleSet)) return false; AttributeSelectionExampleSet es = (AttributeSelectionExampleSet)o; for (int i = 0; i < getNumberOfAttributes(); i++) { if (es.isAttributeUsed(i) != this.isAttributeUsed(i)) return false; } return true; } // --------------------------------------------------------- /** Returns a new example set containing all used attributes. */ public ExampleSet createExampleSet() { ExampleSet eSet = (ExampleSet)delegate.clone(); for (int i = eSet.getNumberOfAttributes()-1; i >= 0; i--) { Attribute attribute = eSet.getAttribute(i); if (!isAttributeUsed(attribute)) eSet.removeAttribute(attribute); } return eSet; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -