Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / Javascript

SIM.JS - Discrete Event Simulation in JavaScript

Rate me:
Please Sign up or sign in to vote.
4.83/5 (4 votes)
28 Mar 2013LGPL36 min read 26.1K   1K   11  
A general-purpose Discrete Event Simulation library written entirely in JavaScript
<html>
	<head>
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
	<script type="text/javascript" src="sim-0.26.js"></script>
	<script type="text/javascript" src="traffic_lights.js"></script>
	<link rel="stylesheet" href="examples.css" type="text/css" />
	
	<script type="text/javascript">
	$(function () {
		$("#run_simulation").click(function () {
			
			var green_time = parseFloat($('[name="GREEN_TIME"]').val());
			var mean_arrival = parseFloat($('[name="MEAN_ARRIVAL"]').val());
			var seed = parseFloat($('[name="SEED"]').val());
			var simtime = parseFloat($('[name="SIMTIME"]').val());
			
			var results = trafficLightSimulation(green_time, mean_arrival, seed, simtime);
			var msg = "Simulation Results for ["
								+ "Green Time = " + green_time
								+ ", Mean Arrival = " + mean_arrival
								+ ", Seed = " + seed
								+ ", Simulation time = " + simtime
								+ "]<br>"
								+ "Average time waiting at intersection (min) = " 
								+ results[0].toFixed(4)
								+ "<br>Average number of vehicles waiting at intersection = " 
								+ results[2].toFixed(4)
								+ "<hr>";
			$(".results").append(msg);
		});
		
		$("#clear_result").click(function () {
			$(".results").html('<a href="" id="clear_results">Clear</a><hr>');
		});
		
	});
	</script>
		
	</head>
	<body>
		<div class="header">
			<a href="traffic_lights.js">View Source</a>
		</div>
		
		<div class="form">
		<table>
			<tr><td>Time that the traffic light is green (min): 
				<td><input type="text" name="GREEN_TIME" value="3.0"/> </tr>
			<tr><td>Mean interval between vehicle arrivals (min):
				<td><input type="text" name="MEAN_ARRIVAL" value="0.5"/> </tr>
			<tr><td>Random number generator seed:
				<td><input type="text" name="SEED" value="1234" /> </tr>
			<tr><td>Simulation time (min):
				<td><input type="text" name="SIMTIME" value="240.0"/> </tr>
		</table>
		<input type="button" value="Run Simulation" id="run_simulation"/>
		</div>
		
		<div class="results">
			<a href="" id="clear_results">Clear</a><hr>
		</div>
	</body>
</html>
	
	

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions