def annealing_optimize( result_from_coll_filter, movie_had_seen, result_from_cluster, movie_name_arr ): temperature = 5000.0 cool = 0.96 # generate the first pop and add to the pop list sol = [] sol_tmp = [] # create the first pop payoff_list_coll, name_list_coll = gt.gen_payoff_coll_filt(result_from_coll_filter) sol = zip(payoff_list_coll, name_list_coll) # mian loop while temperature > 0.1: payoff_list_tmp, name_list_tmp = gt.gen_payoff_cluster(result_from_cluster, movie_name_arr, name_list_coll, movie_had_seen) sol_tmp = zip(payoff_list_tmp, name_list_tmp) sol = annealing_choice( sol, sol_tmp, temperature) # decrease the temperature temperature =temperature * cool sol.sort() sol.reverse() payoff_list_from_annealing, name_list_from_annealing = split_payoff_and_name( sol ) return name_list_from_annealing, payoff_list_from_annealing
def annealing_optimize(result_from_coll_filter, movie_had_seen, result_from_cluster, movie_name_arr): temperature = 5000.0 cool = 0.9 # generate the first pop and add to the pop list sol = [] sol_tmp = [] # create the first pop payoff_list_coll, name_list_coll = gt.gen_payoff_coll_filt( result_from_coll_filter) sol = zip(payoff_list_coll, name_list_coll) # mian loop while temperature > 0.1: payoff_list_tmp, name_list_tmp = gt.gen_payoff_cluster( result_from_cluster, movie_name_arr, name_list_coll, movie_had_seen) sol_tmp = zip(payoff_list_tmp, name_list_tmp) sol = annealing_choice(sol, sol_tmp, temperature) # decrease the temperature temperature = temperature * cool sol.sort() sol.reverse() payoff_list_from_annealing, name_list_from_annealing = split_payoff_and_name( sol) return name_list_from_annealing, payoff_list_from_annealing
def origin_coll_filter_analysis(user_id): result_from_coll_filter, movie_had_seen = coll.run_collaborative_filter( user_id) payoff_list, name_from_coll_result = gt.gen_payoff_coll_filt( result_from_coll_filter) print 'collaborative filter analysis finished.' return name_from_coll_result, payoff_list
def genetic_opt(result_from_coll_filter, movie_had_seen, result_from_cluster, movie_name_arr): # generate the first pop and add to the pop list pop = [] # pop_list is a list whose element is some lists like [(a,b),(c,d)] pop_list = [] elite_rate = 0.2 iteration = 100 mutate_prob = 0.2 payoff_list, name_list = gt.gen_payoff_coll_filt(result_from_coll_filter) pop = zip(payoff_list, name_list) pop_list.append(pop) # define pop size pop_size = 50 while len(pop_list) < pop_size: payoff_list, name_list = gt.gen_payoff_cluster(result_from_cluster, movie_name_arr, name_list, movie_had_seen) pop = zip(payoff_list, name_list) pop_list.append(pop) # define pop top elite number top_elite_num = int(elite_rate * pop_size) for i in range(iteration): pop_element_costs = [(payoff_func(j), j) for j in pop_list] pop_element_costs.sort(cmp=None, key=None, reverse=False) pop_element_costs.reverse() ranked_pops = [v for (s, v) in pop_element_costs] pop_list = ranked_pops[0:top_elite_num] while len(pop_list) < pop_size: if random.random() < mutate_prob: one_in_pop_elite = random.randint(0, top_elite_num) new_from_mutate = mutate_for_genetic( ranked_pops[one_in_pop_elite], result_from_cluster, movie_name_arr, movie_had_seen) if new_from_mutate != None: pop_list.append(new_from_mutate) else: one_in_pop_elite = random.randint(0, top_elite_num) new_from_cross_over = cross_over_for_genetic( ranked_pops[one_in_pop_elite], result_from_cluster, movie_name_arr, movie_had_seen) if new_from_cross_over != None: pop_list.append(new_from_cross_over) name_list_from_genetic, payoff_list_from_genetic = split_payoff_and_name( pop_list[0]) return name_list_from_genetic, pop_element_costs[0][0]
def genetic_opt_plus(result_from_coll_filter, movie_had_seen, result_from_cluster, movie_name_arr): # generate the first pop and add to the pop list pop = [] elite_rate = 0.6 iteration = 1000 mutate_prob = 0.2 # create the first pop payoff_list, name_list = gt.gen_payoff_coll_filt(result_from_coll_filter) pop = zip(payoff_list, name_list) # define pop top elite number top_elite_num = int(elite_rate * len(pop)) for i in range(iteration): payoff_list, name_list = split_payoff_and_name(pop) current_payoff = sum(payoff_list) # sort the pop from best to worse about payoff pop.sort(cmp=None, key=None, reverse=False) pop.reverse() # mutation if random.random() < mutate_prob: one_in_pop_elite = random.randint(0, top_elite_num) new_from_mutate = mutate_for_genetic_plus(pop, one_in_pop_elite, result_from_cluster, movie_name_arr, movie_had_seen) new_from_mutate_payoff = sum([s for (s, n) in new_from_mutate]) if new_from_mutate != None and new_from_mutate_payoff > current_payoff: pop = new_from_mutate current_payoff = new_from_mutate_payoff # cross over else: one_in_pop_elite = random.randint(0, top_elite_num) new_from_cross_over = cross_over_for_genetic_plus( pop, one_in_pop_elite, result_from_cluster, movie_name_arr, movie_had_seen) new_from_cross_over_payoff = sum( [s for (s, n) in new_from_cross_over]) if new_from_cross_over != None and new_from_cross_over_payoff > current_payoff: pop = new_from_cross_over current_payoff = new_from_cross_over_payoff # fetch the result payoff_list_from_genetic_plus, name_list_from_genetic_plus = split_payoff_and_name( pop) return name_list_from_genetic_plus, payoff_list_from_genetic_plus
def genetic_opt(result_from_coll_filter, movie_had_seen, result_from_cluster, movie_name_arr): # generate the first pop and add to the pop list pop = [] # pop_list is a list whose element is some lists like [(a,b),(c,d)] pop_list = [] elite_rate = 0.2 iteration = 100 mutate_prob = 0.2 payoff_list, name_list = gt.gen_payoff_coll_filt(result_from_coll_filter) pop = zip(payoff_list, name_list) pop_list.append(pop) # define pop size pop_size = 50 while len(pop_list) < pop_size: payoff_list, name_list = gt.gen_payoff_cluster(result_from_cluster, movie_name_arr, name_list, movie_had_seen) pop = zip(payoff_list, name_list) pop_list.append(pop) # define pop top elite number top_elite_num = int(elite_rate * pop_size) for i in range( iteration ): pop_element_costs = [(payoff_func(j),j) for j in pop_list] pop_element_costs.sort(cmp=None, key=None, reverse=False) pop_element_costs.reverse() ranked_pops = [ v for (s,v) in pop_element_costs] pop_list = ranked_pops[0:top_elite_num] while len(pop_list) < pop_size: if random.random() < mutate_prob: one_in_pop_elite = random.randint(0, top_elite_num) new_from_mutate = mutate_for_genetic( ranked_pops[one_in_pop_elite], result_from_cluster, movie_name_arr, movie_had_seen) if new_from_mutate != None: pop_list.append(new_from_mutate) else: one_in_pop_elite = random.randint(0, top_elite_num) new_from_cross_over = cross_over_for_genetic( ranked_pops[one_in_pop_elite], result_from_cluster, movie_name_arr, movie_had_seen ) if new_from_cross_over != None: pop_list.append(new_from_cross_over) name_list_from_genetic, payoff_list_from_genetic = split_payoff_and_name( pop_list[0] ) return name_list_from_genetic, pop_element_costs[0][0]
def genetic_opt_plus(result_from_coll_filter, movie_had_seen, result_from_cluster, movie_name_arr): # generate the first pop and add to the pop list pop = [] elite_rate = 0.6 iteration = 800 mutate_prob = 0.2 # create the first pop payoff_list, name_list = gt.gen_payoff_coll_filt(result_from_coll_filter) pop = zip(payoff_list, name_list) # define pop top elite number top_elite_num = int(elite_rate * len(pop)) for i in range( iteration ): payoff_list, name_list = split_payoff_and_name(pop) current_payoff = sum(payoff_list) # sort the pop from best to worse about payoff pop.sort(cmp=None, key=None, reverse=False) pop.reverse() # mutation if random.random() < mutate_prob: one_in_pop_elite = random.randint(0, top_elite_num) new_from_mutate = mutate_for_genetic_plus( pop, one_in_pop_elite, result_from_cluster, movie_name_arr, movie_had_seen) new_from_mutate_payoff = sum([s for (s,n) in new_from_mutate]) if new_from_mutate != None and new_from_mutate_payoff > current_payoff: pop = new_from_mutate current_payoff = new_from_mutate_payoff # cross over else: one_in_pop_elite = random.randint(0, top_elite_num) new_from_cross_over = cross_over_for_genetic_plus( pop, one_in_pop_elite, result_from_cluster, movie_name_arr, movie_had_seen ) new_from_cross_over_payoff = sum([s for (s,n) in new_from_cross_over]) if new_from_cross_over != None and new_from_cross_over_payoff > current_payoff: pop = new_from_cross_over current_payoff = new_from_cross_over_payoff # fetch the result payoff_list_from_genetic_plus, name_list_from_genetic_plus = split_payoff_and_name( pop ) return name_list_from_genetic_plus, payoff_list_from_genetic_plus
def origin_coll_filter_analysis(user_id): result_from_coll_filter, movie_had_seen = coll.run_collaborative_filter(user_id) payoff_list, name_from_coll_result = gt.gen_payoff_coll_filt(result_from_coll_filter) print 'collaborative filter analysis finished.' return name_from_coll_result, payoff_list