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

📄 network.java

📁 JAVA版的蚂蚁算法(Ant Colony Optimization Algorithms)
💻 JAVA
字号:
package dk.itu.nulx30.networkModel;

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

import java.util.ArrayList;
import java.util.StringTokenizer;

import dk.itu.nulx30.graph.Graph;
import dk.itu.nulx30.graph.Vertex;

import dk.itu.nulx30.networkModel.exceptions.UninitializedWireException;

/**
 * Class <code>Network</code> is a subclass of <code>Graph</code> and contains
 * a collection of Routers and a collection of Wires. A <code>Network</code>
 * can be created from a file and drawn to a jpg-file (just like
 * <code>Graph</code>).
 *
 * @author  Mikkel Bundgaard
 * @author  Troels C. Damgaard
 * @author  Federico Decara
 * @author  Jacob W. Winther
 */
public class Network extends Graph {
  /**
   * Class constructor for Network, which creates an empty <code>Network</code>.
   */
  public Network() {
    super();
  }

  /**
   * This method reads a file at the location given in <code>fileName</code> amd
   * constructs a corresponding graph. The initial trailvalue of the edges in
   * the graph is given as <code>initValue</code>. Below is given an example
   * of a file:<br>
   * Routers<br>
   * 4<br>
   * Wires<br>
   * 1<br>
   * The routers<br>
   * Name RouterDelay InputQueueCapacity Horizontal Vertical<br>
   * 0 10 10 10 10<br>
   * 1 100 100 100 100<br>
   * 2 200 100 200 100<br>
   * 3 200 10 200 10<br>
   * The wires<br>
   * From To Delay OutputQueueCapacity<br>
   * 1 0 10 10 10<br>
   *
   * @param name the name of this <code>Network</code>
   * @param fileName the name of the file to read the graph from
   * @param initValue the initial trailvalue on the edges in the graph
   */
  public void createFromGraphMaker( String name, String fileName, double initValue ){
    BufferedReader input = null;
    StringTokenizer st;
    int numberOfRouters;
    int numberOfWires;
    String tmpInput;

    try {
      input = new BufferedReader( new FileReader( fileName ) );
    }
    catch ( FileNotFoundException FNFe ) {
      throw new RuntimeException("Could not find file : " + fileName );
    }
    try {
      this.name = name;
      this.fileName = fileName;

      // Read the header of the file
      while ( !input.readLine().equals( "Routers" ) ){};

      numberOfRouters = Integer.parseInt( input.readLine() );

      // Read the rest of the header of the file
      while ( !input.readLine().equals( "Wires" ) ){};
      numberOfWires = Integer.parseInt( input.readLine() );

      while ( !input.readLine().equals( "Name RouterDelay InputQueueCapacity " +
                                                      "Horizontal Vertical" ) ){};

      this.vertices = new Vertex[ numberOfRouters ];
      this.edges = new ArrayList( numberOfWires );

      // Create all the routers
      for ( int i = 0; i < numberOfRouters; i++) {
        st = new StringTokenizer( input.readLine() );

        int index = Integer.parseInt( st.nextToken() );

        int delay = Integer.parseInt( st.nextToken() );
        int queueCapacity = Integer.parseInt( st.nextToken() );

        this.vertices[ i ] = new Router( index, delay, queueCapacity );

        //Initialize the routingTable for the router
        ( ( Router ) this.vertices[ i ] ).initRoutingTable( numberOfRouters );

        this.vertices[ i ].setXPos( ( int ) Double.parseDouble(
                                                   st.nextToken() ) * X_SCALE );
        this.vertices[ i ].setYPos( ( int ) Double.parseDouble(
                                                   st.nextToken() ) * Y_SCALE );
      }

      while ( !input.readLine().equals( "From To Delay OutputQueueCapacity" ) ){};

      // Create all thr wires
      for ( int i = 0; i < numberOfWires; i++ ) {
        st = new StringTokenizer( input.readLine() );
        int fromVertex;
        int toVertex;
        int wiredelay;
        int outputcapacity;

        fromVertex = Integer.parseInt( st.nextToken() );
        toVertex = Integer.parseInt( st.nextToken() );
        wiredelay = Integer.parseInt( st.nextToken() );
        outputcapacity = Integer.parseInt( st.nextToken() );

        Wire wire1;
        Wire wire2;
        try {
          // Add output wire to the router at fromVertex
          wire1 = ( ( Router ) this.vertices[ fromVertex ] ).addOutputWire(
              ( Router ) this.vertices[ toVertex ], wiredelay, outputcapacity );
          // Add output wire to the router at toVertex
          wire2 = ( ( Router ) this.vertices[ toVertex ] ).addOutputWire(
            ( Router ) this.vertices[ fromVertex ], wiredelay, outputcapacity );

          this.vertices[ fromVertex ].getEdges().add( wire1 );
          // Insert edge in Graph-objects own list of edges
          this.edges.add( wire1 );

          this.vertices[ toVertex ].getEdges().add( wire2  );
          // Insert edge in Graph-objects own list of edges
          this.edges.add( wire2 );

          // Add the wires to the routingTables for the two routers
          ( (Router) this.vertices[ fromVertex ]
                                     ).getRoutingTable().addOutputWire( wire1 );
          ( (Router) this.vertices[ toVertex ]
                                     ).getRoutingTable().addOutputWire( wire2 );
        }
        catch ( UninitializedWireException UWe ) {
          System.err.println( UWe );
          System.exit( 1 );
        }
      }

      input.close();
    }
    catch ( IOException IOe ) {
      IOe.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

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