intersection simulation

Z

zaebos

X-No-Archive

Hello,

I am teaching myself about java threads and concurrency, and have a
textbook exercise that I am trying to implement, but I am not sure if
my design is even on the right track.

The problem is to model an intersection with trafficlights and 2
sensors at each trafficlight, one for arriving and entering, and one
for exiting..., and cars as threads.

the time the car stays in for, the length of time it takes the lights
to switch colors, and the number of cars must all be definable and easy
to change..

I was consiering having each car as a thread, and sensors and
trafficlights as a monitors, when a sensor detects a car arriving, it
notifies the trafficlight to change, and trafficlight will block or
allow the car thread to run, ie enter or leave...

im not sure if im even on the right track, and cant even find any
example solutions to this problem, so if anyone has any idea I would be
very gratefull

Jason
 
R

Roedy Green

im not sure if im even on the right track, and cant even find any
example solutions to this problem, so if anyone has any idea I would be
very gratefull

Way way back in the days of the IBM 360 there was a language for
solving these sorts of problems -- GPSS. I looked it up on the web and
lo, it is still kicking!
http://www.wolverinesoftware.com/GPSSHOverview.htm

Anyway I invented a similar language embedded in PL/I I called QSL
that ran simulations many times faster..

The way it works is you have an event queue. Things that are going to
happen in future are in the event queue sorted by time.

There a master process that reads the next event and interprets it,
which often results in adding more events to the queue.

You only need one thread.

So for example there might be an event for a light turning green. When
you interpret that event, you schedule another for the light turning
red in future, and change the state variable for the light (not in the
queue) for other events to look at.

What you want to avoid is a real time simulation where threads sleep
for 10 minutes, just the way real objects would wait in the real
world.
 
J

jan V

The problem is to model an intersection with trafficlights and 2
sensors at each trafficlight, one for arriving and entering, and one
for exiting..., and cars as threads.

I'm not going to pronounce on your current design, but if I were you, I'd
definitely add a visualization layer/element/whatever so that you can
actually see your intersection with all the components you're modeling. It
will aid debugging, if anything.
 
O

Oliver Wong

Roedy Green said:
Way way back in the days of the IBM 360 there was a language for
solving these sorts of problems -- GPSS. I looked it up on the web and
lo, it is still kicking!
http://www.wolverinesoftware.com/GPSSHOverview.htm

Oh God...

I thought I would never have to see those 4 letters again, but now I see
it has come back to haunt me!

I think the OP is more interested in learning about Java threads and
concurrency than in modelling a traffic light intersection specifically.
Unfortunately, while I did take a course in concurrent programming, it's not
my speciality, so I don't feel qualified to comment on the OP's design,
other than to try to steer the thread back towards Java and as far away from
GPSS as possible.

- Oliver
 
J

jan V

Way way back in the days of the IBM 360 there was a language for
Oh God...

I thought I would never have to see those 4 letters again, but now I see
it has come back to haunt me!

Roedy likes dredging up computing corpses from the past. A bit like a dear
old grandfather who keeps talking about the war (the First, that is). ;-)
That is not to say that there's no occasional wisdom to be gleaned from such
stories...
 
B

blmblm

Oh God...

I thought I would never have to see those 4 letters again, but now I see
it has come back to haunt me!


I think the OP is more interested in learning about Java threads and
concurrency than in modelling a traffic light intersection specifically.
Unfortunately, while I did take a course in concurrent programming, it's not
my speciality, so I don't feel qualified to comment on the OP's design,
other than to try to steer the thread back towards Java and as far away from
GPSS as possible.

Seconded -- if the point is to model the intersection, Roedy's approach
sounds better. If the point is to learn about threads, though ....

I'm not really an expert on this kind of design, but the OP's design
sounds like it could be useful as a way of exploring threads and
concurrency. I might take it a step further and create a thread for
each of the objects that are interacting, i.e., a thread for each
car and also a thread for the traffic light. Keep us posted, maybe,
on how it goes?
 
Z

zaebos

well, i have a basic intersection happening, but I am unsure how to
implement some sort of sensor, after trying various things.

what i have so far:

/* most simple intersection ***************************

pedestrian road
| |
| |* light for cars
------------ -------------
car road

------------ -------------
light for cars *| |
| |

*********************************************************/

import java.util.*;

public class Traffic{
public static void main(String[] args){
Random random = new Random();
int n = 0;

while (++n < 100){
new Car().start();
try{
Thread.sleep(500 + random.nextInt(1000)); //car arriving
intervals
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}
}

class InterSection{
static String[] nums = {"First", "Second", "Third", "Fourth",
"Fifth",
"Sixth", "Seventh", "Eigth", "Nineth", "Tenth", "Eleventh",
"Twelfth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth",
"", "", "", "", "", "", "", "", ""};
TraficLight tt;
int carNum; // temporary car number at the intersection

public InterSection(){
tt = new TraficLight(this);
carNum = 0;
}

public boolean carCanGo(){
return ! tt.isRed();
}

public void carArrived(){
System.out.println(nums[carNum] + " car arrived");
++carNum;
}

public void carPassed(){
System.out.println(nums[carNum - 1] + " car passed");
--carNum;
}
}

class Car extends Thread{
static InterSection is;
static int num = 0; // serial number of passing cars
boolean printed;

public Car(){
++num;
if (is == null){
is = new InterSection();
}
printed = false;
}

public void run(){
is.carArrived(); //story starts at a car arriving the intersection
while (! is.carCanGo()){
if (! printed){
System.out.println("...and waiting(car serial num: " + num +
")");
printed = true;
}
}
is.carPassed(); //story ends at car leaving the intersection
}
}

class TraficLight{
boolean red;
Timer timer;
TimerTask ttask;
long delay;
InterSection is;

public TraficLight(InterSection s){
timer = new Timer();
ttask = new TimerTask(){
public void run(){
if (is != null && is.carNum == 1){
setRed(false); //first arriving car has privilege
cancel();
}
else{
setRed(! isRed());
}
}
};
delay = 8000; // eight second interval
setRed(true);
timer.schedule(ttask, delay, delay);
}

public boolean isRed(){
return red;
}

public void setRed(boolean value){
red = value;
}
}

however I have run into problems attempting to make it a 2 way
intersection or to have some sort of sensor.., i wanted 2 sensors for
each direction, 1 for arriving/entering and one for exiting the
intersection....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top