NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

JDK1.0.2 simulations (1996-2001) => kinematics => Topic started by: Fu-Kwun Hwang on July 30, 2008, 10:29:42 am

Title: Can we increase traffic flow rate by increasing maximum speed limit?
Post by: Fu-Kwun Hwang on July 30, 2008, 10:29:42 am
Is it possible to increase the traffic flow by increasing the maximum allowed speed on the road?
The answer is NO. Due to reaction time, we need to keep the car in front of us a minimum distance.
If the reaction time is dt, and the speed of the car is v. You need to keep at least s=v*dt to be safe.
If the car is moving faster, the safety distance need to be increase proportionally.

There are 3 lanes in the following applet. The maximum speed Vmax for each lanes are different.
You can change Vmax, if you click Vmax label in the applet and enter your value into the textfield at the top.
Remember to hit ENTER when you are done (and the applet will accept your new value).
Nmax is the allowed maximum of cars on the road.
N is the current number of cars on the road, and n is the total number of cars pass the right edge.
I hope you can enjoy the related physics!

Related topic: Reaction time measurement (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=137.0) , Reaction time and car accident (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=138.0), Traffic Light System (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=139.0)

Title: Re: Can we increase traffic flow rate by increasing maximum speed limit?
Post by: kidashley25 on June 29, 2010, 08:45:03 am
Speed limits could be reduced or increased. Local speed limits are determined by Roads Service traffic. Make sure the exterior of your car is clean, but also focus on the interior.


Title: Re: Can we increase traffic flow rate by increasing maximum speed limit?
Post by: GTRR on August 30, 2010, 05:16:48 pm
hmmm...interesting! I think it is good to study it more.

Title: Re: Can we increase traffic flow rate by increasing maximum speed limit?
Post by: laramsf on May 15, 2013, 03:20:40 am
plz can u give me source code of traffic light system

Title: Re: Can we increase traffic flow rate by increasing maximum speed limit?
Post by: Fu-Kwun Hwang on May 15, 2013, 10:32:28 am
The source code is available as attached file.
It is also listed as following
import java.awt.*;

public class traffic extends java.applet.Applet implements Runnable{
   int yOffset=40;
   TextField iField;   //record time elapse
   double time=0.0,ts=0.;
   Dimension area;
   int N=3;
   //Rectangle   R[]=new Rectangle[N],L[]=new Rectangle[N];
   //Rectangle   R2[]=new Rectangle[N],L2[]=new Rectangle[N];
   //boolean Rs[]=new boolean[N],Ls[]=new boolean[N];
   Image bgImage,fgImage;
   Graphics gb,g;//background drawing
   Color bgColor=Color.lightGray;
   car c[]=new car[N];
   Label XY;
   String rts,STR[]={"Reset","Time"};
   public void init() {
      for(int i=0;i         if((rts=getParameter(STR))!=null)
            STR=new String(rts);
      Panel p=new Panel();
//      p.add(new Label(STR[1]));
//      p.add(timeField=new TextField("0.0",3));
//      timeField.setEditable(false);
      p.add(new Button(STR[0]));
      p.add(iField=new TextField("",3));
      p.add(XY=new Label("(   X   ,   Y   )"));
      //p.add(new Button("Start"));
      reset(true);// false for auto start

   public boolean action(Event ev, Object arg) {
      if (ev.target instanceof Button) {
         String label = (String)arg;
         }/*else if(label.equals("Start")){
      }else if(ev.target==iField&&changed!=null){
         double val=Double.valueOf(iField.getText()).doubleValue();
         if(mode            int dval=5;
               for(int i=0;i                  car.maxCount=dval;
            }else   car.maxCount[mode]=dval;
               for(int i=0;i                  car.vmax=val;
            }else   car.vmax[mode%N]=val;
      return true;
   int xx,yy;
   public void reset(boolean status){
   String d2String(double value){
      float f=(float)((int)(value*100.)/100.);
      String str=String.valueOf(f);
      return str;
   // animation code
   boolean running=true;
   Thread animThread;
   long startTime=0,lastTime;
   long delay=50,delta;
   //  This starts the threads.
   public void start(){
      //Start animating!
      if (animThread == null) {
         animThread = new Thread(this);
      //Remember the starting time. of thread
      lastTime=startTime = System.currentTimeMillis();
    public void stop() {
      //Stop the animating thread.
      animThread = null;

   public void run() {
      //Just to be nice, lower this thread's priority
      //This is the animation loop.
      while (Thread.currentThread() == animThread) {
         //Advance the animation frame. with delta time
         try {
         } catch (InterruptedException e) {
   void advanced(double dt){
      for(int i=0;i         c.advanced(dt);
   boolean rightClick=false;
   Color cpass=Color.green,cstop=Color.red;
   int mode;// 0-2:Nmax,3-5:Vmax,6-8:N
   int clickCount;
   int xs,ys;
   public boolean mouseDown(Event e, int x, int y){
      if((y-=yOffset)<0)return false;
      if(e.modifiers==Event.META_MASK){//"Right Click, ";
         if(x         for(int i=0;i            if(carNmax.inside(x,y)){
            }else   if(carVmax.inside(x,y)){
            //}else   if(carN.inside(x,y)){
            //   mode=2*N+i;
            //   changed=carN;
            //   break;
         else iField.setEditable(false);
      return true;
   public boolean mouseMove(Event e, int x, int y){
      if((y-=yOffset)<0||running)return false;
      XY.setText("("+x+", "+y+")");
      return true;

   public boolean mouseDrag(Event e, int x, int y){
      if((y-=yOffset)<0||running)return false;
      XY.setText("("+(xs-x)+", "+(y-ys)+")");
      return true;
   /*public boolean mouseUp(Event e, int x, int y){
      if((y-=yOffset)<0)return false;
      return true;
   FontMetrics fm;
   int chy,chy2;
   int w=10,w2=2*w,dy,dx,dy2,xi,xf;
   Rectangle carNmax[]=new Rectangle[N];
   Rectangle carVmax[]=new Rectangle[N];
   //Rectangle carN[]=new Rectangle[N];
   Rectangle changed;
   static int ds=80;
   void clear(boolean status){
         bgImage = createImage(area.width, area.height);
         gb = bgImage.getGraphics();
         fgImage = createImage(area.width, area.height);
         g = fgImage.getGraphics();
         int dd=dy2+xi;
         for(int i=0;i         /*   R=new Rectangle(xf,yy,xi,w);
            R2=new Rectangle(xf,yy+dd,xi,w);
            L=new Rectangle(0,yy+dd,xi,w);
            L2=new Rectangle(0,yy,xi,w);*/
            carNmax=new Rectangle(xx=car.xi2,yy=car.yi2+chy,ds,chy);
            //carN=new Rectangle(xx+ds,yy,ds,chy);
            carVmax=new Rectangle(xx,yy+chy,ds,chy);
      }else if(status)car.init(g,w,area.width,area.height);
      gb.fillRect(0, 0, area.width, area.height);
      for(int i=0;i         gb.drawRect(0,yy,w,dy2);
         c=new car(i,0);
/*      for(int i=0;i         Rs=true;
      int yy2=yy-dy;
      for(int i=0;i         gb.drawLine(i,yy,i+w,yy);
//   void drawRect(Graphics g,Rectangle r){
//      g.fillOval(r.x,r.y,r.width,r.height);
//   }
   public void paint(Graphics gs){
   public void update(Graphics gs){
      g.drawImage(bgImage, 0, 0, this);
      for(int i=0;i.draw();
            int dh;
            if(mode            for(int i=0;i               g.fillRect(changed.x,carNmax.y+dh,changed.width,changed.height);
         }   else g.fillRect(changed.x,changed.y,changed.width,changed.height);
      for(int i=0;i         g.drawString("Nmax="+car.maxCount,xx=carNmax.x,yy=carNmax.y+chy);
      // drawing work
      gs.drawImage(fgImage, 0, yOffset, this);

class car extends rk4{
   static int N=3,size,size2,size3,size0;
   static int xmax,xmin,dy2;
   static double ai=10.,vmax[]={20.,30.,40.};   // reaction time set to 1.0 sec
   static double reactionTime=0.5,dmin[]=new double[N]
      ,dmin2[]=new double[N],dmax[]=new double[N];
   static int xi,xf,yi[]=new int[N],yf[]=new int[N];
   static int xi2,xf2,xi3,xf3,yi2[]=new int[N],yf2[]=new int[N];
   static int maxCount[]={20,20,20},count[]={0,0,0},passed[]={0,0,0};
   static car top[]=new car[3];
   static boolean loop[]={false,false,false};
   static double delta;
   static car tmp=null;
   static Graphics g;
   //static boolean lastTurn=false;
   int mode=0;// right,down,left,up
   boolean turn=false;
   boolean gone=false;
   static boolean debug=false,running=true;
   int X,Y,id;
   double a;
   car next=null;
   double accelerated=1.,sign=1.;
   //static int width=8,height=5;
   car(int idi,int modei){
      init(2,true);// 1D motion
   static void init(Graphics gi,int w,int width,int height){
      int y0=(int)(w/4.+0.5);
      int dy=height-w;
      for(int i=0;i         yi=y0;
   static void setRange(int i){
   public void derivs(double x,double y[],double dydx[]){
      dydx[1]=a; //
   void advanced(double dt){
      case 0://right
            if(this!=top[id]&&next!=null && Math.random()>0.5){//非頭尾則決定是否離開
               //if(next!=null && next.y[1]==0.)next.a=next.sign*ai;
               y[1]=y[1]/2.;// 慢
            //if(next!=null )next.turn=true;
         }else if(y[0]>xf2&&next!=null&&next.mode==3)next.turn=true;
             }else if(y[1]==0. && next.mode==3 && next.Y               next.y[1]=0.;
      case 1://down      
            //if(next!=null )next.turn=true;
         }else   {
         //if(turn && Y>yf2[id])y[1]/=2.;
            }else if(y[1]==0. &&next.mode==0&& next.X>xf2){//卡住後車暫停
      case 2://left
         //if(X         //else
         if(y[0]            if(this!=top[id] && next!=null&& Math.random()>0.5){
            //   if(next==null&&top[id].y[0]==0.]top[id].y[1]=top[id].sign*ai;
               //if(next!=null && next.y[1]==0.)next.a=next.sign*ai;
         }else if(y[0]         //if(turn && X         if(next!=null&&next.turn){
            if(X               next.turn=false;
            }else if(y[1]==0. &&next.mode==1&& next.Y>yf2[id]){
      case 3://up
         if(y[0]            y[0]=xi;
            //if(next!=null )next.turn=true;
            if(y[0]         }
         //if(turn && Y         if(next!=null&&next.turn){
            if(Y               next.turn=false;
            }else if(y[1]==0. &&next.mode==2 && next.X               next.y[1]=0.;
    //} catch (NullPointerException e){ }
       }else {
      if(count[id]         y[0]=yi2[id]+dmin2[id];
      }else if(a!=0.){
      }   else y[0]+=y[1]*dt;//等速度運動
         }else if(next.mode==mode){//同一車道
            if(delta>dmax[id])next.accelerated=1.0; // 太遠
            else if(Math.abs(delta)                  next.y[1]=0.;
            }else if(sign*(next.y[1]-y[1])>0.)next.accelerated=-1.;
            if(Math.abs(next.y[1])               next.a=next.sign*ai*next.accelerated;
         }//else {//不同車道
         //   if(next.y[1]==0.&& (mode-next.mode>1. || !next.turn)) next.a=next.sign*ai;
         else if(Math.abs(next.X-X)+Math.abs(next.Y-Y)               next.y[1]=0.;
         if(next.X>xmax || next.X<0.){
      /*   if(loop[id] && count[id]            if(mode==2 && X               next.a=0.;
               tmp=new car(id,2);
            //}else if(mode==0 &&X>xi2){
            //   next.a=0.;
            //   next.y[1]/=2.;
            //   next.turn=true;
            //   tmp=new car(id,0);
            //   tmp.next=next;
            //   next=tmp;
      }else{ // last car
         if(loop[id]&&top[id].mode==mode){//繞過一圈後 同軌道
            else if(Math.abs(delta)                  top[id].y[1]=0.;
            }else if(sign*(y[1]-top[id].y[1])>0.)top[id].accelerated=-1.;
            if(Math.abs(top[id].y[1])               top[id].a=top[id].sign*ai*top[id].accelerated;
            /*/if(y[1]==0. && top[id].y[1]==0.)top[id].a=sign*ai;/*else{//速度太快
         }else   if(mode!=0 && top[id].y[1]==0.){//不同軌道
            //if(mode!=0 || X>xi2)
         if( count[id]            //if( (mode!=0 && top[id].mode>mode)  || X>xi2 && (top[id].mode!=3 || top[id].y[0]==yi2[id]+dmin2[id])&&
            //   (!loop[id] || top[id].mode>mode && top[id].y[0]>yi[id] || top[id].y[1]==0. ) )
            if( top[id].mode>mode ||!loop[id]) {
               if(X>xi2)next=new car(id,0);
            }else if(top[id].mode==mode){
               if(sign*(top[id].y[0]-y[0])>0)next=new car(id,0);
         }else   if(top[id].y[1]==0.){//不同軌道
            if(mode!=0 || X>xi2)
   static int xx,yy;
   void draw(){
         else g.setColor(Color.gray);
      }else if(sign*a>0.)g.setColor(Color.blue);
      else g.setColor(Color.red);
      else g.drawRect(X,Y,size,size);
         else g.drawLine(xx=X+size0,yy=Y+size0,xx-(int)y[1],yy);

Title: Re: Can we increase traffic flow rate by increasing maximum speed limit?
Post by: ericaclayton453 on September 17, 2015, 01:46:19 pm
It is interesting. :)

Title: Re: Can we increase traffic flow rate by increasing maximum speed limit?
Post by: chengfu on October 26, 2015, 04:02:10 pm
Thumbs up admin  ;D never should be die the source code.