コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
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]
コード例 #4
0
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]