📄 weightingcrossover.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.AttributeWeightedExampleSet;import edu.udo.cs.yale.tools.RandomGenerator;import edu.udo.cs.yale.operator.features.*;import java.util.List;import java.util.ListIterator;import java.util.LinkedList;/** Crossover operator for the used weights of example sets. An example set is selected * with a given fixed propability and a mating partner is determined randomly. Crossover * can be either one point or uniform. * Only useful if all example sets have the same attributes. * * @version $Id: WeightingCrossover.java,v 1.2 2003/08/27 15:28:15 mierswa Exp $ */public class WeightingCrossover implements PopulationOperator { protected static final String[] CROSSOVER_TYPES = { "one_point", "uniform" }; public static final int ONE_POINT = 0; public static final int UNIFORM = 1; private int type; private double prob; private RandomGenerator random; public WeightingCrossover(int type, double prob) { this.prob = prob; this.type = type; this.random = RandomGenerator.getGlobalRandomGenerator(); } public void crossover(AttributeWeightedExampleSet es1, AttributeWeightedExampleSet es2) { switch (type) { case ONE_POINT: int n = 1+random.nextInt(es1.getNumberOfAttributes()-1); n = es1.getExampleTable().getBlockEndIndex(n); for (int i = n; i < es1.getNumberOfAttributes(); i++) { double dummy = es1.getWeight(i); es1.setWeight(i, es2.getWeight(i)); es2.setWeight(i, dummy); } break; case UNIFORM: boolean[] swap = new boolean[es1.getNumberOfAttributes()]; for (int i = 0; i < swap.length; i++) { boolean blockBoolean = random.nextBoolean(); int endIndex = es1.getExampleTable().getBlockEndIndex(i); for (int j = i; j <= endIndex; j++) { if (j < swap.length) swap[j] = blockBoolean; } i = endIndex; } for (int i = 0; i < swap.length; i++) { if (swap[i]) { double dummy = es1.getWeight(i); es1.setWeight(i, es2.getWeight(i)); es2.setWeight(i, dummy); } } break; default: } } public void operate(Population population) { if (population.getNumberOfIndividuals() < 2) return; int n = population.getNumberOfIndividuals(); boolean[] parent = new boolean[n]; // remember which individuals were already used LinkedList matingPool = new LinkedList(); for (int i = 0; i < population.getNumberOfIndividuals(); i++) matingPool.add(population.get(i).clone()); List l = new LinkedList(); while (matingPool.size() > 1) { AttributeWeightedExampleSet p1 = (AttributeWeightedExampleSet)matingPool.remove(random.nextInt(matingPool.size())); AttributeWeightedExampleSet p2 = (AttributeWeightedExampleSet)matingPool.remove(random.nextInt(matingPool.size())); if (random.nextDouble() < prob) { crossover(p1, p2); if (p1.getNumberOfUsedAttributes() > 0) l.add(p1); if (p2.getNumberOfUsedAttributes() > 0) l.add(p2); } else { l.add(p1); l.add(p2); } } l.addAll(matingPool); population.clear(); ListIterator i = l.listIterator(); while (i.hasNext()) population.add((AttributeWeightedExampleSet)i.next()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -