📄 swap.java
字号:
/*** This code was written by Kent Paul Dolan, based on source code in** Scott Robert Ladd's TravellerMutator.java. See that file for his** copyright notice. See accompanying file TravellerDoc.html for status** of the new modifications here for your use.*/package com.well.www.user.xanthian.java.genetic.reproducers.asexual;import com.coyotegulch.genetic.*;import com.well.www.user.xanthian.java.genetic.*;import com.well.www.user.xanthian.java.tools.*;import com.well.www.user.xanthian.java.ui.*;public class Swap implements Mutator{ private static boolean DB = false; private static boolean VDB = false; private static VisualDebugger m_vdb = null;/*** We provide separate interfaces for mutate and reproduce for the three** cases where it doesn't matter and they just do the same thing, so** that that two cases where it does matter can operate differently to** avoid polluting the permutation adaptation with mutation results when** they are not wanted and drive Traveller to very slow operation.*/ public Chromosome mutate(Chromosome parent) { return this.reproduce( parent ); } public Chromosome reproduce(Chromosome parent) { try {/*** Debugging hook abbreviation. During development, turn on debugging** just for this class by setting this variable to true, here. When the** code is stable, set it to false here, and control debugging from the** checkbox controls panel, instead. This variable is global to this** class, so it controls debugging thoughout the class when set here at** the top of the entry method for the class.*/ DB = false; if (CheckBoxControls.getState(CheckBoxControls.CBC_DEBUG_PRINTOUTS)) { DB = true; System.out.println ( "Entered Swap.reproduce( Chromosome parent)" ); }/*** Rename the input to a less burdensome type.*/ TravellerChromosome p = (TravellerChromosome) parent; TravellerChromosome child = algorithm( p ); child.setOriginator( "Swap" ); child.checkValidity(); return (Chromosome) child; } catch (Exception e) { System.err.println ( "Swap.reproduce() threw!" ); }/*** This code should never be reached, it is just here to pacify javac.*/ return parent; } private TravellerChromosome algorithm( TravellerChromosome parent ) { VDB = false; if (CheckBoxControls.getState(CheckBoxControls.CBC_DEBUG_VISUAL_WINDOWS)) { VDB = true; } if (VDB) { if ( m_vdb == null ) { m_vdb = new VisualDebugger( "Swap" ); } } else { if ( m_vdb != null ) { m_vdb.closeWindow(); m_vdb = null; } } if (VDB) { m_vdb.toFront(); } MersenneTwister mt = MersenneTwister.getTwister();/*** Pick two indices differing from one another between which to swap** codons.*/ int k, j; TravellerChromosome offspring = new TravellerChromosome( parent ); offspring.canonicalize(); if (VDB) { m_vdb.setup( offspring ); } j = mt.nextInt( ValuatorControls.getNumberOfCities() ); do { k = mt.nextInt( ValuatorControls.getNumberOfCities() ); } while ( k == j ); // swap city indexes int t = offspring.getCity( k ); offspring.setCity( k, offspring.getCity( j ) ); offspring.setCity( j, t ); offspring.canonicalize(); if (VDB) { m_vdb.step( offspring ); } if (VDB) { m_vdb.done( parent, offspring ); } return offspring; } public boolean isSuitableForMultipleMutationRuns() { return true; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -