Example #1
0
    def getCollisions(self):
        """
        Function to check if the kuche collides with one of the players or the boundaries
        """
        new_pos = self.kuchen.get_pos()
        self.kuchen_collision = "none"
        if (self.status[0:2] == "P1"):
            self.status, self.kuchen_collision = check_collision(
                self.kuchen, self.player2, self.status)
        elif (self.status[0:2] == "P2"):
            self.status, self.kuchen_collision = check_collision(
                self.kuchen, self.player1, self.status)

        if self.status != "P1_Hit" and self.status != "P2_Hit":
            self.status, self.kuchen_collision = check_collision_boundaries(
                self.kuchen, 0, 0, self.MAX_X, self.MAX_Y, self.status)
        else:
            if (self.status == "P1_Hit"):
                self.score = (self.score[0] + 1, self.score[1])
            elif (self.status == "P2_Hit"):
                self.score = (self.score[0], self.score[1] + 1)
        if (self.status[2:] == "_Hit") or (self.status[2:] == "_Missed"):
            #self.kuchen.load_image_play("kuchen_splash_down.png", -1)
            self.manageKuchen()
        self.txtStatus.set_value(self.status)
        return new_pos
Example #2
0
 def getCollisions(self):
     new_pos = self.kuchen.get_pos()
     if (self.status[0:2] == "P1"):
         self.status = check_collision(self.kuchen, self.player2,
                                       self.status)
     elif (self.status[0:2] == "P2"):
         self.status = check_collision(self.kuchen, self.player1,
                                       self.status)
     if self.status != "P1_Hit" and self.status != "P2_Hit":
         self.status = check_collision_boundaries(self.kuchen, 0, 0,
                                                  self.MAX_X, self.MAX_Y,
                                                  self.status)
     return new_pos
Example #3
0
 def getCollisions(self):
     new_pos = self.Kuchen.update()
     if (self.gameStatus[0:2] == "P1"):
         self.gameStatus = check_collision(self.Kuchen, self.Player2,
                                           self.gameStatus)
     elif (self.gameStatus[0:2] == "P2"):
         self.gameStatus = check_collision(self.Kuchen, self.Player1,
                                           self.gameStatus)
     if self.gameStatus != "P1_Hit" and self.gameStatus != "P2_Hit":
         self.gameStatus = check_collision_boundaries(
             self.Kuchen, 0, 0, self.screen_width, self.screen_height,
             self.gameStatus)
     return new_pos
def update():

    gfw.world.update()
    generator.update()

    # if not gfw.world.count_at(gfw.layer.enemy) == 0:
    #     for n in gfw.world.objects_at(gfw.layer.enemy):
    #         print(n)

    collision.check_collision()
    score.update()

    #print(gfw.world.count_at(gfw.layer.enemy))

    pattern.update()
    stage_gen.update()
Example #5
0
    def update(self, dt, keys):
        # update all monsters
        for monster in self.monsters:
            monster.update(dt, self.layer.tiles)

        # update player
        self.update_player(dt, keys)

        # check if monsters collides with player
        for monster in self.monsters:
            if collision.check_collision(self.player, monster):
                self.sm.change_state( states.GameoverState() )
                return

        # is level end reached?
        if collision.check_collision(self.player, self.goal):
            self.sm.change_state( game.LevelCompletedState() )
            return
def check_collisions(source, objects):
    # if bullet is active
    if source.active:
        # go through all monsters
        for obj in objects:
            # check if bullet hits active monster
            if obj.active and collision.check_collision(source.sprite, obj):
                # deactivate monster and bullet
                obj.deactivate()
                source.deactivate()
                # massive explosion...
                # explosion.create(obj.x, obj.y)
                return
Example #7
0
def update():
    global state, score
    if state != STATE_IN_GAME:
        return

    score += gfw.delta_time
    gfw.world.update()
    generator.update(score)
    dead, full = check_collision()
    if dead:
        end_game()
    if full:
        score += 5
Example #8
0
def display_map(self):
    pos = self.p.get_positions()
    sprite = self.p.get_animation("Down")
    img = self.images.get("in_game")
    p_health = self.p.get_player_info("Health")
    max_health = self.p.get_player_info("MaxHealth")
    pos, sprite = movement.check_movement(self)
    pos = collision.check_collision(self, pos)

    self.p.change_positions(pos[0], pos[1])
    self.window.blit(img.get("map"), (0 - pos[0], 0 - pos[1]))
    for i in self.mobs:
        anim = i.get_animation("Down")
        self.window.blit(anim[1], (i.pos[0] - pos[0], i.pos[1] - pos[1]))
    self.window.blit(img.get("empty_bar"), (1110, 760), (0, 0, 100, 25))
    self.window.blit(img.get("heal_bar"), (1110, 760),
                     (0, 0, floor(p_health / max_health * 100), 25))
    self.window.blit(sprite[self.index_animation], (620, 400))
Example #9
0
    def resolve_collision(self, entity, elapsed_time):

        neighbors = []
        self.quadtree.retrieve(neighbors, entity)

        for neighbor in neighbors:

            if entity != neighbor:

                mtv = collision.check_collision(entity, neighbor, self)

                if mtv is not False:

                    if entity.move_component is not None:
                        entity.move_component.vel *= 0
                        entity.pos[0] -= mtv[0]
                        entity.pos[1] -= mtv[1]

                    if neighbor.move_component is not None:
                        neighbor.move_component.vel *= 0
                        neighbor.pos[0] += mtv[0]
                        neighbor.pos[1] += mtv[1]
Example #10
0
def main():
    desc = "Keras implementation of DCENet for trajectory prediction"
    parser = argparse.ArgumentParser(description=desc)

    parser.add_argument(
        '--num_pred',
        type=int,
        default=25,
        help='This is the number of predictions for each agent')
    parser.add_argument('--obs_seq',
                        type=int,
                        default=8,
                        help='Number of time steps observed')
    parser.add_argument('--enviro_pdim',
                        type=int,
                        default=[32, 32, 3],
                        help='The dimension of the environment after padding')
    parser.add_argument('--pred_seq',
                        type=int,
                        default=12,
                        help='Number of time steps to be predicted')
    parser.add_argument('--dist_thre',
                        type=float,
                        default=1.0,
                        help='The distance threhold for group detection')
    parser.add_argument(
        '--ratio',
        type=float,
        default=0.95,
        help='The overlap ratio of coexisting for group detection')
    parser.add_argument('--n_hidden',
                        type=int,
                        default=512,
                        help='This is the hidden size of the cvae')
    parser.add_argument('--z_dim',
                        type=int,
                        default=2,
                        help='This is the size of the latent variable')
    parser.add_argument(
        '--encoder_dim',
        type=int,
        default=16,
        help='This is the size of the encoder output dimension')
    parser.add_argument('--z_decoder_dim',
                        type=int,
                        default=64,
                        help='This is the size of the decoder LSTM dimension')
    parser.add_argument('--hidden_size',
                        type=int,
                        default=32,
                        help='The size of LSTM hidden state')
    parser.add_argument('--batch_size',
                        type=int,
                        default=192,
                        help='Batch size')  # 192
    parser.add_argument('--o_drop',
                        type=float,
                        default=0.2,
                        help='The dropout rate for occupancy')  # 0.2
    parser.add_argument('--s_drop',
                        type=float,
                        default=0.1,
                        help='The dropout rate for trajectory sequence')  # 0.1
    parser.add_argument('--z_drop',
                        type=float,
                        default=0.15,
                        help='The dropout rate for z input')  # 0.15
    parser.add_argument('--beta', type=float, default=0.65,
                        help='Loss weight')  # 0.75#0.65
    parser.add_argument('--query_dim',
                        type=int,
                        default=4,
                        help='The dimension of the query')
    parser.add_argument('--keyvalue_dim',
                        type=int,
                        default=4,
                        help='The dimension for key and value')
    parser.add_argument('--train_mode',
                        type=bool,
                        default=True,
                        help='This is the training mode')
    parser.add_argument('--train_set',
                        type=str,
                        choices=['Train'],
                        default='Train',
                        help='This is the directories for the training data')
    parser.add_argument('--challenge_set',
                        type=str,
                        choices=['Test'],
                        default='Test',
                        help='This is the directories for the challenge data'
                        )  # it is the online test set
    parser.add_argument('--split',
                        type=float,
                        default=0.8,
                        help='the split rate for training and validation')
    parser.add_argument('--lr', type=float, default=3e-5,
                        help='Learning rate')  # 3-5
    parser.add_argument('--aug_num',
                        type=int,
                        default=4,
                        help='Number of augmentations')
    parser.add_argument('--epochs',
                        type=int,
                        default=100,
                        help='Number of batches')
    parser.add_argument(
        '--patience',
        type=int,
        default=5,
        help='Maximum mumber of continuous epochs without converging')
    parser.add_argument('--x_encoder_dim',
                        type=int,
                        default=64,
                        help='Mapping dimension of x_encoder')
    parser.add_argument('--y_encoder_dim',
                        type=int,
                        default=64,
                        help='Mapping dimension of y_encoder')
    parser.add_argument(
        '--x_encoder_layers',
        type=int,
        default=3,
        help='Number of transformer block layers for x_encoder')
    parser.add_argument('--y_encoder_layers',
                        type=int,
                        default=3,
                        help='Number of transformer block layer for y_encoder')
    parser.add_argument('--x_encoder_head',
                        type=int,
                        default=8,
                        help='Head number of x_encoder')
    parser.add_argument('--y_encoder_head',
                        type=int,
                        default=8,
                        help='Head number of y_encoder')
    parser.add_argument('--occu_encoder_x_dim',
                        type=int,
                        default=32,
                        help='Mapping dimension of the x occupancy encoder')
    parser.add_argument('--occu_encoder_y_dim',
                        type=int,
                        default=32,
                        help='Mapping dimension of the y occupancy encoder')
    parser.add_argument(
        '--occu_encoder_x_layers',
        type=int,
        default=3,
        help='Number of transformer block layers for x_encoder')
    parser.add_argument('--occu_encoder_y_layers',
                        type=int,
                        default=3,
                        help='Number of transformer block layer for y_encoder')
    parser.add_argument('--occu_encoder_x_head',
                        type=int,
                        default=2,
                        help='Head number of x_encoder')
    parser.add_argument('--occu_encoder_y_head',
                        type=int,
                        default=2,
                        help='Head number of y_encoder')
    parser.add_argument('--preprocess_data',
                        type=bool,
                        default=False,
                        help='Process and merge dataset')

    args = parser.parse_args(sys.argv[1:])

    # Make all the necessary folders
    mak_dir()

    # # specify the directory for training and challenge data
    train_paths = sorted(
        glob.glob("../WORLD H-H TRAJ/%s/**/*.txt" % (args.train_set)))
    # # NOTE, here the challenge set is the "ONLINE" test set
    # # This is different from the "OFFLINE" test set
    # ToDo chenge this to make compatible with linus
    challenge_paths = sorted(
        glob.glob("../WORLD H-H TRAJ/%s/**/*.txt" % (args.challenge_set)))
    # challenge_paths = sorted(glob.glob("../trajectories/%s/**/*.txt"%(args.challenge_set)))

    # Process the data
    for path in train_paths:
        dataname = os.path.splitext(os.path.basename(path))[0]
        if not os.path.exists("../processed_data/train/%s.npz" % dataname):
            # preprocess_data(path, args.obs_seq+args.pred_seq-1, args.enviro_pdim, "train")
            preprocess_data(seq_length=args.obs_seq + args.pred_seq - 1,
                            size=args.enviro_pdim,
                            dirname="train",
                            path=path,
                            aug_num=args.aug_num,
                            save=True)

    for path in challenge_paths:
        # dataname = path.split('\\')[-1].split('.')[0]
        dataname = os.path.splitext(os.path.basename(path))[0]
        if not os.path.exists("../processed_data/challenge/%s.npz" % dataname):
            # preprocess_data(path, args.obs_seq-1, args.enviro_pdim, "challenge")
            preprocess_data(seq_length=args.obs_seq - 1,
                            size=args.enviro_pdim,
                            dirname="challenge",
                            path=path,
                            save=True)

    # Check the daatinfo for dataset partition
    Datalist = datainfo()
    # Datalist = datainfo(4)

    # Define the callback and early stop
    timestr = time.strftime("%Y%m%d-%H%M%S")
    filepath = "../models/DCENet_%0.f_%s.hdf5" % (args.epochs, timestr)
    ## Eraly stop
    # reduce_lr = ReduceLROnPlateau(monitor='val_loss', patience=100, mode='auto')
    earlystop = EarlyStopping(monitor='val_loss',
                              mode='min',
                              verbose=1,
                              patience=args.patience)
    checkpoint = ModelCheckpoint(filepath,
                                 monitor='val_loss',
                                 verbose=0,
                                 save_best_only=True,
                                 mode='min')
    callbacks_list = [earlystop, checkpoint]

    # # Instantiate the model
    DCENet = dcenet(args)
    # Contruct the cave model
    train = DCENet.training()
    train.summary()

    x_encoder = DCENet.X_encoder()
    decoder = DCENet.Decoder()
    # sys.exit()

    # Start training phase
    if args.train_mode:
        if args.preprocess_data:
            traindata_list = Datalist.train_data
        else:
            traindata_list = Datalist.train_merged

        print("train data list", traindata_list)

        # # NOTE: this is the "OFFLINE" test set. This is only used to plot if the prediction is feasible
        # # This test set has nothing to do with the challenge data set ("ONLINE" test set)
        testdata_list = Datalist.train_biwi
        print("test data list", testdata_list)

        # Get the data fro training and validation
        np.random.seed(10)
        offsets, traj_data, occupancy = loaddata(traindata_list,
                                                 args,
                                                 datatype="train")
        train_val_split = np.random.rand(len(offsets)) < args.split

        train_x = offsets[train_val_split, :args.obs_seq - 1, 4:6]
        train_occu = occupancy[train_val_split, :args.obs_seq - 1,
                               ..., :args.enviro_pdim[-1]]
        train_y = offsets[train_val_split, args.obs_seq - 1:, 4:6]
        train_y_occu = occupancy[train_val_split, args.obs_seq - 1:,
                                 ..., :args.enviro_pdim[-1]]

        val_x = offsets[~train_val_split, :args.obs_seq - 1, 4:6]
        val_occu = occupancy[~train_val_split, :args.obs_seq - 1,
                             ..., :args.enviro_pdim[-1]]
        val_y = offsets[~train_val_split, args.obs_seq - 1:, 4:6]
        val_y_occu = occupancy[~train_val_split, args.obs_seq - 1:,
                               ..., :args.enviro_pdim[-1]]

        print(
            "%.0f trajectories for training\n %.0f trajectories for valiadation"
            % (train_x.shape[0], val_x.shape[0]))

        test_offsets, test_trajs, test_occupancy = loaddata(testdata_list,
                                                            args,
                                                            datatype="test")
        test_x = test_offsets[:, :args.obs_seq - 1, 4:6]
        test_occu = test_occupancy[:, :args.obs_seq - 1,
                                   ..., :args.enviro_pdim[-1]]
        last_obs_test = test_offsets[:, args.obs_seq - 2, 2:4]
        y_truth = test_offsets[:, args.obs_seq - 1:, :4]
        xy_truth = test_offsets[:, :, :4]
        print("%.0f trajectories for testing" % (test_x.shape[0]))

        print("Start training the model...")
        # Retrain from last time
        # train.load_weights("../models/best.hdf5")
        train.fit(x=[train_occu, train_x, train_y_occu, train_y],
                  y=train_y,
                  shuffle=True,
                  epochs=args.epochs,
                  batch_size=args.batch_size,
                  verbose=1,
                  callbacks=callbacks_list,
                  validation_data=([val_occu, val_x, val_y_occu,
                                    val_y], val_y))
        train.load_weights(filepath)

        # Start inference phase
        # Retrieve the x_encoder and the decoder
        x_encoder = DCENet.X_encoder()
        decoder = DCENet.Decoder()
        x_encoder.summary()
        decoder.summary()

        # get the x_encoded_dense as latent feature for prediction
        x_latent = x_encoder.predict([test_occu, test_x],
                                     batch_size=args.batch_size)
        # x_latent = x_encoder.predict(test_x, batch_size=args.batch_size)

        # Using x_latent and z as input of the decoder for generating future trajectories
        print("Start predicting")
        predictions = []
        for i, x_ in enumerate(x_latent):
            last_pos = last_obs_test[i]
            x_ = np.reshape(x_, [1, -1])
            for i in range(args.num_pred):
                # sampling z from a normal distribution
                z_sample = np.random.rand(1, args.z_dim)
                y_p = decoder.predict(np.column_stack([z_sample, x_]))
                y_p_ = np.concatenate(([last_pos], np.squeeze(y_p)), axis=0)
                y_p_sum = np.cumsum(y_p_, axis=0)
                predictions.append(y_p_sum[1:, :])
        predictions = np.reshape(predictions,
                                 [-1, args.num_pred, args.pred_seq, 2])

        print('Predicting done!')
        print(predictions.shape)
        plot_pred(xy_truth, predictions)
        # Get the errors for ADE, DEF, Hausdorff distance, speed deviation, heading error
        print("\nEvaluation results @top%.0f" % args.num_pred)
        errors = get_errors(y_truth, predictions)
        check_collision(y_truth)

        ##
        ## Get the first time prediction by g
        ranked_prediction = []
        for prediction in predictions:
            ranks = gauss_rank(prediction)
            ranked_prediction.append(prediction[np.argmax(ranks)])
        ranked_prediction = np.reshape(ranked_prediction,
                                       [-1, 1, args.pred_seq, 2])
        print("\nEvaluation results for most-likely predictions")
        # ranked_errors = get_errors(y_truth, ranked_prediction)

    else:
        print('Run pretrained model')
        train.load_weights("../models/best.hdf5")

    challenge_list = Datalist.challenge_data
    for challenge_dataname in challenge_list:
        print(challenge_dataname, "\n")
        challenge_offsets, challenge_trajs, challenge_occupancy = loaddata(
            [challenge_dataname], args, datatype="challenge")
        print(challenge_offsets.shape, challenge_trajs.shape,
              challenge_occupancy.shape)

        challenge_x = challenge_offsets[:, :args.obs_seq - 1, 4:6]
        challenge_occu = challenge_occupancy[:, :args.obs_seq - 1,
                                             ..., :args.enviro_pdim[-1]]
        last_obs_challenge = challenge_trajs[:, args.obs_seq - 1, 2:4]
        print("%.0f trajectories for challenge" % (challenge_x.shape[0]))

        # Start inference phase
        # Retrieve the x_encoder and the decoder
        x_encoder = DCENet.X_encoder()
        decoder = DCENet.Decoder()

        # get the x_encoded_dense as latent feature for prediction
        x_latent = x_encoder.predict([challenge_occu, challenge_x],
                                     batch_size=args.batch_size)

        # Using x_latent and z as input of the decoder for generating future trajectories
        print("Start predicting the challenge data")
        challenge_predictions = []
        for i, x_ in enumerate(x_latent):
            last_pos = last_obs_challenge[i]
            x_ = np.reshape(x_, [1, -1])
            for i in range(args.num_pred):
                # sampling z from a normal distribution
                z_sample = np.random.rand(1, args.z_dim)
                y_p = decoder.predict(np.column_stack([z_sample, x_]))
                y_p_ = np.concatenate(([last_pos], np.squeeze(y_p)), axis=0)
                y_p_sum = np.cumsum(y_p_, axis=0)
                challenge_predictions.append(y_p_sum[1:, :])
        challenge_predictions = np.reshape(
            challenge_predictions, [-1, args.num_pred, args.pred_seq, 2])

        print('Predicting done!')
        print(challenge_predictions.shape)

        ##
        ## Get the first time prediction
        challenge_ranked_prediction = []
        for prediction in challenge_predictions:
            ranks = gauss_rank(prediction)
            challenge_ranked_prediction.append(prediction[np.argmax(ranks)])
        challenge_ranked_prediction = np.reshape(challenge_ranked_prediction,
                                                 [-1, 1, args.pred_seq, 2])

        challenge_pred_traj = writer.get_index(challenge_trajs,
                                               challenge_predictions)
        print("Collision in ranked prediction")
        check_collision(np.squeeze(challenge_pred_traj))
        writer.write_pred_txt(challenge_trajs, challenge_predictions,
                              challenge_dataname, "prediction")
Example #11
0
 def check_collision(self, objs):
     for obj in objs:
         if collision.check_collision(self, obj):
             return True
     return False
Example #12
0
while not gameStatus == "Finished":
    Kuchen_pos = Kuchen.get_pos()
    screen.fill(0)
    screen.blit(P1_sprite, P1_pos)
    screen.blit(P2_sprite, P2_pos)
    screen.blit(Kuchen_sprite, Kuchen_pos)
    if (gameStatus[2:] == "_Ready"):
        windTxt.draw(screen)
        forceTxt.draw(screen)
        angleTxt.draw(screen)
        throwButton.draw(screen)
    elif (gameStatus[2:] == "_Threw"):
        Kuchen_pos = Kuchen.update()
        if (gameStatus[0:2] == "P1"):
            gameStatus = check_collision(Kuchen, Player2, gameStatus)
        elif (gameStatus[0:2] == "P2"):
            gameStatus = check_collision(Kuchen, Player1, gameStatus)
        if gameStatus != "P1_Hit" and gameStatus != "P2_Hit":
            gameStatus = check_collision_boundaries(Kuchen, 0, 0, MAX_X, MAX_Y, gameStatus)
    elif (gameStatus[2:] == "_Missed"):
        continueButton.draw(screen)
    elif (gameStatus[2:] == "_Hit"):
        if score_added == False:
            if gameStatus[0:2] == "P1":
                score_P1 += 1
            elif gameStatus[0:2] == "P2":
                score_P2 += 1
            score_added = True
        PxWinsTxt = Textbox(gameStatus[0:2] + " Wins!", (200, 50))
        WinCounterTxt = Textbox(str(score_P1) + " : " + str(score_P2), (200, 150))