Exemplo n.º 1
0
def normal_sequence_example():
    input_size = 1
    hidden_sizes = [25]
    output_size = 1

    for i in range(0, 4):
        X = np.array([range(0, 100)]).T
        y = [1]
        values = [1]
        for j in range(1, 100):
            y.append(y[-1] + np.random.normal(0, 1))
        y = np.array([y]).T

        X_scale = np.power(10, len(str(int(np.amax(X)))))
        y_scale = np.power(10, len(str(int(np.amax(y)))))

        X = X / X_scale
        y = y / y_scale

        NN = NeuralNetwork(input_size, hidden_sizes, output_size)
        NN.train(X, y, 10000)

        plt.subplot(221 + i)
        plot_predictions(lambda x: NN.predict(x), X, y, X_scale, y_scale)
    plt.show()
Exemplo n.º 2
0
def random_decision_boundary_example():
    input_size = 2
    hidden_sizes = [10, 20, 30, 20, 10]
    output_size = 1

    np.random.seed(0)
    X = np.random.randn(100, 2)
    y = np.random.randint(0, 2, (100, 1))
    NN = NeuralNetwork(input_size, hidden_sizes, output_size)
    NN.train(X, y, 10000)
    plot_decision_boundary(lambda x: NN.predict(x), X, y)
    plt.show()
Exemplo n.º 3
0
def moons_decision_boundary_example():
    input_size = 2
    hidden_sizes = [5, 10, 5]
    output_size = 1

    np.random.seed(0)
    X, y = make_moons(200, noise=0.20)
    y = np.array([y]).T
    NN = NeuralNetwork(input_size, hidden_sizes, output_size)
    NN.train(X, y, 10000)
    plot_decision_boundary(lambda x: NN.predict(x), X, y)
    plt.show()
Exemplo n.º 4
0
def exponential_sequence_example():
    input_size = 1
    hidden_sizes = [100, 200, 100]
    output_size = 1

    sequence = np.arange(0, 10, 0.1)

    X = np.array([sequence]).T
    y = np.array([np.exp(sequence)]).T

    X_scale = np.power(10, len(str(int(np.amax(X)))))
    y_scale = np.power(10, len(str(int(np.amax(y)))))

    X = X / X_scale
    y = y / y_scale

    NN = NeuralNetwork(input_size, hidden_sizes, output_size)
    NN.train(X, y, 10000)

    plot_predictions(lambda x: NN.predict(x), X, y, X_scale, y_scale)
    plt.show()
Exemplo n.º 5
0
def random_sequence_example():
    input_size = 1
    hidden_sizes = [25]
    output_size = 1

    for i in range(0, 4):
        X = np.sort(np.random.uniform(0, 100, (25, input_size)), axis=0)
        X = np.sort(np.concatenate((X, X * 0.75)), axis=0)
        y = np.sort(np.random.uniform(0, 100, (25, output_size)), axis=1)
        y = np.sort(np.concatenate((y, y * 0.75)), axis=1)

        X_scale = np.power(10, len(str(int(np.amax(X)))))
        y_scale = np.power(10, len(str(int(np.amax(y)))))

        X = X / X_scale
        y = y / y_scale

        NN = NeuralNetwork(input_size, hidden_sizes, output_size)
        NN.train(X, y, 10000)

        plt.subplot(221 + i)
        plot_predictions(lambda x: NN.predict(x), X, y, X_scale, y_scale)
    plt.show()
Exemplo n.º 6
0
class Bird(Particle):
    def __init__(self, brain=None):
        super().__init__()
        xRandom = random.uniform(0.09, 0.14)
        self.moveTo(Vector2(display.width * xRandom, display.height // 2))
        if brain is None:
            self.brain = NeuralNetwork(6, 10, 1)
        else:
            self.brain = brain
        self.alive = True
        self.vision = []
        self.fitness = 0
        self.seed = random.randint(0, 7)

    def getBrain(self):
        return self.brain.copyNetwork()

    def getPosition(self):
        return self.pos

    def update(self, pipes):
        self.applyForce(gravity)
        self.think(pipes)
        self.doPhysics()
        if self.alive:
            self.fitness += 1

    def think(self, pipes):
        inputs = [self.pos.y / display.height, self.vel.y / self.maxVelocity]

        self.vision = self.perceive(pipes)
        for v in self.vision:
            inputs.append(v.x / display.width)
            inputs.append(v.y / display.height)
            # inputs.append(self.pos.angle_to(v) / 360)

        while len(inputs) < 4:
            inputs.append(0)

        predictions = self.brain.predict(inputs)
        prediction = predictions.pop()
        if prediction > 0.50:
            self.jump()

    def perceive(self, pipescollection):
        # for now, I'll just look at the bottom left of pipe[0] and top left of pipe[1]
        # TODO: look at the next pipe in the future
        seen = []
        pipes = pipescollection.getPipes()
        if len(pipes) > 0:
            seen.append(Vector2(pipes[0].getRect().bottomleft))
        if len(pipes) > 1:
            seen.append(Vector2(pipes[1].getRect().topleft))
        return seen

    def draw(self):
        display.drawBird(self.pos, self.vision, seed=self.seed)

    def kill(self):
        self.alive = False

    def isDead(self):
        return not self.alive

    def offScreen(self):
        return self.pos.y < 0 or self.pos.y > display.height

    def collide(self, pipeRects):
        birdRect = display.birdRect
        birdRect.center = self.pos
        return birdRect.collidelist(pipeRects) > -1

    def jump(self):
        self.applyForce(Vector2(0, -30))
Exemplo n.º 7
0
class Snake:
    def __init__(self, brain=None):
        if brain == None:
            self.brain = NeuralNetwork(NETWORK)  # new brain
        else:
            self.brain = NeuralNetwork([], brain)  # copy brain
        self.active = True
        self.ticks_alive = 0
        self.body = []
        self.body.append(
            Element(
                (BOARD_LEFT + BOARD_SIZE // 2, BOARD_TOP + BOARD_SIZE // 2),
                Direction.UP))
        self.turns = []
        self.think_lock = 0
        # every snake has his own apple to collect
        self.apple = Element((0, 0))
        self.placeApple()
        self.apples_eaten = 0
        self.steps_without_apple = 0

    def draw(self, window):
        for element in self.body:
            pygame.draw.circle(window, pygame.Color(SNAKE_COLOR),
                               (element.x, element.y), RADIUS)
        # apple
        pygame.draw.circle(window, pygame.Color(APPLE_COLOR),
                           (int(self.apple.x), int(self.apple.y)), RADIUS)

    def placeApple(self):
        good_position = False
        while good_position == False:
            self.apple.x = np.random.randint(BOARD_LEFT + RADIUS,
                                             BOARD_RIGHT - RADIUS)
            self.apple.y = np.random.randint(BOARD_TOP + RADIUS,
                                             BOARD_DOWN - RADIUS)

            good_position = True
            for element in self.body:
                if self.apple.calcDistance(element) < RADIUS:
                    good_position = False
                    break

    def addTail(self):
        self.apples_eaten += 1
        dir = self.body[-1].direction
        x = self.body[-1].x + (dir == Direction.LEFT) * \
            2 * RADIUS - (dir == Direction.RIGHT) * 2 * RADIUS
        y = self.body[-1].y + (dir == Direction.UP) * 2 * \
            RADIUS - (dir == Direction.DOWN) * 2 * RADIUS
        self.body.append(Element((x, y), dir))

    def calcFitness(self):
        self.fitness = self.ticks_alive * np.power(2, self.apples_eaten)

    def changeDir(self, dir):
        if dir.value == self.body[0].direction.value * (
                -1) or self.body[0].direction == dir:
            return

        self.turns.append((self.body[0].x, self.body[0].y, dir))
        self.steps_without_apple += 1

    def move(self):
        self.think_lock -= 1
        self.ticks_alive += 1

        # change position
        for element in self.body:
            for turn in self.turns:
                if element.x == turn[0] and element.y == turn[1]:
                    element.direction = turn[2]
                    if element == self.body[-1]:
                        self.turns.remove(turn)

            element.x += -(element.direction == Direction.LEFT) * \
                SPEED + (element.direction == Direction.RIGHT) * SPEED
            element.y += -(element.direction == Direction.UP) * \
                SPEED + (element.direction == Direction.DOWN) * SPEED

        # check for collisions
        # with wall
        if self.body[0].x < BOARD_LEFT + RADIUS or self.body[0].x > BOARD_RIGHT - RADIUS \
                or self.body[0].y > BOARD_DOWN - RADIUS or self.body[0].y < BOARD_TOP + RADIUS:
            self.active = False
        # with body
        for element in self.body:
            if len(self.body) > 1 and element != self.body[0] and self.body[
                    0].calcDistance(element) < RADIUS:
                self.active = False
        # with apple
        if self.body[0].calcDistance(self.apple) < 2 * RADIUS:
            self.addTail()
            self.placeApple()
            self.steps_without_apple = 0

        if self.steps_without_apple > STEPS_WITHOUT_APPLE_MAX:  # 15
            self.active = False

    def think(self):
        if self.think_lock > 0:
            return

        input = self.getInputArray()
        decision = np.argmax(self.brain.predict(input))
        if decision == 0:
            self.changeDir(Direction.UP)
        elif decision == 1:
            self.changeDir(Direction.DOWN)
        elif decision == 2:
            self.changeDir(Direction.LEFT)
        elif decision == 3:
            self.changeDir(Direction.RIGHT)

        self.think_lock = THINK_LOCK

    def checkSide(self, line, condition1, condition2, dist_value, dir=None):
        inputs = [0] * 3
        head = self.body[0]
        if dir != None and head.direction.value == -1 * dir.value:
            inputs[0] = 1
        else:
            for element in self.body:
                if element != head and element.checkCollisionWithLine(
                        line) and condition1(element):
                    inputs[0] = 1
                    break

        inputs[1] = 1 * \
            (self.apple.checkCollisionWithLine(line) and condition2)

        if line[0] == None or line[0] == 0:
            inputs[2] = 1 / (abs(dist_value) + 0.01)
        else:
            inputs[2] = 1 / checkDist(line[0], head.x, head.y, dist_value[0],
                                      dist_value[1])

        return inputs

    def getInputArray(self):
        inputs_array = []
        head = self.body[0]
        # check up
        inputs_array += self.checkSide([None, head.x],
                                       lambda element: element.y < head.y,
                                       self.apple.y < head.y,
                                       head.y - RADIUS - BOARD_TOP,
                                       Direction.UP)
        # check down
        inputs_array += self.checkSide([None, head.x],
                                       lambda element: element.y > head.y,
                                       self.apple.y > head.y,
                                       head.y + RADIUS - BOARD_DOWN,
                                       Direction.DOWN)
        # check left
        inputs_array += self.checkSide([0, head.y],
                                       lambda element: element.x < head.x,
                                       self.apple.x < head.x,
                                       head.x - RADIUS - BOARD_LEFT,
                                       Direction.LEFT)
        # check right
        inputs_array += self.checkSide([0, head.y],
                                       lambda element: element.x > head.x,
                                       self.apple.x > head.x,
                                       head.x + RADIUS - BOARD_RIGHT,
                                       Direction.RIGHT)
        # check up right
        inputs_array += self.checkSide([-1, head.y + head.x],
                                       lambda element: element.y < head.y,
                                       self.apple.y < head.y,
                                       (BOARD_RIGHT, BOARD_TOP))
        # check down left
        inputs_array += self.checkSide([-1, head.y + head.x],
                                       lambda element: element.y > head.y,
                                       self.apple.y > head.y,
                                       (BOARD_LEFT, BOARD_DOWN))
        # check up left
        inputs_array += self.checkSide([1, head.y - head.x],
                                       lambda element: element.y < head.y,
                                       self.apple.y < head.y,
                                       (BOARD_LEFT, BOARD_TOP))
        # check down right
        inputs_array += self.checkSide([1, head.y - head.x],
                                       lambda element: element.y > head.y,
                                       self.apple.y > head.y,
                                       (BOARD_RIGHT, BOARD_DOWN))

        return inputs_array