Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
library(ggplot2)
library(jsonlite)
library(rmongodb)
setwd("C:/Users/User/Desktop/LiF")
BASE_URL = "http://localhost:8080"
key = "51fc58011"
question_nr = 2626
customers <- 4000
push <- function(A, value){
if(any(is.na(A))){
i <- sum(!is.na(A))
A[i+1] <- value
} else {
A <- c(A,value)
A <- A[-1]
}
return(A)
}
x1 <- c( 0, 30, 60, 90 )
f1 <- c( .4, .4, .2, .2 )
x2 <- c( 0, 30, 60, 90 )
f2 <- c( .4, .4, .7, .0 )
x3 <- c( 0, 30, 60, 90 )
f3 <- c( .2, .2, .1, .8 )
s1 <- splinefun(x1, f1,method="fmm")
s2 <- splinefun(x2, f2,method="fmm")
s3 <- function(x){
return( 1 - s1(x) - s2(x) )
}
arr.x <- rep(NA, customers)
y <- 0.4
for(i in 1:customers){
request <- paste( BASE_URL,"/2/getAction.json?key=",key,"&context={\"question\":",question_nr,"}", sep="")
raw.data <- readLines(request, warn = "F")
result <- fromJSON(raw.data)
if(result$action$x > 90) result$action$x <- 90
if(result$action$x < 0) result$action$x <- 0
x = result$action$x
t = result$action$t
arr.x[i] <- x
sampleChoice <- sample( c("A","B","C"), 1, replace=TRUE, prob=c(s1(x), s2(x), s3(x)))
if (sampleChoice == "A") {
y <- 30*0.05 # 5% profit on $30 of A
} else if (sampleChoice == "B"){
y <- 60*0.06 # 6% profit of $60 for B
} else {
y <- 60*0.04 # 4% profit on $60 for C
}
request <- paste( BASE_URL,"/2/setReward.json","?key=",key,"&context={\"question\":",question_nr,"}",
"&action={\"x\":",x,",\"t\":",t,"}","&reward=",y, sep="")
raw.data <- readLines(request, warn = "F")
}
plot(arr.x, type="l")
mongo = mongo.create(host = "localhost")
if (mongo.is.connected(mongo)) {
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "q", question_nr)
mongo.bson.buffer.append(buf, "type", "setreward")
query <- mongo.bson.from.buffer(buf)
cursor <- mongo.find(mongo, "logs.logs", query)
res <- mongo.cursor.to.data.frame(cursor)
}
plot(as.numeric(as.character(res$t)),as.numeric(as.character(res$x0)), type="l")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.