intersection simulation

Discussion in 'Java' started by zaebos@gmail.com, Sep 11, 2005.

  1. Guest

    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
    , Sep 11, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 11 Sep 2005 14:59:10 -0700, wrote or quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
    Roedy Green, Sep 12, 2005
    #2
    1. Advertising

  3. jan V Guest

    > 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.
    jan V, Sep 12, 2005
    #3
  4. Oliver Wong Guest

    "Roedy Green" <> wrote in message
    news:...
    >
    > 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!

    > On 11 Sep 2005 14:59:10 -0700, wrote or quoted :
    >
    > > 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.


    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
    Oliver Wong, Sep 12, 2005
    #4
  5. jan V Guest

    > > 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!


    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...
    jan V, Sep 12, 2005
    #5
  6. Guest

    In article <PhhVe.238683$tt5.102757@edtnps90>,
    Oliver Wong <> wrote:
    >
    >"Roedy Green" <> wrote in message
    >news:...
    >>
    >> 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!
    >
    >> On 11 Sep 2005 14:59:10 -0700, wrote or quoted :
    >>
    >> > 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.

    >
    > 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?

    --
    | B. L. Massingill
    | ObDisclaimer: I don't speak for my employers; they return the favor.
    , Sep 13, 2005
    #6
  7. Guest

    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....

    wrote:
    > In article <PhhVe.238683$tt5.102757@edtnps90>,
    > Oliver Wong <> wrote:
    > >
    > >"Roedy Green" <> wrote in message
    > >news:...
    > >>
    > >> 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!
    > >
    > >> On 11 Sep 2005 14:59:10 -0700, wrote or quoted :
    > >>
    > >> > 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.

    > >
    > > 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?
    >
    > --
    > | B. L. Massingill
    > | ObDisclaimer: I don't speak for my employers; they return the favor.
    , Sep 14, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SPG
    Replies:
    15
    Views:
    12,658
    ACE01
    Nov 7, 2006
  2. Replies:
    1
    Views:
    3,371
    Betty
    Apr 11, 2005
  3. Alain Frisch
    Replies:
    3
    Views:
    419
    Richard Tobin
    May 3, 2005
  4. Ryan R. Rosario

    Intersection of Multiple Sets

    Ryan R. Rosario, Sep 6, 2003, in forum: C++
    Replies:
    1
    Views:
    3,711
    Rob Williscroft
    Sep 6, 2003
  5. jasperng
    Replies:
    0
    Views:
    1,297
    jasperng
    Nov 27, 2008
Loading...

Share This Page