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

📄 picsandstatgenerator.java

📁 JAVA版的蚂蚁算法(Ant Colony Optimization Algorithms)
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package dk.itu.nulx30.ant;

import java.text.DecimalFormat;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.util.Calendar;
import java.util.Collections;
import java.util.List;

import dk.itu.nulx30.util.Algorithms;
import dk.itu.nulx30.util.ShortestPaths;
import dk.itu.nulx30.eventSimulation.EventSimulation;
import dk.itu.nulx30.graph.Graph;

/**
 * This class generates pictures and stats from an ACOMetaHeuristic given as
 * argument to the constructor. This class contains several different kinds
 * of methods for drawing graphs and writing statistic to files.
 *
 * @author  Mikkel Bundgaard
 * @author  Troels C. Damgaard
 * @author  Federico Decara
 * @author  Jacob W. Winther
 */
public class PicsAndStatGenerator{
  /** twoDigitFormat formats a given number to two digits (no decimals)*/
  private static DecimalFormat twoDigitFormat = new DecimalFormat( "00" );

  /** <code>twoDigitFormat</code> formats a given number to use ','-separated */
  private static DecimalFormat commaFormat = new DecimalFormat( "0.##" );
  /**
   * the <code>Calendar</code> object is used to get access to the current
   * date/time
   */
  private static Calendar cal;
  /**
   * The ACO-algorithm this object is going to work upon, if <code>isAttachedToAco
   * </code> is <code>true</code>.
   * @see #isAttachedToAco
   */
  protected ACOMetaHeuristic acoAlg;
  /**
   * The <code>EventSimulation</code> this object is going to work upon,
   * if <code>isAttachedToAco</code> is <code>false</code>.
   * @see #isAttachedToAco
   */
  protected EventSimulation eventSim;
  /** the string representation of the working directory for this object*/
  protected String picsAndStatDirectory;
  /** the string representation of the current date*/
  protected String dateStr;
  /** the string representation of the current time*/
  protected String timeStr;

  /** This boolean variable is set when an object is constructed. It is true
   * if this <code>PicsAndStatGenerator</code> is attached to an <code>
   * ACOMetaheuristic</code>, it is false if it is attached to an <code>
   * EventSimulation</code> (of a network).
   */
  protected boolean isAttachedToAco;

  /**
   * Constructs an <code>AntPicsAndStatGenerator</code> with a
   * <code>ACOMetaHeuristic</code> to generate statistics and graph-pictures.
   *
   * @param aco the <code>ACOMetaHeuristic</code> this object
   * is going to work upon
   */
  public PicsAndStatGenerator( ACOMetaHeuristic aco ) {
    acoAlg = aco;
    init( true );
  }

  /**
   * Constructs an <code>AntPicsAndStatGenerator</code> with a
   * <code>EventSimulation</code> to generate statistics and graph-pictures.
   *
   * @param eventSim the <code>EventSimulation</code> this object
   * is going to work upon
   */  
  public PicsAndStatGenerator( EventSimulation eventSim ) {
    this.eventSim = eventSim;
    init( false );
  }

  /**
   * Initialize this class. This resets the <code>Calendar</code> object contained
   * in this class.
   *
   * @param isAttachedToAco is this object attached to an ACO class
   */
  private void init( boolean isAttachedToAco ) {
    this.isAttachedToAco = isAttachedToAco;
    cal = Calendar.getInstance();
  }

  /**
   * The method creates a directory based on the following formula :<br>
   * <code>
   * ..//Pics_and_Stat//className//[date]_([time])_args#args[0]_args[1]_..._args[n]#
   * </code><br>
   *
   * Further the String-field <code>picsAndStatDirectory</code> is set to
   * place all later generated pictures and stat-info in this directory.<br>
   *
   * Note that the timestamp for the test is set, when this method is run.<br>
   *
   * (Note that other methods in this class actually require the field
   * <code>picsAndStatDirectory</code> to have been set.)
   *
   * @param args the <code>String</code>-array given to the <code>main</code>-method
   * in the ACO-algorithm / the <code>routePackage</code>-method in the
   * routing-algorithm in EventSimulation.
   *
   * @return the <code>String</code> naming the directory
   *
   * @see #picsAndStatDirectory
   */
  public String createAndSetPicsAndStatDir( String[] args ){
    // Construct directory-name and timestamp
    dateStr = "02-" + twoDigitFormat.format( cal.get( Calendar.MONTH ) + 1 ) +
                "-" + twoDigitFormat.format( cal.get( Calendar.DAY_OF_MONTH ) );
    timeStr = twoDigitFormat.format( cal.get( Calendar.HOUR_OF_DAY ) ) + "." +
              twoDigitFormat.format( cal.get( Calendar.MINUTE ) );

    // Remove the leading "Class "
    String className = getAlgorithmClass().toString().substring( 6 );

    picsAndStatDirectory = "..//..//..//..//..//Pics_and_Stat//" +
                             className + "//" + dateStr + "_(" + timeStr + ")_args#";
    if ( args.length > 0 ){
      picsAndStatDirectory += args[ 0 ];
      for ( int i = 1; i < args.length; i++ ){
        picsAndStatDirectory += "_" + args[ i ];
      }
    }
    else {
      picsAndStatDirectory += "none";
    }
    picsAndStatDirectory += "#";

    // create dir (if not already exists)
    new java.io.File( picsAndStatDirectory ).mkdirs();
    // returns the string representing the directory - for convinence
    return picsAndStatDirectory;
  }

  /**
  * This method returns the class of the user-implemented algorithm by querying
  * either the <code>ACOMetaheuristic</code> or the <code>EventSimulation</code>.
  *
  * @return the class of the object which this <code>PicsAndStatGenerator</code>
  * works on.
  */
  private Class getAlgorithmClass() {
    if ( isAttachedToAco ) {
      return acoAlg.getClass();
    }
    else {
      return eventSim.getRoutingAlgorithm().getClass();
    }
  }

  /**
  * This method returns the <code>graph</code> of the run by querying
  * either the <code>ACOMetaheuristic</code> or the <code>EventSimulation</code>.
  *
  * @return the <code>graph</code> of the run 
  */
  private Graph getGraph() {
    if ( isAttachedToAco ) {
      return acoAlg.theGraph;
    }
    else {
      return eventSim.getNetwork();
    }
  }

  /**
   * generateGraphPic draws a jpg-file (in the file outputFile) which represents
   * the vertices and the edges given as argument with a description of the graph.
   * The <code>weight</code>-attribute is depicted on the edges. 
   * This method calls drawGraph( String, List, String, boolean ) in the class Graph.
   *
   * @param outputFile the name of the output file
   * @param edges a <code>List</code> containing the edges to be drawn
   * @param shortDescription a short description of the graph
   * @param doubleEdges does this graph have double edges (that is two edges pr.
   * nodepair)
   *
   * @see dk.itu.nulx30.graph.Graph#drawGraph(String, List, String, boolean)
   */
  public void generateGraphPic( String outputFile, List edges,
                                String shortDescription, boolean doubleEdges ) {
    getGraph().drawGraph( picsAndStatDirectory + "//" + outputFile,
                                         edges, shortDescription, doubleEdges );
  }

  /**
   * generateGraphPic draws a jpg-file (in the file outputFile) which represents
   * the vertices and the edges with the chosen field given as argument.
   * The values can be normalized or not. This method calls
   * drawGraph( String, String, boolean, String, boolean ) in the class Graph.
   *
   * @param outputFile the name of the output file
   * @param shortDescription a short description of the graph
   * @param normalize if true the data is normalized otherwise the data remains
   * unchanged in the graph
   * @param edgeLabelField the name of the field on the edges
   * @param doubleEdges does this graph have double edges (that is two edges pr.
   * nodepair)
   *
   * @see
   * dk.itu.nulx30.graph.Graph#drawGraph(String, String, boolean, String, boolean)
   */
  public void generateGraphPic( String outputFile, String shortDescription,
                     boolean normalize, String edgeLabelField, boolean doubleEdges ){
    getGraph().drawGraph( picsAndStatDirectory + "//" + outputFile,
                          shortDescription, normalize, edgeLabelField, doubleEdges );
  }

  /**
   * generateGraphPic draws a jpg-file (in the file outputFile) which represents
   * the vertices and the edges with the chosen field given as argument.
   * The values can be normalized or not and only the xx percent of the top or
   * bottom can be shown. This method calls drawGraph( String, String, boolean,
   * int, String, boolean, boolean, boolean) in the class Graph.
   *
   * @param outputFile the name of the output file
   * @param shortDescription a short description of the graph
   * @param normalize if true the data is normalized otherwise the data remains
   * unchanged in the graph
   * @param selectPercent the percentage of edges to show (works together with
   * the argument showTopPercent)
   * @param edgeLabelField the name of the field on the edges
   * @param showEdgesInColor decides if the edges should be shown in color
   * @param showTopPercent decides if the top percent should be shown or
   * the bottom percent
   * @param doubleEdges does this graph have double edges (that is two edges pr.
   * nodepair)   
   */
  public void generateGraphPic( String outputFile, String shortDescription,
            boolean normalize, int selectPercent, String edgeLabelField,
            boolean showEdgesInColor, boolean showTopPercent, boolean doubleEdges ){

⌨️ 快捷键说明

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