📄 evolutionaryweighting.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.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.IOObject;
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.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.12 2004/09/14 08:39:06 ingomierswa 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 IOObject[] apply() throws OperatorException {
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,
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"), getParameterAsBoolean("bounded_mutation"));
preOps.add(weighting);
postOps = new LinkedList();
// mutation adaption
if (getParameterAsBoolean("1_5_rule"))
postOps.add(new VarianceAdaption(weighting));
return super.apply();
}
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.setWeightForBlock(j, RandomGenerator.getGlobalRandomGenerator().nextDouble());
//nes.setWeightForBlock(j, 1.0d);
}
initPop.add(nes);
}
return initPop;
}
public List getParameterTypes() {
List types = super.getParameterTypes();
ParameterType type = new ParameterTypeInt("population_size", "Number of individuals per generation.", 1, Integer.MAX_VALUE, 5);
type.setExpert(false);
types.add(type);
type = new ParameterTypeInt("maximum_number_of_generations",
"The maximum number of generations.",
1, Integer.MAX_VALUE, 10);
type.setExpert(false);
types.add(type);
type = new ParameterTypeInt("generations_without_improval",
"The maximum number of generations without improval.",
1, Integer.MAX_VALUE, 5);
type.setExpert(false);
types.add(type);
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 ParameterTypeBoolean("bounded_mutation",
"If set to true, the weights are bounded between 0 and 1.", false));
types.add(new ParameterTypeDouble("init_variance", "The init variance for each mutation.", 0.0d,
Double.POSITIVE_INFINITY, 1.0d));
type = new ParameterTypeDouble("p_crossover",
"Probability for an individual to be selected for crossover.", 0, 1, 0.3);
type.setExpert(false);
types.add(type);
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 + -