formatting
This commit is contained in:
parent
ba2fb856fa
commit
2189ba86c4
4 changed files with 66 additions and 63 deletions
|
@ -1,6 +1,6 @@
|
|||
package simulation;
|
||||
|
||||
class Event{
|
||||
class Event {
|
||||
public double eventTime;
|
||||
public int eventType;
|
||||
public Event next;
|
||||
|
|
|
@ -1,33 +1,35 @@
|
|||
package simulation;
|
||||
|
||||
public class EventList{
|
||||
|
||||
public class EventList {
|
||||
|
||||
private Event list, last;
|
||||
|
||||
EventList(){
|
||||
|
||||
EventList() {
|
||||
list = new Event();
|
||||
last = new Event();
|
||||
list.next = last;
|
||||
last = new Event();
|
||||
list.next = last;
|
||||
}
|
||||
|
||||
public void InsertEvent(int type, double TimeOfEvent){
|
||||
Event dummy, predummy;
|
||||
Event newEvent = new Event();
|
||||
newEvent.eventType = type;
|
||||
newEvent.eventTime = TimeOfEvent;
|
||||
predummy = list;
|
||||
dummy = list.next;
|
||||
while ((dummy.eventTime < newEvent.eventTime) & (dummy != last)){
|
||||
predummy = dummy;
|
||||
dummy = dummy.next;
|
||||
}
|
||||
predummy.next = newEvent;
|
||||
newEvent.next = dummy;
|
||||
}
|
||||
|
||||
public Event FetchEvent(){
|
||||
Event dummy;
|
||||
dummy = list.next;
|
||||
|
||||
public void InsertEvent(int type, double TimeOfEvent) {
|
||||
Event dummy, predummy;
|
||||
predummy = this.list;
|
||||
dummy = this.list.next;
|
||||
|
||||
Event newEvent = new Event();
|
||||
newEvent.eventType = type;
|
||||
newEvent.eventTime = TimeOfEvent;
|
||||
|
||||
while ((dummy.eventTime < newEvent.eventTime) && (dummy != last)) {
|
||||
predummy = dummy;
|
||||
dummy = dummy.next;
|
||||
}
|
||||
|
||||
predummy.next = newEvent;
|
||||
newEvent.next = dummy;
|
||||
}
|
||||
|
||||
public Event FetchEvent() {
|
||||
Event dummy = list.next;
|
||||
list.next = dummy.next;
|
||||
dummy.next = null;
|
||||
return dummy;
|
||||
|
|
|
@ -2,20 +2,22 @@ package simulation;
|
|||
|
||||
import java.io.*;
|
||||
|
||||
public class MainSimulation extends GlobalSimulation{
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Event actEvent;
|
||||
EventList myEventList = new EventList();
|
||||
State actState = new State(myEventList);
|
||||
myEventList.InsertEvent(ARRIVAL, 0);
|
||||
myEventList.InsertEvent(MEASURE, 5);
|
||||
while (time < 50000){
|
||||
actEvent = myEventList.FetchEvent();
|
||||
time = actEvent.eventTime;
|
||||
actState.TreatEvent(actEvent);
|
||||
}
|
||||
System.out.println("Mean number of customers: " + 1.0*actState.accumulated/actState.noMeasurements);
|
||||
System.out.println("Number of measurements done: " + actState.noMeasurements);
|
||||
}
|
||||
public class MainSimulation extends GlobalSimulation {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Event actEvent;
|
||||
EventList myEventList = new EventList();
|
||||
|
||||
State actState = new State(myEventList);
|
||||
|
||||
myEventList.InsertEvent(ARRIVAL, 0);
|
||||
myEventList.InsertEvent(MEASURE, 5);
|
||||
while (MainSimulation.time < 50000) {
|
||||
actEvent = myEventList.FetchEvent();
|
||||
MainSimulation.time = actEvent.eventTime;
|
||||
actState.TreatEvent(actEvent);
|
||||
}
|
||||
System.out.println("Mean number of customers: " + (double) actState.accumulated / actState.noMeasurements);
|
||||
System.out.println("Number of measurements done: " + actState.noMeasurements);
|
||||
}
|
||||
}
|
|
@ -2,25 +2,24 @@ package simulation;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
class State extends GlobalSimulation{
|
||||
|
||||
class State extends GlobalSimulation {
|
||||
|
||||
public int numberInQueue = 0, accumulated = 0, noMeasurements = 0;
|
||||
|
||||
|
||||
private EventList myEventList;
|
||||
|
||||
Random slump = new Random();
|
||||
|
||||
State(EventList x){
|
||||
static Random slump = new Random();
|
||||
|
||||
State(EventList x) {
|
||||
myEventList = x;
|
||||
}
|
||||
|
||||
private void InsertEvent(int event, double timeOfEvent){
|
||||
|
||||
private void InsertEvent(int event, double timeOfEvent) {
|
||||
myEventList.InsertEvent(event, timeOfEvent);
|
||||
}
|
||||
|
||||
|
||||
public void TreatEvent(Event x){
|
||||
switch (x.eventType){
|
||||
|
||||
public void TreatEvent(Event x) {
|
||||
switch (x.eventType) {
|
||||
case ARRIVAL:
|
||||
arrival();
|
||||
break;
|
||||
|
@ -32,25 +31,25 @@ class State extends GlobalSimulation{
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private double generateMean(double mean){
|
||||
return 2*mean*slump.nextDouble();
|
||||
|
||||
private double generateMean(double mean) {
|
||||
return 2 * mean * slump.nextDouble();
|
||||
}
|
||||
|
||||
private void arrival(){
|
||||
|
||||
private void arrival() {
|
||||
if (numberInQueue == 0)
|
||||
InsertEvent(READY, time + generateMean(1));
|
||||
numberInQueue++;
|
||||
InsertEvent(ARRIVAL, time + generateMean(2));
|
||||
}
|
||||
|
||||
private void ready(){
|
||||
|
||||
private void ready() {
|
||||
numberInQueue--;
|
||||
if (numberInQueue > 0)
|
||||
InsertEvent(READY, time + generateMean(1));
|
||||
}
|
||||
|
||||
private void measure(){
|
||||
|
||||
private void measure() {
|
||||
accumulated = accumulated + numberInQueue;
|
||||
noMeasurements++;
|
||||
InsertEvent(MEASURE, time + generateMean(5));
|
||||
|
|
Loading…
Reference in a new issue