def choose_action(num: int, network: NNetwork): print(num, ": ") data = None if num == 0: data = ds.BAD_ZERO elif num == 1: data = ds.BAD_ONE elif num == 2: data = ds.BAD_TWO elif num == 3: data = ds.BAD_THREE elif num == 4: data = ds.BAD_FOUR elif num == 5: data = ds.BAD_FIVE elif num == 6: data = ds.BAD_SIX elif num == 7: data = ds.BAD_SEVEN elif num == 8: data = ds.BAD_EIGHT else: data = ds.BAD_NINE print("-----------------------------------") network.recognize(data)
def eliminate(self, bot, replace=False): self.time_since_last_death = 0.0 self.bots.remove(bot) if replace: random_rgb = (np.random.randint(30, 256), np.random.randint(30, 256), np.random.randint(30, 256)) self.bots.append( Bot(NNetwork((1, 2, 4), (sigmoid, softmax)), random_rgb, self))
def main(): network = NNetwork() while True: print("Input number [0-9] of [exit]: ") try: s = input() if s == 'exit': break num = int(s) if 0 <= num <= 9: choose_action(num, network) except ValueError: print("Incorrect value")
def __init__(self, size, mutation_rate): assert (size >= 5) assert (0 < mutation_rate < 1) self.SIZE = size self.mutation_rate = mutation_rate self.bots = [] self.food = [] self.time_since_last_death = 0.0 # The neural network will have 1 neuron in the input layer, 1 hidden # layer with 2 neurons, and 4 neurons in the output layer. The sigmoid # activation function will be used on the hidden layer, and a softmax # activation function will be used on the output layer. Input consists # of the bot's direction and if there is or isn't food in the bots field # of vision. Output consists of whether or not to move foward, turn # left, turn right, or do nothing. for i in range(size): random_rgb = (np.random.randint(30, 256), np.random.randint(30, 256), np.random.randint(30, 256)) self.bots.append( Bot(NNetwork((1, 2, 4), (sigmoid, softmax)), random_rgb, self)) self.food.append(Food(self))
def gameLoop(game_time): # generate <MINE_NUM> mines with random positions mines = [Mine() for _ in range(MINE_NUM)] # generate <TANK_NUM> tanks with random positions agents = [Agent(NNetwork(N_INS, N_OUTS, N_HLS, N_HLNS)) for _ in range(AGENT_NUM)] # game loop print "\033[?47h" for i in range(game_time): # move all the tanks for a in agents: a.update(mines) # check collision with a mine if a.checkCollision(mines, 2) != -1: mines[a.ClosestMine].reset() a.Fitness += 1 # update the terminal screen given the delay frequency if i % DELAY == 0: updateTerminal(mines, agents) # return the tanks' fitness scores return [a.Fitness for a in agents]
def create_neural_network(self, act_fun_lay, act_fun_pos): """ Creates 2 hidden fully connected neural network with chosen last activation function :param act_fun_lay: layer, activation function as layer :param act_fun_pos: activation function position :return: NNetwork, neural network created """ nn = NNetwork() curt_lay = layer.FCLayer(self.data.shape[1] + self.targets.shape[1], constants.FC_1_NEURONS, constants.EDGE_GRADIENT, True) nn.add_layer(curt_lay, constants.FC_1_POS) nn.add_layer(layer.ReLULayer(), constants.RELU_1_POS) curt_lay = layer.FCLayer(constants.FC_1_NEURONS, constants.FC_2_NEURONS, constants.EDGE_GRADIENT, True) nn.add_layer(curt_lay, constants.FC_2_POS) nn.add_layer(layer.ReLULayer(), constants.RELU_2_POS) curt_lay = layer.FCLayer(constants.FC_2_NEURONS, constants.FC_OUTPUT_NEURONS, constants.EDGE_GRADIENT, True) nn.add_layer(curt_lay, constants.FC_OUTPUT_POS) nn.add_layer(act_fun_lay, act_fun_pos) return nn