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

📄 evolutionaryweighting.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.weighting;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.example.Attribute;import edu.udo.cs.yale.example.AttributeWeightedExampleSet;import edu.udo.cs.yale.operator.features.*;import edu.udo.cs.yale.operator.features.ga.*;import edu.udo.cs.yale.operator.performance.PerformanceVector;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.tools.RandomGenerator;import java.util.List;import java.util.LinkedList;import java.util.Random;/** This operator performs the weighting of features with a evolutionary approach. *    *  @version $Id: EvolutionaryWeighting.java,v 1.4 2003/08/26 04:48:07 mierswa Exp $ */public class EvolutionaryWeighting extends FeatureOperator {    /** The selection modes which are usable with this genetic algorithm. */    private static final String[] SELECTION_MODES = {	"keep_best_n", "roulette_wheel", "keep_best"    };    /** Indicates that the n best individuals should be used for the next generation. */    private static final int KEEP_BEST_N    = 0;    /** Indicates that a roulette wheel selection should be used for the next generation. */    private static final int ROULETTE_WHEEL = 1;    /** Indicates that only the best individual should be used for the next generation. */    private static final int KEEP_BEST      = 2;    /** The number of individuals in each population. */    private int numberOfIndividuals;    /** The maximum generation. */    private int maxGeneration;    /** The maximum number of generations without improval. */    private int maxWithoutImproval;    /** The list with pre-evaluation population operators. */    private List preOps  = new LinkedList();    /** The list with post-evaluation population operators. */    private List postOps = new LinkedList();    public void initApply() throws OperatorException {	super.initApply();	this.numberOfIndividuals = getParameterAsInt("population_size");	this.maxGeneration       = getParameterAsInt("maximum_number_of_generations");	this.maxWithoutImproval  = getParameterAsInt("generations_without_improval");	preOps = new LinkedList();	// selection	int selectionMode = getParameterAsInt("selection_mode");	switch (selectionMode) {	    case KEEP_BEST_N:		preOps.add(new KeepBest(numberOfIndividuals));		break;	    case ROULETTE_WHEEL:		preOps.add(new RouletteWheel(numberOfIndividuals, 					     RandomGenerator.getGlobalRandomGenerator(), 					     getParameterAsBoolean("keep_best_individual")));		break;	    case KEEP_BEST:		preOps.add(new BestSelection());		break;	}	// crossover & mutation	preOps.add(new WeightingCrossover(getParameterAsInt("crossover_type"), 					  getParameterAsDouble("p_crossover")));	WeightingMutation weighting = new WeightingMutation(getParameterAsDouble("init_variance"));	preOps.add(weighting);	postOps = new LinkedList();	// mutation adaption	if (getParameterAsBoolean("1_5_rule")) 	    postOps.add(new VarianceAdaption(weighting));    }    public boolean solutionGoodEnough(Population population) {	return ((population.getGeneration() >= maxGeneration) || 		(population.getGenerationsWithoutImproval() >= maxWithoutImproval));    }    public List getPreEvaluationPopulationOperators() {	return preOps;    }    public List getPostEvaluationPopulationOperators() {	return postOps;    }    public Population createInitialPopulation(ExampleSet exampleSet) {	Population initPop = new Population();	for (int i = 0; i < numberOfIndividuals; i++) {	    AttributeWeightedExampleSet nes = new AttributeWeightedExampleSet((ExampleSet)exampleSet.clone());	    for (int j = 0; j < nes.getNumberOfAttributes(); j++) {		nes.setWeight(j, RandomGenerator.getGlobalRandomGenerator().nextDouble());		//nes.setWeight(j, 1.0d);	    }	    initPop.add(nes);	}	return initPop;    }    public List getParameterTypes() {	List types = super.getParameterTypes();	types.add(new ParameterTypeInt("population_size", "Number of individuals per generation.", 1, Integer.MAX_VALUE, 5));	types.add(new ParameterTypeInt("maximum_number_of_generations", 				       "The maximum number of generations.", 				       1, Integer.MAX_VALUE, 10));	types.add(new ParameterTypeInt("generations_without_improval", 				       "The maximum number of generations without improval.", 				       1, Integer.MAX_VALUE, 5));	types.add(new ParameterTypeCategory("selection_mode", "The selection mode for this genetic algorithm.", 					    SELECTION_MODES, KEEP_BEST_N));	types.add(new ParameterTypeBoolean("keep_best_individual", 					   "If set to true, the best individual of each generations is guaranteed to be selected for the next generation (elitist selection, only used for roulette wheel).", false));	types.add(new ParameterTypeBoolean("1_5_rule", 					   "If set to true, the 1/5 rule for variance adaption is used.", true));	types.add(new ParameterTypeDouble("init_variance", "The init variance for each mutation.", 0.0d, 					  Double.POSITIVE_INFINITY, 1.0d));	types.add(new ParameterTypeDouble("p_crossover", 					  "Probability for an individual to be selected for crossover.", 0, 1, 0.3));	types.add(new ParameterTypeCategory("crossover_type", 					    "Type of the crossover.", 					    WeightingCrossover.CROSSOVER_TYPES, WeightingCrossover.UNIFORM));	return types;    }}

⌨️ 快捷键说明

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