def test_evaluate_empty_board(self): '''Evaluate empty board. set fields of board: [ ][ ][ ] [ ][ ][ ] [ ][ ][ ] ''' self.assertEqual( evaluate(self.board, player=Field.CIRCLE, opponent=Field.CROSS), 0) self.assertEqual( evaluate(self.board, player=Field.CROSS, opponent=Field.CIRCLE), 0)
def test_evaluate_not_win_and_not_draw(self): '''Evaluate not win game and not draw game - continue game. set fields of board: [X][ ][ ] [ ][O][ ] [ ][ ][ ] ''' self.board[0] = Field.CROSS self.board[4] = Field.CIRCLE self.assertEqual( evaluate(self.board, player=Field.CIRCLE, opponent=Field.CROSS), 0) self.assertEqual( evaluate(self.board, player=Field.CROSS, opponent=Field.CIRCLE), 0)
def test_evaluate_win_in_second_row(self): '''Win player in second row. set fields of board: [O][O][X] [X][X][X] [ ][O][ ] ''' self.board[0] = Field.CIRCLE self.board[1] = Field.CIRCLE self.board[2] = Field.CROSS self.board[3] = Field.CROSS self.board[4] = Field.CROSS self.board[5] = Field.CROSS self.board[7] = Field.CIRCLE self.assertEqual( evaluate(self.board, player=Field.CROSS, opponent=Field.CIRCLE), 10) self.assertEqual( evaluate(self.board, player=Field.CIRCLE, opponent=Field.CROSS), -10)
def test_evaluate_win_in_third_column(self): '''Win player in third column. set fields of board: [X][O][X] [O][O][X] [ ][ ][X] ''' self.board[0] = Field.CIRCLE self.board[1] = Field.CROSS self.board[2] = Field.CROSS self.board[3] = Field.CIRCLE self.board[4] = Field.CIRCLE self.board[5] = Field.CROSS self.board[8] = Field.CROSS self.assertEqual( evaluate(self.board, player=Field.CROSS, opponent=Field.CIRCLE), 10) self.assertEqual( evaluate(self.board, player=Field.CIRCLE, opponent=Field.CROSS), -10)
def test_evaluate_draw_game(self): '''Evauate draw game. set fields of board: [X][O][X] [O][X][X] [O][X][O] ''' self.board[0] = Field.CROSS self.board[1] = Field.CIRCLE self.board[2] = Field.CROSS self.board[3] = Field.CIRCLE self.board[4] = Field.CROSS self.board[5] = Field.CROSS self.board[6] = Field.CIRCLE self.board[7] = Field.CROSS self.board[8] = Field.CIRCLE self.assertEqual( evaluate(self.board, player=Field.CIRCLE, opponent=Field.CROSS), 0) self.assertEqual( evaluate(self.board, player=Field.CROSS, opponent=Field.CIRCLE), 0)
def ai_route(moves): if moves == 'o': brd = board.Board('5') elif moves == 'x': brd = board.Board('') else: player = board.Board(moves) if player.who_won() != None: brd = player else: res = ai.evaluate(player) brd = player.make_move(res[2]) return render(brd)
def ai_route(moves): if moves == 'o': brd = board.Board('5') elif moves == 'x': brd = board.Board('') else: player = board.Board(moves) if player.who_won()!=None: brd = player else: res = ai.evaluate(player) brd = player.make_move(res[2]) return render(brd)
def ai_eval(chromo): return ai.evaluate(chromo, NETWORK_ARCH)
def main(): global NETWORK_ARCH global NUM_VALS parser = ArgumentParser('Genetic') parser.add_argument('--pop', type=int, default=50, help='Population size') parser.add_argument( '--keep', type=int, default=10, help='Number of chromosomes to keep between generations') parser.add_argument('--probm', type=float, default=0.5, help='Probability to mutate a chromosome') parser.add_argument('--probg', type=float, default=0.1, help='Probability to mutate a gene') parser.add_argument('--kpoint', type=int, default=2, help='k-point') parser.add_argument('--no-print', action='store_true', help='Prevent printing durring evaluation') parser.add_argument('--time', action='store_true', help='Time each function') parser.add_argument('--epoch', type=int, default=10, help='Generations per epoch') parser.add_argument('--max-gen', type=int, default=200, help='Maximum number of generations') parser.add_argument('--view', action='store_true', help='View the best chromosome of the population') parser.add_argument("--arch", default=[], type=int, nargs='*', help="Neural network architecture") args = parser.parse_args() NETWORK_ARCH = list(args.arch) NUM_VALS = 0 for i in range(1, len(NETWORK_ARCH)): NUM_VALS += ((NETWORK_ARCH[i - 1] + 1) * NETWORK_ARCH[i]) if NETWORK_ARCH: NUM_VALS += (13 * NETWORK_ARCH[0]) NUM_VALS += ((NETWORK_ARCH[-1] + 1) * 4) else: NUM_VALS = 52 n_pop = args.pop n_keep = args.keep prob_m = args.probm gene_m = args.probg k = args.kpoint times = [0, [], [], [], [], [], []] times[0], pop = time_exec(args.time, initialize, n_pop) gen = 0 fitnesses = [] for gen in range(args.max_gen): tmp, fitness = time_exec(args.time, evaluate, pop) times[1].append(tmp) fitnesses.append(max(fitness)) if gen % args.epoch == 0: if not args.no_print: if args.view: print("{} GEN: {:5} FIT: {}".format( "\n" * 30, gen, max(fitness))) pop, fitness = sort_by_fitness(pop, fitness) ai.evaluate(pop[0], NETWORK_ARCH, display=True, sleep=0.05, avg=1) else: print("GEN: {:5} FIT: {}".format(gen, max(fitness))) pop_copy = [np.copy(chrom) for chrom in pop] tmp, new_pop = time_exec(args.time, selection, pop_copy, fitness, n_keep) times[2].append(tmp) tmp, new_pop = time_exec(args.time, crossover, new_pop, k) times[3].append(tmp) tmp, new_pop = time_exec(args.time, mutate, new_pop, prob_m, gene_m) times[4].append(tmp) tmp, pop = time_exec(args.time, replace, pop, fitness, new_pop, n_keep) times[5].append(tmp) print("FITNESS: {}".format(max(fitness))) if (args.time): print("INIT: {:f}s".format(times[0])) print("EVAL: {:f}s".format(sum(times[1]) / gen)) print("SELE: {:f}s".format(sum(times[2]) / gen)) print("CROS: {:f}s".format(sum(times[3]) / gen)) print("MUTA: {:f}s".format(sum(times[4]) / gen)) print("REPL: {:f}s".format(sum(times[5]) / gen)) plot.plot_fitness( "{}-{}-{}-{}-{}-{}.png".format( '.'.join(['12'] + [str(x) for x in NETWORK_ARCH] + ['4']), n_pop, args.max_gen, k, prob_m, gene_m), fitnesses)
return 1 elif move == 'd': return 2 elif move == 'a': return 3 elif move == 'q': return -1 elif move == 'r': print(snake.snake_ray(position, N)) if __name__ == "__main__": # print("\033[2J\033[H", end='', flush=True) parser = ArgumentParser() parser.add_argument("N", nargs='?', type=int, default=20, help="Size of snake grid") # parser.add_argument( # "--K", nargs='?', type=int, default=5, help="Size of snake grid") args = parser.parse_args() print(ai.evaluate(np.random.rand(100).tolist(), args.N, False)) # snake.snake(args.N, depthai.depth_ai(args.K), True) # snake.snake(args.N, user_input, True) # snake.snake(args.N, user.input_time, True) # snake.snake(args.N, dumbai.dumb_ai, True) # snake.snake(args.N, perfectai.perfect_ai, True)