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

📄 activitybasedtripgenerator.java

📁 linux下用于移动节点的移动活动生成工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package de.uni_stuttgart.informatik.canu.tripmodel.generators;

import de.uni_stuttgart.informatik.canu.mobisim.core.*;
import de.uni_stuttgart.informatik.canu.mobisim.notifications.*;
import de.uni_stuttgart.informatik.canu.mobisim.extensions.Graph;
import de.uni_stuttgart.informatik.canu.spatialmodel.core.*;
import de.uni_stuttgart.informatik.canu.spatialmodel.geometry.*;
import de.uni_stuttgart.informatik.canu.tripmodel.core.*;
import de.uni_stuttgart.informatik.canu.tripmodel.pathalgorithms.*;

/**
 * <p>Title: Trip Model</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002-2003</p>
 * <p>Company: University of Stuttgart</p>
 * @author Illya Stepanov
 * @version 1.3
 */

/**
 * This class generates node trips according to automaton of activity sequences
 * @author Illya Stepanov
 */
public class ActivityBasedTripGenerator extends ExtensionModule
                                      implements InitialPositionGenerator,
                                                  TripGenerator
{
  /**
   * Path searching algorithm
   */
  protected PathSearchingAlgorithm algo = new Dijkstra();

  /**
   * Template for automaton of activity sequences
   */
  protected Automaton template_automaton = new Automaton();

  /**
   * Automata for every node
   */
  protected java.util.Map automata = new java.util.HashMap();

  /**
   * Destinations of movements for every node
   */
  protected java.util.Map destinations = new java.util.HashMap();

  /**
   * Flag to reflect or ignore the road directions during the path calculation
   */
  protected int reflect_directions = PathSearchingAlgorithm.FLAG_IGNORE_DIRECTIONS;

  /**
   * Constructor
   */
  public ActivityBasedTripGenerator()
  {
  }

  /**
   * Returns the module's description. <br>
   * <br>
   * @return extension module's description
   */
  public String getDescription()
  {
    return "Activity-Based Trip Model";
  }

  /**
   * Performs the module initialization. <br>
   * <br>
   * The method is called after finishing the scenario file processing.
   */
  public void initialize()
  {
    SpatialModel spatialModel = (SpatialModel)u.getExtension("SpatialModel");
    Graph graph = spatialModel.getGraph();

    // check the points
    java.util.Iterator iter1 = template_automaton.getStates().iterator();
    while (iter1.hasNext())
    {
      State s = (State)iter1.next();
      
      java.util.Iterator iter2 = s.getLocations().iterator();
      while (iter2.hasNext())
      {
        Point p = ((Location)iter2.next()).getPoint();

        if (graph==null)
        { 
          if((p.getX()<0.0)||(p.getY()<0.0)||
             (p.getX()>u.getDimensionX())||(p.getY()>u.getDimensionY()))
          {
            System.err.println("Fatal error: Position is outside Universe dimensions: Position3D("+p.getX()+","+p.getY()+")");
            System.exit(1);
          }
        }
        else
        {
          if((p.getX()<graph.getLeftmostCoordinate())||(p.getY()<graph.getLowermostCoordinate())||
             (p.getX()>graph.getRightmostCoordinate())||(p.getY()>graph.getUppermostCoordinate()))
          {
            System.err.println("Fatal error: Position is outside movement area graph: Position3D("+p.getX()+","+p.getY()+")");
            System.exit(1);
          }
        }
      }
    }
  }

  /**
   * Executes the extension. <br>
   * <br>
   * The method is called on every simulation timestep. 
   * @return 0 - the module should be executed on next timesteps,
   *        -1 - the module should not be executed on further timesteps and should be removed from the extensions' list
   */
  public int act()
  {
    return 0;
  }

  /**
   * Chooses a node initial position according to the automaton of activity sequences. <br>
   * <br>
   * @param node node
   * @return new initial position
   */
  public Point getInitialPosition(Node node)
  {
    java.util.Random rand = u.getRandom();

    // create the automaton for the node
    Automaton a = (Automaton)template_automaton.clone();
    automata.put(node, a);
    
    java.util.ArrayList locations = a.getCurrentState().getLocations();
    Location ld = (Location)locations.get(rand.nextInt(locations.size()));
    destinations.put(node, ld);
    
    return ld.getPoint();
  }

  /**
   * Generates a new trip for the node according to the automaton of activity sequences. <br>
   * <br>
   * @param node node
   * @return new trip for node
   */
  public Trip genTrip(Node node)
  {
    java.util.Random rand = u.getRandom();
    SpatialModel spatialModel = (SpatialModel)u.getExtension("SpatialModel");
    
    Position3D pos = node.getPosition();
    Point ps = new Point(pos);

    // switch automata to the next state
    Automaton a = (Automaton)automata.get(node);
    a.switchToNextState();

    // choose a trip destination
    java.util.ArrayList locations = a.getCurrentState().getLocations();
    Location ld = (Location)locations.get(rand.nextInt(locations.size()));
    destinations.put(node, ld);
    
    Point pd = ld.getPoint();

    Graph graph = spatialModel.getGraph();
    if (graph==null)
    {
      // generate a straight path to the point
      Trip trip = new Trip();
      
      java.util.ArrayList path = trip.getPath();
      path.add(ps);
      path.add(pd);
      
      return trip;
    }
    else
    {
      // use the path searching algorithm to calculate the path
      Trip trip = algo.getPath(spatialModel, node, ps, pd, reflect_directions);
      if (trip==null)
      {
        // add an empty trip
        trip = new Trip();

        java.util.ArrayList path = trip.getPath();
        path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
        path.add(new Point(node.getPosition().getX(), node.getPosition().getY()));
      }
        
      return trip;
    }
  }

  /**
   * Chooses a time of staying at the current position. <br>
   * <br>
   * @param node node
   * @return stay duration (in ms)
   */
  public int chooseStayDuration(Node node)
  {
    // check if the automaton is in the final state
    Automaton a = (Automaton)automata.get(node);
    if (a.isFinalState(a.getCurrentState()))
    {
      // infinite state duration
      return Integer.MAX_VALUE;
    }

    // choose an appropriate stay duration for the current location
    Location ll = (Location)destinations.get(node);
    return (int)(ll.getMinStay()+(ll.getMaxStay()-ll.getMinStay())*u.getRandom().nextFloat());
  }

  /**
   * Initializes the object from XML tag. <br>
   * <br>
   * @param element source tag
   * @throws Exception Exception if parameters are invalid
   */
  public void load(org.w3c.dom.Element element) throws Exception
  {
    u.sendNotification(new LoaderNotification(this, u,
      "Loading ActivityBasedTripGenerator extension"));

    super.load(element);

    org.w3c.dom.Node n;
    String classTag = element.getAttribute("path_algorithm").trim();
    if (classTag.length()!=0)
    {
      algo = (PathSearchingAlgorithm)Class.forName(classTag).newInstance();

⌨️ 快捷键说明

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