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

📄 antpackage.java

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

import java.util.ArrayList;

import dk.itu.nulx30.networkModel.NetworkPackage;
import dk.itu.nulx30.networkModel.Router;
import dk.itu.nulx30.networkModel.Wire;

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

/**
 * This class extends the class <code>Ant</code> and implements the
 * interface <code>NetworkPackage</code>. So an <code>AntPackage</code>
 * is both a packet and an ant.
 *
 * @see ACOMetaHeuristic
 * @see dk.itu.nulx30.graph.Graph
 *
 * @author  Mikkel Bundgaard
 * @author  Troels C. Damgaard
 * @author  Federico Decara
 * @author  Jacob W. Winther
 */
public class AntPackage extends Ant implements NetworkPackage {
  /**
   * The destination Router for this AntPackage, cannot be modified after
   * initialization
   */
  private Router destination;

  /**
   * Indicates the Time To Live for this AntPackage - cannot be externaly modified
   */
  private int ttl;

  /** Current time for package*/
  private double time = 0;

  /** The time when the AntPackage is created */
  private double startTime = 0;

  /**
   * Constructor specifying the source router, the destination router,
   * TTL, and the startTime of this package
   *
   * @param s The source router.
   * @param d The destination router.
   * @param ttl time-to-live value.
   * @param startTime the time at which this package was started
   */
  public AntPackage( Router s, Router d, int ttl, int startTime ) {
    super( s );

    destination = d;
    this.ttl = ttl;
    this.startTime = startTime;
  }

  /**
   * Should append the router to the pathList. A
   * <code>PackageTTLException</code> should be thrown if a package has exceeded
   * its TTL limit.
   *
   * @param r Router to add to pathList
   * @param w the wire to move by
   *
   * @exception PackageTTLException a <code>PackageTTLException</code> is
   * thrown if a package has exceeded its TTL limit.
   *
   * @see dk.itu.nulx30.networkModel.exceptions.PackageTTLException
   */
  public void addRouterToPathList( Wire w, Router r ) throws PackageTTLException {
    if ( ttl-- == 0 ) {
      throw new PackageTTLException( "AntPackage from " + getSource().getIndex() +
                " to " + destination.getIndex() + " Timed out " );
    }
    if ( !r.equals( getSource() ) ) {
      makeMove( w, r );
    }
    else {
      makeMove( w, null );          // as the source-router is added when
    }                               // constructing the package
  }

  /**
   * Returns the path list (in Routers) for the package
   *
   * @return the path list for this object
   */
  public ArrayList getPathList(){
    return (ArrayList) getVisitedVertices();  
  }


  /**
   * Returns the source Router for this package
   *
   * @return the source Router for this package
   */
  public Router getSource(){
    return (Router) getStartVertex();
  }

  /**
   * Returns the destination Router for this package
   *
   * @return the destination Router for this package
   */
  public Router getDestination() {
    return destination;
  }

  /**
   * Updates the time of this package to <code>newTime</code>.
   *
   * @param newTime the new time of this object
   */
  public void setTime( double newTime ) {
    time = newTime;
  }

  /**
   * Gets the time of this package
   *
   * @return the time of this package
   */
  public double getTime() {
    return time;
  }

  /**
   * Get the difference between creation time and current time for package
   *
   * @return the time this package has spent in the network
   */
  public double getTimeInNetwork() {
    return time - startTime;
  }
}

⌨️ 快捷键说明

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