📄 unbalancedcrossover.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.ga;
import edu.udo.cs.yale.example.Attribute;
import edu.udo.cs.yale.example.ExampleSet;
import edu.udo.cs.yale.example.AttributeWeightedExampleSet;
import edu.udo.cs.yale.tools.RandomGenerator;
import java.util.List;
import java.util.Iterator;
import java.util.LinkedList;
/** This <tt>PopulationOperator</tt> applies a crossover on two example sets.
* Crossover type can be ONE_POINT or UNIFORM. In difference to SelectionCrossover
* the attribute vectors can have different lengths. <br>
* This crossover type should only be used for SINGLE_VALUEs,
* i.e. attributes without a block number!
*
* @author ingo
* @version $Id: UnbalancedCrossover.java,v 2.9 2004/08/27 11:57:36 ingomierswa Exp $
*/
public class UnbalancedCrossover extends SelectionCrossover {
private class AttributeWeightContainer {
private Attribute attribute;
private double weight;
public AttributeWeightContainer(Attribute attribute, double weight) {
this.attribute = attribute;
this.weight = weight;
}
public Attribute getAttribute() { return attribute; }
public double getWeight() { return weight; }
public String toString() {
return attribute.getName() + "(" + weight + ")";
}
}
/** Creates a new generating crossover with the given type which will
* be applied with the given probability. */
public UnbalancedCrossover(int type, double prob) {
super(type, prob);
}
/** Applies the crossover. Works directly on the given example sets. */
public void crossover(AttributeWeightedExampleSet es1, AttributeWeightedExampleSet es2) {
LinkedList dummyList1 = new LinkedList();
LinkedList dummyList2 = new LinkedList();
int maxSize = Math.max(es1.getNumberOfAttributes(), es2.getNumberOfAttributes());
if (maxSize < 2) return;
switch (getType()) {
case SelectionCrossover.ONE_POINT:
int splitPoint = 1 + RandomGenerator.getGlobalRandomGenerator().nextInt(maxSize - 2);
while (es1.getNumberOfAttributes() > splitPoint) {
double weight = es1.getWeight(es1.getAttribute(splitPoint));
Attribute attribute = es1.removeAttribute(splitPoint);
dummyList1.add(new AttributeWeightContainer(attribute, weight));
}
while (es2.getNumberOfAttributes() > splitPoint) {
double weight = es2.getWeight(es2.getAttribute(splitPoint));
Attribute attribute = es2.removeAttribute(splitPoint);
dummyList2.add(new AttributeWeightContainer(attribute, weight));
}
break;
case SelectionCrossover.UNIFORM:
for (int i = es1.getNumberOfAttributes() - 1; i >= 0; i--) {
if (RandomGenerator.getGlobalRandomGenerator().nextBoolean()) {
Attribute attribute = es1.getAttribute(i);
double weight = es1.getWeight(attribute);
es1.removeAttribute(attribute);
dummyList1.add(new AttributeWeightContainer(attribute, weight));
}
}
for (int i = es2.getNumberOfAttributes() - 1; i >= 0; i--) {
if (RandomGenerator.getGlobalRandomGenerator().nextBoolean()) {
Attribute attribute = es2.getAttribute(i);
double weight = es2.getWeight(attribute);
es2.removeAttribute(attribute);
dummyList2.add(new AttributeWeightContainer(attribute, weight));
}
}
break;
default:
}
mergeAttributes(es1, dummyList2);
mergeAttributes(es2, dummyList1);
}
private void mergeAttributes(AttributeWeightedExampleSet exampleSet, List attributeWeights) {
Iterator i = attributeWeights.iterator();
while (i.hasNext()) {
AttributeWeightContainer attributeWeight = (AttributeWeightContainer)i.next();
Attribute attribute = attributeWeight.getAttribute();
if (exampleSet.getAttribute(attribute.getName()) == null) {
exampleSet.addAttribute(attribute);
}
exampleSet.setWeight(attribute, attributeWeight.getWeight());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -