Commit d7040ee9 authored by Leon's avatar Leon

Started work on Q1

parents
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>11</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/CacheSimulation.iml" filepath="$PROJECT_DIR$/CacheSimulation.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="e90fe0da-ae4d-4890-93ca-a29a3175dada" name="Default Changelist" comment="" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="ProjectId" id="1T6m0aO6fSjNCyy0xG3nr56oz1V" />
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
</component>
<component name="RecentsManager">
<key name="CreateClassDialog.RecentsKey">
<recent name="" />
</key>
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="RunManager">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="CacheSimulation" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="e90fe0da-ae4d-4890-93ca-a29a3175dada" name="Default Changelist" comment="" />
<created>1572788696209</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1572788696209</updated>
<workItem from="1572788701224" duration="1221000" />
<workItem from="1572790772456" duration="7776000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
public class Main {
public static void main(String[] args) {
Simulation simulation = new Simulation(10, 1000, EvictionPolicy.FIFO);
simulation.run(10000000.0);
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Simulation {
private int misses;
private int requests;
private final int currentTime;
private double rateSum;
private List<Integer> cache;
private List<Double> cumulativeRateSum;
private EvictionPolicy evictionPolicy;
public void run(double simulationTime) {
double currTime = 0;
while (currTime < simulationTime) {
currTime += nextArrival();
int item = getItem();
processItemRequest(item);
System.out.println(((double) misses)/requests);
}
}
public Simulation(int m, int n, EvictionPolicy evictionPolicy) {
this.evictionPolicy = evictionPolicy;
this.cache = new ArrayList<>();
this.cumulativeRateSum = new ArrayList<>();
this.misses = 0;
this.requests = 0;
this.currentTime = 0;
// Populating cache
for (int i = 1; i <= m; i++) {
cache.add(i);
rateSum += 1.0/i;
}
cumulativeRateSum.add(1/rateSum);
for (int i = 2; i <= n; i++) {
Double prev = cumulativeRateSum.get(cumulativeRateSum.size() - 1);
cumulativeRateSum.add(prev + (1/(rateSum * i)));
}
}
public double nextArrival() {
return -Math.log(Math.random()) / rateSum;
}
public double exp(double lam) {
return -Math.log(Math.random()) / lam;
}
public int getItem() {
double uniform = Math.random();
return -1 - Collections.binarySearch(cumulativeRateSum, uniform);
}
public void processItemRequest(int item) {
requests ++;
if (!cache.contains(item)) {
misses++;
}
if (evictionPolicy == EvictionPolicy.FIFO) {
cache.remove(0);
cache.add(item);
} else if (evictionPolicy == EvictionPolicy.RAND) {
int i = (int) (Math.random() * cache.size());
cache.remove(i);
cache.add(i, item);
}
}
// public void evictItem(int item) {
//
// }
}
enum EvictionPolicy {FIFO, RAND}
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