masterseeder.java

来自「经典的货郎担问题解决办法」· Java 代码 · 共 389 行

JAVA
389
字号
package com.well.www.user.xanthian.java.seeders;import java.awt.*;import com.coyotegulch.genetic.*;import com.coyotegulch.ui.*;import com.well.www.user.xanthian.java.tools.*;import com.well.www.user.xanthian.java.ui.*;public class MasterSeeder{  private static SmallDisplay m_progressDisplay = null;  private final static String m_progressDisplayName =    "Salesman's Wild Oats";  private static MinimalSpanningTree   m_mst         = null;  private static ElasticNet            m_en          = null;  private static boolean               DB            = false;  private static int                   m_popMember   = 0;  public static void seedGenomes  (    TravellerChromosome [] chromosomes,    TravellerWorld world  )  {    DB = false;    if ( CheckBoxControls.getState(CheckBoxControls.CBC_DEBUG_PRINTOUTS) )    {      DB = true;      System.out.println( "Entered MasterSeeder.seedGenomes()" );    }    setupGenomeSeeders( world );/*** Figure out how may seeders are enabled, so we can split the** population among them fairly, except that random seeding, as the** default, gets any leftovers from seeders that can produce only a** limited number of seed genomes.*/    int seeders =      (        (          CheckBoxControls.getState          (            CheckBoxControls.CBC_SEED_FROM_RANDOM_POINTS          )          ||  // FIXME same as random until implemented          CheckBoxControls.getState          (            CheckBoxControls.CBC_SEED_FROM_LOCAL_OPTIMIZATION          )          ||  // FIXME same as random until implemented          CheckBoxControls.getState          (            CheckBoxControls.CBC_SEED_FROM_DELAUNEY_TRIANGULATION          )        )        ? 1        : 0      )      +      (        CheckBoxControls.getState        (          CheckBoxControls.CBC_SEED_FROM_MINIMAL_SPANNING_TREE        )        ? 1        : 0      )      +      (        CheckBoxControls.getState        (          CheckBoxControls.CBC_SEED_FROM_ELASTIC_NET        )        ? 1        : 0      );    if (DB) System.out.println( "seeders: " + seeders );/*** Fill in the population with seed genomes, in a round robin, until all** the genomes have been assigned.*/    m_popMember = 0;    while ( true )    {      updateCounter();/*** Always start with a random one if random seeds are enabled; we want** member zero not to begin as a high fitness genome, so that we can** notice while watching the interface when a high fitness genome does** reach position zero and start accumulating clones, an indication of** possible global or local optimum convergence.*/      if ( m_popMember >= ValuatorControls.getPopulationSize() ) { break; }      if      (        CheckBoxControls.getState        (          CheckBoxControls.CBC_SEED_FROM_RANDOM_POINTS        )      )      {        chromosomes[m_popMember] =          new TravellerChromosome( world, "Random seed genome" );        m_popMember++;      }/*** Seeds derived from the minimal spanning tree have good fitness, but** the current implementation of sampling from the MST is weak on** variety, and a better one would be hard to do, hard on the garbage** collector, and slow (though I've figured out how it would work);** compensate a bit by mixing MST seeds with random ones.  The roulette** wheel is going to kill us on getting to the random ones, though.*/      if ( m_popMember >= ValuatorControls.getPopulationSize() ) { break; }      if      (        CheckBoxControls.getState        (          CheckBoxControls.CBC_SEED_FROM_MINIMAL_SPANNING_TREE        )      )      {        chromosomes[m_popMember] =          ( m_popMember < seeders )          ? new TravellerChromosome            (              world,              "minimal spanning tree seed genome greedily derived",              m_mst.getGreedilyDerivedNodeList( world )            )          : new TravellerChromosome            (              world,              "minimal spanning tree seed genome usual derived",              m_mst.getDerivedNodeList( world )            )          ;        if (DB)        {          if ( m_popMember < seeders )          {            System.out.println            (              ( (TravellerChromosome) chromosomes[m_popMember] ).testFitness()              + " getBestDerivedNodeList"            );          }          else          {            System.out.println            (              ( (TravellerChromosome) chromosomes[m_popMember] ).testFitness()              + " getDerivedNodeList"            );          }        }        m_popMember++;      }/*** Elastic net chromosomes for seed use are in short supply.  The** algorithm is deterministic, so run to its iteration limit, the** algorithm produces only one particular genome, repeatably for the** same set of city locations.  Up our odds of finding something useful** (or at least interesting) by taking sample genomes at various steps** of the iteration.  They should range from awful to excellent that** way.*/      if ( m_popMember >= ValuatorControls.getPopulationSize() ) { break; }      if      (        CheckBoxControls.getState        (          CheckBoxControls.CBC_SEED_FROM_ELASTIC_NET        )      )      {        if        (          m_en.moreIterationsAreAvailable()        )        {          int iterationsToRequest = (int)            (              ( (float) seeders / (float) ValuatorControls.getPopulationSize() )              *              (float) ElasticNet.ITERATION_LIMIT            );          if (DB) System.out.println          (            "seeders "            + seeders            + " popSize "            + ValuatorControls.getPopulationSize()            + " LIMIT "            + ElasticNet.ITERATION_LIMIT            + " iterationsToRequest "            + iterationsToRequest          );          int temp[] = m_en.iterate( iterationsToRequest );          chromosomes[m_popMember] =            new TravellerChromosome            (              world,              "elastic net seed genome",              temp              // m_en.iterate( iterationsToRequest )            );          if (DB) System.out.println          (            "member "            + m_popMember            + " new city list "            + Debugging.dump( temp )            + " gives fitness "            + chromosomes[m_popMember].testFitness()          );          m_popMember++;        }        else        {/*** Cater for the problem where we've run out of elastic net seeds but we** only chose elastic net seeding, by filling in the rest with random** seeds.*/          if ( seeders == 1 )          {            chromosomes[m_popMember] = new              TravellerChromosome( world, "spillover random seed genome" );            m_popMember++;          }        }      }    }/*** Clear out tools allocated on static handles before exiting, so that** the memory space they occupy can be garbage collected.*/    if ( m_mst != null ) { m_mst.closeWindow(); m_mst = null; }    if ( m_en != null ) { m_en.closeWindow(); m_en = null; }    if ( m_progressDisplay != null )    {      m_progressDisplay.closeWindow();      m_progressDisplay = null;    }  }  private static void setupGenomeSeeders( TravellerWorld world )  {/*** FIXME Put minimal spanning tree construction here.  Counter good** programming practice bummer!  All this calculation slop is _inside** the constructor_, which means that the canvas is not yet ready to** display a minimal spanning tree construction as it is happening,** which has to happen before the minimal spanning tree can be used as** an alternative way to fill the chromosome city/Codon arrays.** Refactor this ugly mess to put all the math initialization outside** this constructor.*/    if     (      CheckBoxControls.getState      (        CheckBoxControls.CBC_SEED_FROM_MINIMAL_SPANNING_TREE      )    )    {/*** Grrr.  Java language designers didn't make methods first class** objects!  You cannot pass them as parameters.*///      m_mst = new//         MinimalSpanningTree//         (//           cityDistancesVA.getValue,//           world.getNumberOfCities//         );/*** FIXME This is trashy, MinimalSpanningTree has no business knowing** about something as complex as a TravellerWorld, but to fix it requires** ripping the city location information out of TravellerWorld and into** a public class; live with it for now.*/      m_mst = new MinimalSpanningTree( world );    }    if     (      CheckBoxControls.getState      (        CheckBoxControls.CBC_SEED_FROM_ELASTIC_NET      )    )    {      m_en = new ElasticNet ( world );    }  }  private static void updateCounter()  {    if    (      CheckBoxControls.getState(CheckBoxControls.CBC_DEBUG_PROGRESS_COUNTERS)    )     {      if ( m_progressDisplay == null )      {        m_progressDisplay =          new SmallDisplay( m_progressDisplayName );      }      m_progressDisplay.updateDisplay      (        "seed "        + m_popMember        + " of 0 to "        + ( ValuatorControls.getPopulationSize() - 1 )        + " being sown"      );    }    else    {      if ( m_progressDisplay != null )      {        m_progressDisplay.closeWindow();        m_progressDisplay = null;      }    }  }  public static double getFloor()  {    if    (      CheckBoxControls.getState      (        CheckBoxControls.CBC_SEED_FROM_MINIMAL_SPANNING_TREE      )    )    {      return MinimalSpanningTree.getFloor();    }    else    {      return 0.0D;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?