-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.py
More file actions
181 lines (150 loc) · 5.14 KB
/
Simulation.py
File metadata and controls
181 lines (150 loc) · 5.14 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import subprocess
import random
from itertools import product
from flask_pymongo import PyMongo
from pymongo import MongoClient
import matplotlib.pyplot as plt
from EpsilonGreedy import EpsilonGreedy
from AnnealedEpsilonGreedy import AnnealedEpsilonGreedy
from Softmax import Softmax
from UCB import UCB
from MOEpsilonGreedy import MOEpsilonGreedy
from MOSoftmax import MOSoftmax
from datetime import datetime, timedelta
from UserPreferences import Node, getTree
from User import User
import random as rand
import numpy as np
def writeStats(filename, date, clicks, time, r, std_dev):
with open(filename, "a") as file:
file.write(str(date) + ",")
versions = db.Clicks.find()
for i, v in enumerate(versions):
file.write(str(v.get("percentage")) + ",") if i != versions.count() - 1 else file.write(str(v.get("percentage")))
# file.write(str(v.get("count") - counts[i]) + ",")
# counts[i] = v.get("count")
# file.write(str(clicks) + "," + str(time) + "," + str(r) + "," + str(std_dev))
file.write("\n")
# print counts
file.close()
# Set MongoDB details
client = MongoClient('localhost:27017')
db = client.ClickData
# Simulation variables
horizon = 10
simulations = 3
# avg_rewards =[0.0 for i in range(simulations)]
avg_rewards = []
epsilon = [0.1]
algos = ["ucb"]
# Website variables
layouts = ["grid", "list"]
# layouts = ["grid"]
font_sizes = ["small", "large"]
colour_schemes = ["dark", "light"]
# colour_schemes = ["light"]
features = [layouts, font_sizes, colour_schemes]
stats_file = "static/dashboard/data/data.csv"
# stats_file = "static/dashboard/data/ucb.csv"
dt = datetime.now()
stats_date = dt.date()
# stats_date = 0
# open file
file = open("simulation.txt", "w")
for algo in algos:
# initialise all possible versions into DB
versions = list(product(features[0], features[1], features[2]))
counts = [0 for i in versions]
# testing only the top 4 versions
# versions = [('grid', 'small', 'dark'), ('grid', 'large', 'light'), ('list', 'small', 'dark'), ('list', 'large', 'dark')]
headers = "date,"
for i, v in enumerate(versions):
if db.Clicks.find({"$and": [{"layout": v[0]}, {"font_size": v[1]}, {"colour_scheme": v[2]}]}).count() == 0:
db.Clicks.insert_one({'layout': v[0], 'colour_scheme': v[2], 'font_size': v[1], 'count': 0, 'value': 0.0, 'clicks': 0, 'time': 0, 'percentage': 0})
headers += "version" + str(i + 1) if i == len(versions) - 1 else "version" + str(i + 1) + ","
# headers += "version" + str(i + 1) + ","
# headers += "clicks,time,reward,std_dev"
# write all versions to Stats file
# "w" is overwriting the current file
f = open(stats_file, "w")
f.write(headers + "\n")
f.close()
# Choose algo type
if algo == "e-greedy":
bandit = MOEpsilonGreedy(0.1, features)
elif algo == "softmax":
bandit = MOSoftmax(0.1, features)
elif algo == "ucb":
bandit = UCB(features)
for i in range(simulations):
reward_sum = 0.0
avg_clicks = 0.0
avg_time = 0.0
std_dev = 0.0
rewards_arr = [0.0 for i in range(horizon)]
for j in range(horizon):
# create user object
user = User()
# get preferences tree
tree = getTree("UserPreferencesTree.pkl")
# set preferences
user.buildPreferences(tree, versions)
# Get layout version from Bandit algorithm
version = bandit.getVersion()
layout = version.get('layout')
colour_scheme = version.get('colourScheme')
font_size = version.get('fontSize')
# show the worst version as a baseline
# layout = "list"
# colour_scheme = "light"
# font_size = "large"
# print "version: ", version
# print "user: ", user.preferences
rating = 0
for v in user.preferences:
if v["version"][0] == layout and v["version"][1] == font_size and v["version"][2] == colour_scheme:
rating = v["rating"]
clicks = 0
prob = rand.random()
if prob <= 0.6827:
clicks = rand.randint(rating-1, rating+1)
elif prob < 0.9545:
clicks = rand.randint(rating-2, rating+2)
else:
clicks = rand.randint(rating-3, rating+3)
if clicks < 0:
clicks = 0
time_multiplier = rand.randint(3,6)
time = rating*time_multiplier
rewards = {"clicks": clicks, "time": time}
bandit.updateValue(version, rewards)
r = (0.75 * clicks + 0.25 * time) / 13.5
print "reward: ", rewards, rating, r
# rewards[index] = reward
reward_sum += r
avg_clicks += clicks
avg_time += time
rewards_arr[j] = r
# calculate average reward for simulation
# avg_rewards[i] = reward_sum / float(horizon)
avg_rewards.append(reward_sum / float(horizon))
std_dev = np.std(rewards_arr)
print i, avg_rewards[-1], std_dev
print avg_rewards
# write to Stats file - considering each horizon iteration to be one day
writeStats(stats_file, stats_date, avg_clicks / float(horizon), avg_time / float(horizon), avg_rewards[-1], std_dev)
# increment date
stats_date += timedelta(days=1)
# stats_date += 1
# write to Simulation file
# results = ""
# for r in avg_rewards:
# results += str(r)
# results += " "
# file.write(results + "\n")
# print "overall avg reward: ", float(sum(avg_rewards)) / float(simulations)
# clear DB for next epsilon simulation
# db.Clicks.drop()
# close file
file.close()
# plotResults(simulations, horizon, "simulation.txt")