picsandstatgenerator.java

来自「JAVA版的蚂蚁算法(Ant Colony Optimization Algor」· Java 代码 · 共 743 行 · 第 1/3 页

JAVA
743
字号
    getGraph().drawGraph( picsAndStatDirectory + "//" + outputFile,
                     shortDescription, normalize, selectPercent,
                     edgeLabelField, showEdgesInColor, showTopPercent, doubleEdges );
  }

  /**
   * This little method generates a prefix string to the pictures and statistics
   * from the arguments given to the routing algorithm in an eventsimulated network.
   *
   * @return prefix string to pics and stat for <code>EventSimulation</code>  
   */
  private String generateSimPrefixString() {
    String[] argsArray = eventSim.getArgsArray();
    StringBuffer res = new StringBuffer();
    Double tmp;
    int i = 0;
    for ( ; i < argsArray.length - 1; i++ ) {
      tmp = new Double( argsArray[ i ] );
      res.append( commaFormat.format( tmp ) + ", " );
    }
    if ( i < argsArray.length ) {
      tmp = new Double( argsArray[ i ] );
      res.append( commaFormat.format( tmp ) );
    }
    return res.toString();
  }

  /**
   * Performs a test of an non-ant routing algorithm, so there is no alpha/beta
   * varying.
   *
   * @param numberOfRuns number of runs
   */
  public void testNonAntRoutingAlgorithm( int numberOfRuns ){
    testRoutingAlgorithm( 0, 0, 0, 0, 1, numberOfRuns );
  }


  /**
   * This method performs a test on the <code>RoutingAlgorithm</code> in
   * a network. A number of runs determined by parameter <code>innerCycle</code> is
   * made, if the routing algorithm is an <code>AntRoutingSystem</code> this number
   * of runs is made for every combination of alpha and beta-values. The possible
   * combinations are determined by parameters <code>alphaMin</code>, <code>
   * alphaMax</code>, <code>betaMin</code>, <code>betaMax</code> and
   * <code>incAlphaBeta</code>.
   *
   * @param alphaMin minimum test value of alpha
   * @param alphaMax maximum test value of alpha
   * @param betaMin minimum test value of beta
   * @param betaMax maximum test value of beta
   * @param incAlphaBeta increment on alpha and beta
   * @param innerCycle number of runs
   */
  public void testRoutingAlgorithm( double alphaMin, double alphaMax,
                                    double betaMin, double betaMax,
                                    double incAlphaBeta, int innerCycle ) {
    System.err.println( "testRoutingAlgorithm() starting...");
    int numberOfAlpha = (int) ( ( alphaMax - alphaMin ) / incAlphaBeta ) + 1;
    int numberOfBeta  = (int) ( ( betaMax - betaMin ) / incAlphaBeta ) + 1;
    int numberOfRuns = numberOfAlpha * numberOfBeta;

    double deathArray[][] = new double[ numberOfAlpha ][ numberOfBeta ];
    double distanceArray[][] = new double[ numberOfAlpha ][ numberOfBeta ];

    String argsString;
    eventSim.initialize();
    createAndSetPicsAndStatDir( new String[] {
                         eventSim.getNetworkName(), eventSim.getSimulationName() } );
    int i = 0;
    int j = 0;
    for ( double alpha = alphaMin; alpha <= alphaMax; alpha += incAlphaBeta, i++ ) {
      for ( double beta = betaMin; beta <= betaMax; beta += incAlphaBeta, j++ ) {
        double counter = 0;
        for ( int k = 0; k < innerCycle; k++ ){
          if ( !eventSim.isInitialized() ) {
            eventSim.initialize();
          }
          if ( eventSim.getRoutingAlgorithm() instanceof AntRoutingSystem && (
               alphaMin >= 0 && betaMin >= 0 ) ) {
            ( ( AntRoutingSystem ) eventSim.getRoutingAlgorithm() ).setAlpha(
                                                                  ( double ) alpha );
            ( ( AntRoutingSystem ) eventSim.getRoutingAlgorithm() ).setBeta(
                                                                 ( double ) beta );
            // As alpha and beta value are set manually here, the values from the
            // simulation-file are not correct.
            eventSim.setArgs( 0, String.valueOf( alpha ) );
            eventSim.setArgs( 1, String.valueOf( beta  ) );
          }
          eventSim.runSimulation();
          counter += eventSim.getLostPackages();
        }
        deathArray[i][j] = counter / innerCycle;
        argsString = generateSimPrefixString();
        System.out.println( numberOfRuns-- );
        generateStandardRoutingStats( argsString );
        generateStandardRoutingGraphPics( argsString );
      }
      j = 0;
    }
    i = 0;
    j = 0;
    StringBuffer sb = new StringBuffer();
    sb.append( "Alpha : " ).append( alphaMin ).append( " - " ).append(
                                                            alphaMax ).append( "\n");
    sb.append( "Beta : " ).append( betaMin ).append( " - " ).append(
                                                             betaMax ).append( "\n");
    sb.append( "Alpha is row, Beta is column\n");

    for ( double beta = betaMin; beta <= betaMax; beta += incAlphaBeta ){
      sb.append("\t").append( commaFormat.format( beta ) );
    }

    sb.append( "\n" );
    for ( double alpha = alphaMin; alpha <= alphaMax; alpha += incAlphaBeta, i++ ){
      sb.append( commaFormat.format( alpha ) );
      for ( double beta = betaMin; beta <= betaMax; beta += incAlphaBeta, j++ ) {
        sb.append( "\t" ).append( commaFormat.format( deathArray[i][j] ) );
      }
      sb.append( "\n" );
      j = 0;
    }
    writeToFile( sb.toString(), generateSimPrefixString() + "_DeathPivot.xls" );

    saveSimulationFiles();

    generateGraphPic( eventSim.getNetworkName() + ".jpg",
              getGraph().getAllEdges(), "The network with wire-delays." , true );
    System.err.println( "testRoutingAlgorithm() completed succesfully.");
  }

  /**
   * This method saves the simulation files (the network and simulation config-files)
   * used in the simulation to the directory specified by <code>picsAndStatDirectory
   * </code>.
   */
  private void saveSimulationFiles() {
    String fileSourcePath = eventSim.getPath();
    String fileNetworkName = eventSim.getNetworkName() + ".txt";
    String fileSimulationName = eventSim.getSimulationName() + ".txt";
    String res1 = "";
    String res2 = "";
    String inStr = "";
    System.err.println( "saveSimulationFiles() running..." );
    try {
      BufferedReader in = new BufferedReader( new FileReader(
                                                fileSourcePath + fileNetworkName ) );
      try {
        while ( true ) {
          if ( (inStr = in.readLine()) == null ) {
            break;
          }
          else {
            res1 += inStr + ( char ) Character.LINE_SEPARATOR +
                            ( char ) Character.LETTER_NUMBER;
          }
        }
      }
      catch ( IOException IOe ) {
        IOe.printStackTrace();
        System.exit(1);
      } // Finished reading file

      in = new BufferedReader( new FileReader(
                                             fileSourcePath + fileSimulationName ) );

      try {
        while ( true ) {
          if ( ( inStr = in.readLine() ) == null ) {
            break;
          }
          else {
            res2 += inStr + ( char ) Character.LINE_SEPARATOR +
                            ( char ) Character.LETTER_NUMBER;
          }
        }
      }
      catch ( IOException IOe ) {
        IOe.printStackTrace();
        System.exit( 1 );
      } // Finished reading file
    }
    catch ( FileNotFoundException FNFe ) {
      System.err.println( "Files not found in PicsAndStatGenerator." +
                          "saveSimulationFiles() in path " + fileSourcePath );
    } // Finished reading file
    
    writeToFile( res1, fileNetworkName );
    writeToFile( res2, fileSimulationName );
  }

  /**
   * This method generates a few standard pictures for an eventsimulated network-
   * routing. The files are generated in the directory specified by <code>
   * picsAndStatDirectory</code>.
   *
   * @param prefix the prefix for the filenames
   */
  public void generateStandardRoutingGraphPics( String prefix ) {
    System.err.println( "Starting generateStandardRoutingGraphPics()...");
    String title = prefix + "_TotalPackages";
    generateGraphPic( title + "_Color.jpg",
      eventSim.getSimulationName() + " - Total load on wires", true, 100,
      "totalAnts", true, true, true );
/*    generateGraphPic( title + ".jpg",
      eventSim.getSimulationName() + " - Total load on wires", false, 100,
      "totalAnts", false, true, true );*/
    System.err.println( "generateStandardRoutingGraphPics() completed succesfully.");
  }

  /**
   * This method generates two standard stat-files for an eventsimulated network-
   * routing. The files contain a summary of the important facts and parameters
   * for a simulation-run and a file containing the statistics collected during a
   * run. The files are generated in the directory specified by
   * <code>picsAndStatDirectory</code>.
   *
   * @param prefix the prefix for the filenames
   *
   * @see EventSimulation#addStat( double[] )
   * @see EventSimulation#addStatHeaders( String[] )
   * @see dk.itu.nulx30.eventSimulation.EventGatherStat
   */
  public void generateStandardRoutingStats( String prefix  ) {
    System.err.println( "Starting generateStandardRoutingStats()...");

    // Gather simulation parameters
    StringBuffer strBuf = new StringBuffer();
    strBuf.append( "Network\t" ).append( eventSim.getNetworkName() ).append( "\n");
    strBuf.append( "Simulation\t" + eventSim.getSimulationName() + "\n");
    strBuf.append( "Random seed\t" + eventSim.getSeed() + "\n" );
    strBuf.append( "Routing algorithm\t" + getAlgorithmClass() + "\n" );
    strBuf.append( "EndTime\t" +
                            twoDigitFormat.format( eventSim.getSimTime() ) + "\n" );
    if ( eventSim.getRoutingAlgorithm() instanceof AntRoutingSystem ) {
      AntRoutingSystem antRoutingAlg = ( ( AntRoutingSystem )
                                                    eventSim.getRoutingAlgorithm() );
      strBuf.append( "Alpha\t" + commaFormat.format( antRoutingAlg.getAlpha() ) +
                     "\n" + "Beta\t" + commaFormat.format( antRoutingAlg.getBeta() )
                     + "\n" );
      strBuf.append( "Random walk possibility\t" ).append( commaFormat.format(
                                       antRoutingAlg.getRandomRouteProbability() ) );
      strBuf.append( "\n" );
      strBuf.append( "Trail deposit scalefactor\t" ).append(
                         commaFormat.format( antRoutingAlg.getTrailDepositScale() ));
      strBuf.append( "\n" );
      strBuf.append( "Dead-ant negative feedback factor\t" ).append(
                    commaFormat.format( antRoutingAlg.getNegativeFeedbackScale() ) );

⌨️ 快捷键说明

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