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

📄 population.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.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.5 2003/08/25 19:21:11 mierswa 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));		    return Double.compare(p1.getMainCriterion().getFitness(), p2.getMainCriterion().getFitness());		}	    };    /** List of ExampleSet */    private ArrayList individuals;    /** Current generation number */    private int generation;        /** All generations' best individual. */    private AttributeWeightedExampleSet best;    /** 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();	//if (lastBest != null) lastBest = (AttributeWeightedExampleSet)lastBest.clone();	PerformanceVector lastBestPerformance = (lastBest == null) ?  null : (PerformanceVector)lastBest.getUserData("performance");	PerformanceVector bestPerformance     = (best == null) ?      null : (PerformanceVector)best.getUserData("performance");	if ((best == null) ||	    ((lastBest != null) &&	     //(lastBestPerformance.compareTo(bestPerformance) > 0))) {	     (Double.compare(lastBestPerformance.getMainCriterion().getFitness(), bestPerformance.getMainCriterion().getFitness()) > 0))) {	    best = (AttributeWeightedExampleSet)lastBest.clone();	    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 all generations' best individual. */    public AttributeWeightedExampleSet bestEver() {	return best;    }    /** 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 + -