-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Panda7777
committed
Jun 30, 2017
1 parent
4869e1e
commit 17fcd72
Showing
5 changed files
with
197 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from NeuralNetwork import NeuralNetwork | ||
import numpy as np | ||
|
||
|
||
class Angel: | ||
def __init__(self, sides): | ||
self.sides = sides | ||
self.position = int(sides / 2) * sides + int(sides / 2) | ||
# initially in the center | ||
self.moves = [self.position] | ||
|
||
self.escaped = False | ||
|
||
input_nodes = sides ** 2 | ||
hidden_nodes = 200 | ||
output_nodes = 4 | ||
learning_rate = 0.1 | ||
weight_wih = np.random.normal(0.0, pow(hidden_nodes, -0.5), (hidden_nodes, input_nodes)) | ||
weight_who = np.random.normal(0.0, pow(output_nodes, -0.5), (output_nodes, hidden_nodes)) | ||
self.consciousness = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, weight_wih, weight_who, | ||
learning_rate) | ||
|
||
def has_escaped(self): | ||
return self.escaped | ||
|
||
def get_position(self): | ||
if self.escaped: | ||
return None | ||
return self.position | ||
|
||
def get_moves(self): | ||
return self.moves | ||
|
||
def is_trapped(self, all_devils): | ||
if self.position - self.sides in all_devils.get_positions() and self.position + self.sides in \ | ||
all_devils.get_positions() and self.position - 1 in all_devils.get_positions() and self.position + 1 \ | ||
in all_devils.get_positions(): | ||
return True | ||
return False | ||
|
||
def angel_move(self, all_devils, move): | ||
if (self.position + move) in all_devils.get_positions(): | ||
return False | ||
if self.position + move < 0 or self.position + move > self.sides ** 2 - 1: | ||
self.escaped = True | ||
if (self.position % self.sides == self.sides - 1 and (self.position + move) % self.sides == 0) or ( | ||
self.position % self.sides == 0 and (self.position + move) % self.sides == self.sides - 1): | ||
self.escaped = True | ||
self.position += move | ||
self.moves.append(self.position) | ||
return True | ||
|
||
def train(self, boards, has_won): | ||
win_error = 1 if has_won else 0 | ||
for i in range(len(boards)): | ||
reinforced_error = np.array([None, None, None, None]).reshape(4, 1) | ||
reinforced_error[self.moves[i]] = win_error | ||
self.consciousness.train(boards[i], reinforced_error) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import random | ||
|
||
|
||
class Blocks: | ||
def __init__(self): | ||
self.positions = [] | ||
|
||
def get_positions(self): | ||
return self.positions | ||
|
||
def add_block(self, angel_position, position): | ||
if angel_position == position: | ||
return | ||
self.positions.append(position) | ||
|
||
|
||
class Devil: | ||
def __init__(self, sides): | ||
self.blocks = Blocks() | ||
self.sides = sides | ||
|
||
def place_block(self, angel): | ||
random_place = random.randrange(0, self.sides ** 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from Board import Board | ||
|
||
board = Board(60, 9) | ||
while True: | ||
board.run() | ||
winner = board.get_winner() | ||
if winner is not None: | ||
print(winner) | ||
break | ||
# for i in range(100): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import numpy | ||
import scipy.special | ||
|
||
|
||
class NeuralNetwork: | ||
def __init__(self, input_node, hidden_node, output_node, initial_wih, initial_who, learn_rate): | ||
self.i_nodes = input_node | ||
self.h_nodes = hidden_node | ||
self.o_nodes = output_node | ||
|
||
self.wih = initial_wih | ||
self.who = initial_who | ||
|
||
self.lr = learn_rate | ||
self.activation_function = lambda x: scipy.special.expit(x) | ||
pass | ||
|
||
def train(self, inputs_list, targets_list): | ||
input_array = numpy.array(inputs_list, ndmin=2).T | ||
target_array = numpy.array(targets_list, ndmin=2).T | ||
|
||
hidden_inputs = numpy.dot(self.wih, input_array) | ||
hidden_outputs = self.activation_function(hidden_inputs) | ||
|
||
final_inputs = numpy.dot(self.who, hidden_outputs) | ||
final_outputs = self.activation_function(final_inputs) | ||
|
||
for i in range(self.o_nodes): | ||
if target_array[i] is None: | ||
target_array[i] = final_outputs[i] | ||
output_errors = target_array - final_outputs | ||
hidden_errors = numpy.dot(self.who.T, output_errors) | ||
|
||
self.who += self.lr * (output_errors * final_outputs * (1.0 - final_outputs)) * numpy.transpose(hidden_outputs) | ||
self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), | ||
numpy.transpose(input_array)) | ||
pass | ||
|
||
def query(self, input_list): | ||
input_list = numpy.array(input_list, ndmin=2).T | ||
|
||
hidden_inputs = numpy.dot(self.wih, input_list) | ||
hidden_outputs = self.activation_function(hidden_inputs) | ||
|
||
final_inputs = numpy.dot(self.who, hidden_outputs) | ||
final_outputs = self.activation_function(final_inputs) | ||
|
||
return final_outputs | ||
|
||
def return_wih(self): | ||
return self.wih | ||
|
||
def return_who(self): | ||
return self.who |