Commit 5d013675 authored by  Rewaj  Shrestha's avatar Rewaj Shrestha

rs6017: Working CTMC simulation

parent 5d6fef49
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Main {
public static void main(String[] args) {
Simulation.run(50, 100, 1000000.0, 10, 1000000.0, EvictionPolicy.RAND);
Simulation.run(2, 3 ,200000.0, 10, 1000.0, EvictionPolicy.FIFO , 95, true);
}
......
public class Measure {
private double measureSum = 0.0;
private double measureSumSq = 0.0;
private int n = 0;
public void addObservation(double obs) {
measureSum += obs;
measureSumSq += Math.pow(obs, 2);
n += 1;
}
public int getNumObservations() {
return n;
}
public double sampleMean() {
return measureSum / n;
}
public double sampleVariance() {
return (measureSumSq - (Math.pow(measureSum, 2) / n)) / (n - 1);
}
public double ciHalfWidth(double alpha) {
var s = Math.sqrt(sampleVariance());
var z = StudentstTable.getPercentile(n - 1, alpha);
return z * s / Math.sqrt(n);
}
}
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class Simulation {
private int misses;
private int requests;
private final int currentTime;
private double currentTime;
private double rateSum;
private List<Integer> cache;
private List<Double> cumulativeRateSum;
private EvictionPolicy evictionPolicy;
private HashMap<List<Integer>, Double> stateToTime;
public void run(double simulationTime) {
double currTime = 0;
while (currTime < simulationTime) {
currTime += nextArrival();
while (currentTime < simulationTime) {
double nextArrivalTime = nextArrival();
if (!stateToTime.containsKey(cache)) {
stateToTime.put(cache, nextArrivalTime);
} else {
stateToTime.put(cache, stateToTime.get(cache) + nextArrivalTime);
}
// System.out.println("Miss ratio is " + (double) misses/requests);
currentTime += nextArrivalTime;
int item = getItem();
processItemRequest(item);
}
}
public static List<Double> run(int m, int n, double simulationTime, int numberOfSimulations, double warmupTime, EvictionPolicy evictionPolicy) {
List<Double> cacheMissRates = new ArrayList<>();
for (int i = 0; i < numberOfSimulations ; i ++) {
Simulation warmUpSimulation = new Simulation(m, n, evictionPolicy);
warmUpSimulation.run(warmupTime);
System.out.println(((double) warmUpSimulation.misses) / warmUpSimulation.requests);
Simulation recordSimulation = new Simulation(m, n, evictionPolicy, warmUpSimulation.cache);
recordSimulation.run(simulationTime);
cacheMissRates.add(((double) recordSimulation.misses) / recordSimulation.requests);
System.out.println(((double) recordSimulation.misses) / recordSimulation.requests);
}
double sum = cacheMissRates.stream().reduce(Double::sum).get();
return cacheMissRates;
}
public Simulation(int m, int n, EvictionPolicy evictionPolicy) {
this (m, n, evictionPolicy, new ArrayList<>());
this (m, n, evictionPolicy, new ArrayList<>(), true);
}
public Simulation (int m, int n, EvictionPolicy evictionPolicy, List<Integer> cache) {
public Simulation (int m, int n, EvictionPolicy evictionPolicy, List<Integer> cache, boolean populateCache) {
this.evictionPolicy = evictionPolicy;
this.cache = cache;
this.cumulativeRateSum = new ArrayList<>();
stateToTime = new HashMap<>();
this.misses = 0;
this.requests = 0;
this.currentTime = 0;
// Populating cache
for (int i = 1; i <= m; i++) {
cache.add(i);
if (populateCache) {
for (int i = 1; i <= m; i++) {
cache.add(i);
}
}
for (int i = 1; i<= n; i++) {
......@@ -73,9 +67,63 @@ public class Simulation {
this.cumulativeRateSum.add(prev + (1.0/(rateSum * i)));
}
}
public static void run(int m, int n, double simulationTime, int numberOfSimulations, double warmupTime, EvictionPolicy evictionPolicy,
double confidence, boolean printStates) {
Measure missesMeasure = new Measure();
HashMap<List<Integer>, Measure> stateToMeasures = new HashMap<>();
for (int i = 0; i < numberOfSimulations ; i ++) {
Simulation warmUpSimulation = new Simulation(m, n, evictionPolicy);
warmUpSimulation.run(warmupTime);
Simulation recordSimulation = new Simulation(m, n, evictionPolicy, warmUpSimulation.cache, false);
recordSimulation.run(simulationTime);
missesMeasure.addObservation(((double) recordSimulation.misses) / recordSimulation.requests);
for (List<Integer> state : recordSimulation.stateToTime.keySet()) {
if (!stateToMeasures.containsKey(state)) {
Measure stateMeasure = new Measure();
stateMeasure.addObservation(recordSimulation.stateToTime.get(state) / recordSimulation.currentTime);
stateToMeasures.put(state, stateMeasure);
} else {
stateToMeasures.get(state).addObservation(recordSimulation.stateToTime.get(state) / recordSimulation.currentTime);
}
}
}
double mean = missesMeasure.sampleMean();
double ciRange = missesMeasure.ciHalfWidth(confidence);
double upperVal = mean + ciRange;
double lowerVal = mean - ciRange;
System.out.println("Miss Mean is " + missesMeasure.sampleMean() + " CI is ( " + lowerVal + ", " + upperVal +")");
if (printStates) {
for (List<Integer> state : stateToMeasures.keySet()) {
Measure stateMeasure = stateToMeasures.get(state);
if (stateMeasure.getNumObservations() > 1) {
double stateMean = stateMeasure.sampleMean();
double stateCiRange = stateMeasure.ciHalfWidth(confidence);
double stateUpperVal = stateMean + stateCiRange;
double stateLowerVal = stateMean - stateCiRange;
System.out.println("For state " + state + "Mean is " + stateMean + " CI is ( " + stateLowerVal + ", " + stateUpperVal + ")");
} else {
double stateMean = stateMeasure.sampleMean();
System.out.println("For state " + state + "Mean is " + stateMean);
}
}
}
}
public double nextArrival() {
return -Math.log(Math.random()) / rateSum;
}
......@@ -86,10 +134,11 @@ public class Simulation {
public int getItem() {
double uniform = Math.random();
return -1 - Collections.binarySearch(cumulativeRateSum, uniform * cumulativeRateSum.get(cumulativeRateSum.size()-1));
return -1 - Collections.binarySearch(cumulativeRateSum, uniform * cumulativeRateSum.get(cumulativeRateSum.size()-1)) + 1;
}
public void processItemRequest(int item) {
cache = new ArrayList<>(cache);
requests ++;
if (!cache.contains(item)) {
misses++;
......@@ -102,9 +151,6 @@ public class Simulation {
cache.add(i, item);
}
}
}
// public void evictItem(int item) {
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment