-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.R
85 lines (77 loc) · 2.39 KB
/
stats.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
source("markov.R")
# Run the Delivery Man game n times.
runNtimes = function(n,fname="test.dat") {
sum=0
runs=list()
for (i in 1:n) {
#moves=runWheresCroc(markovMoves,T,0)
moves=runWheresCroc(randomWC,T,0)
runs[[i]]=moves
sum=sum+moves
}
print("DONE!")
print(paste("Average no. moves = ",sum/n))
lapply("vals",write,fname,append=TRUE,ncolumns=1000)
lapply(runs,write,fname,append=TRUE,ncolumns=1000)
}
# Plot a histogram from data written to a file in runNtimes.
plotHist = function(fname) {
data=read.csv(file=fname,sep=",",head=TRUE)
hist(data$vals)
hist(data$vals,main="Distribution of runs",xlab="Number of moves")
}
# Plot a boxgraph from data written to a file in runNtimes.
plotBox = function(fname1,fname2=NULL,xlab=NULL) {
data1=read.csv(file=fname1, sep=",",head=TRUE)
if(is.null(fname2)) {
boxplot(data1$vals,main='Distribution of runs',ylab='Number of moves')
if(!is.null(names)) {
boxplot(data1$vals,names=xlab,main='Distribution of runs',ylab='Number of moves')
}
else {
boxplot(data1$vals,main='Distribution of runs',ylab='Number of turns')
}
}
else {
if(!is.null(names)) {
data2=read.csv(file=fname2,sep=",",head=TRUE)
boxplot(data1$vals,data2$vals,names=xlab,main='Distribution of runs',ylab='Number of turns')
}
else {
data2=read.csv(file=fname2,sep=",",head=TRUE)
boxplot(data1$vals,data2$vals,main='Distribution of runs',ylab='Number of turns')
}
}
}
appendData = function(fname1,fname2,data,val) {
lapply(data,write,fname1,append=TRUE,ncolumns=1000)
lapply(val,write,fname2,append=TRUE,ncolumns=1000)
}
accuracy = function(fname1,fname2) {
trans=transitionMatrix()
src1=read.csv(file=fname1,sep=",",head=TRUE)
src2=read.csv(file=fname2,sep=",",head=TRUE)
data=src1$vals
vals=src2$vals
acc=0
for(i in 1:length(data)) {
path=getPath(trans,data[[i]],vals[[i]],1,NULL,list())
dist=path[[1]]
if(dist==0) { dist=1 }
acc = acc+(1/dist)
#if(data[[i]]==vals[[i]]) {
#acc=acc+1
#}
}
return(acc/length(data))
}
# Calculate the standard deviation from data written to a file in runNtimes.
stdDeviation = function(fname) {
data=read.csv(file=fname,sep=",",head=TRUE)
print(sd(data$vals))
}
# Calculate the average from data written to a file in runNtimes.
average = function(fname) {
data=read.csv(file=fname,sep=",",head=TRUE)
print(mean(data$vals))
}