js-simulator
Inspired by the MASON Multiagent Simulation library in Java, js-simulator is a general-purpose discrete-event multiagent simulator for agent-based modelling and simulation. It was written entirely in Javascript.
Install
Run the following npm command to install
npm install js-simulator
Demo
The following HTML demo is available:
- Flocking Boids HTML DEMO
- Conway's Game of Life HTML DEMO
- School Yard HTML DEMO
- L-System (Fractal Plant) HTML DEMO
- L-System (Dragon Curve) HTML DEMO
- Ant System HTML DEMO
- Particle Swarm HTML DEMO
- Virus HTML DEMO
- (Spring) Balls HTML DEMO
- Solar System HTML DEMO
- Near Beer Game HTML DEMO
More demo and HTML GUI supports will be added in the subsequent releases.
Usage
Create and schedule discrete events or agents
The discrete-event simulator is managed via the Scheduler class, which can be created as shown below:
jssim = ;var scheduler = ;
The scheduler schedules and fires events based on their time and rank (i.e. the order of the event) spec.
To schedule the an event to fire at a particular time:
var rank = 1; // the higher the rank, the higher the priority assigned and the higher-rank event will be fired first for all events occurring at the same time intervalvar evt = rank;evtid = 20; evt { console; // the code below allows the evt to send message to another agent (i.e., the agent referred by receiver variable) /* var receiver_id = receiver.guid() this.sendMsg(receiver_id, { content: "Hello" }); */ // the code below allows the evt to process messages sent from another agent /* var messages = this.readInbox(); for(var i = 0; i < messages.length; ++i){ var msg = messages[i]; var sender_id = msg.sender; var recipient_id = msg.recipient; // should equal to this.guid() var time = msg.time; var rank = msg.rank; // the messages[0] contains the highest ranked message and last messages contains lowest ranked var content = msg.content; // for example the "Hello" text from the sendMsg code above } */}; var time_to_fire = 10; // fire this event at time = 10scheduler;
The main logic for an event is defined in its update(deltaTime) method, as shown in the code above. Events with higher ranks and earlier time_to_fire will always be executed first by the scheduler.
An event can also be sheduled to fire at a later time from the current time (e.g., such an event can be fired within another event):
var delta_time_later = 10; // the event will be fired 10 time units from now, where now refers to the current scheduler timescheduler;
In terms of multi-agent system, an event can be thought of as an agent. Such an agent may need to execute repeatedly. In the js-simulator, this is achieved by firing an event repeatedly at a fixed interval:
var interval = 2; // time interval between consecutive firing of the eventvar start_time = 12; // time to fire the event for the first timescheduler;
If the start_time is at from the start of the simulation, then the above scheduling can also be replaced by:
scheduler;
Execute the scheduler loop for the main discrete-event simulation
After the events/agents are scheduled, they are not fired immediately but only fired when scheduler.update() method is called, each call to scheduler.update() to move the time forward. At each time forwarded, events with higher rank will be executed (by calling their update(delaTime) method) first. Also events with the same rank will be shuffled before execution.
The scheduler can be executed in the following loop:
whilescheduler var evts_fired = scheduler;
The above will run until no more events to fire in the scheduler, to stop the scheduler at a particular instead, use the following loop:
whileschedulercurrent_time < 20 // stop the scheduler when current scheduler time is 20 scheduler;
The current scheduler time can be obtained by calling (this is useful if we want to know the current time inside the scheduler):
var current_scheduler_time = schedulercurrent_time;
Sample Codes
Flocking behavior Demo
The source code below shows how to create a flocking of 15 boids (12 preys and 3 predators) that demonstrate the flocking principles:
Firstly we will declare a Boid class the inherits from the jsssim.SimEvent class, which defines the behavior of a single boid:
var jssim = ; var { var rank = 1; jssimSimEvent; thisid = id; thisspace = space; thisspace; thissight = 75; thisspeed = 12; thisseparation_space = 30; thisvelocity = Math Math; thisisPredator = isPredator; thisborder = 100;}; Boidprototype = Object;Boidprototype { var boids = thisspace; var pos = thisspace; ifthisisPredator var prey = null; var min_distance = 10000000; for var boidId in boids var boid = boidsboidId; if!boidisPredator var boid_pos = thisspace; var distance = pos; ifmin_distance > distance min_distance = distance; prey = boid; ifprey != null var prey_position = thisspace; thisvelocityx += prey_positionx - posx; thisvelocityy += prey_positiony - posy; else for var boidId in boids var boid = boidsboidId; var boid_pos = thisspace; var distance = pos; if boid != this && !boidisPredator if distance < thisseparation_space // Separation thisvelocityx += posx - boid_posx; thisvelocityy += posy - boid_posy; else if distance < thissight // Cohesion thisvelocityx += boid_posx - posx * 005; thisvelocityy += boid_posy - posy * 005; if distance < thissight // Alignment thisvelocityx += boidvelocityx * 05; thisvelocityy += boidvelocityy * 05; if boidisPredator && distance < thissight // Avoid predators. thisvelocityx += posx - boid_posx; thisvelocityy += posy - boid_posy; // check speed var speed = thisvelocitylength; ifspeed > thisspeed thisvelocity; posx += thisvelocityx; posy += thisvelocityy; // check boundary var val = thisboundary - thisborder; if posx < thisborder posx = thisboundary - thisborder; if posy < thisborder posy = thisboundary - thisborder; if posx > val posx = thisborder; if posy > val posy = thisborder; console;};
Once the boid is defined we can then create and schedule the flocking event simulator using the code below:
var scheduler = ;scheduler; var space = ;forvar i = 0; i < 15; ++i var is_predator = i > 12; var boid = i 0 0 space is_predator; scheduler; whileschedulercurrent_time < 20 scheduler;
Conway's Game of Life
The sample code below shows how to create the game of life simulation:
var jssim = ; var { jssimSimEvent; thisworld = world;}; CellularAgentprototype = Object;CellularAgentprototype { var width = thisworldwidth; var height = thisworldheight; var past_grid = thisworld; forvar i=0; i < width; ++i forvar j = 0; j < height; ++j var count = 0; forvar dx = -1; dx < 2; ++dx var x = i + dx; if x >= width x = 0; if x < 0 x = width - 1; forvar dy = -1; dy < 2; ++dy var y = j + dy; ify >= height y = 0; ify < 0 y = height - 1; count += past_grid; if count <= 2 || count >= 5 thisworld; // dead if count == 3 thisworld; // live }; var scheduler = ;var grid = 640 640; scheduler;grid; grid;grid;grid;grid;grid;grid;grid; scheduler; whileschedulercurrent_time < 20 // this assumes that we want to terminate at time 20 scheduler;
School Yard Demo
The sample code below shows the school yard demo:
var { jssimSimEvent; thisid = id; thisyard = yard; thisnetwork = network; thisMAX_FORCES = 30; thisforceToSchoolMultiplier = 001; thisrandomMultiplier = 01;};Studentprototype = Object;Studentprototype { var students = thisyard; var me = thisyard; var sumForces = 0 0; var forceVector = 0 0; var edges = thisnetwork; var len = edgeslength; for var buddy = 0; buddy < len; ++buddy var e = edgesbuddy; var buddiness = einfo; var him = thisyard; if buddiness >= 0 forceVector; if forceVectorlength > thisMAX_FORCES forceVector; else forceVector; if forceVectorlength > thisMAX_FORCES forceVector; else ifforceVectorlength > 0 forceVector; sumForces; sumForces; sumForces; sumForces; mex = sumForcesx; mey = sumForcesy; console;}; var scheduler = ;var yard = ;var network = 30;yardwidth = 50;yardheight = 50; scheduler;yard;network;forvar i=0; i < 30; ++i var student = i yard network; yard; scheduler; var buddies = {};for var i = 0; i < 30; ++i var student = i; var studentB = i; do studentB = Math; while student == studentB; var buddiness = Math; if!network network; var studentB = i; do studentB = Math; while student == studentB; buddiness = Math; if!network network; while schedulercurrent_time < 2 scheduler;