コード例 #1
0
  /** Returns a (random) coordinate that is between two adjacent MapNodes */
  @Override
  public Coord getInitialLocation(boolean translate) {
    List<MapNode> nodes = map.getNodes();
    MapNode n, n2;
    Coord n2Location, nLocation, placement;
    double dx, dy;
    double rnd = rng.nextDouble();

    // choose a random node (from OK types if such are defined)
    do {
      n = nodes.get(rng.nextInt(nodes.size()));
    } while (okMapNodeTypes != null && !n.isType(okMapNodeTypes));

    // choose a random neighbor of the selected node
    n2 = n.getNeighbors().get(rng.nextInt(n.getNeighbors().size()));

    nLocation = n.getLocation();
    n2Location = n2.getLocation();

    placement = n.getLocation().clone();

    dx = rnd * (n2Location.getX() - nLocation.getX());
    dy = rnd * (n2Location.getY() - nLocation.getY());
    if (translate == false) {
      dx = 0;
      dy = 0;
    }
    placement.translate(dx, dy); // move coord from n towards n2

    this.lastMapNode = n;
    return placement;
  }
  /**
   * Creates a new instance of HomeActivityMovement
   *
   * @param settings
   */
  public HomeActivityMovement(Settings settings) {
    super(settings);
    distance = 100;
    pathFinder = new DijkstraPathFinder(null);
    mode = WALKING_HOME_MODE;

    String homeLocationsFile = null;
    try {
      homeLocationsFile = settings.getSetting(HOME_LOCATIONS_FILE_SETTING);
    } catch (Throwable t) {
      // Do nothing;
    }

    timeDiffSTD = settings.getInt(STD_FOR_TIME_DIFF_SETTING);

    if (homeLocationsFile == null) {
      MapNode[] mapNodes = (MapNode[]) getMap().getNodes().toArray(new MapNode[0]);
      int homeIndex = rng.nextInt(mapNodes.length - 1);
      homeLocation = mapNodes[homeIndex].getLocation().clone();
    } else {
      try {
        allHomes = new LinkedList<Coord>();
        List<Coord> locationsRead = (new WKTReader()).readPoints(new File(homeLocationsFile));
        for (Coord coord : locationsRead) {
          SimMap map = getMap();
          Coord offset = map.getOffset();
          // mirror points if map data is mirrored
          if (map.isMirrored()) {
            coord.setLocation(coord.getX(), -coord.getY());
          }
          coord.translate(offset.getX(), offset.getY());
          allHomes.add(coord);
        }
        homeLocation = allHomes.get(rng.nextInt(allHomes.size())).clone();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (timeDiffSTD == -1) {
      timeDifference = rng.nextInt(DAY_LENGTH) - DAY_LENGTH / 2;
    } else if (timeDiffSTD == 0) {
      timeDifference = 0;
    } else {
      timeDifference =
          (int)
              Math.min(
                  Math.max((rng.nextGaussian() * timeDiffSTD), -DAY_LENGTH / 2), DAY_LENGTH / 2);
    }
  }