def negative_align(architecture, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) goal_indices = get_goal_indices(status.goals_performed[-1]) index = choice(symbol_indices) row = architecture.language_matrix[index, :] row[choice(goal_indices)] *= architecture.neg_beta row[row < architecture.minimal] = architecture.minimal row /= row.sum()
def negative_align(architecture, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) goal_indices = get_goal_indices(status.goals_performed[-1]) test = architecture.language_matrix[symbol_indices, goal_indices] # for symbol in symbol_indices: min_ = test.argmin() row = architecture.language_matrix[min_,:] row[min_] *= architecture.neg_beta row[row < architecture.minimal] = architecture.minimal row /= row.sum()
def react_to_success(self, status, env): symbol_indices = get_symbol_indices(status.sequence) meaning_indices = get_goal_indices(status.goal) for position, symbol_index in enumerate(symbol_indices): row = self.language_matrix[symbol_index, :, position] mask = zeros(row.shape, dtype=bool) mask[meaning_indices] = True row[~mask] -= 1 row[mask] += 1 row[row < 0] = 0
def react_to_success(self, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) meaning_indices = get_goal_indices(status.goal) for position, symbol_index in enumerate(symbol_indices): row = self.language_matrix[symbol_index, :, position] mask = zeros(row.shape, dtype=bool) mask[meaning_indices] = True row[~mask] *= self.neg_alpha row[mask] *= self.pos_alpha row[row < self.threshold] = 0 self.language_matrix[symbol_index, :, position] = row / row.sum()
def react_to_failure(self, status, env): if self.align_strat: self.align_strat(self, status, env) else: symbol_indices = get_symbol_indices(status.sequence, env.symbols) goal_indices = get_goal_indices(status.goals_performed[-1]) for position, symbol in enumerate(symbol_indices): # column = self.interaction_memory.columns[position] for goal_index in goal_indices: column = self.language_matrix[symbol, goal_index, :] column[position] *= self.neg_beta column /= column.sum()
def react_to_success(self, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) goal_indices = get_goal_indices(status.goal) for position, symbol in enumerate(symbol_indices): for goal_index in goal_indices: column = self.language_matrix[symbol, goal_index, :] mask = ones(column.shape[0], dtype=bool) mask[position] = 0 column[mask] *= self.neg_alpha column[position] *= self.pos_alpha column[column < self.minimal] = self.minimal column[column > self.maximal] = self.maximal column /= column.sum()
def choose_goal_to_perform(self, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) is_first_search = False if not self.interaction_memory.cats_columns: self.interaction_memory.cats_columns = self.divide_matrix_in_categories( symbol_indices) if not self.interaction_memory.flat_cats: self.interaction_memory.flat_cats = [ cat.flatten().argsort()[::-1] for cat in self.interaction_memory.cats_columns ] is_first_search = True symbols = [] if is_first_search else self.interaction_memory.symbols columns = [] if is_first_search else self.interaction_memory.columns if is_first_search: for i, cat in enumerate(self.interaction_memory.cats_columns): index_to_search = self.interaction_memory.cats_searched[i] self.interaction_memory.cats_searched[i] += 1 row, column = unravel_index( self.interaction_memory.flat_cats[i][index_to_search], cat.shape) real_row = symbol_indices[row] symbols.append(real_row) real_column = categories_columns[i][column] columns.append(real_column) ## we need to make sure there are no repeated symbols # self.eliminate_repeated(symbols, columns, symbol_indices) else: values = [ self.interaction_matrix[symbols[i], columns[i]] for i in range(len(symbols)) ] sorted_values = list(argsort(values)) is_chosen = False while not is_chosen and sorted_values: minimum = sorted_values.pop(0) is_chosen = self.check_available_values(minimum) if is_chosen: next_row, next_column = self.find_next_greatest(minimum) if next_row is not None: real_row = symbol_indices[next_row] real_column = categories_columns[minimum][next_column] symbols[minimum] = real_row columns[minimum] = real_column else: minimum = argmin(values) symbols[minimum] = None columns[minimum] = None self.interaction_memory.symbols = symbols self.interaction_memory.columns = columns return self.get_goal_from_meanings(columns, env, status)
def negative_align(architecture, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) # symbol_indices = architecture.interaction_memory.symbols goal_indices = get_goal_indices(status.goals_performed[-1]) mask = ones(architecture.language_matrix.shape[1], dtype=bool) mask[symbol_indices] = 0 for goal in goal_indices: colum = architecture.language_matrix[:, goal] colum[~mask] *= architecture.neg_beta for symbol in symbol_indices: row = architecture.language_matrix[symbol, :] # row[~mask] *= architecture.neg_beta row[row < architecture.minimal] = architecture.minimal row /= row.sum()
def react_to_success(self, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) goal_indices = get_goal_indices(status.goal) mask = ones(self.language_matrix.shape[1], dtype=bool) mask[symbol_indices] = 0 for goal in goal_indices: colum = self.language_matrix[:, goal] colum[mask] *= self.neg_alpha colum[~mask] *= self.pos_alpha for symbol in symbol_indices: row = self.language_matrix[symbol, :] row[row < self.minimal] = self.minimal row[row > self.maximal] = self.maximal row /= row.sum()
def build_combinations(self, status): symbol_indices = get_symbol_indices(status.sequence) for position, symbol_index in enumerate(symbol_indices): meanings = self.language_matrix[symbol_index, :, position] if nonzero(meanings)[0].size > 0: sorted_meanings = meanings.argsort()[::-1] non = nonzero(meanings[sorted_meanings]) sorted_meanings = sorted_meanings[:len(non[0])] for meaning in sorted_meanings: self.store_possible_meaning(meaning) self.known_categories = [ (k, v) for k, v in self.interaction_memory.iteritems() if v ] if self.known_categories: return deque(product(*(t[1] for t in self.known_categories)))
def build_combinations(self, status): symbol_indices = get_symbol_indices(status.sequence) for position, symbol_index in enumerate(symbol_indices): position += 1 row = self.language_matrix[symbol_index, :] meaning_indices = nonzero(row)[0] possible_meanings = ( meaning_index for meaning_index in meaning_indices if self.language_matrix[symbol_index, meaning_index] == position) for pos_meaning in possible_meanings: self.store_possible_meaning(pos_meaning) self.known_categories = [ (k, v) for k, v in self.interaction_memory.iteritems() if v ] if self.known_categories: return list(product(*(t[1] for t in self.known_categories)))
def build_combinations(self, status): used = {} symbol_indices = get_symbol_indices(status.sequence) for position, symbol_index in enumerate(symbol_indices): used[position] = [] means = self.language_matrix[symbol_index, :, position] sorted_meanings = means.argsort()[::-1] for meaning in sorted_meanings: category = meanings_dict[meanings[meaning]] if meaning not in used[position]: self.interaction_memory[category].append( (meaning, means[meaning])) used[position].append(meaning) for k, v in self.interaction_memory.iteritems(): v.sort(key=lambda t: t[1], reverse=True) indices = list(set(value[0] for value in v)) self.interaction_memory[k] = indices[:self.memory_capacity] self.known_categories = [ (k, v) for k, v in self.interaction_memory.iteritems() if v ] if self.known_categories: return deque(product(*(t[1] for t in self.known_categories)))
def choose_goal_to_perform(self, status, env): symbol_indices = get_symbol_indices(status.sequence, env.symbols) is_first_search = False if not self.interaction_memory.positions: self.interaction_memory.positions = [ self.interaction_matrix[index, :, i] for i, index in enumerate(symbol_indices) ] if not self.interaction_memory.ordered_positions: self.interaction_memory.ordered_positions = [ row.argsort()[::-1] for row in self.interaction_memory.positions ] is_first_search = True columns = [] if is_first_search else self.interaction_memory.columns if is_first_search: for i, pos in enumerate(self.interaction_memory.ordered_positions): index_to_search = self.interaction_memory.cats_searched[i] self.interaction_memory.cats_searched[i] += 1 columns.append(pos[index_to_search]) self.interaction_memory.cats = [ columns_categories[column] for column in columns ] repeated = self.find_repeated(self.interaction_memory.cats, columns) if repeated: for cat, indexes in repeated.iteritems(): sorted_cats = sorted( indexes, key=lambda index: self.interaction_memory.positions[ index[0]][self.interaction_memory.cats_searched[ cat] - 1], reverse=True) self.interaction_memory.conflicts[cat] = sorted_cats[1:] for item in self.interaction_memory.conflicts[cat]: columns[item[0]] = None else: if self.interaction_memory.conflicts: k = self.interaction_memory.conflicts.keys()[0] conflicted = [ item for item in self.interaction_memory.columns if item is not None and columns_categories[item] == k ] if len(conflicted) > 1: raise Exception( "There should not be two values of the same category after purging" ) conf_index = columns.index(conflicted[0]) columns[conf_index] = None index, column = self.interaction_memory.conflicts[k].pop() columns[index] = column if not self.interaction_memory.conflicts[k]: del self.interaction_memory.conflicts[k] # else: self.interaction_memory.columns = columns goal = self.try_to_guess(columns, env, status) if goal: return goal else: return self.choose_feasible_goal(env, status)