init
This commit is contained in:
commit
68ead7f219
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Launch Main",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "Main",
|
||||||
|
"projectName": "assignment_0_9a9b1a91",
|
||||||
|
"args": "0.5 1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Launch Current File",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "${file}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Launch Main",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "Main",
|
||||||
|
"projectName": "assignment_87438f",
|
||||||
|
"args": "0.5 1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
{
|
||||||
|
"java.project.sourcePaths": ["src"],
|
||||||
|
"java.project.outputPath": "bin",
|
||||||
|
"java.project.referencedLibraries": [
|
||||||
|
"lib/**/*.jar"
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
|
||||||
|
|
||||||
|
## Folder Structure
|
||||||
|
|
||||||
|
The workspace contains two folders by default, where:
|
||||||
|
|
||||||
|
- `src`: the folder to maintain sources
|
||||||
|
- `lib`: the folder to maintain dependencies
|
||||||
|
|
||||||
|
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
|
||||||
|
|
||||||
|
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
|
||||||
|
|
||||||
|
## Dependency Management
|
||||||
|
|
||||||
|
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,47 @@
|
||||||
|
import os.path,subprocess
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
from subprocess import STDOUT,PIPE
|
||||||
|
from typing import ByteString
|
||||||
|
|
||||||
|
def compile_java(java_file):
|
||||||
|
subprocess.check_call(['javac', java_file])
|
||||||
|
|
||||||
|
def execute_java(java_file, stdin, probability, width):
|
||||||
|
java_class,ext = os.path.splitext(java_file)
|
||||||
|
cmd = ['java', java_class, str(probability), str(width)]
|
||||||
|
proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
|
||||||
|
stdout,stderr = proc.communicate(stdin)
|
||||||
|
return stdout.decode('utf-8')
|
||||||
|
|
||||||
|
filename = 'Main.java'
|
||||||
|
compile_java(filename)
|
||||||
|
|
||||||
|
# fixed width
|
||||||
|
width_list = [x for x in range(1,100,30)]
|
||||||
|
for w in width_list:
|
||||||
|
probabily_list = [x/100 for x in range(0,100,5)]
|
||||||
|
time_list = []
|
||||||
|
for probability in probabily_list:
|
||||||
|
time_list.append(float(execute_java(filename, '', probability, w).strip('\n')))
|
||||||
|
print(time_list)
|
||||||
|
plt.plot(probabily_list, time_list)
|
||||||
|
plt.xlabel('Probability')
|
||||||
|
plt.ylabel('Time')
|
||||||
|
plt.title('Fixed Width')
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
# variable width
|
||||||
|
# p = 0.5
|
||||||
|
# w_list = [x for x in range(2,201)]
|
||||||
|
# time_list = []
|
||||||
|
# for width in w_list:
|
||||||
|
# time_list.append(float(execute_java(filename, '', p, width).strip('\n')))
|
||||||
|
# print(time_list)
|
||||||
|
# plt.plot(w_list, time_list)
|
||||||
|
# plt.xlabel('Width')
|
||||||
|
# plt.ylabel('Time')
|
||||||
|
# plt.title('Variable width')
|
||||||
|
# plt.show()
|
||||||
|
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,21 @@
|
||||||
|
public class Cell {
|
||||||
|
private double probability;
|
||||||
|
private boolean cellState;
|
||||||
|
|
||||||
|
Cell(double probability) {
|
||||||
|
this.probability = probability;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSensorState() {
|
||||||
|
double random = Math.random();
|
||||||
|
if (random <= probability) {
|
||||||
|
cellState = true;
|
||||||
|
} else {
|
||||||
|
cellState = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getSensorState() {
|
||||||
|
return cellState;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,11 @@
|
||||||
|
|
||||||
|
public class Clock {
|
||||||
|
private int time = 0;
|
||||||
|
public int getTime() {
|
||||||
|
return time;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void tick() {
|
||||||
|
time += 10;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,10 @@
|
||||||
|
public class Infiltrator {
|
||||||
|
private int location = 0;
|
||||||
|
public int getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void proceedForward() {
|
||||||
|
location += 1;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -0,0 +1,66 @@
|
||||||
|
class Main {
|
||||||
|
static int maxtime = 10000000;
|
||||||
|
static boolean isInfinite = false;
|
||||||
|
public static double average(int[] timeTaken) {
|
||||||
|
int sum = 0;
|
||||||
|
for (double currentTime : timeTaken)
|
||||||
|
sum += currentTime;
|
||||||
|
return (double)sum / timeTaken.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
int numOfIterations = 50;
|
||||||
|
int[] timeTaken = new int[numOfIterations];
|
||||||
|
|
||||||
|
double probability = Double.parseDouble(args[0]);
|
||||||
|
int width = Integer.parseInt(args[1]);
|
||||||
|
for (int i = 0; i < numOfIterations; i++) {
|
||||||
|
|
||||||
|
Cell cellUp = new Cell(probability);
|
||||||
|
Cell cellRightUp = new Cell(probability);
|
||||||
|
Cell cellLeftUp = new Cell(probability);
|
||||||
|
Cell cellPresent = new Cell(probability);
|
||||||
|
|
||||||
|
Infiltrator infiltrator = new Infiltrator();
|
||||||
|
Clock clock = new Clock();
|
||||||
|
cellUp.setSensorState();
|
||||||
|
cellRightUp.setSensorState();
|
||||||
|
cellLeftUp.setSensorState();
|
||||||
|
while(cellLeftUp.getSensorState() && cellRightUp.getSensorState() && cellUp.getSensorState()) {
|
||||||
|
cellLeftUp.setSensorState();
|
||||||
|
cellRightUp.setSensorState();
|
||||||
|
cellUp.setSensorState();
|
||||||
|
clock.tick();
|
||||||
|
}
|
||||||
|
|
||||||
|
infiltrator.proceedForward();
|
||||||
|
clock.tick();
|
||||||
|
|
||||||
|
while (clock.getTime() < 1000000 && infiltrator.getLocation() < width) {
|
||||||
|
cellUp.setSensorState();
|
||||||
|
cellRightUp.setSensorState();
|
||||||
|
cellLeftUp.setSensorState();
|
||||||
|
cellPresent.setSensorState();
|
||||||
|
if (!cellPresent.getSensorState()&&(!cellLeftUp.getSensorState()||!cellRightUp.getSensorState()||!cellUp.getSensorState())) {
|
||||||
|
infiltrator.proceedForward();
|
||||||
|
}
|
||||||
|
if (infiltrator.getLocation() == width) {
|
||||||
|
cellPresent.setSensorState();
|
||||||
|
while (cellPresent.getSensorState()) {
|
||||||
|
clock.tick();
|
||||||
|
cellPresent.setSensorState();
|
||||||
|
}
|
||||||
|
infiltrator.proceedForward();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
clock.tick();
|
||||||
|
}
|
||||||
|
if(clock.getTime() >= maxtime) {
|
||||||
|
isInfinite = true;
|
||||||
|
}
|
||||||
|
timeTaken[i] = clock.getTime();
|
||||||
|
}
|
||||||
|
System.out.println(average(timeTaken));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue