def evaluate(env, agent, verbose=True):
    experience_source = experience_single.ExperienceSourceSingleEnvFirstLast(env, agent, gamma=params.GAMMA, n_step=params.N_STEP)
    done = False
    state = env.reset()
    agent_state = agent.initial_agent_state()
    episode_reward = 0.0
    num_buys = 0
    info = None
    step_idx = 0
    while (not done):
        step_idx += 1
        states_input = []
        processed_state = experience_source.get_processed_state(state)
        states_input.append(processed_state)
        agent_states_input = []
        agent_states_input.append(agent_state)
        (new_actions, new_agent_states) = agent(states_input, agent_states_input)
        agent_state = new_agent_states[0]
        action = new_actions[0]
        if (action == Action.MARKET_BUY.value):
            num_buys += 1
            if (num_buys > 10):
                action_str = 'BUY({0})'.format(10)
            else:
                action_str = 'BUY({0})'.format(num_buys)
        else:
            action_str = env.get_action_meanings()[action]
        msg = '[{0:2}|{1}] OHLCV: {2}, {3}, {4}, {5}, {6:<10.1f}, Action: {7:7} --> '.format(step_idx, env.data.iloc[env.transaction_state_idx]['datetime_krw'], env.data.iloc[env.transaction_state_idx]['open'], env.data.iloc[env.transaction_state_idx]['high'], env.data.iloc[env.transaction_state_idx]['low'], env.data.iloc[env.transaction_state_idx]['final'], env.data.iloc[env.transaction_state_idx]['volume'], action_str)
        (next_state, reward, done, info) = env.step(action)
        if (action in [Action.HOLD.value]):
            msg += 'Reward: {0:.3f}, hold coin: {1:.1f}'.format(reward, info['hold_coin'])
        elif (action == Action.MARKET_BUY.value):
            if (num_buys <= 10):
                coin_krw_str = '{0:.1f}'.format(info['coin_krw'])
                commission_fee_str = '{0:.1f}'.format(info['commission_fee'])
            else:
                coin_krw_str = '-'
                commission_fee_str = '-'
            msg += 'Reward: {0:.3f}, slippage: {1:.1f}, coin_unit_price: {2:.1f}, coin_krw: {3}, commission: {4}, hold coin: {5:.1f}'.format(reward, info['slippage'], info['coin_unit_price'], coin_krw_str, commission_fee_str, info['hold_coin'])
        elif (action == Action.MARKET_SELL.value):
            msg += 'Reward: {0:.3f}, slippage: {1:.1f}, coin_unit_price: {2:.1f}, coin_krw: {3:.1f}, commission: {4:.1f}, sold coin: {5:.1f}, profit: {6:.1f}'.format(reward, info['slippage'], info['coin_unit_price'], info['coin_krw'], info['commission_fee'], info['sold_coin'], info['profit'])
        else:
            raise ValueError()
        if verbose:
            print(msg)
        episode_reward += reward
        state = next_state
    if verbose:
        print('SAMPLED TRANSACTION DONE! - START DATETIME: {0}, EPISODE REWARD: {1:>8.3f}, PROFIT: {2:>10.1f}, STEPS: {3}'.format(env.transaction_start_datetime, episode_reward, info['profit'], step_idx))
    return (info['profit'], step_idx)
def evaluate(env, agent, verbose=True):
    experience_source = experience_single.ExperienceSourceSingleEnvFirstLast(env, agent, gamma=params.GAMMA, n_step=params.N_STEP)
    done = False
    state = env.reset()
    agent_state = agent.initial_agent_state()
    episode_reward = 0.0
    num_buys = 0
    info = None
    step_idx = 0
    while (not done):
        step_idx += 1
        states_input = []
        processed_state = experience_source.get_processed_state(state)
        states_input.append(processed_state)
        agent_states_input = []
        agent_states_input.append(agent_state)
        (new_actions, new_agent_states) = agent(states_input, agent_states_input)
        agent_state = new_agent_states[0]
        action = new_actions[0]
        if (action == Action.MARKET_BUY.value):
            num_buys += 1
            if (num_buys > 10):
                action_str = 'BUY({0})'.format(10)
            else:
                action_str = 'BUY({0})'.format(num_buys)
        else:
            action_str = env.get_action_meanings()[action]
        msg = '[{0:2}|{1}] OHLCV: {2}, {3}, {4}, {5}, {6:<10.1f}, Action: {7:7} --> '.format(step_idx, env.data.iloc[env.transaction_state_idx]['datetime_krw'], env.data.iloc[env.transaction_state_idx]['open'], env.data.iloc[env.transaction_state_idx]['high'], env.data.iloc[env.transaction_state_idx]['low'], env.data.iloc[env.transaction_state_idx]['final'], env.data.iloc[env.transaction_state_idx]['volume'], action_str)
        (next_state, reward, done, info) = env.step(action)
        if (action in [Action.HOLD.value]):
            msg += 'Reward: {0:.3f}, hold coin: {1:.1f}'.format(reward, info['hold_coin'])
        elif (action == Action.MARKET_BUY.value):
            if (num_buys <= 10):
                coin_krw_str = '{0:.1f}'.format(info['coin_krw'])
                commission_fee_str = '{0:.1f}'.format(info['commission_fee'])
            else:
                coin_krw_str = '-'
                commission_fee_str = '-'
            msg += 'Reward: {0:.3f}, slippage: {1:.1f}, coin_unit_price: {2:.1f}, coin_krw: {3}, commission: {4}, hold coin: {5:.1f}'.format(reward, info['slippage'], info['coin_unit_price'], coin_krw_str, commission_fee_str, info['hold_coin'])
        elif (action == Action.MARKET_SELL.value):
            msg += 'Reward: {0:.3f}, slippage: {1:.1f}, coin_unit_price: {2:.1f}, coin_krw: {3:.1f}, commission: {4:.1f}, sold coin: {5:.1f}, profit: {6:.1f}'.format(reward, info['slippage'], info['coin_unit_price'], info['coin_krw'], info['commission_fee'], info['sold_coin'], info['profit'])
        else:
            raise ValueError()
        if verbose:
            print(msg)
        episode_reward += reward
        state = next_state
    if verbose:
        print('SAMPLED TRANSACTION DONE! - START DATETIME: {0}, EPISODE REWARD: {1:>8.3f}, PROFIT: {2:>10.1f}, STEPS: {3}'.format(env.transaction_start_datetime, episode_reward, info['profit'], step_idx))
    return (info['profit'], step_idx)