⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 move.java

📁 经典的货郎担问题解决办法
💻 JAVA
字号:
/*** This code was written by Kent Paul Dolan.  See accompanying file** TravellerDoc.html for status for your use.*/package com.well.www.user.xanthian.java.genetic.reproducers.asexual;import com.coyotegulch.tools.*;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 Move  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 Move.reproduce( Chromosome parent)"        );      }/*** Pass the input as a less burdensome type.*/      TravellerChromosome child = algorithm( (TravellerChromosome) parent );      child.setOriginator( "Move" );      child.checkValidity();      return (Chromosome) child;    }    catch (Exception e)    {      System.err.println      (        "Move.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( "Move" );      }    }    else    {      if ( m_vdb != null )      {        m_vdb.closeWindow();        m_vdb = null;      }    }    if (VDB) { m_vdb.toFront(); }    TravellerChromosome offspring = new TravellerChromosome( parent );    offspring.canonicalize();    if (VDB) { m_vdb.setup( offspring ); }    MersenneTwister mt = MersenneTwister.getTwister();/*** Pick two indicies that differ from one another, one the slot _from_** which to move the city, one the slot _to_ which to move the city.*/    int from, to;    from = mt.nextInt(offspring.getNumCities());    do { to = mt.nextInt(offspring.getNumCities()); } while (to == from);/*** Move the selected city to the new location, rolling the intervening** cities to fill in the gap.*/    int temp = offspring.getCity(from);    if ( from < to)    {      for ( int i = from + 1 ; i <= to ; i++ )      {        int roll = offspring.getCity(i);        offspring.setCity(i-1,roll);      }    }    else    {      for ( int i = from - 1 ; i >= to ; i-- )      {        int roll = offspring.getCity(i);        offspring.setCity(i+1,roll);      }    }    offspring.setCity(to,temp);    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 + -