Commit 8666c04d authored by Amelie Royer's avatar Amelie Royer

Cleaning Synth dataset

parent 177e5d1e
Item 0
Item 1
Item 2
Item 3
Item 4
\ No newline at end of file
This diff is collapsed.
0 1 1
1 1 1
2 1 1
3 1 1
4 1 1
\ No newline at end of file
This diff is collapsed.
781 States
5 Actions (Items)
5 user profiles
4 history length
5 product clustering level
-----> Probability inference
-----> End
All outputs are in /home/amelie/Rotations/ChatterjeeRotation/Code/Models/Synth545/synth_u5_k4_pl5
This diff is collapsed.
This diff is collapsed.
......@@ -58,8 +58,8 @@ void mainMEMDP(M model, std::string datafile_base, std::string algo, int horizon
start = std::chrono::high_resolution_clock::now();
std::cout << "\n" << current_time_str() << " - Evaluation results\n";
AIToolbox::POMDP::Policy policy(model.getS(), model.getA(), model.getO(), std::get<1>(solution));
evaluate_policy_interactiveMEMDP(500, model, policy, horizon, verbose, true);
//evaluate_policyMEMDP(datafile_base + ".test", model, policy, horizon, verbose, true);
//evaluate_policy_interactiveMEMDP(500, model, policy, horizon, verbose, true);
evaluate_policyMEMDP(datafile_base + ".test", model, policy, horizon, verbose, true);
testing_time = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start).count() / 1000000.;
}
......
......@@ -8,7 +8,6 @@
#include "utils.hpp"
/**
* CURRENT_TIME_STR
*/
......@@ -201,7 +200,7 @@ void evaluate_policyMDP(std::string sfile,
for (auto it = begin(aux); it != end(aux); ++it) {
// Identity
user++;
std::cerr << "\r User " << user << "/" << aux.size() << std::flush;
//std::cerr << "\r User " << user << "/" << aux.size() << std::flush;
cluster = std::get<0>(*it);
set_lengths[cluster] += 1;
session_length = std::get<1>(*it).size();
......@@ -229,7 +228,6 @@ void evaluate_policyMDP(std::string sfile,
accuracy += accuracy_score(prediction, action);
precision += avprecision_score(action_scores, action);
}
mean_accuracy[cluster] += accuracy / session_length;
mean_precision[cluster] += precision / session_length;
mean_total_reward[cluster] += total_reward / session_length;
......
......@@ -176,7 +176,7 @@ def load_data(base_name, plevel, ulevel, hlength, alpha, trfr, sv=False):
product_clusterID = product_to_cluster[int(sale[0])]
product_profit[product_clusterID] += float(sale[5]) - float(sale[6])
product_profit_nrm[product_clusterID] += 1
for _ in xrange(int(sale[7])):
for _ in xrange(1):#int(sale[7])):
user_sessions[customer_to_cluster[int(sale[2])]][int(sale[2])].append(product_clusterID)
f.close()
......@@ -299,8 +299,10 @@ if __name__ == "__main__":
###### 5. Add a positive factor for the recommended action transition
print "\n\033[91m-----> Probability inference\033[0m"
max_upscale = 0.9995
old_probs, probs = [], []
with open("%s.transitions" % output_base, 'w') as f:
for user_profile in xrange(js_count.shape[0]):
probs = []
print >> sys.stderr, "\n > Profile %d / %d: \n" % (user_profile + 1, js_count.shape[0]),
sys.stderr.flush()
# For fixed s1
......@@ -319,11 +321,18 @@ if __name__ == "__main__":
# Negative (s1, b, s1.a)
beta = float(nrm - new_count) / (nrm - count)
assert(beta > 0 and beta <= 1), "AssertionError: Beta out of range"
probs.append(new_count)
for b, count in enumerate(s1_counts):
if a - 1 != b:
s2 = get_next_state_id(s1, b + 1)
f.write("%d\t%d\t%d\t%s\n" % (s1, a, s2, beta * count if not args.norm else beta * count / nrm))
probs.append(beta * count)
f.write("\n")
from scipy.stats import pearsonr
if len(old_probs) > 0:
print pearsonr(old_probs, probs)
old_probs = list(probs)
args.alpha += 0.1
print "\n\n\033[92m-----> End\033[0m"
......
......@@ -16,7 +16,6 @@ from random import randint
from utils import *
def init_output_dir(plevel, hlength):
"""
Initializa the output directory.
......@@ -38,14 +37,8 @@ def init_output_dir(plevel, hlength):
os.makedirs(output_dir)
return os.path.join(output_dir, output_base)
##################################################### M A I N R O U T I N E #######
if __name__ == "__main__":
###### 0. Set Parameters
base_folder = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
parser = argparse.ArgumentParser(description='Generate syntehtic POMCP parameters with a high discrepancy between environments.')
......@@ -53,18 +46,16 @@ if __name__ == "__main__":
parser.add_argument('-n', '--nactions', type=int, default=3, help="Number of items (actions) and clusters.")
parser.add_argument('-k', '--history', type=int, default=2, help="Length of the history to consider for one state of the MEMDP.")
parser.add_argument('-t', '--test', type=int, default=2000, help="Number of test sessions to generate.")
#parser.add_argument('--ordered', action='store_true', help="If present, the states of the MEMDP are ordered product sequences. TODO.")
parser.add_argument('--norm', action='store_true', help="If present, normalize the output transition probabilities.")
parser.add_argument('-a', '--alpha', type=float, default=1.1, help="Positive rescaling of transition probabilities matching the recommendation.")
args = parser.parse_args()
###### 0-bis. Check assertions
assert(args.nactions > 0), "plevel argument must be strictly positive"
assert(args.history > 1), "history length must be strictly greater than 1"
assert(args.test > 0), "Number of test sessions must be strictly positive"
logger = Logger(sys.stdout)
sys.stdout = logger
###### 1. Load data and create product/user profile
n_items = args.nactions
......@@ -73,7 +64,7 @@ if __name__ == "__main__":
init_base_writing(n_items, args.history)
n_states = get_nstates(n_items, args.history)
output_base = init_output_dir(args.nactions, args.history)
exc = 4 * (n_users - 1) #Ensure 0.8
exc = 4 * (n_users - 1) # Ensure 0.8 probability given to action i
#### 2. Write dummy files
with open("%s.items" % output_base, "w") as f:
......@@ -82,8 +73,7 @@ if __name__ == "__main__":
with open("%s.profiles" % output_base, "w") as f:
f.write("\n".join("%d\t1\t1" % i for i in xrange(n_users)))
##### Create dummy test sessions
##### 3. Create dummy test sessions
with open("%s.test" % output_base, 'w') as f:
for user in xrange(args.test):
cluster = randint(0, n_users - 1)
......@@ -101,17 +91,16 @@ if __name__ == "__main__":
session.append(a)
session.append(s2)
f.write("%d\t%d\t%s\n" % (user, cluster, ' '.join(str(x) for x in session) ))
###### 2. Set rewards
###### 4. Set rewards
with open("%s.rewards" % output_base, 'w') as f:
for s1 in xrange(n_states):
for item in actions:
f.write("%d\t%d\t%d\t%.5f\n" % (s1, item, get_next_state_id(s1, item), 1))
###### 3. Assign random transition probabilities
###### 5. Create transition function
print "\n\033[91m-----> Probability inference\033[0m"
total_count = exc + n_items - 1
with open("%s.transitions" % output_base, 'w') as f:
for user_profile in xrange(n_users):
print >> sys.stderr, "\n > Profile %d / %d: \n" % (user_profile + 1, n_users),
......@@ -120,18 +109,20 @@ if __name__ == "__main__":
for s1 in xrange(n_states):
sys.stderr.write(" state: %d / %d \r" % (s1 + 1, n_states))
sys.stderr.flush()
# For fixed a
for a in actions:
count = exc if a == user_profile + 1 else 1
beta = (total_count - args.alpha * count) / (total_count - count)
# For every s2, sample T(s1, a, s2)
for link in actions:
count = exc if link == user_profile + 1 else 1
s2 = get_next_state_id(s1, link)
f.write("%d\t%d\t%d\t%s\n" % (s1, a, s2, count if not args.norm else count / (exc + n_actions - 1)))
for s2_link in actions:
s2 = get_next_state_id(s1, s2_link)
if (s2_link == a):
f.write("%d\t%d\t%d\t%s\n" % (s1, a, s2, args.alpha * count if not args.norm else args.alpha * count / total_count))
else:
f.write("%d\t%d\t%d\t%s\n" % (s1, a, s2, beta * count if not args.norm else beta * count / total_count))
f.write("\n")
###### 6. Summary
print "\n\n\033[92m-----> End\033[0m"
print " All outputs are in %s" % output_base
with open("%s.summary" % output_base, 'w') as f:
......
......@@ -62,8 +62,12 @@ def get_nstates(n_items, hlength):
"""
return (n_items ** (hlength + 1) - 1) / (n_items - 1)
aux = {}
l= [3658,626,6302,4594,6644,7293,3697,2240,6823,2854,10052,1695,7809,3559,4117,3208,4353,2660,5916,2652,10197,5292,1300,3619,6331,5467,8183,5534,2783,6513,4245,1208,2812,3940,3182,7537,5098,10093,6386,7174,4048,5422,9617,4457,4316,7419,6848,3862,7680,7889,3515,1382,4154,3229,3287,5215,6025,5885,9458,2227,19,8854,5068,1945,9096,5713,4643,8218,2408,3348,1148,4462,5282,7062,5128,854,6789,6244,9278,9845,4593,1508,8177,714,4645,2274,4014,5350,8085,9542,2303,6751,3703,8444,7697,6109,5383,7651,9378,3732,2943,5646,31,9575,7749,6973,7837,9624,4822,1493,1603,10084,10256,3038,2340,1111,1597,811,2357,7876,7804,1896,372,4569,1494,264,9347,8223,1685,1661,8090,10198,7756,4668,6613,9118,6483,6953,9439,5470,6672,4311,4882,4988,3247,6088,10211,380,9046,1264,8944,28479946,7543,8026,5873,7921,1765,4570,10036,8648,6243,3606,40,7725,2135,3770,1771,2308,10199,6425,2984,5677,6602,1066,599,295,7438,9818]
def assign_customer_cluster(user):
global l
from random import randint
"""
Assigns user profile given customer data from the foodmart dataset.
......@@ -73,10 +77,15 @@ def assign_customer_cluster(user):
Returns:
* ``cluster`` (*int*): Cluster ID
"""
gender = int(user[19] == 'F')
age_category = ((1997 - int(user[16].split('-', 1)[0])) / 10) / 3
return gender * 10 + age_category
return randint(0, 5)
#max_income = int(''.join([x for x in user[18].strip().split('$')[-1] if x.isdigit()]))
#if (int(user[19] == 'F') and int(user[20]) >= 3) and max_income <= 50:
# return 1
#else:
# return 0
def print_customer_cluster(cluster):
......
......@@ -34,7 +34,7 @@ ctest -V
* ``[5]`` Proportion of the dataset to keep for parameter inference (Defaults to 0.8).
* ``[6]`` Path to the Foodmart dataset (either ``tar.gz`` archive or ``Foodmart/data`` directory).
* ``[7]`` Path to the main output directory (Defaults to ``../Code/Models``).
* ``[--norm]`` If present, normalize the transition probabilities. (*Note*: Do not use this option for this code, as the transition probabilities are normalized in the C routines anyway).
* ``[--norm]`` If present, normalize the transition probabilities. (*Note*: Transition probabilities are already normalized in the main C routines).
* ``[--help]`` displays help about this command
**Example** *(6 environments, 3 actions, 13 states)* : ``python prepare_foodmart.py -d Foodmart.tar.gz -pl 4 -k 2``
......@@ -51,7 +51,7 @@ ctest -V
* ``[2]`` History length (Defaults to 2). Must be strictly greater than 1.
* ``[3]`` Number of test sessions to generate according to the synthetic distribution (Defaults to 2000).
* ``[4]`` Path to the output directory (Defaults to ``../Code/Models``).
* ``[--norm]`` If present, normalize the transition probabilities. (*Note*: Do not use this option for this code, as the transition probabilities are normalized in the C routines anyway).
* ``[--norm]`` If present, normalize the transition probabilities. (*Note*: Transition probabilities are already normalized in the main C routines).
* ``[--help]`` displays help about this command
**Example** *(3 environments, 3 actions, 13 states)* : ``python prepare_synth.py -n 3 -k 2``
......
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