Skip to content

Commit

Permalink
Turned board into class
Browse files Browse the repository at this point in the history
  • Loading branch information
Panda7777 committed Jul 4, 2017
1 parent e8d207b commit 2046e2b
Show file tree
Hide file tree
Showing 15 changed files with 757 additions and 18 deletions.
20 changes: 12 additions & 8 deletions Angel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from NeuralNetwork import NeuralNetwork
import numpy as np

from NeuralNetwork import NeuralNetwork


class Angel:
def __init__(self, sides):
Expand All @@ -11,9 +12,9 @@ def __init__(self, sides):
input_nodes = int(sides ** 2)
hidden_nodes = 140
output_nodes = 4
learning_rate = 0.1
learning_rate = 0.8
weight_wih = np.random.randn(hidden_nodes,
int(input_nodes)) \
int(input_nodes)) \
/ np.sqrt(input_nodes)
weight_who = np.random.randn(output_nodes, hidden_nodes) / np.sqrt(hidden_nodes)
self.consciousness = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
Expand All @@ -31,6 +32,7 @@ def reset(self):
self.moves = []
self.consciousness.reset()

# only for player mode
def god_move(self, move):
if move == -self.sides:
self.moves.append(0)
Expand All @@ -42,19 +44,21 @@ def god_move(self, move):
self.moves.append(3)
self.position += move

# only for AI mode
def angel_move(self, board):
turn = np.argmax(self.consciousness.query(board))
if np.argmax(turn) == 0:
# print(str(self.position)+" "+str(turn))
if turn == 0:
self.position += -self.sides
if np.argmax(turn) == 1:
if turn == 1:
self.position += 1
if np.argmax(turn) == 2:
if turn == 2:
self.position += self.sides
if np.argmax(turn) == 3:
if turn == 3:
self.position += -1
self.moves.append(turn)


# train angel
def train(self, has_won):
if has_won is "devil":
self.consciousness.train(False)
Expand Down
49 changes: 47 additions & 2 deletions Board.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pygame
from Devil import Devil
from pygame.locals import *

from Angel import Angel
from Devil import Devil


class Board:
Expand Down Expand Up @@ -39,7 +40,7 @@ def representation(self):
elif i in self.devil.get_blocks():
board_rep.append(0.99)
else:
board_rep.append(0.01)
board_rep.append(0.0)
return board_rep

def check_winner(self, current_player):
Expand Down Expand Up @@ -107,7 +108,51 @@ def get_winner(self):
def get_angel(self):
return self.angel

def get_devil(self):
return self.devil

def rect_equ(self, position):
return (
(position % self.sides) * self.margin, int(position / self.sides) * self.margin, self.margin,
self.margin)

def init_draw(self):
pygame.init()
self.window = pygame.display.set_mode((self.side_length, self.side_length))
self.canvas = self.window.copy()


self.black = (0, 0, 0, 255)
self.white = (255, 255, 255)
self.gray = (220, 220, 220)
self.silver = (192, 192, 192)
self.red = (255, 0, 0)

def display_board(self):
self.window.fill(self.white)

# grid
for side in range(0, self.side_length, self.margin):
pygame.draw.lines(self.window, self.gray, False, ((side, 0), (side, self.side_length)))
pygame.draw.lines(self.window, self.gray, False, ((0, side), (self.side_length, side)))

# angel
if self.winner is None:
pygame.draw.rect(self.window, self.silver, self.rect_equ(self.angel.get_position()))

# devil barriers
for devil in self.devil.get_blocks():
pygame.draw.rect(self.window, self.red, (self.rect_equ(devil)))

pygame.display.update()

def button_pressed(self):
for e in pygame.event.get():
if e.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
x_box = int(pos[0] / self.margin)
y_box = int(pos[1] / self.margin)
box_index = x_box + self.sides * y_box
self.devil.god_place(box_index)
return True
return False
2 changes: 2 additions & 0 deletions Devil.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class Devil:
def __init__(self, sides):
# self.blocks = [39, 43, 31,32,80,76,75,74,45]
self.blocks = []
self.sides = sides

Expand All @@ -29,3 +30,4 @@ def place_block(self, angel_pos):
random_place = angel_pos - 1
random_dir = random.randrange(0, 4)
self.blocks.append(random_place)
return
21 changes: 16 additions & 5 deletions Main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from Board import Board
from matplotlib import pyplot as plt
from scipy import stats

from Board import Board

# board = Board(60, 9, True)
# while True:
# board.players_play()
Expand All @@ -13,21 +14,31 @@
ratios = []
winners = {"angel": 0, "devil": 0}
winner = None
for i in range(80000):
for i in range(800000):
while winner is None:
board.angels_turn()
board.devils_turn()
board.angels_turn()
winner = board.get_winner()
winners[winner] += 1
if not i == 0 and i % 100 == 0:
if not i == 0 and i % 100 == 0 and not winners["angel"] == 0:
print(i)
print(board.get_angel().moves)
print(board.get_devil().get_blocks())
print(winners)
ratios.append(winners["angel"] / winners["devil"])
ratios.append(winners["devil"] / winners["angel"])
winners = {"angel": 0, "devil": 0}
board.train_angel()
board.reset()
winner = None
plt.scatter([i for i in range(len(ratios))],ratios)
slope, intercept, r_value, p_value, std_err = stats.linregress([i for i in range(len(ratios))],ratios)
print(slope)
plt.show()

board.init_draw()
while True:
board.display_board()
pressed = board.button_pressed()
if pressed is True:
board.angels_turn()

8 changes: 5 additions & 3 deletions NeuralNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ def reset(self):
self.hidden_outputs = []
self.final_outputs = []

# train based on previous decisions
def train(self, has_won):
who_update = np.zeros_like(self.who)
wih_update = np.zeros_like(self.wih)
for k in range(len(self.inputs)):
move = np.argmax(self.final_outputs[k])

target_array = np.array(self.final_outputs[k])
target_array = np.array([0.5, 0.5, 0.5, 0.5]).reshape(4,1)
target_array[move] = 0.99 if has_won else 0.01

output_errors = target_array - np.array(self.final_outputs[k])
Expand All @@ -40,16 +41,17 @@ def train(self, has_won):
# error2 = numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)),
# numpy.transpose(input_array))
who_update += (output_errors * self.final_outputs[k] *
(1.0 - self.final_outputs[k])) * np.transpose(self.hidden_outputs[k])
(1.0 - self.final_outputs[k])) * np.transpose(self.hidden_outputs[k])
wih_update += np.dot((hidden_errors * self.hidden_outputs[k] * (1.0 - self.hidden_outputs[k])),
np.transpose(self.inputs[k]))
np.transpose(self.inputs[k]))
# print("================================================")
# print(who_update)
# print(wih_update)
self.who += self.lr * who_update
self.wih += self.lr * wih_update
pass

# get AI decision
def query(self, input_list):
input_list = np.array(input_list, ndmin=2).T
self.inputs.append(input_list)
Expand Down
96 changes: 96 additions & 0 deletions attempt_4/Angel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import numpy as np

from attempt_4.NeuralNetwork import NeuralNetwork


class Angel:
def __init__(self, sides):
self.sides = sides
self.position = int(sides / 2) * sides + int(sides / 2)
self.moves = []

input_nodes = int(sides ** 2)
hidden_nodes = 140
output_nodes = 1
learning_rate = 0.1
weight_wih = np.random.randn(hidden_nodes, int(input_nodes)) / np.sqrt(input_nodes)
weight_who = np.random.randn(output_nodes, hidden_nodes) / np.sqrt(hidden_nodes)
self.left = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
learning_rate)

weight_wih = np.random.randn(hidden_nodes, int(input_nodes)) / np.sqrt(input_nodes)
weight_who = np.random.randn(output_nodes, hidden_nodes) / np.sqrt(hidden_nodes)
self.right = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
learning_rate)

weight_wih = np.random.randn(hidden_nodes, int(input_nodes)) / np.sqrt(input_nodes)
weight_who = np.random.randn(output_nodes, hidden_nodes) / np.sqrt(hidden_nodes)
self.up = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
learning_rate)

weight_wih = np.random.randn(hidden_nodes, int(input_nodes)) / np.sqrt(input_nodes)
weight_who = np.random.randn(output_nodes, hidden_nodes) / np.sqrt(hidden_nodes)
self.down = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who,
learning_rate)

def get_last_move(self):
if not len(self.moves) == 0:
return self.moves[len(self.moves) - 1]

def get_position(self):
return self.position

def reset(self):
self.position = int(self.sides / 2) * self.sides + int(self.sides / 2)
self.moves = []
self.left.reset()
self.up.reset()
self.right.reset()
self.down.reset()

# only for player mode
def god_move(self, move):
if move == -self.sides:
self.moves.append(0)
if move == 1:
self.moves.append(1)
if move == self.sides:
self.moves.append(2)
if move == -1:
self.moves.append(3)
self.position += move

# only for AI mode
def angel_move(self, board):
turn0 = self.up.query(board)
turn1 = self.right.query(board)
turn2 = self.down.query(board)
turn3 = self.left.query(board)
selection = np.argmax([turn0, turn1, turn2, turn3])

if np.argmax(selection) == 0:
self.up.save(board)
self.position += -self.sides
if np.argmax(selection) == 1:
self.right.save(board)
self.position += 1
if np.argmax(selection) == 2:
self.down.save(board)
self.position += self.sides
if np.argmax(selection) == 3:
self.left.save(board)
self.position += -1
self.moves.append(selection)

# train angel
def train(self, has_won):
if has_won is "devil":
self.up.train(False)
self.left.train(False)
self.down.train(False)
self.right.train(False)
if has_won is "angel":
self.up.train(True)
self.left.train(True)
self.down.train(True)
self.right.train(True)
Loading

0 comments on commit 2046e2b

Please sign in to comment.