Пример #1
0
  @Override
  public void endLegAndComputeNextState(double now) {
    // Post this event here.

    events.processEvent(new PersonArrivalEvent(now, id, currentLinkId, leg.getMode()));
    super.endLegAndComputeNextState(now);
  }
Пример #2
0
  public static void main(String[] args) {

    // Dummy MATSim scenario including dummy network
    Config conf = ConfigUtils.createConfig();
    conf.global().setCoordinateSystem("EPSG:3395");
    Scenario sc = ScenarioUtils.createScenario(conf);
    //		Sim2DConfig conf2d = Sim2DConfigUtils.createConfig();
    //		Sim2DScenario sc2d = Sim2DScenarioUtils.createSim2dScenario(conf2d);
    //		sc.addScenarioElement(Sim2DScenario.ELEMENT_NAME,sc2d);
    Network net = sc.getNetwork();
    NetworkFactory fac = net.getFactory();
    Node n0 = fac.createNode(Id.create("0", Node.class), new Coord(minX, minY));
    Node n1 = fac.createNode(Id.create("1", Node.class), new Coord(minX, maxY));
    Node n2 = fac.createNode(Id.create("2", Node.class), new Coord(maxX, maxY));
    Node n3 = fac.createNode(Id.create("3", Node.class), new Coord(maxX, minY));
    net.addNode(n0);
    net.addNode(n1);
    net.addNode(n2);
    net.addNode(n3);

    // visualization stuff
    EventsManager em = new EventsManagerImpl();
    EventBasedVisDebuggerEngine vis = new EventBasedVisDebuggerEngine(sc);
    em.addHandler(vis);

    // routing algorithm - for now standard Dijkstra could be replaced by A*  in future
    Dijkstra d = new Dijkstra(env);

    // dummy data
    int number = 20;
    for (double time = 0; time <= 1000; time++) {
      List<XYVxVyEventImpl> events = createDummyData(MatsimRandom.getRandom().nextInt(21), time);

      List<TwoDObject> objs = new ArrayList<TwoDObject>();
      for (XYVxVyEventImpl e : events) {
        em.processEvent(e); // needed for visualization
        objs.add(e);
      }

      System.out.println("");
      LinearQuadTreeLD quad = new LinearQuadTreeLD(objs, env, em);

      // routing
      List<Quad> fromL = quad.query(new Envelope(0, 0.1, 0, 0.1));
      Quad from = fromL.get(0);
      List<Quad> toL = quad.query(new Envelope(19.9, 20, 19.9, 20));
      Quad to = toL.get(0);
      LinkedList<Quad> path = d.computeShortestPath(quad, from, to);
      path.addFirst(from);
      draw(path, em);
      //			to.getEnvelope().get
      try {
        Thread.sleep(2000); // to slow it down a bit
      } catch (InterruptedException e1) {
        e1.printStackTrace();
      }
    }
  }
Пример #3
0
 private static void draw(Quad quad, EventsManager em) {
   RectEvent re =
       new RectEvent(
           0,
           quad.getEnvelope().getMinX(),
           quad.getEnvelope().getMaxY(),
           quad.getEnvelope().getWidth(),
           quad.getEnvelope().getHeight(),
           false);
   em.processEvent(re);
 }
Пример #4
0
  private static void draw(LinkedList<Quad> path, EventsManager em) {
    if (path.size() == 0) {
      return;
    }
    for (Quad q : path) {
      draw(q, em);
    }

    Quad start = path.get(0);
    double x = start.getEnvelope().getMinX() + start.getEnvelope().getWidth() / 2;
    double y = start.getEnvelope().getMinY() + start.getEnvelope().getHeight() / 2;

    for (int i = 1; i < path.size(); i++) {
      Quad next = path.get(i);
      LineSegment ls = new LineSegment();
      ls.x0 = x;
      ls.y0 = y;
      x = next.getEnvelope().getMinX() + next.getEnvelope().getWidth() / 2;
      y = next.getEnvelope().getMinY() + next.getEnvelope().getHeight() / 2;
      ls.x1 = x;
      ls.y1 = y;
      em.processEvent(new LineEvent(0, ls, false, 0, 255, 255, 255, 0));
    }
  }
  private static void runIt(
      Network net, List<Link> linksLR, List<Link> linksRL, Scenario sc, double th, Setting s) {
    // visualization stuff
    EventsManager em = new EventsManagerImpl();
    // // // if (iter == 9)

    if (AbstractCANetwork.EMIT_VIS_EVENTS) {
      EventBasedVisDebuggerEngine vis = new EventBasedVisDebuggerEngine(sc);
      em.addHandler(vis);
      QSimDensityDrawer qDbg = new QSimDensityDrawer(sc);
      em.addHandler(qDbg);
      vis.addAdditionalDrawer(new InfoBox(vis, sc));
      vis.addAdditionalDrawer(qDbg);
    }
    CANetworkFactory fac;
    if (USE_MULTI_LANE_MODEL) {
      fac = new CAMultiLaneNetworkFactory();
      fac.setDensityEstimatorFactory(new CAMultiLaneDensityEstimatorSPHFactory());

    } else {
      fac = new CASingleLaneNetworkFactory();
      if (USE_SPH) {
        fac.setDensityEstimatorFactory(new CASingleLaneDensityEstimatorSPHFactory());
      } else {
        fac.setDensityEstimatorFactory(new CASingleLaneDensityEstimatorSPAFactory());
      }
    }

    CANetwork caNet = fac.createCANetwork(net, em, null);

    int agents = 0;
    {
      CAMultiLaneLink caLink = (CAMultiLaneLink) caNet.getCALink(linksLR.get(0).getId());
      int lanes = caLink.getNrLanes();
      CAMoveableEntity[] particles = caLink.getParticles(0);
      System.out.println("part left:" + particles.length);
      double oldR = 1;
      int tenth = particles.length / 10;
      int cnt = 0;
      for (int i = particles.length - 1; i >= 0; i--) {
        for (int lane = 0; lane < lanes; lane++) {
          double r = MatsimRandom.getRandom().nextDouble();
          if (r > th) {
            continue;
          }
          cnt++;
          if (cnt > s.pL) {
            break;
          }
          // agents++;
          CAMoveableEntity a =
              new CASimpleDynamicAgent(
                  linksLR, 1, Id.create("g" + agents++, CASimpleDynamicAgent.class), caLink);
          a.materialize(i, 1, lane);
          caLink.getParticles(lane)[i] = a;
          CASimAgentConstructEvent ee = new CASimAgentConstructEvent(0, a);
          em.processEvent(ee);

          CAEvent e =
              new CAEvent(
                  1 / (AbstractCANetwork.V_HAT * AbstractCANetwork.RHO_HAT),
                  a,
                  caLink,
                  CAEventType.TTA);
          caNet.pushEvent(e);
          caNet.registerAgent(a);
        }
      }
    }

    {
      CAMultiLaneLink caLink = (CAMultiLaneLink) caNet.getCALink(linksRL.get(0).getId());
      int lanes = caLink.getNrLanes();
      CAMoveableEntity[] particles = caLink.getParticles(0);
      System.out.println("part left:" + particles.length);
      double oldR = 1;
      int tenth = particles.length / 10;
      int cnt = 0;
      for (int i = 0; i < particles.length - 1; i++) {
        // for (int i = 0; i < 0; i++) {
        for (int lane = 0; lane < lanes; lane++) {
          double r = MatsimRandom.getRandom().nextDouble();
          if (r > th) {
            continue;
          }
          cnt++;
          if (cnt > s.pR) {
            break;
          }
          // agents++;
          CAMoveableEntity a =
              new CASimpleDynamicAgent(
                  linksRL, 1, Id.create("b" + agents++, CASimpleDynamicAgent.class), caLink);
          a.materialize(i, -1, lane);
          caLink.getParticles(lane)[i] = a;
          CASimAgentConstructEvent ee = new CASimAgentConstructEvent(0, a);
          em.processEvent(ee);

          CAEvent e =
              new CAEvent(
                  1 / (AbstractCANetwork.V_HAT * AbstractCANetwork.RHO_HAT),
                  a,
                  caLink,
                  CAEventType.TTA);
          caNet.pushEvent(e);
          caNet.registerAgent(a);
        }
      }
    }

    Monitor monitor =
        new CALinkMultiLaneMonitor((CAMultiLaneLink) caNet.getCALink(Id.createLinkId("2")), 3.);

    // CALinkMonitorExact monitor = new CALinkMonitorExactIIUni(
    // caNet.getCALink(Id.createLinkId("0")), 10.,
    // ((CAMultiLaneLink) caNet.getCALink(Id.createLinkId("0")))
    // .getParticles(0), caNet.getCALink(Id.createLinkId("0"))
    // .getLink().getCapacity());
    caNet.addMonitor(monitor);
    monitor.init();
    caNet.run(3600);
    try {
      monitor.report(bw);
      bw.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #6
0
 private void processEvent(Event event) {
   eventsManager.processEvent(event);
 }