Ejemplo n.º 1
0
 def on_tick(self, training_packet: TrainingTickPacket):
     packet: GameTickPacket = training_packet.game_tick_packet
     my_car = packet.game_cars[0].physics
     team = packet.game_cars[0].team
     ball = packet.game_ball.physics
     self.dribble_sphere.center = my_car.location
     if self.info == None:
         self.info: utils.Info = utils.Info(team, packet)
     self.info.update(packet)
     #Keep track of the balls max speed for score
     if magnitude(my_car.velocity) > self.max_speed:
         self.max_speed = magnitude(my_car.velocity)
     #Fail if ball is rolling on the ground
     if self.fail_area.is_in(ball.location) and abs(ball.velocity.z) < 10:
         return ScoredFail(0)
     #Fail if we've reached the ball but then thrown it away
     if self.dribble_sphere.is_in(ball.location):
         self.reached = True
     elif self.reached:
         return ScoredFail(0)
     #Fail if we've put it in our own net
     if self.info.scored_enemy:
         return ScoredFail(0)
     #Pass if we score on the enemy net
     if self.info.scored_me:
         return ScoredPass(self.max_speed)
Ejemplo n.º 2
0
 def checkOptions(self, player, otherPos, rotSeq):
     currentPos = (self.dict['x'], self.dict['y'])
     if otherPos == currentPos:
         options = self.generateCollisionOptions(player)
         bestDirection = utils.direction((self.dict['x'], self.dict['y']), self.dict['prevpos'])
         bdpos = utils.round_vector(bestDirection)
         if utils.magnitude(bdpos) == 0:
             bdpos = rand.choice(optKeys)
         if options[bdpos] and player.getRoom().validPosition(utils.add_vector(currentPos, bdpos)):
             self.dict['x'] += bdpos[0]
             self.dict['y'] += bdpos[1]
             return True
         else:
             for rot in rotSeq:
                 testvec = utils.rotate(bdpos, rot)
                 testvec = utils.round_vector(testvec)
                 if options[testvec]:
                     self.dict['x'] += testvec[0]
                     self.dict['y'] += testvec[1]
                     return True
             # if nothing worked -- this could be expanded to do proper step-by-step
             # checking until invalid
             self.dict['x'] = self.dict['prevpos'][0]
             self.dict['y'] = self.dict['prevpos'][1]
             return True
     return False
Ejemplo n.º 3
0
def part1() -> int:
    # It will be one of the particles with the smallest acceleration
    # However it appears there are 3 of them

    data = load_data()
    min_a = None
    min_accels = {}

    for i, particle in enumerate(data):
        mag = utils.magnitude(*particle.a.as_tuple())
        if mag == min_a:
            min_accels[i] = particle
        else:
            old_min = min_a
            min_a = utils.safe_min(min_a, mag)
            if old_min != min_a:
                min_accels = {i: particle}

    while True:
        # run simulation until all particles have all position,velocity, and
        # acceleration components with the same signs (or zero)

        should_break = True

        for particle in min_accels.values():
            tick(particle)
            for dim in "xyz":
                a = getattr(particle.a, dim)
                v = getattr(particle.v, dim)
                p = getattr(particle.p, dim)

                if a != 0 and not ((a > 0 and v > 0 and p > 0) or
                                   (a < 0 and v < 0 and p < 0)):
                    should_break = False
                    break

        if should_break:
            # do some more steps just in case
            for _ in range(1000):
                for particle in min_accels.values():
                    tick(particle)

            break

    min_dist = None
    min_i = None

    for i, particle in min_accels.items():
        dist = utils.manhattan(*particle.p.as_tuple())
        min_dist = utils.safe_min(min_dist, dist)
        if min_dist == dist:
            min_i = i

    return min_i
Ejemplo n.º 4
0
 def on_tick(self, training_packet: TrainingTickPacket):
     packet: GameTickPacket = training_packet.game_tick_packet
     my_car = packet.game_cars[0].physics
     team = packet.game_cars[0].team
     ball = packet.game_ball.physics
     if self.time == None:
         self.time = packet.game_info.seconds_elapsed
     if self.score_me == None:
         self.score_me = packet.teams[team].score
     if self.score_enemy == None:
         self.score_enemy = packet.teams[(team - 1) * -1].score
     if packet.game_info.seconds_elapsed - self.time > self.timer:
         return ScoredFail(-1000)
     if self.score_me < packet.teams[team].score:
         print("FINAL VELOCITY -------------- ", self.last_speed)
         return ScoredPass(self.last_speed)
     if self.score_enemy < packet.teams[(team - 1) * -1].score:
         return ScoredFail(-1000)
     if magnitude(ball.velocity) > 0:
         self.last_speed = magnitude(ball.velocity)
     return None
Ejemplo n.º 5
0
def load_data():
    data = ld.LoadHAR(ROOT_FOLDER).uci_har_v1()
    x_test = utils.spectrogram_2d(utils.magnitude(data['x_test'])).astype(
        theano.config.floatX)

    return dict(
        output_dim=int(data['y_test'].shape[-1]),
        X_train=theano.shared(
            utils.spectrogram_2d(utils.magnitude(data['x_train'])).astype(
                theano.config.floatX)),
        y_train=theano.shared(data['y_train'].astype(theano.config.floatX)),
        X_valid=theano.shared(x_test),
        y_valid=theano.shared(data['y_test'].astype(theano.config.floatX)),
        X_test=theano.shared(x_test),
        y_test=theano.shared(data['y_test'].astype(theano.config.floatX)),
        num_examples_train=data['x_train'].shape[0],
        num_examples_valid=data['x_test'].shape[0],
        num_examples_test=data['x_test'].shape[0],
        seq_len=int(x_test.shape[1]),
        input_width=x_test.shape[2],
        input_height=x_test.shape[3])
Ejemplo n.º 6
0
    def config(self):
        """A finite iterable of robot configurations.

        Makes the (big) assumption that the robot's initial state is at (0, 0),
        and faces 0 radians.

        Each configuration point is a (right speed, left speed, duration) tuple,
        where the speeds are in rad/sec.
        """
        # Fix the wheel speeds, because I'm almost out of rum.
        speed = 10.0
        left_turn_speed = -speed / 2
        right_turn_speed = speed / 2

        # Radius of wheel
        r = 0.5
        # Distance from center of robot to center of wheel
        L = 0.6

        # Convert the path points into vectors
        vectors = [p2 - p1 for p1, p2 in pairwise(self.points)]
        # Get the angles between each vector. Loop back around to close the loop.
        angles = [
            angle_between(v1, v2)
            for v1, v2 in pairwise(vectors + [vectors[0]])
        ]
        # Get the length of each vector
        distances = [magnitude(v) for v in vectors]

        for distance, angle in zip(distances, angles):
            print(f'distance = {distance}')
            # Calculate how long it will take to drive the straight distance.
            drive_time = distance / (2 * np.pi * r * speed)
            yield speed, speed, drive_time

            print(f'angle = {angle}')
            # Calculate how long it will take to turn the given angle with fixed wheel speeds.
            # BUG: Either this calculation is incorrect, or the robot isn't driving the wheels
            #      at the given speeds for this time.
            turn_time = (2 * L *
                         angle) / (r * (right_turn_speed - left_turn_speed))
            yield right_turn_speed, left_turn_speed, turn_time