Skip to content

Commit

Permalink
Merge pull request #7 from omar753sahl/dev
Browse files Browse the repository at this point in the history
Model (M/M/C/K) officially done!
  • Loading branch information
omarsahl authored Dec 18, 2017
2 parents 9bae040 + 3edd43f commit 4739515
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 61 deletions.
4 changes: 0 additions & 4 deletions src/main/java/models/MM1KQueueModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

import static java.lang.Math.*;

/**
* Created by Omar
* Date: 18-Dec-17.
*/
public final class MM1KQueueModel extends QueueModel {
@Override
protected PerformanceMetrics calculatePerformanceMetrics(QueueSystemInput input) throws QueueModelException {
Expand Down
46 changes: 23 additions & 23 deletions src/main/java/models/MMCKQueueModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import static utils.MathUtils.fact;
import static utils.MathUtils.pow;

public class MMCKQueueModel extends QueueModel{
public final class MMCKQueueModel extends QueueModel {

double lambda, lambdaDash, mue, r, rho, P0;
int servers, capacity, C, K;
private double lambda, lambdaDash, mue, r, rho, P0;
private int servers, capacity, C, K;

@Override
protected PerformanceMetrics calculatePerformanceMetrics(QueueSystemInput input) throws QueueModelException {
Expand All @@ -28,7 +28,6 @@ protected PerformanceMetrics calculatePerformanceMetrics(QueueSystemInput input)

double L, Lq, W, Wq;


//Lambda(dash) calculation
{
lambdaDash = lambda * (1 - getP(K));
Expand All @@ -37,7 +36,7 @@ protected PerformanceMetrics calculatePerformanceMetrics(QueueSystemInput input)
//Lq calculation
{
Lq = 0.0;
for(int n = C + 1; n <= K; ++n){
for (int n = C + 1; n <= K; ++n) {
double Pn = getP(n);
Lq += (n - C) * Pn;
}
Expand All @@ -48,8 +47,8 @@ protected PerformanceMetrics calculatePerformanceMetrics(QueueSystemInput input)
L = Lq + C;

double neg = 0;
for(int n = 0; n <= C - 1; ++n){
neg += (C - n) * pow(r, n) / fact(n);
for (int n = 0; n <= C - 1; ++n) {
neg += ((C - n) * pow(r, n)) / fact(n);
}

L -= P0 * neg;
Expand All @@ -70,40 +69,41 @@ protected PerformanceMetrics calculatePerformanceMetrics(QueueSystemInput input)


//Calculates P(n)
double getP(int n){
double getP(int n) {

double P = Double.NaN;

if(n >= 0 && n < C){
P = pow(r, n) / fact(n) * P0;
}
else if(n >= C && n <= K){
P = pow(r, n) / (pow(C, n - C) * fact(C)) * P0;
if (n >= 0 && n < C) {
P = (pow(r, n) / fact(n)) * P0;
} else if (n >= C && n <= K) {
P = (pow(r, n) / (pow(C, n - C) * fact(C))) * P0;
}

return P;
}

//Calculates P(0)
double getP0(){
double getP0() {

double P0_inv = 0.0;

MathUtils.prepareFact(C);
MathUtils.prepareFact(Math.max(C, K));

if(rho == 1){
if (rho == 1.0) {

for(int n = 0; n <= C - 1; ++n){
P0_inv += (pow(r, n) / fact(n)) + (pow(r, C) / fact(C) * (K - C + 1));
for (int n = 0; n <= C - 1; ++n) {
P0_inv += (pow(r, n) / fact(n));
}
}
else{
P0_inv += (pow(r, C) / fact(C)) * (K - C + 1);

} else {

for(int n = 0; n <= C - 1; ++n){
P0_inv += (pow(r, n) / fact(n)) + (pow(r, C) / fact(C) * ((1 - pow(rho, K - C + 1)) / (1 - rho)));
for (int n = 0; n <= C - 1; ++n) {
P0_inv += (pow(r, n) / fact(n));
}
P0_inv += (pow(r, C) / fact(C)) * ((1 - pow(rho, K - C + 1)) / (1 - rho));
}

return 1 / P0_inv;
return 1 / P0_inv;
}
}
44 changes: 44 additions & 0 deletions src/main/java/models/MMCQueueModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package models;

import utils.MathUtils;
import static java.lang.Math.*;

public class MMCQueueModel extends QueueModel {
@Override
protected PerformanceMetrics calculatePerformanceMetrics(QueueSystemInput inputs) throws QueueModelException {
double lambda = inputs.getArrivalRate();
double mue = inputs.getServiceRate();
double c = inputs.getNumberOfServers();
double r = lambda / mue;

MathUtils.prepareFact((int) (c + 1));

double p0 = calculateP0(c, r);
double Lq = calculateLq(lambda, mue, c, r, p0);

double Wq = Lq / lambda;
double W = Wq + (1 / mue);
double L = Lq + r;

return new PerformanceMetrics(L, Lq, W, Wq);
}

private double calculateLq(double lambda, double mue, double c, double r, double p0) {
double numerator = pow(r, c) * lambda * mue;
double denominator = MathUtils.fact((int) (c - 1)) * pow((c * mue) - lambda, 2);
return (numerator / denominator) * p0;
}

private double calculateP0(double c, double r) {
double firstTerm = 0;
double secondTerm = ((c * pow(r, c)) / (MathUtils.fact((int) c) * (c - r)));

for (int n = 0; n <= c - 1; n++) {
firstTerm += (pow(r, n) / MathUtils.fact(n));
}

double result = firstTerm + secondTerm;

return 1 / result;
}
}
33 changes: 29 additions & 4 deletions src/main/java/ui/DD1KOutputScreen.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
package ui;

/**
* Created by Omar
* Date: 18-Dec-17.
*/
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;

public class DD1KOutputScreen {

@FXML
private BorderPane root;

@FXML
private Label labelL;

@FXML
private Label labelLq;

@FXML
private Label labelW;

@FXML
private Label labelWq;

@FXML
private Label backButton;

@FXML
void onBackClicked(MouseEvent event) {

}

}
4 changes: 0 additions & 4 deletions src/main/java/utils/InputFieldValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import com.jfoenix.validation.base.ValidatorBase;
import javafx.scene.control.TextInputControl;

/**
* Created by Omar
* Date: 17-Dec-17.
*/
public class InputFieldValidation extends ValidatorBase {
@Override
protected void eval() {
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/utils/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,37 @@ public static Double eval(String expressionStr) throws Exception {
}


static long f[];
public static void prepareFact(int MX){
private static long f[];

public static void prepareFact(int MX) {
f = new long[MX + 1];
f[0] = 1;
for(int i = 1; i <= MX; ++i) f[i] = f[i - 1] * i;
for (int i = 1; i <= MX; ++i) f[i] = f[i - 1] * i;
}

//O(1) factorial query
public static long fact(int x){
public static long fact(int x) {
return f[x];
}

//Binary exponentiation O(log2(exponent)) runtime
public static double pow(double base, int exponent){
public static double pow(double base, int exponent) {

double res = 1.0;

double res = 1;
boolean reversed = false;

if (exponent < 0) {
reversed = true;
exponent *= -1;
}

while(exponent != 0){
if((exponent & 1) == 1) res = res * base;
while (exponent != 0) {
if ((exponent & 1) == 1) res = res * base;
exponent /= 2;
base = base * base;
}

return res;
return reversed ? 1 / res : res;
}
}
104 changes: 94 additions & 10 deletions src/main/resources/fxml/dd1k_output_screen.fxml
Original file line number Diff line number Diff line change
@@ -1,14 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import com.jfoenix.controls.JFXRippler?>
<?import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIconView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="ui.DD1KOutputScreen"
prefHeight="400.0" prefWidth="600.0">
<BorderPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="650.0" prefWidth="394.0" style="-fx-background-color: #252525;" stylesheets="@../css/style_sheet.css" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.DD1KOutputScreen">
<center>
<ScrollPane BorderPane.alignment="CENTER">
<content>
<VBox alignment="CENTER" nodeOrientation="LEFT_TO_RIGHT" prefHeight="543.0" prefWidth="392.0" spacing="12.0">
<children>
<Label alignment="CENTER" text="D/D/1/K Performance Metrics" textFill="WHITE">
<font>
<Font name="Lato Light" size="23.0" />
</font>
<padding>
<Insets bottom="25.0" />
</padding>
</Label>
<VBox alignment="CENTER_LEFT">
<children>
<Label alignment="CENTER" text="N(t)" textFill="WHITE">
<font>
<Font name="Lato Medium" size="28.0" />
</font>
</Label>
<Label alignment="CENTER" text="Average number of customers (entities) in the system." textFill="WHITE" wrapText="true">
<font>
<Font name="Lato Light" size="15.0" />
</font>
</Label>
<Label fx:id="labelL" layoutX="10.0" layoutY="118.0" text="0.6667" textFill="WHITE">
<font>
<Font name="Lato Semibold" size="32.0" />
</font>
</Label>
</children>
</VBox>
<VBox alignment="CENTER_LEFT" layoutX="25.0" layoutY="310.0">
<children>
<Label alignment="CENTER" text="Wq" textFill="WHITE">
<font>
<Font name="Lato Medium" size="28.0" />
</font>
</Label>
<Label alignment="CENTER" text="Average time it takes a customer to start being served." textFill="WHITE" wrapText="true">
<font>
<Font name="Lato Light" size="15.0" />
</font>
</Label>
<Label fx:id="labelWq" layoutX="10.0" layoutY="118.0" text="3.3333 " textFill="WHITE">
<font>
<Font name="Lato Semibold" size="32.0" />
</font>
</Label>
</children>
</VBox>
</children>
<padding>
<Insets left="40.0" right="40.0" />
</padding>
</VBox>
</content>
</ScrollPane>
</center>
<top>

</AnchorPane>
</top>
<top>
<StackPane>
<children>
<JFXRippler styleClass="material_icon_rippler">
<Label fx:id="backButton" alignment="CENTER" onMouseClicked="#onBackClicked" textAlignment="CENTER" StackPane.alignment="TOP_LEFT">
<graphic>
<MaterialDesignIconView fill="WHITE" glyphName="ARROW_LEFT" size="32" />
</graphic>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</Label>
<StackPane.margin>
<Insets left="10.0" right="10.0" top="10.0" />
</StackPane.margin>
</JFXRippler>
</children>
<BorderPane.margin>
<Insets bottom="10.0" />
</BorderPane.margin>
</StackPane>
</top>
</BorderPane>
2 changes: 1 addition & 1 deletion src/main/resources/fxml/main_screen.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<Font name="Lato Medium" size="22.0" />
</font>
</JFXButton>
<JFXButton fx:id="mmkButton" alignment="CENTER" layoutX="76.0" layoutY="253.0" onAction="#onMmkClicked" prefHeight="47.0" prefWidth="262.0" ripplerFill="#101010" style="-fx-background-color: #ffffffff;" text="M/M/k" textFill="#252525">
<JFXButton fx:id="mmkButton" alignment="CENTER" layoutX="76.0" layoutY="253.0" onAction="#onMm1kClicked" prefHeight="47.0" prefWidth="262.0" ripplerFill="#101010" style="-fx-background-color: #ffffffff;" text="M/M/k" textFill="#252525">
<font>
<Font name="Lato Medium" size="22.0" />
</font>
Expand Down
Loading

0 comments on commit 4739515

Please sign in to comment.