def analyze_all_hands(em, num_generations): all_hand_dict = {} for generation in range(num_generations): all_hand_dict[generation] = [] em.gm.generation = generation grasp_list = em.gm.get_all_grasps() hand_list = em.interface.load_hands_for_grasps(grasp_list) grasp_dict = grasp_sorting_utils.get_grasps_by_hand(grasp_list) for hand in hand_list: hand.energy_list = [] hand_dict = dict() for h in hand_list: hand_dict[h.hand_id] = h total_energy = 1e10 try: total_energy = em.eval_functor(grasp_dict[h.hand_id], h) except Exception as e: print "analyze_all_hands::Exception %s"%(e); pdb.set_trace() h.energy_list.append(total_energy) all_hand_dict[generation].append(h) return all_hand_dict
def GA_get_elite_hands(grasp_list, hand_list, eval_functor): new_hand_list = list() #Organize the grasp list grasp_dict = grasp_sorting_utils.get_grasps_by_hand(grasp_list) #organize the hand list into a dictionary sorted by hand_id keys. hand_dict = dict() for h in hand_list: hand_dict[h.hand_id] = h energy_list = list() #Evaluate the score of each hand using the grasp_dict. Lower scores are better. key_list = [] for k in hand_dict: try: energy_list.append(eval_functor(grasp_dict[k],hand_dict[k])) key_list.append(k) except: #If the evaluation fails, assign an arbitrarily high score. print k energy_list.append(1e10) #Now we get the best hands by lowest energy import pdb pdb.set_trace() sorted_key_indices = [hand_dict.keys()[i] for i in argsort(energy_list)] elite_list_len = 4 elite_hand_list = [hand_dict[k] for k in sorted_key_indices[:elite_list_len]] return elite_hand_list, energy_list
def GA_generation(grasp_list, hand_list, eval_functor, rel_stdev): """ @brief Run the GA on an entire generation @param grasp_list - The list of grasps for all hands in this generation to be considered @param hand_list - The list of hands to mutate and cross @param eval_functor - The function to use to generate a score for each hand @param rel_stdev - The scaling factor for mutation to use. @returns A list of new hands derived from the old hands. """ new_hand_list = list() #Organize the grasp list grasp_dict = grasp_sorting_utils.get_grasps_by_hand(grasp_list) #organize the hand list into a dictionary sorted by hand_id keys. hand_dict = dict() for h in hand_list: hand_dict[h.hand_id] = h energy_list = list() #Evaluate the score of each hand using the grasp_dict. Lower scores are better. for k in hand_dict: try: hand_dict[k].energy_list = list() energy_list.append(eval_functor(grasp_dict[k],hand_dict[k])) except: #If the evaluation fails, assign an arbitrarily high score. print k energy_list.append(1e10) #Now we get the best hands by lowest energy sorted_key_indices = [hand_dict.keys()[i] for i in argsort(energy_list)] elite_list_len = 4 elite_hand_list = [hand_dict[k] for k in sorted_key_indices[:elite_list_len]] #Get a list of all pairs of the elite hands #and then pair them all to generate a set of new hands hand_pair_list = itertools.combinations(elite_hand_list, 2) for pair in hand_pair_list: new_hand = GA_pair(pair[0], pair[1]) if new_hand: new_hand_list.append(new_hand) #Now get 6 mutated hands - The parent hands are selected randomly from the elite hand list. mutation_number = 6 rand_ind_list = [rnd.randrange(0,elite_list_len) for i in range(mutation_number)] for ind in rand_ind_list: new_hand = [] #Not all generated hands will be legal, try ten times to generate a valid hand. #This is kind of arbitrary for i in range(10): new_hand = GA_mutate(elite_hand_list[ind], rel_stdev) if new_hand: break new_hand_list.append(new_hand) #Now, the elite hand list is passed on to the next generation unmodified. #update the hand generation to reflect this for hand in elite_hand_list: hand.generation.append(hand.generation[-1] + 1) new_hand_list.append(hand) return new_hand_list
def GA_generation(grasp_list, hand_list, eval_functor, rel_stdev): """ @brief Run the GA on an entire generation @param grasp_list - The list of grasps for all hands in this generation to be considered @param hand_list - The list of hands to mutate and cross @param eval_functor - The function to use to generate a score for each hand @param rel_stdev - The scaling factor for mutation to use. @returns A list of new hands derived from the old hands. """ new_hand_list = list() #Organize the grasp list grasp_dict = grasp_sorting_utils.get_grasps_by_hand(grasp_list) #organize the hand list into a dictionary sorted by hand_id keys. hand_dict = dict() for h in hand_list: hand_dict[h.hand_id] = h energy_list = list() #Evaluate the score of each hand using the grasp_dict. Lower scores are better. for k in hand_dict: try: energy_list.append(eval_functor(grasp_dict[k],hand_dict[k])) except: #If the evaluation fails, assign an arbitrarily high score. print k energy_list.append(1e10) #Now we get the best hands by lowest energy sorted_key_indices = [hand_dict.keys()[i] for i in argsort(energy_list)] elite_list_len = 4 elite_hand_list = [hand_dict[k] for k in sorted_key_indices[:elite_list_len]] #Get a list of all pairs of the elite hands #and then pair them all to generate a set of new hands hand_pair_list = itertools.combinations(elite_hand_list, 2) for pair in hand_pair_list: new_hand = GA_pair(pair[0], pair[1]) if new_hand: new_hand_list.append(new_hand) #Now get 6 mutated hands - The parent hands are selected randomly from the elite hand list. mutation_number = 6 rand_ind_list = [rnd.randrange(0,elite_list_len) for i in range(mutation_number)] for ind in rand_ind_list: new_hand = [] #Not all generated hands will be legal, try ten times to generate a valid hand. #This is kind of arbitrary for i in range(10): new_hand = GA_mutate(elite_hand_list[ind], rel_stdev) if new_hand: break new_hand_list.append(new_hand) #Now, the elite hand list is passed on to the next generation unmodified. #update the hand generation to reflect this for hand in elite_hand_list: hand.generation.append(hand.generation[-1] + 1) new_hand_list.append(hand) return new_hand_list