views:

784

answers:

4
+1  Q: 

Simulation in java

Hi, I am novice to the simulation world, and want to learn how programmers develop real simulation projects in java. I would use eclipse. Could anyone point to other things that I need to know (e.g. other packages, software etc. and their purposes)?

I am afraid the question might seem a bit vague as it is not clear which type of project I am talking about. But being a novice, let me say that it's to begin how to code a simulation project.

+1  A: 

The short answer is that it depends.

Unless you can make the question more specific, there is no way to give an answer.

What do you want to simulate?

For example, if you want to simulate adding two numbers, you can do it using something like:

a = b + c;

If you want to simulate the bouncing of a ball, you can do that using a little bit of math equations and the graphic libraries.

If you want to simulate a web browser, you can do that too.

So the exact answer depends on what simulation you want to do.

Niyaz
+5  A: 

If you are building a Monte-Carlo model for a discrete event simulation or simulation model for pricing derivatives you should find there is a body of framework code already out there. If you are doing a numerical simulation such as a finite-element model you will be basing your simulation on a matrix computation library. Other types of simulation exist, but these are the two most likely cases.

I've never written a finite element model and know next to nothing about these, although I did have occasion to port one to DEC Visual FORTRAN at one point. Although the program (SAFIR, if anyone cares) was commented in French, the porting exercise consisted of modifying two date functions for a total of 6 lines of FORTRAN code - and writing a makefile.

Monte-Carlo models consist of measuring some base population to get distributions of one or more variables of interest. Then you take a Pseudo-Random number generator with good statistical and geometric properties (the Mersenne Twister algorithm is widely used for this) and write a function to convert the output of this to a random variable with the appropriate distribution. You will probably be able to find library functions that do this unless your variables have a really unusual distribution.

Then you build or obtain a simulation framework and write a routine that takes the random variables and does whatever computation you want to do for the model. You run it, storing the results of each simulation, until the error is within some desired tolerance level. After that, you calculate statistics (means, distributions etc.) from all of the runs of the simulation model.

There are quite a lot of resources on the web, and many books on simulation modelling, particularly in the area of derivatives pricing. You should hunt around and see what you can find.

As an aside, the random module on Python has conversion functions for quite a few distributions. If you want one you could get that and port the appropriate conversion function to java. You could use the output of the python one with the same random number seed to test the correctness of the java one.

ConcernedOfTunbridgeWells
+2  A: 

Discrete-event simulation is a good option for problems that can be modeled as individual events that take place at specific times. Key activities are:

  • randomly generating times and durations based on empirical data, and
  • accumulating statistics as the simulation runs.

For example, you could simulate the activity in a parking garage as the entries and departures of a cars and the loss of customers who can't enter because the garage is full. This can be done with two model classes, a Car and the Garage, and three infrastructure classes, an Event class (described below), a Schedule to manage events, and a Monitor to accumulate data.

Here's a brief sketch of how it could work.

Event

An Event has a time, and represents calling a specific method on an object of a specific class.

Schedule

The Schedule keeps a queue of Events, ordered by Event time. The Schedule drives the overall simulation with a simple loop. As long as there are remaining Events (or until the Event that marks the end of the simulation run):

  1. take the earliest Event from the queue,
  2. set the "world clock" to the time of that event, and
  3. invoke whatever action the Event specifies.

Car

The Car class holds the inter-arrival and length-of-stay statistics.

When a Car arrives, it:

  1. logs its arrival with the Monitor,

  2. consults the world clock, determines how long before the next Car should arrive, and posts that arrival Event on the Schedule.

  3. asks the Garage whether it is full:

    1. if full, the Car logs its departure as a lost customer with the Monitor.

    2. if not full, the Car:

      1. logs its entry with the Monitor,

      2. tells the Garage it has entered (so that the Garage can decrease its available capacity),

      3. determines how long it will stay, and posts its departure Event with the Schedule.

When a Car departs, it:

  1. tells the Garage (so the Garage can increase available capacity), and

  2. logs its departure with the Monitor.

Garage

The Garage keeps track of the Cars that are currently inside, and knows about its available capacity.

Monitor

The Monitor keeps track of the statistics in which you're interested: number of customers (successfully-arriving Cars), number of lost customers (who arrived when the lot was full), average length of stay, revenue (based on rate charged for parking), etc.

A simulation run

Start the simulation by putting two Events into the schedule:

  1. the arrival of the first Car (modeled by instantiating a Car object and calling its "arrive" event) and

  2. the end of the simulation.

Repeat the basic simulation loop until the end-of-simulation event is encountered. At that point, ask the Garage to report on its current occupants, and ask the Monitor to report the overall statistics for the session.

joel.neely
+1  A: 

Come up with a problem first.

There's no such things as a generic "simulation". There are lots of techniques out there.

If you're just a gamer who wants to have pseudo-physics, maybe something like this would be what you had in mind.

duffymo