

def train_net(model, params):
    filename = params_to_filename(params)
    observe = 1000
    epsilon = 1
    train_frames = 1000000
    batchSize = params['batchSize']
    buffer = params['buffer']
    max_car_distance = 0
    car_distance = 0
    t = 0
    data_collect = []
    replay = []
    loss_log = []
    game_state = carmunk.GameState()
    (_, state) = game_state.frame_step(2)
    start_time = timeit.default_timer()
    while (t < train_frames):
        t += 1
        car_distance += 1
        if ((random.random() < epsilon) or (t < observe)):
            action = np.random.randint(0, 3)
        else:
            qval = model.predict(state, batch_size=1)
            action = np.argmax(qval)
        (reward, new_state) = game_state.frame_step(action)
        replay.append((state, action, reward, new_state))
        if (t > observe):
            if (len(replay) > buffer):
                replay.pop(0)
            minibatch = random.sample(replay, batchSize)
            (X_train, y_train) = process_minibatch(minibatch, model)
            history = LossHistory()
            model.fit(X_train, y_train, batch_size=batchSize, nb_epoch=1, verbose=0, callbacks=[history])
            loss_log.append(history.losses)
        state = new_state
        if ((epsilon > 0.1) and (t > observe)):
            epsilon -= (1 / train_frames)
        if (reward == (- 500)):
            data_collect.append([t, car_distance])
            if (car_distance > max_car_distance):
                max_car_distance = car_distance
            tot_time = (timeit.default_timer() - start_time)
            fps = (car_distance / tot_time)
            print(('Max: %d at %d\tepsilon %f\t(%d)\t%f fps' % (max_car_distance, t, epsilon, car_distance, fps)))
            car_distance = 0
            start_time = timeit.default_timer()
        if ((t % 25000) == 0):
            model.save_weights((((('saved-models/' + filename) + '-') + str(t)) + '.h5'), overwrite=True)
            print(('Saving model %s - %d' % (filename, t)))
    log_results(filename, data_collect, loss_log)

dataset/ETHPy150Open harvitronix/reinforcement-learning-car/learning.py train_net/original