⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attributeweightedexampleset.java

📁 著名的开源仿真软件yale
💻 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 the weighting of the attributes. Weights can be queried by  *  the method {@link #getWeight(int)}.  */public class AttributeWeightedExampleSet extends ExampleSetAdapter {    private static class Weight {	private double weight = 1.0d;	private Weight(double weight) {	    this.weight = weight;	}    }    /** Flags indicating the weight of each attribute. */    private Map weightMap = new HashMap();    /** Clone constructor. */    public AttributeWeightedExampleSet(AttributeWeightedExampleSet exampleSet) {	super((ExampleSetAdapter)exampleSet);	Iterator i = exampleSet.weightMap.keySet().iterator();	while (i.hasNext()) {	    String name = (String)i.next();	    Weight weight = (Weight)exampleSet.weightMap.get(name);	    this.weightMap.put(name, new Weight(weight.weight));	}    }        /** Constructs a new AttributeWeightedExampleSet. Initially all examples are weighted with 1.0. */    public AttributeWeightedExampleSet(ExampleSet exampleSet) {	super(exampleSet);	for (int i = 0; i < getNumberOfAttributes(); i++) {	    weightMap.put(getAttribute(i).getName(), new Weight(1.0d));	}    }    /** Returns the weight of the attribute. */    public double getWeight(Attribute attribute) {	Weight weight = (Weight)weightMap.get(attribute.getName());	if (weight == null) {	    throw new RuntimeException("Attribute " + attribute.getName() + " is not a regular attribute!");	} else return weight.weight;    }      /** Sets the weight of the attribute. */    public void setWeight(Attribute attribute, double weightValue) {	Weight weight = (Weight)weightMap.get(attribute.getName());	weight.weight = weightValue;	clearUserData();    }      /** Returns the number of selected attributes. */    public int getNumberOfUsedAttributes() {	int counter = 0;	for (int i = 0; i < getNumberOfAttributes(); i++) 	    if (getWeight(i) != 0.0d) counter++;	return counter;    }    // -------------------- wrapper methods --------------------    /** Returns the weight of the attribute with the given index. */    public double getWeight(int i) {	return getWeight(getAttribute(i));    }      /** Sets the weight of the attribute with the given index. */    public void setWeight(int index, double weight) {	setWeight(getAttribute(index), weight);    }      /** Sets the weights of all attributes to 1.0. */    public void selectAll() {	setAll(1.0d);    }    /** Sets the weights of all attributes to 0.0. */    public void deselectAll() {	setAll(0.0d);    }    /** Sets all flags to the given value. */    private void setAll(double weight) {	for (int i = 0; i < getNumberOfAttributes(); i++) {	    setWeight(getAttribute(i), weight);	}    }    // -------------------- overridden methods --------------------    public void addAttribute(Attribute attribute) {	super.addAttribute(attribute);	weightMap.put(attribute.getName(), new Weight(1.0d));    }    public void removeAttribute(Attribute attribute) {	super.removeAttribute(attribute);	weightMap.remove(attribute.getName());    }    public boolean equals(Object o) {	if (!super.equals(o)) return false;	if (!(o instanceof AttributeWeightedExampleSet)) return false;	AttributeWeightedExampleSet es = (AttributeWeightedExampleSet)o;	for (int i = 0; i < getNumberOfAttributes(); i++) {	    if (es.getWeight(i) != this.getWeight(i))		return false;	}	return true;    }    public String toString() {	StringBuffer buffer = new StringBuffer(super.toString());	buffer.append("\nWeights: ");	for (int i = 0; i < getNumberOfAttributes(); i++) {	    if (i != 0) buffer.append("\t");	    if (i > 50) { buffer.append("..."); break; }	    buffer.append(i + ": " + getWeight(i));	}	return buffer.toString();    }    // ---------------------------------------------------------    /** 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 (getWeight(attribute) == 0.0d) eSet.removeAttribute(attribute);	    else if (getWeight(attribute) != 1.0d) {		ExampleReader reader = eSet.getExampleReader();		while (reader.hasNext()) {		    Example example = reader.next();		    example.setValue(attribute, example.getValue(attribute) * getWeight(attribute));		}	    }	}	return eSet;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -