📄 unbalancedcrossover.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.ga;import edu.udo.cs.yale.example.ExampleSet;import edu.udo.cs.yale.tools.RandomGenerator;import java.util.List;import java.util.ListIterator;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! * @author ingo * @version $Id: UnbalancedCrossover.java,v 2.3 2003/06/29 14:40:43 mierswa Exp $ */public class UnbalancedCrossover extends SelectionCrossover { /** Creates a new generating crossover with the given type which will * be applied with the given probability. ParentsSurvive indicates if the original attributes * maintain. */ public UnbalancedCrossover(int type, double prob, boolean parentsSurvive) { super(type, prob, parentsSurvive, RandomGenerator.getGlobalRandomGenerator()); } /** Applies the crossover. Works directly on the given example sets. */ public void crossover(ExampleSet es1, ExampleSet es2) { LinkedList dummyList1 = null; LinkedList dummyList2 = null; switch (type) { case SelectionCrossover.ONE_POINT: int maxSize = Math.max(es1.getNumberOfAttributes(), es2.getNumberOfAttributes()); if (maxSize < 2) return; int splitPoint = 1 + random.nextInt(maxSize - 1); dummyList1 = new LinkedList(); dummyList2 = new LinkedList(); while (es1.getNumberOfAttributes() > splitPoint) dummyList1.add(es1.removeAttribute(splitPoint)); while (es2.getNumberOfAttributes() > splitPoint) dummyList2.add(es2.removeAttribute(splitPoint)); es1.addAllAttributes(dummyList2); es2.addAllAttributes(dummyList1); break; case SelectionCrossover.UNIFORM: int biggerSize = Math.max(es1.getNumberOfAttributes(), es2.getNumberOfAttributes()); boolean[] swap = new boolean[biggerSize]; for (int i = 0; i < swap.length; i++) { swap[i] = random.nextBoolean(); } dummyList1 = new LinkedList(); dummyList2 = new LinkedList(); for (int i = swap.length - 1; i >= 0; i--) { if (swap[i]) { if (i < es1.getNumberOfAttributes()) dummyList1.addFirst(es1.removeAttribute(i)); if (i < es2.getNumberOfAttributes()) dummyList2.addFirst(es2.removeAttribute(i)); } } es1.addAllAttributes(dummyList2); es2.addAllAttributes(dummyList1); break; default: } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -