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

📄 population.java

📁 一个很好的LIBSVM的JAVA源码。对于要研究和改进SVM算法的学者。可以参考。来自数据挖掘工具YALE工具包。
💻 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.example.ExampleSet;
import edu.udo.cs.yale.example.AttributeWeightedExampleSet;
import edu.udo.cs.yale.operator.performance.PerformanceVector;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Comparator;

/** A set of individuals (of class <tt>ExampleSet</tt>). Stores generation number and
 *  best individuals.
 *
 *  @author simon
 *  @version $Id: Population.java,v 2.12 2004/08/27 11:57:36 ingomierswa Exp $
 */
public class Population {

    public static final Comparator PERFORMANCE_COMPARATOR =
    new Comparator() {
	public int compare(Object o1, Object o2) {
	    PerformanceVector p1 = (PerformanceVector)((ExampleSet)o1).getUserData("performance");
	    PerformanceVector p2 = (PerformanceVector)((ExampleSet)o2).getUserData("performance");
	    return p1.compareTo(p2);
	}
    };

    /** List of ExampleSet */
    private ArrayList individuals;

    /** Current generation number */
    private int generation;
    
    /** All generations' best individual. */
    private AttributeWeightedExampleSet bestEver;

    /** All generations' best performance. */
    private PerformanceVector bestPerformanceEver;

    /** Last generation's best individual. */
    private AttributeWeightedExampleSet lastBest;

    /** Generation of the last improval. */
    private int generationOfLastImproval;

    /** Construct an empty generation. */
    public Population() {
	individuals = new ArrayList();
	generation = 0;
	generationOfLastImproval = 0;
    }

    /** Removes all individuals. */
    public void clear() { individuals.clear(); }
    /** Adds a single individual. */
    public void add(AttributeWeightedExampleSet individual) { individuals.add(individual); }
    /** Removes a single individual. */
    public void remove(AttributeWeightedExampleSet individual) { individuals.remove(individual); }
    /** Removes a single individual. */
    public void remove(int i) { individuals.remove(i); }
    /** Returns a single individual. */
    public AttributeWeightedExampleSet get(int i) { return (AttributeWeightedExampleSet)individuals.get(i); }
    /** Returns the number of all individuals. */
    public int getNumberOfIndividuals() { return individuals.size(); }
    /** Returns true is the population contains no individuals. */
    public boolean empty() { return individuals.size() == 0; }

    /** Increase the generation number by one. */
    public void nextGeneration() { generation++; }

    /** Returns the current number of the generation. */
    public int getGeneration() { return generation; }

    /** Returns the number of generations without improval.
     */
    public int getGenerationsWithoutImproval() {
	return generation - generationOfLastImproval;
    }

    /** Remember the current generation's best individual and update
     *  the best individual. */
    public void updateEvaluation() {
	lastBest = best();
	PerformanceVector lastBestPerformance = (lastBest == null) ?  null : (PerformanceVector)lastBest.getUserData("performance");
	if ((bestEver == null) ||
	    ((lastBest != null) &&
	     (lastBestPerformance.compareTo(bestPerformanceEver) > 0))) {
	    bestEver = (AttributeWeightedExampleSet)lastBest.clone();
	    bestPerformanceEver = (PerformanceVector)bestEver.getUserData("performance");
	    //System.out.println("NEW BEST in generation " + generation + ":\n"+bestEver);
	    //System.out.println("performance: " + bestPerformanceEver);
	    generationOfLastImproval = generation;
	}
    }

    /** Finds the current generation's best individual. Returns null, if
     *  there are unevaluated individuals. Probably you will want to use
     *  <tt>bestEver()</tt> or <tt>lastBest()</tt> because they don't cause
     *  comparisons. */
    private AttributeWeightedExampleSet best() {
	try {
	    return (AttributeWeightedExampleSet)Collections.max(individuals, PERFORMANCE_COMPARATOR);
	} catch (NullPointerException e) {
	    return null;
	} catch (NoSuchElementException e) {
	    return null;
	}
    }

    /** Returns the best performance. */
    public PerformanceVector bestPerformance() {
	return bestPerformanceEver;
    }

    /** Returns all generations' best individual. */
    public AttributeWeightedExampleSet bestEver() {
	return bestEver;
    }

    /** Returns the last generation's best individual. */
    public AttributeWeightedExampleSet lastBest() {
	return lastBest;
    }

    /** Sorts the individuals in ascending order according to their performance, thus
     *  the best one will be in last position. */
    public void sort() {
	Collections.sort(individuals, PERFORMANCE_COMPARATOR);
    }

    public String toString() {
	String s = generation + ": [ ";
	for (int i = 0; i < getNumberOfIndividuals(); i++) {
	    s += get(i) + " ";
	}
	return s + "] best: " + best() + "\never: " + bestEver();
    }
}

⌨️ 快捷键说明

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