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