def alphabeta(depth, tt, net_score=0, alpha=-maxint, beta=maxint, max_node=True): if depth == 0: # TODO do we need static evaluator? how should we scale its value return net_score, [] best_move = [] undo_stack = [] if max_node: v = -maxint - 1 # negative infinity for move in gen_moves.gen_moves(tt, debug=True): tt.cur_player = 1 #print("max move %s: " % move) meeple_count = make_move(move, tt.board, undo_stack) last_step = move[-1] score = net_score + scoring.calc_score(last_step, meeple_count, undo_stack, tt) #print("scored: %s" % score) value = alphabeta(depth - 1, tt, score, alpha, beta, False)[0] #print("after alphabeta: %s" % value) unmake(undo_stack) if value > v: v = value best_move = move #print("new max: %d, %s" % (v, best_move)) alpha = max(alpha, v) if beta <= alpha: break return v, best_move else: v = maxint # positive infinity for move in gen_moves.gen_moves(tt): tt.cur_player = 2 #print("min move %s: " % move) meeple_count = make_move(move, tt.board, undo_stack) last_step = move[-1] score = net_score - scoring.calc_score(last_step, meeple_count, undo_stack, tt) #print("min scored: %s" % score) value = alphabeta(depth - 1, tt, score, alpha, beta, True)[0] #print("after alphabeta: %s" % value) unmake(undo_stack) if value < v: v = value best_move = move #print("new min: %d, %s" % (v, best_move)) beta = min(beta, v) if alpha >= beta: break return v, best_move
def greedy_solution(images, callback=None, callback_score_increment=1000, thresh=0): slides = [Slide((im, )) for im in images] solution = [slides.pop(0)] aaa = 0 last_callback_score = aaa while len(slides) > 0: max_score = 0 best = None side = None ind_best = 0 pos_right = len(solution[-1].tags) / 2 pos_left = len(solution[0].tags) / 2 for idx, s in enumerate(slides): sr = calc_score([solution[-1], s]) if sr > max_score: best = s ind_best = idx side = 'r' max_score = sr if max_score > min(thresh, pos_right): break sl = calc_score([s, solution[0]]) if sl > max_score: best = s side = 'l' ind_best = idx max_score = sl if max_score > min(thresh, pos_left): break if best is None: solution += [slides.pop(0)] else: if side == 'l': solution = ([best] + solution) else: solution = (solution + [best]) slides.pop(ind_best) aaa += max_score print(len(slides), ' ', aaa) if callback is not None and aaa > last_callback_score + callback_score_increment: last_callback_score = aaa callback(solution) return solution
def post(self, game_id): # TODO: is the submitted city part of the game? # TODO: did the player already submit a guess for this city? request_data = request.get_json() player_id = request_data['player_id'] player_key = ndb.Key(urlsafe=player_id) city_id = request_data['city_id'] game_key = ndb.Key(urlsafe=game_id) game = game_key.get() city_key = ndb.Key(urlsafe=city_id) city = city_key.get() if game.state == GameState.waitingForPlayers: game.state = GameState.running game.put() guess = Guess(parent=game_key) guess.game = game_key guess.player = player_key guess.city = city_key guess.long = request_data['long'] guess.lat = request_data['lat'] remaining_time = request_data['remaining_time'] guess.score = calc_score("easy", city.lat, city.long, remaining_time, guess.lat, guess.long) guess.put() for player in game.players: # token = Util.get_token(player.urlsafe()) # print("sending message to" + token) msg = Messages() msg.player = player msg.game = game_key msg.msg = str(Util.to_json(guess, False, False)) msg.put() # channel.send_message(token, str(Util.to_json(guess, False, False))) # TODO this is not stable yet - we have to make sure only one guess per city/player pair can be submitted guess_query = Guess.query(Guess.player == player_key, ancestor=game_key) if guess_query.count() == len(game.cities): game.state = GameState.done game.put() save_score(game_key, player_id, guess_query.fetch()) return Util.to_json(guess, False), 201
def get_most_similar_restricted(limit=120): df, df_i = import_data() counts = df_i['ingredient'].value_counts() ings = counts.index.values found_ings, embeddings = retrieve_embeddings(model, ings) #found_ings, embeddings = np.load('word2vec_embeddings.npy') ranks = get_nearest_neighbors(embeddings) print_nearest_neighbors(ings[:limit], found_ings, ranks) highest_ranks, avg_rankings, random_avg_rankings = calc_score(ranks, limit, print_scores=False, score_path='../model/scores.csv') indices = found_ings[found_ings<highest_ranks.shape[0]] highest_ranks = highest_ranks[indices] avg_rankings = avg_rankings[indices] random_avg_rankings = random_avg_rankings[indices] print (highest_ranks<=3).sum(dtype=float) / np.isfinite(highest_ranks).sum() print highest_ranks[np.isfinite(highest_ranks)].mean() print avg_rankings[np.isfinite(avg_rankings)].mean() print random_avg_rankings[np.isfinite(random_avg_rankings)].mean()
def minimax(depth, tt): if depth == 0: # TODO do we need static evaluator? how should we scale its value return 0, [] best_move = [] max_score = -maxint - 1 # negative infinity for move in gen_moves.gen_moves(tt): undo_stack = [] meeple_count = make_move(move, tt.board, undo_stack) last_step = move[-1] score = scoring.calc_score(last_step, meeple_count, undo_stack, tt)\ - minimax(depth - 1, tt)[0] unmake(undo_stack) if (score > max_score): print("better score: %s" % score) max_score = score best_move = move return max_score, best_move
def main(): num_ingredients = 1000 ings_per_prod = None use_embeddings = False output_cat = 'shelf' df, df_i = import_data() counts = df_i['ingredient'].value_counts() inputs_, outputs, idx_to_cat = gen_input_outputs_cat( df, counts, num_ingredients, output_cat, ings_per_prod) if use_embeddings: #embeddings = np.load('embeddings/embeddings_{}.npy'.format(num_ingredients)) #embeddings = np.load('../word2vec/word2vec_embeddings.npy')[1][:num_ingredients] embeddings = 2*np.random.random((num_ingredients, 300))-1 # Try random embeddings embeddings = embeddings.astype('float32') inputs = input_from_embeddings(inputs_, embeddings, normalize=False) #inputs = (2*np.random.random(inputs_.shape)-1).astype('float32') # Completely random inputs. else: inputs = inputs_ num_outputs = outputs.max()+1 print "# of data points:", len(inputs) # Scramble inputs/outputs np.random.seed(3) random_idx = np.random.permutation(len(inputs)) inputs = inputs[random_idx] outputs = outputs[random_idx] test_split_idx = int(len(inputs))*0.8 inputs, X_test = inputs[:test_split_idx], inputs[test_split_idx:] outputs, y_test = outputs[:test_split_idx], outputs[test_split_idx:] X_train, X_valid, y_train, y_valid = train_test_split( inputs, outputs, test_size=0.2, random_state=42) print "Running models..." # Max entropy model # Normalize #inputs_n = inputs / np.sum(inputs, axis=1)[:,None] #regr = max_entropy(X_train, y_train, X_valid, y_valid) #predict_cat(counts, regr, idx_to_cat, num_ingredients, ings) # Neural network model classifier, predict_model = run_nn(X_train, y_train, X_valid, y_valid, X_train.shape[1], num_outputs, m=100, n_epochs=10, batch_size=10, learning_rate=0.1, L2_reg=0.0001) pred_valid = predict_model(X_valid) pred_test = predict_model(X_test) if output_cat != 'aisle': lower_to_upper_cat = get_upper_cat(df, output_cat, 'aisle') print "Validation set:\n", calc_accuracy(pred_valid, y_valid) print calc_accuracy(pred_valid, y_valid, lower_to_upper_cat) print "Test set:\n", calc_accuracy(pred_test, y_test) print calc_accuracy(pred_test, y_test, lower_to_upper_cat) #print_predictions(X_valid, y_valid, # np.argmax(pred_valid, axis=1), idx_to_cat, counts, limit=100) if not use_embeddings: embeddings_out = classifier.hiddenLayer.W.get_value() ranks, neigh = get_nearest_neighbors(embeddings_out) #print_nearest_neighbors(counts.index.values[:num_ingredients], ranks) highest_rank, score, avg_rank_of_ing_cat, random_score = calc_score( ranks, num_ingredients)
def main(): num_ingredients = 1000 use_embeddings = False ings_per_prod = 5 frac_weighted = 0.95 invalid_multiplier = 1 df, df_i = import_data() counts = df_i['ingredient'].value_counts() inputs_v_, outputs_v = gen_input_outputs_valid( df, df_i, num_ingredients, ings_per_prod) inputs_i_w_, outputs_i_w = gen_input_outputs_invalid(inputs_v_, invalid_multiplier*frac_weighted, num_ingredients, ings_per_prod, weighted=True) inputs_i_, outputs_i = gen_input_outputs_invalid(inputs_v_, invalid_multiplier*(1-frac_weighted), num_ingredients, ings_per_prod, weighted=False) if use_embeddings: embeddings = np.load('embeddings/embeddings_{}.npy'.format(num_ingredients)) #embeddings = np.load('../word2vec/word2vec_embeddings.npy')[1][:num_ingredients] #embeddings = 2*np.random.random((num_ingredients, 20))-1 # Try random embeddings embeddings = embeddings.astype('float32') normalize = False inputs_v = input_from_embeddings(inputs_v_, embeddings, normalize=normalize) inputs_i_w = input_from_embeddings(inputs_i_w_, embeddings, normalize=normalize) inputs_i = input_from_embeddings(inputs_i_, embeddings, normalize=normalize) else: inputs_v = inputs_v_ inputs_i_w = inputs_i_w_ inputs_i = inputs_i_ X_train_v, X_test_v, y_train_v, y_test_v = train_test_split( inputs_v, outputs_v, test_size=1/3., random_state=42) X_train_i_w, X_test_i_w, y_train_i_w, y_test_i_w = train_test_split( inputs_i_w, outputs_i_w, test_size=1/3., random_state=42) X_train_i, X_test_i, y_train_i, y_test_i = train_test_split( inputs_i, outputs_i, test_size=1/3., random_state=42) X_train = np.vstack((X_train_v, X_train_i_w, X_train_i)) y_train = np.hstack((y_train_v, y_train_i_w, y_train_i)) X_test = np.vstack((X_test_v, X_test_i_w, X_test_i)) y_test = np.hstack((y_test_v, y_test_i_w, y_test_i)) # Scramble inputs/outputs np.random.seed(3) random_idx_tr = np.random.permutation(len(X_train)) random_idx_te = np.random.permutation(len(X_test)) X_train = X_train[random_idx_tr] y_train = y_train[random_idx_tr] X_test = X_test[random_idx_te] y_test = y_test[random_idx_te] print "Running models..." # Max entropy model #regr = max_entropy(X_train, y_train, X_test, y_test) #predict_cat(counts, regr, idx_to_cat, num_ingredients, ings) # Neural network model classifier, predict_model = run_nn(X_train, y_train, X_test, y_test, X_train.shape[1], num_outputs=2, m=20, n_epochs=10, batch_size=10, learning_rate=0.05, L2_reg=0.0001) #pred = predict_model(X_test) #pred_cats = np.argmax(pred, axis=1) #print calc_accuracy(pred, y_test) #print_predictions(X_test, y_test, pred_cats, counts, limit=100) print "Max Entropy (Valid, Invalid, Invalid weighted):" print calc_accuracy(regr.predict_proba(X_test_v), y_test_v) print calc_accuracy(regr.predict_proba(X_test_i), y_test_i) print calc_accuracy(regr.predict_proba(X_test_i_w), y_test_i_w) print "Neural Network (Valid, Invalid, Invalid weighted):" print calc_accuracy(predict_model(X_test_v), y_test_v) print calc_accuracy(predict_model(X_test_i), y_test_i) print calc_accuracy(predict_model(X_test_i_w), y_test_i_w) if not use_embeddings: embeddings_out = classifier.hiddenLayer.W.get_value() ranks, neigh = get_nearest_neighbors(embeddings_out) #print_nearest_neighbors(counts.index.values[:num_ingredients], ranks) highest_rank, score, avg_rank_of_ing_cat, random_score = calc_score( ranks, num_ingredients)
from scoring import calc_score from simple_solutions import * from hash_io import Input_parser if __name__ == '__main__': input_paths = ['input/a_example.txt', 'input/b_lovely_landscapes.txt', 'input/c_memorable_moments.txt', 'input/d_pet_pictures.txt', 'input/e_shiny_selfies.txt'] total_score = 0 for input_path in input_paths: images = Input_parser(input_path).images slides = dumb_solution(images) total_score = total_score + calc_score(slides) print(total_score)