def __init__(self, y): self.x = 100 self.y = y self.r = 20 self.vy = 0.1 self.score = 0 self.fintess = 0 self.nn = nNet.NeuralNetwork(ARQUI, activation="tanh") self.image = pygame.image.load('sprite1.png')
def __init__(self, n = NeuralNet.NeuralNetwork(\ NETWORK_INPUTS, \ NETWORK_OUTPUTS, \ NETWORK_LAYERS, \ NETWORK_HIDDEN_LAYER_AF, \ NETWORK_OUTPUT_LAYER_AF)): Entity.__init__(self) self.nn = n self.score = 0 self.target = None global BOT_COUNTER self.id = str(BOT_COUNTER) BOT_COUNTER += 1 self.food_line = None self.dx = 0 self.dy = 0
def train(self): print('Training some bots...') for x in range(TRAINING_EPOCH): print('Generation: ' \ + str(x + 1) + '/' \ + str(TRAINING_EPOCH) \ + ', Mutations: ' + str(self.mutations) \ + ', Avg. Score: ' \ + str(self.avg_score) \ + ', High Score: ' \ + str(self.high_score), \ end = ' \r') self.avg_score = 0 world = BotWorld(self.bots) # run the simulation for EPOCH_LENGTH time steps world.update(EPOCH_LENGTH) # create a roulette wheel for bot pairing roulette = [] for bot in self.bots: # each bot has a chance, even if they captured no food roulette += [bot] * (self.bots[bot].score + 1) self.avg_score += self.bots[bot].score if self.bots[bot].score > self.high_score: self.high_score = self.bots[bot].score self.elite_bot = {bot: self.bots[bot]} # calculate the average score for this generation self.avg_score = round(self.avg_score / len(self.bots), 2) newbots = {} for _ in range(POPULATION): # choose two random parents from the roulette wheel parent_a = random.choice(roulette) parent_b = random.choice(roulette) # get the first parent's weights as a list parent_a_weights = self.bots[parent_a].nn.encoded() x = 0 # make sure parents are different while parent_a == parent_b and x < POPULATION: parent_b = random.choice(roulette) x += 1 parent_b_weights = self.bots[parent_b].nn.encoded() # pick a random point to cross over parent a and b crossover = random.randint(1, len(parent_a_weights) - 1) # randomly choose a parent for first segment of genes if random.randint(0, 1): crossed_weights = parent_a_weights[:crossover] crossed_weights += parent_b_weights[crossover:] else: crossed_weights = parent_b_weights[:crossover] crossed_weights += parent_a_weights[crossover:] n = NeuralNet.NeuralNetwork(\ NETWORK_INPUTS, \ NETWORK_OUTPUTS, \ NETWORK_LAYERS, \ NETWORK_HIDDEN_LAYER_AF, \ NETWORK_OUTPUT_LAYER_AF) # randomly mutate a weight if random.random() < EVOLUTION_MUTATION_RATE: crossed_weights[random.randint( \ 0, len(crossed_weights) - 1)] = \ random.uniform(-1, 1) self.mutations += 1 # create new bot, add it to the next generation's population n.decode(crossed_weights) bot = Bot(n) newbots[bot.id] = bot self.bots = newbots print()
import NeuralNet as nNet import numpy as np #entradas simuladas pos x,y distancia con el tubo 3 entradas 1 salida nn = nNet.NeuralNetwork([3, 2, 1], activation="tanh") X = np.array([250, 250, 40]) print(nn.predict(X))
mntesting = MNIST('testing') image_test, image_label = mntesting.load_testing() # nn = NeuralNet(images, labels, images.shape[1], 100, 100, labels.shape[1]) # nn.train_neural_network(images, labels) np_testing_X = np.array([images[i] for i in range(len(images))]) np_testing_Y = np.zeros((len(image_label), 10)) for i in range(np_testing_Y.shape[0]): index = image_label[i] np_testing_Y[i, index] = 1 nn = NeuralNet.NeuralNetwork(np_testing_X, np_testing_Y, np_testing_X.shape[1], 800, np_testing_Y.shape[1]) # nn.train_neural_network(X_test, y_test) # Just gonna save the theta in a file. # nn.save_theta() nn.load_theta("theta0.csv", "theta1.csv") # Pick a random index. index = random.randrange(0, len(np_testing_X)) print(mndata.display(np_testing_X[index])) # Make a prediction
"weight_mult": 2, "layer_num": 5, "hidden_num": 40, "output_num": 13, "input_num": 16, "layer_funcs": [NN.sigmoid, NN.sigmoid, NN.sigmoid, NN.sigmoid, NN.sigmoid, NN.sigmoid] } for i in range(network_num): networks.append(NN.NeuralNetwork(True, settings)) print(networks[1].calculate_outputs(np.random.rand(16, 1))) print(networks[1].calculate_outputs(np.random.rand(16, 1))) print(networks[1].calculate_outputs(np.random.rand(16, 1))) controller1.connect() print('made it') controller2.connect() print('hereee') state = "fight" p_stock1 = 4 p_stock2 = 4
def graphical_inference(size_of_hidden_layer: int = 20, load_dir = "checkpoints", load_time_step: int = 500, play: bool = False, scalingFactor: int = 9): """ To render graphics of the trained agents :param size_of_hidden_layer: :param load_dir: :param load_time_step: :param play: :param scalingFactor: :return: """ import pygame import GraphicsEnv numSnakes = Constants.numberOfSnakes # type: int if play: numSnakes += 1 colors = np.random.randint(0, 256, size=[numSnakes, 3]) if play: # user interacts with the agents colors[0] = (0, 0, 0) # player's snake is always black # Create Game Window win = pygame.display.set_mode((scalingFactor * Constants.gridSize, scalingFactor * Constants.gridSize)) screen = pygame.Surface((Constants.gridSize+1, Constants.gridSize+1)) # Grid Screen pygame.display.set_caption("Snake Game") crashed = False targetNetwork = [] # type: List[NueralNetwork] targetSess = [] # type: List[tf.Session] if play: targetNetwork.append(None) targetSess.append(None) length = Agent.getStateLength() for idx in range(int(play), numSnakes): targetNetwork.append(NeuralNet.NeuralNetwork(num_layers=4, size_of_layers=[length, 64, 4, 1], initializer='he', optimizer='adam')) #targetNetwork.append(FunctionApproximator.NeuralNetwork(length, size_of_hidden_layer=size_of_hidden_layer)) targetSess.append(tf.Session(graph=targetNetwork[idx].graph)) targetNetwork[idx].init(targetSess[idx]) targetNetwork[idx].restore_model(targetSess[idx], "{}/target_{}_{}.ckpt".format(load_dir, load_time_step, idx - int(play))) g = Game.Game(numSnakes) episodeRunning = True while episodeRunning and not crashed: for event in pygame.event.get(): if event.type == pygame.QUIT: crashed = True actionList = [] if play: actionList.append( GraphicsEnv.manual_action(g.snakes[0], event) ) for i in range(int(play), numSnakes): snake = g.snakes[i] if not snake.alive: actionList.append(None) continue opponentSnakes = [opponent for opponent in g.snakes if opponent != snake] state = Agent.getState(snake, opponentSnakes, g.food, normalize=True) action, _ = targetNetwork[i].max_permissible_Q(targetSess[i], [state], snake.permissible_actions()) actionList.append(action) singleStepRewards, episodeRunning = g.move(actionList) GraphicsEnv.displayGame(g, win, screen, colors)
def train(max_time_steps: int = 1000, reward: int = 1, penalty: int = -10, size_of_hidden_layer: int = 20, num_threads: int = 4, checkpointFrequency: int = 500, checkpoint_dir = "checkpoints", load: bool = False, load_dir = "checkpoints", load_time_step: int = 500): """ :param max_time_steps: :param reward: :param penalty: :param size_of_hidden_layer: :param num_threads: :param checkpointFrequency: :param checkpoint_dir: :param load: :param load_dir: :param load_time_step: :return: """ policyNetwork = [] # type: List[NeuralNetwork] targetNetwork = [] # type: List[NeuralNetwork] policySess = [] # type: List[tf.Session] targetSess = [] # type: List[tf.Session] if os.path.isdir(checkpoint_dir): # if directory exists, delete it and its contents try: shutil.rmtree(checkpoint_dir) except OSError as e: print ("Error: %s - %s." % (e.filename, e.strerror)) os.makedirs(checkpoint_dir) length = Agent.getStateLength() #Initializing the 2*n neural nets for idx in range(Constants.numberOfSnakes): policyNetwork.append(NeuralNet.NeuralNetwork(num_layers=4, size_of_layers=[length, 64, 4, 1], initializer='he', optimizer='adam')) targetNetwork.append(NeuralNet.NeuralNetwork(num_layers=4, size_of_layers=[length, 64, 4, 1], initializer='he', optimizer='adam')) # policyNetwork.append(FunctionApproximator.NeuralNetwork(length, size_of_hidden_layer)) # targetNetwork.append(FunctionApproximator.NeuralNetwork(length, size_of_hidden_layer)) policySess.append(tf.Session(graph=policyNetwork[idx].graph)) targetSess.append(tf.Session(graph=targetNetwork[idx].graph)) policyNetwork[idx].init(policySess[idx]) targetNetwork[idx].init(targetSess[idx]) checkpoint_path = "{}/transfer_{}.ckpt".format(checkpoint_dir, idx) policyNetwork[idx].save_model(policySess[idx], checkpoint_path) targetNetwork[idx].restore_model(targetSess[idx], checkpoint_path) if load: # resume training from old checkpoints for idx in range(Constants.numberOfSnakes): policyNetwork[idx].restore_model(policySess[idx], "{}/policy_{}_{}.ckpt".format(load_dir, load_time_step, idx)) targetNetwork[idx].restore_model(targetSess[idx], "{}/target_{}_{}.ckpt".format(load_dir, load_time_step, idx)) T = 0 q = Queue() q.put(T) lock = Lock() threads = [Thread(target=async_Q, args=(max_time_steps, reward, penalty, checkpointFrequency, checkpoint_dir, policyNetwork, policySess, targetNetwork, targetSess, lock, q)) for _ in range(num_threads)] # map(lambda t: t.start(), threads) for t in threads: t.start() print(threads) print("main complete")