Вы находитесь на странице: 1из 41

Dr.

B R Ambedkar National Institute of Technology-Jalandhar


GT Road Bye Pass, Jalandhar -144011(Punjab)

Assignment: 03
System Simulation and Modeling
CSX-404
Department of Computer Science and Engineering
Jan-June 2016

Submitted to:

Submitted by:

Mr. Manoj Kumar


Assistant Professor
Dept. of CSE

Raj Kunwar Singh


12103060
CSE (Final year)

Contents
Sr. No.

PROGRAM

9.

Write a Program to Implement


Random Number Generator
Write a Program to Implement
Linear Congruential Generator
Write a Program to Implement
Testing of Random Numbers.(Chi
square)
Write a Program to Implement
Monte Carlo Simulation.
Write a Program to Implement
Simulation of LAG Model.
Write a Program to Implement
COBWEB Model
Write a Program to Implement
Simulation of Single Queue Server
System
Write a Program to Implement
Simulation of 2 Queue Server
System
Write a Program to Implement
Simulation of Inventory System

10.

Write a Program to Implement


Simulation of Telephonic system.

1.
2.

3.
4.
5.
6.
7.

8.

PAGE No.

SIGNATURE

3
5

7
9
11
13
16

23

33

35

1. Write a program for rolling a fair dice that should generate a discrete random variable,
and draw the graph showing the number of 1's 2's 3's6s.
Theory :
A random number generator (RNG) is a computational or physical device designed to
generate a sequence of numbers or symbols that can not be reasonably predicted better than
by a random chance.
Various applications of randomness have led to the development of several different methods
for generating random data, of which some have existed since ancient times,
including dice, coin flipping, the shuffling of playing cards and many other techniques.
Because of the mechanical nature of these techniques, generating large numbers of
sufficiently random numbers (important in statistics) required a lot of work and/or time.
Thus, results would sometimes be collected and distributed as random number tables.
Nowadays, after the advent of computational random-number generators, a growing number
of government-run lotteries and lottery games have started using RNGs instead of more
traditional drawing-methods. RNGs are also used to determine the odds of modern slot
machines
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
srand (time(NULL));
int n,i,x;
int Cnt[7];
memset(Cnt,0,sizeof(Cnt));
cout<<"Enter the number of times you want to Roll the dice:\n";
cin>>n;
for(i=0;i<n;i++){
x=rand()%6;
x++;
Cnt[x]++;
}
for(int i=1;i<7;i++)
cout<<"Count of "<<i<<" ="<<Cnt[i]<<endl;
return 0;
}

Output:

Graph:

2. Write a program to implement Linear Congruential Generator.


Theory:
A linear congruential generator (LCG) is an algorithm that yields a sequence of pseudorandomized numbers calculated with a discontinuous piecewise linear equation. The method
represents one of the oldest and best-known pseudorandom number generator algorithms.The
theory behind them is relatively easy to u
nderstand, and
they are easily implemented and fast, especially on computer hardware which can
provide modulo arithmetic by storage-bit truncation.
The generator is defined by the recurrence relation:
where

is the sequence of pseudorandom values, and


the "modulus"
the "multiplier"
the "increment"

the "seed" or "start value"


are integer constants that specify the generator.
Zi=(a*Zi-1 +c)mod m
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,c,m,z1,z2;
cout<<"Enter the value of a,c,m and Z0\n";
cin>>a>>c>>m>>z1;
cout<<"Enter the number of Random Variables\n";
int n,i;
cin>>n;
for(i=0;i<n;i++){
z2=(a*z1+c)%m;
cout<<"Value of Z"<<i+1<<" is "<<z2<<endl;
z1=z2;
}
return 0;
}

Output:

3. Write a program to implement Chi-Square test


Theory:
A chi-squared test, also referred to as
test (or chi-square test), is any statistical hypothesis
test in which the sampling distribution of the test statistic is a chi-square distribution when
the null hypothesis is true. Chi-squared tests are often constructed from a sum of squared errors,
or through the sample variance. A chi-squared test can then be used to reject the null hypothesis
that the data are independent. The chi-squared test is used to determine whether there is a
significant difference between the expected frequencies and the observed frequencies in one or
more categories.
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int i,n;
cout<<"Enter the loop count\n";
cin>>n;
float o[10], t=0;
float chi=0,e;
cout<<"\nEnter the observed frequencies\n";
for(i=0;i<n;i++){
cin>>o[i];
t=t+o[i];
}
for(i=0;i<n;i++){
t=t+o[i];
}
e=t/n;
cout<<endl<<t<<endl<<e<<endl;
for(i=0;i<n;i++){
chi=chi+(pow((o[i]-e),2)/e);
}
cout<<"Value of Chi is "<<chi;
return 0;
}

Output :

4. Write a program to implement Monte Carlo Simulation.


Theory:
Monte Carlo methods (or Monte Carlo experiments) are a broad class
of computational algorithms that rely on repeated random sampling to obtain numerical results.
They are often used in physical and mathematical problems and are most useful when it is
difficult or impossible to use other mathematical methods. Monte Carlo methods are mainly used
in three distinct problem classes: optimization, numerical integration, and generating draws from
a probability distribution.
Monte Carlo methods vary, but tend to follow a particular pattern:
1. Define a domain of possible inputs.
2. Generate inputs randomly from a probability distribution over the domain.
3. Perform a deterministic computation on the inputs.
4. Aggregate the results.
For example, consider a circle inscribed in a unit square. Given that the circle and the square
have a ratio of areas that is /4, the value of can be approximated using a Monte Carlo method:
1. Draw a square on the ground, then inscribe a circle within it.
2. Uniformly scatter some objects of uniform size (grains of rice or sand) over the square.
3. Count the number of objects inside the circle and the total number of objects.
4. The ratio of the two counts is an estimate of the ratio of the two areas, which is /4.
Multiply the result by 4 to estimate .
In this procedure the domain of inputs is the square that circumscribes our circle. We generate
random inputs by scattering grains over the square then perform a computation on each input
(test whether it falls within the circle). Finally, we aggregate the results to obtain our final result,
the approximation of .
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
float r,n,N,x,y,T;
cout<<"Enter the no of tries"<<endl;
cin>>N;
n=0;
cout<<"Enter Radius of circle"<<endl;
cin>>r;
cout<<endl;
srand(time(NULL));
for(T=0;T<N;T++){
x=rand()%100;
x=x/100;
y=rand()%100;
y=y/100;
x=x*r;
y=y*r;
9

if(((x*x)+(y*y))<=(r*r))
n++;
}
float pi;
pi=4*n/N;
cout<<"The value of PI calculated by Monte Carlo Simulation is "<<pi;
return 0;
}
Output:

10

5. Write a program to implement LAG Model.


Theory:
Many econometric models are dynamic, using lagged variables to incorporate feedback over
time. By contrast, static time series models represent systems that respond exclusively to
current events.
Lagged variables come in several types:
Distributed Lag (DL) variables are lagged values
of observed exogenous predictor
variables .
Autoregressive (AR) variables are lagged values
of observed endogenous response
variables .
Moving Average (MA) variables are lagged values
of unobserved stochastic
innovations processes .
Dynamic models are often constructed using linear combinations of different types of lagged
variables, to create ARMA, ARDL, and other hybrids. The modeling goal, in each case, is to
reflect important interactions among relevant economic factors, accurately and concisely.
Some models, such as seasonal models, use lags at distinct periods in the data.
A distributed-lag model is a dynamic model in which the effect of a regressor x on y occurs
over time rather than all at once.
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
double I,Yr,Tx,C;
int i,n;
cout<<"For How many years do you want to calculate"<<endl;
cin>>n;
int years[n];
cout<<"Enter the years:"<<endl;
for(i=0;i<n;i++)
cin>>years[i];
int G[n];
cout<<"Enter the values of G"<<endl;
for(i=0;i<n;i++)
cin>>G[i];
cout<<"Enter the value of Yr"<<endl;
cin>>Yr;
for(i=0;i<n;i++){
cout<<"For year "<<years[i]<<endl;
11

I=2+(0.1)*Yr;
Yr=45.45+(2.27)*(I+G[i]);
Tx=(0.2)*Yr;
C=20+(0.7)*(Yr-Tx);
cout<<"Value of I: "<<I<<" Value of C: "<<C<<endl<<endl;
}
}
Output:

12

6. Write a program to Implement COBWEB Model.


Theory:
The cobweb model or cobweb theory is an economic model that explains why prices might be
subject to periodic fluctuations in certain types of markets. It describes cyclical supply and
demand in a market where the amount produced must be chosen before prices are observed.
Producers' expectations about prices are assumed to be based on observations of previous
prices. Nicholas Kaldor analyzed the model in 1934, coining the term "cobweb theorem" (see
Kaldor, 1938 and Pashigian, 2008), citing previous analyses in German by Henry
Schultz and Umberto Ricci. The cobweb model is based on a time lag between supply and
demand decisions.
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
double a,b,c,d,P,D,S;
cout<<"Enter the value of P0:\n";
cin>>P;
cout<<"Enter the value of a,b,c,d\n";
cin>>a>>b>>c>>d;
D = a-b*P;
S = c+d*P;
cout<<"P"<<"\t"<<"D"<<"\t"<<"S"<<endl;
cout<<P<<"\t"<<D<<"\t"<<S<<endl;
for(int i=0;i<10;i++){
D = S;
P = (a-D)/b;
S = c+d*P;
cout<<P<<"\t"<<D<<"\t"<<S<<endl;
}
return 0;
}

Output:

13

Graphs:

14

7. Write a program to Implement Simulation of Single Server Queueing Model


Theory:
This example shows how to model a single-queue single-server system with a single
traffic source and an infinite storage capacity. In the notation, the M stands for
Markovian; M/M/1 means that the system has a Poisson arrival process, an exponential
15

service time distribution, and one server. Queuing theory provides exact theoretical
results for some performance measures of an M/M/1 queuing system and this model
makes it easy to compare empirical results with the corresponding theoretical results.
Calling population is infinite. Arrival rate does not change. Units are served according
FIFO
Arrivals are defined by the distribution of the time between arrivals (inter-arrival time)
Service times are according to a distribution
Arrival rate must be less than service rate (stable system)
Otherwise waiting line will grow unbounded (unstable system)
Structure:
The model includes the components listed below:
Time Based Entity Generator block: It models a Poisson arrival process by generating
entities (also known as "customers" in queuing theory).
Exponential Interarrival Time Distribution subsystem: It creates a signal representing
the interarrival times for the generated entities. The interarrival time of a Poisson arrival
process is an exponential random variable. FIFO Queue block: It stores entities that
have yet to be served.
Single Server block: It models a server whose service time has an exponential
distribution.

Code:
import java.util.*;
import java.text.*;
import java.util.*;
class RandTool {
static final long m = 2147483647L;
static final long a = 48271L;
static final long q = 44488L;
static final long r = 3399L;
static long r_seed = 12345678L;
static Random rand = new Random (r_seed);
16

public static void setSeed (long seed){


r_seed = seed;
rand = new Random (r_seed);
}
public static double uniform (){
long hi = r_seed / q;
long lo = r_seed - q * hi;
long t = a * lo - r * hi;
if (t > 0)
r_seed = t;
else
r_seed = t + m;
return ( (double) r_seed / (double) m );
}
public static double uniform (double a, double b){
if (b > a)
return ( a + (b-a) * uniform() );
else {
System.out.println ("ERROR in uniform(double,double):a="+a+",b="+b);
return 0;
}
}
public static long uniform (long a, long b){
if (b > a) {
double x = uniform ();
long c = ( a + (long) Math.floor((b-a+1)*x) );
return c;
}
else if (a == b)
return a;
else {
System.out.println ("ERROR: in uniform(long,long):a="+a+",b="+b);
return 0;
}
}
public static int uniform (int a, int b){
return (int) uniform ((long) a, (long) b);
}
public static double exponential (double lambda){
return (1.0 / lambda) * (-Math.log(1.0 - uniform()));
}
public static double gaussian (){
return rand.nextGaussian ();
}
public static double gaussian (double mean, double stdDeviation){
double x = gaussian ();
17

return mean + x * stdDeviation;


}
}
public class Queue {
double arrivalRate = 0.75;
double serviceRate = 1.0;
LinkedList<Customer> queue;
PriorityQueue<Event> eventList;
double clock;
int numArrivals = 0;
// How many arrived?
int numDepartures;
// How many left?
double totalWaitTime, avgWaitTime; // For time spent in queue.
double totalSystemTime, avgSystemTime; // For time spent in system.
void init (){
queue = new LinkedList<Customer> ();
eventList = new PriorityQueue<Event> ();
clock = 0.0;
numArrivals = numDepartures = 0;
totalWaitTime = totalSystemTime = 0.0;
scheduleArrival ();
}
void simulate (int maxCustomers){
init ();
while (numArrivals < maxCustomers) {
Event e = eventList.poll ();
clock = e.eventTime;
if (e.type == Event.ARRIVAL) {
handleArrival (e);
}
else {
handleDeparture (e);
}
}
stats ();
}
void handleArrival (Event e){
numArrivals ++;
queue.add (new Customer (clock));
if (queue.size() == 1) {
// This is the only customer => schedule a departure.
scheduleDeparture ();
}
scheduleArrival ();
}
18

void handleDeparture (Event e){


numDepartures ++;
Customer c = queue.removeFirst ();
double timeInSystem = clock - c.arrivalTime;
totalSystemTime += timeInSystem;
if (queue.size() > 0) {
Customer waitingCust = queue.get (0);
double waitTime = clock - waitingCust.arrivalTime;
totalWaitTime += waitTime;
scheduleDeparture ();
}
}
void scheduleArrival (){
double nextArrivalTime =clock + randomInterarrivalTime();
eventList.add (new Event (nextArrivalTime, Event.ARRIVAL));
}
void scheduleDeparture (){
double nextDepartureTime = clock + randomServiceTime ();
eventList.add (new Event (nextDepartureTime, Event.DEPARTURE));
}
double randomInterarrivalTime (){
return exponential (arrivalRate);
}
double randomServiceTime (){
return exponential (serviceRate);
}
double exponential (double gamma){
return (1.0 / gamma) * (-Math.log(1.0 - RandTool.uniform()));
}
void stats (){
if (numDepartures == 0) {
return;
}
avgWaitTime = totalWaitTime / numDepartures;
avgSystemTime = totalSystemTime / numDepartures;
}
public String toString (){
String results = "Simulation results:";
results += "\n numArrivals: " + numArrivals;
results += "\n numDepartures: " + numDepartures;
results += "\n avg Wait:
" + avgWaitTime;
results += "\n avg System Time: " + avgSystemTime;
return results;
}
public static void main (String[] argv){
Queue queue = new Queue ();
19

queue.simulate (1000);
System.out.println (queue);
}
}
// Class Customer (one instance per customer) stores whatever we
// need for each customer. Since we collect statistics on waiting
// time at the time of departure, we need to record when a
// customer arrives.
class Customer {
double arrivalTime;
public Customer (double arrivalTime){
this.arrivalTime = arrivalTime;
}
}
// Class Event has everything we need for an event: the type of
// event, and when it occurs. To use Java's PriorityQueue, we need
// have this class implement the Comparable interface where
// one event is "less" if it occurs sooner.
class Event implements Comparable {
public static int ARRIVAL = 1;
public static int DEPARTURE = 2;
int type = -1;
// Arrival or departure.
double eventTime;
// When it occurs.
public Event (double eventTime, int type){
this.eventTime = eventTime;
this.type = type;
}
public int compareTo (Object obj){
Event e = (Event) obj;
if (eventTime < e.eventTime) {
return -1;
}
else if (eventTime > e.eventTime) {
return 1;
}
else {
return 0;
}
}
public boolean equals (Object obj){
return (compareTo(obj) == 0);
}
20

Output:

21

8. Write a Program to implement Simulation of 2 Server Queueing System


Theory:
Here are some examples of ways to combine FIFO Queue and Single Server blocks to model
different situations:
Serial Queue-Server Pairs
Parallel Queue-Server Pairs as Alternatives
Parallel Queue-Server Pairs in Multicasting Serial Connection of Queues
Parallel Connection of Queues
Serial Queue-Server Pairs
Two queue-server pairs connected in series represent successive operations that an entity
undergoes. For example, parts on an assembly line are processed sequentially by two
machines.

While you might alternatively model the situation as a pair of servers without a queue
between them, the absence of the queue means that if the first server completes service on an
entity before the second server is available, the entity must stay in the first server past the end
of service and the first server cannot accept a new entity for service until the second server
becomes available.

Parallel Queue-Server Pairs as Alternatives

22

Two queue-server pairs connected in parallel, in which each entity arrives at one or the other,
represent alternative operations. For example, vehicles wait in line for one of several
tollbooths at a toll plaza.

Parallel Queue-Server Pairs in Multicasting


Two queue-server pairs connected in parallel, in which a copy of each entity arrives at both,
represent a multicasting situation such as sending a message to multiple recipients. Note that
copying entities might not make sense in some applications.

Serial Connection of Queues


Two queues connected in series might be useful if you are using entities to model items that
physically experience two distinct sets of conditions while in storage. For example, additional
inventory items that overflow one storage area have to stay in another storage area in which a
less well-regulated temperature affects the items' long-term quality. Modeling the two storage

23

areas as distinct queue blocks facilitates viewing the average length of time that entities stay in
the overflow storage area.

Parallel Connection of Queues


Two queues connected in parallel, in which each entity arrives at one or the other, represent
alternative paths for waiting. The paths might lead to different operations, such as a line of
vehicles waiting for a tollbooth modeled and a line of vehicles waiting on a jammed exit ramp of
the freeway. You might model the tollbooth as a server and the traffic jam as a gate.

Assumptions: non homogeneous _(t) Poisson arrivals;Service at server 1, then by server 2


service for each customer; service times are RVs with distribution G1 and G2;no customers after
_nal arrival time T.
Code:
import java.util.*;
public class Simulation{
24

// statistics:
static double meanArrival = 1;
static double meanService = 0.5;
static int maxNumberOfTasks = 100;
public static int taskCount = 0;
// simulation clock
static double simTime = 0.0;
// random number generators for arrival and departure
static Random rng1 = new Random();
static Random rng2 = new Random();
// future event set
static PriorityQueue<Event> eventSet = new PriorityQueue<Event>();
// servers (with queues)
static Server server1 = new Server();
static Server server2 = new Server();
static Dispatcher dispatcher = new Dispatcher(eventSet, server1, server2);
public static void main (String args[]){
if (args.length == 0) {
System.err.println("Parameters: \n"+
" 1. Mean inter-arrival time \n" +
" 2. Mean service time \n" +
" 3. Number of tasks \n" +
" 4. Seed 1 for arrival times \n" +
" 5. Seed 2 for service times");
System.exit(0);
}
if (args.length > 0) {
try {
meanArrival = Double.parseDouble(args[0]);
} catch (NumberFormatException e) {
System.err.println("First argument must be a floating point value");
System.exit(1);
}
}
if (args.length > 1) {
try {
meanService = Double.parseDouble(args[1]);
25

} catch (NumberFormatException e) {
System.err.println("Second argument must be a floating point
value");
System.exit(1);
}
}
if (args.length > 2) {
try {
maxNumberOfTasks = Integer.parseInt(args[2]);
} catch (NumberFormatException e) {
System.err.println("Third argument must be an integer");
System.exit(1);
}
}
if (args.length > 3) {
try {
long seed = Integer.parseInt(args[3]);
rng1.setSeed(seed);
} catch (NumberFormatException e) {
System.err.println("4th argument must be an integer");
System.exit(1);
}
}
if (args.length > 4) {
try {
long seed = Integer.parseInt(args[4]);
rng2.setSeed(seed);
} catch (NumberFormatException e) {
System.err.println("5th argument must be an integer");
System.exit(1);
}
}
// Generate first event:
eventSet.add( new ArrivalEvent( getNextArrival() ) );
taskCount++;
// Main loop:
while (!eventSet.isEmpty() && taskCount < maxNumberOfTasks){
Event e = eventSet.poll();
dispatcher.handleEvent(e);
}

26

// printing results:
System.out.println("Server 1:");
System.out.println("Utilization " + server1.getUtilization());
System.out.println("Waiting Time " + server1.getAvgWaitingTime());
System.out.println("Queue Length " + server1.getAvgQueueLength());
System.out.println("Server 2:");
System.out.println("Utilization " + server2.getUtilization());
System.out.println("Waiting Time " + server2.getAvgWaitingTime());
System.out.println("Queue Length " + server2.getAvgQueueLength());
/* // brief output:
System.out.println("Server1: " + server1.getUtilization()
+" "+
server1.getAvgWaitingTime()
+" "+
server1.getAvgQueueLength() );
System.out.println("Server2: " + server2.getUtilization()
+" "+ server2.getAvgWaitingTime()
+" "+
server2.getAvgQueueLength() );
*/
}
static public double getNextArrival() {
return simTime + randomExponential(rng1, 1/meanArrival);
}
static public double getNextDeparture() {
return simTime + randomExponential(rng2, 1/meanService);
}
static double randomExponential(Random rng, double lambda) {
return Math.log( rng.nextDouble() ) / -lambda;
}
static public void setSimTime(double t) {
simTime = t;
}
static public double getSimTime() {
return simTime;
}
}

27

abstract class Event implements Comparable<Event>{


protected static int counter=0;
protected double scheduledTime;
protected int number; // event number (used for tie breaking)
public Event(double scheduledTime) {
this.scheduledTime = scheduledTime;
number = ++counter;
}
public double getScheduledTime() {
return scheduledTime;
}
// comparison method needed for sorting in the priority queue
public int compareTo(Event e) {
if (this.scheduledTime < e.scheduledTime) return -1;
else if (this.scheduledTime > e.scheduledTime) return 1;
else if (this.number < e.number) return -1;
else return 1;
}
public String toString() {
return "Event No. " + number +", scheduled time = " + scheduledTime;
}
}
class ArrivalEvent extends Event {
public ArrivalEvent(double scheduledTime) {
super(scheduledTime);
}
public String toString() {
return "Arrival Event, No. " + number +", scheduled time = " + scheduledTime;
}
}
class DepartureEvent extends Event {
public Server server;
public DepartureEvent(double scheduledTime, Server server) {
super(scheduledTime);
this.server = server;
28

}
public String toString() {
return "Departure Event, No. " + number +", scheduled time = " + scheduledTime;
}
}
class Dispatcher {
PriorityQueue<Event> eventSet; // pointer to event set
Server server1, server2;
// pointers to servers
Server currentServer;
public Dispatcher(PriorityQueue<Event> eventSet, Server s1, Server s2) {
server1 = s1;
server2 = s2;
currentServer = s1;
this.eventSet = eventSet;
}
public void handleEvent(Event e) {
Simulation.setSimTime(e.getScheduledTime());
System.out.println("Handling event: " + e);
if (e instanceof ArrivalEvent)
handleArrival( (ArrivalEvent)e );
else
handleDeparture( (DepartureEvent)e );
}
public void handleArrival(ArrivalEvent e) {
// Task assignment rule:
// If both servers are idle, then tasks are assigned alterningly
// Otherwise, the server with the shorter queue is assigned the task
if (server1.getQueueLength() < server2.getQueueLength())
currentServer = server1;
else if (server1.getQueueLength() > server2.getQueueLength())
currentServer = server2;
else if (currentServer == server1) currentServer = server2;
else currentServer = server1;
// Task assignment:
currentServer.assignTask(e.getScheduledTime());
29

// If there is only one task, then the departure can be scheduled


if (currentServer.getQueueLength() == 1)
eventSet.add( new DepartureEvent( Simulation.getNextDeparture(),
currentServer ) );
// generate the next event of the same type
eventSet.add( new ArrivalEvent( Simulation.getNextArrival() ) );
Simulation.taskCount++;
}
public void handleDeparture(DepartureEvent e) {
e.server.completeTask();
// if there are more tasks in the queue, then schedule the next departure:
if (!e.server.isIdle())
eventSet.add( new DepartureEvent( Simulation.getNextDeparture(),
e.server ) );
}
}
class Task {
public double arrivalTime;
public double activationTime;
public Task(double arrivalTime) {
this.arrivalTime = arrivalTime;
}
}
class Server {
LinkedList<Task> queue = new LinkedList<Task>();
double busyTimeTotal = 0;
double waitTimeTotal = 0;
double cumulatedQueueLength = 0;
int tasksCompleted = 0;
public void assignTask(double arrivalTime) {
Task t = new Task(arrivalTime);
// if the queue is empty, the new task is activated immediately
if (queue.isEmpty()) t.activationTime = Simulation.getSimTime();
queue.add(t);
}
public void completeTask() {
assert(!queue.isEmpty());
// remove task from queue
30

Task t = queue.poll();
// calculate statistics:
tasksCompleted++;
busyTimeTotal += Simulation.getSimTime() - t.activationTime;
waitTimeTotal += t.activationTime - t.arrivalTime;
cumulatedQueueLength += (t.activationTime - t.arrivalTime) * (queue.size()+1);
System.out.println("activation: " + t.activationTime + " arrival: " + t.arrivalTime);
// if the queue is not empty, then the next task is activated
if (!queue.isEmpty())
((Task)queue.peek()).activationTime = Simulation.getSimTime();
}
public int getQueueLength() {
return queue.size();
}
public boolean isIdle() {
return queue.isEmpty();
}
public double getUtilization() {
return busyTimeTotal / Simulation.getSimTime();
}
public double getAvgWaitingTime() {
return waitTimeTotal / tasksCompleted;
}
public double getAvgQueueLength() {
return cumulatedQueueLength / Simulation.getSimTime();
}
}
Output:

31

32

9. Write a Program to implement Simulation of Inventory System.


Theory:
Some amount M of goods is in stock to begin with. As sales continue, the stock
decreases. At the pre-defined interval, N, the stock level is checked, and the goods is reordered.
It is possible that the inventory becomes negative, meaning the goods is in shortage.
The lead time, which is the time between the issuance of re-order and the arrival of the
goods, can be considered as zero in many cases.
Example: The newspaper seller's problem
The paper seller buys the papers for 33 cents each and sells for 50 cents each.
The papers not sold at the end of the day are sold as scrap for 5 cents each.
Newspapers can be purchased in bundles of 10. Thus the paper seller can buy 40, 50, 60,
and so on.
Profit is calculated as
profit = revenue - cost - lost profit from excess demand + salvage
.
Example of Simulation of an (M,N) inventory system.
M is the maximum inventory level, assume it is 11 units.
N is the length of review period, assume it is 5 days.
The initial inventory is 3 units, and an initial order of 8 units is scheduled to arrive in 2
days. This is the initial setting of the simulation.
Code:
#include<bits/stdc++.h>
using namespace std;
int main(){
float D, X, Y, Sk, ESk, UD, DDt, Ct; int i;
Ct = 0; Sk = 125; i=1; UD = 0.0; DDt = 0;
printf("\n\n Enter the values of x and y\t"); scanf("%f %f", &X, &Y);
while(i<=200) {
if(DDt == i) {
Sk = Sk + Y;
UD =0;
}
D = rand();
if (D <=Sk) {
Sk = Sk - D;
Ct = Ct + Sk * 0.95;
}
else{
Ct = Ct + (D - Sk) * 20; Sk = 0;
}
ESk = Sk + UD;
33

if (ESk<=X){
UD = Y;
DDt = i + 3;
Ct = Ct / 80;
}
i = i + 1;
}
printf("\n\n X = %f\n Y=%f\n Z=%f\n\n", X, Y, Ct);
}

Output:

34

10. Simulation of TELEPHONIC SYSTEM


Theory:
Simple simulation of a telephony system in which customers make, accept, merge and hangup both local and long distance calls. The application architecture is in three layers.
The basic objects provide basic functionality to simulate customers, calls and
connections (regular calls have one connection, conference calls have more than one).
The timing feature is concerned with timing the connections and keeping the total
connection time per customer. Aspects are used to add a timer to each connection and to
manage the total time per customer.
The billing feature is concerned with charging customers for the calls they make. Aspects
are used to calculate a charge per connection and, upon termination of a connection, to
add the charge to the appropriate customer's bill. The billing aspect builds upon the
timing aspect: it uses a pointcut defined in Timing and it uses the timers that are
associated with connections.
The simulation of system has three configurations: basic, timing and billing. Programs for the
three configurations are in classes BasicSimulation, versionNTimingSimulation and
versionNBillingSimulation. These share a common superclass AbstractSimulation, which
defines the method run() with the simulation itself and the method wait(double seconds) used
to simulate elapsed time.
The basic objects:
The telecom simulation comprises classes Customer, Call and abstract class Connection with
its two concrete subclasses Local and LongDistance. Customers have a name and a numeric
area code. They also have methods for managing calls. Simple calls are made between one
customer (the caller) and another (the receiver), a Connection object is used to connect them.
Conference calls between more than two customers will involve more than one connection. A
customer may be involved in may calls at one time.

35

Class Customer
Customer has methods call(), pickup(), hangup() and merge() for managing calls.
public class Customer {
private String name;
privateintareacode;
private Vector calls = new Vector();
/**
* unregister a call */
protected void removeCall(Call c){
calls.removeElement(c);
}
/**
* register a call */
protected void addCall(Call c){
calls.addElement(c);
}
/**
* Make a new customer with given name */
public Customer(String name, intareacode) {
this.name = name;
this.areacode = areacode;
}
/**
* String rendition of customer */
public String toString() {
return name + "(" + areacode + ")";
}
/**
* what area is the customer in? */
publicintgetAreacode(){
returnareacode;
}
/**
* Is the other customer in the same area? */
publicbooleanlocalTo(Customer other){
returnareacode == other.areacode;
}
/**
* Make a new call to receiver */
36

public Call call(Customer receiver) {


Call call = new Call(this, receiver);
addCall(call); return call;
}
/**
* pick up a call */
public void pickup(Call call) {
call.pickup();
addCall(call);
}
/**
* hang up a call */
public void hangup(Call call) {
call.hangup(this);
removeCall(call);
}
/**
* Merge a pair of calls -- conference them
* PRE: call1.includes(this)
* call2.includes(this)
* call1.connected()
* call2.connected()
* POST: call1 includes all customers connected by call1@pre and call2@pre */
public void merge(Call call1, Call call2){
call1.merge(call2); removeCall(call2);
}
}
Class Call
Calls are created with a caller and receiver who are customers. If the caller and receiver have the
same area then the call can be established with a Local connection (see below), otherwise a
LongDistance connection is required. A call comprises a number of connections between
customers. Initially there is only the connection between the caller and receiver but additional
connections can be added if calls are merged to form conference calls.
public class Call {
private Customer caller, receiver;
private Vector connections = new Vector();
public Call(Customer caller, Customer receiver){
this.caller = caller;
37

this.receiver = receiver;
Connection c;
if (receiver.localTo(caller)) {
c = new Local(caller, receiver);
}
else {
c = new LongDistance(caller, receiver);
}
connections.add(c);
}
public void pickup() {
Connection connection = (Connection)connections.lastElement();
connection.complete();
}
publicbooleanisConnected(){
return ((Connection)connections.lastElement()).getState() ==
Connection.COMPLETE;
}
public void hangup(Customer c) {
for(Enumeration e = connections.elements(); e.hasMoreElements();) {
((Connection)e.nextElement()).drop();
}
}
publicboolean includes(Customer c){
boolean result = false;
for(Enumeration e = connections.elements(); e.hasMoreElements();) {
result = result || ((Connection)e.nextElement()).connects(c);
}
return result;
}
public void merge(Call other){
for(Enumeration e = other.connections.elements(); e.hasMoreElements();){
Connection conn = (Connection)e.nextElement();
other.connections.remove(conn);
connections.addElement(conn);
}
}
}
Class Connection
Connection models the physical details of establishing a connection between customers. It does
this with a simple state machine (connections are initially PENDING, then COMPLETED and
finally DROPPED). Messages are printed to the console so that the state of connections can be
observed. Connection is an abstract class with two concrete subclasses: Local and LongDistance.
38

abstract class Connection {


public static final int PENDING = 0;
public static final int COMPLETE = 1;
public static final int DROPPED = 2;
Customer caller, receiver;
privateint state = PENDING;
/**
* Creatte a new Connection between a and b */
Connection(Customer a, Customer b)
{ this.caller = a;
this.receiver = b;
}
/**
* what is the state of the connection? */
publicintgetState()
{ return state;
}
/**
* get the customer who initiated this connection */
public void merge(Call other){
for(Enumeration e = other.connections.elements(); e.hasMoreElements();)
{ Connection conn = (Connection)e.nextElement();
other.connections.remove(conn);
connections.addElement(conn);
}
}
}
Class Connection
Connection models the physical details of establishing a connection between customers. It does
this with a simple state machine (connections are initially PENDING, then COMPLETED and
finally DROPPED). Messages are printed to the console so that the state of connections can be
observed. Connection is an abstract class with two concrete subclasses: Local and LongDistance.
abstract class Connection {
public static final int PENDING = 0; public static final int COMPLETE = 1; public static
final int DROPPED = 2;

39

Customer caller, receiver; privateint state = PENDING;


/**
* Creatte a new Connection between a and b */
Connection(Customer a, Customer b) {
this.caller = a;
this.receiver = b;
}
/**
* what is the state of the connection? */
publicintgetState(){
return state;
}
/**
* get the customer who initiated this connection */
public Customer getCaller() {
return caller;
}
public Customer getReceiver() {
return receiver;
}
void complete() {
state = COMPLETE;
System.out.println("connection completed");
}
/**
*
Called when the connection is dropped from a call. Is intended to
*
free up any resources the connection was consuming.
*/
void drop() {
state = DROPPED; System.out.println("connection dropped");
}
/**
* Is customer c connected by this connection? */
publicboolean connects(Customer c){
return (caller == c || receiver == c);
}
}
Class Local (in Connection.java) class Local extends Connection {
Local(Customer a, Customer b) {
40

super(a, b);
System.out.println("[new local connection from " + a + " to " + b + "]");
}
}
Class LongDistance (in Connection.java) classLongDistance extends Connection {
LongDistance(Customer a, Customer b) {
super(a, b);
System.out.println("[new long distance connection from " + a + " to " + b + "]");
}
}

41

Вам также может понравиться