
    &`i                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZ  G d de          Zed	k    r e                                d
                              edd          Ze de	 diZ ej        ej        e ej        ed eeg          g                                                    ZdS dS )a  Example showing how to define a custom Logger class for an RLlib Algorithm.

The script uses the AlgorithmConfig's `debugging` API to setup the custom Logger:

```
config.debugging(logger_config={
    "type": [some Logger subclass],
    "ctor_arg1", ...,
    "ctor_arg2", ...,
})
```

All keys other than "type" in the logger_config dict will be passed into the Logger
class's constructor.
By default (logger_config=None), RLlib will construct a Ray Tune UnifiedLogger object,
which logs results to JSON, CSV, and TBX.

NOTE that a custom Logger is different from a custom `ProgressReporter`, which defines,
how the (frequent) outputs to your console will be formatted. To see an example on how
to write your own Progress reporter, see:
https://github.com/ray-project/ray/tree/master/rllib/examples/ray_tune/custom_progress_reporter.py  # noqa

Below examples include:
- Disable logging entirely.
- Using only one of tune's Json, CSV, or TBX loggers.
- Defining a custom logger (by sub-classing tune.logger.py::Logger).


How to run this script
----------------------
`python [script file name].py`


Results to expect
-----------------
You should see log lines similar to the following in your console output. Note that
these logged lines will mix with the ones produced by Tune's default ProgressReporter.
See above link on how to setup a custom one.

ABC Avg-return: 20.609375; pi-loss: -0.02921550187703246
ABC Avg-return: 32.28688524590164; pi-loss: -0.023369029412534572
ABC Avg-return: 51.92; pi-loss: -0.017113141975661456
ABC Avg-return: 76.16; pi-loss: -0.01305474770361625
ABC Avg-return: 100.54; pi-loss: -0.007665307738129169
ABC Avg-return: 132.33; pi-loss: -0.005010405003325517
ABC Avg-return: 169.65; pi-loss: -0.008397869592997183
ABC Avg-return: 203.17; pi-loss: -0.005611495616764371
Flushing
Closing

    )tune)	PPOConfig)DEFAULT_MODULE_ID)ENV_RUNNER_RESULTSEPISODE_RETURN_MEANLEARNER_RESULTS)LegacyLoggerCallbackLoggerc                   0    e Zd ZdZd ZdefdZd Zd ZdS )MyPrintLoggerz/Logs results by simply printing out everything.c                     t          d           | j                            d                              d          | _        d S )NzInitializing ...logger_configprefix)printconfiggetr   selfs    }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/examples/ray_tune/custom_logger.py_initzMyPrintLogger._initC   s8     !!!kooo66::8DD    resultc                     |t                    t                   }|t                   t                   d         }t	          | j         d| d|            d S )Npolicy_lossz Avg-return: z
 pi-loss: )r   r   r   r   r   r   )r   r   mean_returnpi_losss       r   	on_resultzMyPrintLogger.on_resultI   sT    /01DE)*;<]KSS{SS'SSTTTTTr   c                 $    t          d           d S )NClosingr   r   s    r   closezMyPrintLogger.closeO   s    ir   c                 (    t          dd           d S )NFlushingT)flushr    r   s    r   r$   zMyPrintLogger.flushS   s    j%%%%%%r   N)	__name__
__module____qualname____doc__r   dictr   r!   r$    r   r   r   r   @   sj        99E E EU U U U U  & & & & &r   r   __main__zCartPole-v1ABC)typer   )r   /g      i@   )stopverbose	callbacks)param_space
run_configN)r(   rayr   ray.rllib.algorithms.ppor   ray.rllib.corer   ray.rllib.utils.metricsr   r   r   ray.tune.loggerr	   r
   r   r%   environment	debuggingr   r0   Tuner
algo_class	RunConfigfitresultsr*   r   r   <module>rA      s  2 2h       . . . . . . , , , , , ,         
 9 8 8 8 8 8 8 8& & & & &F & & &0 z	..& 
 &	 	 
 

 

) D "99$7995AD dj!4> %$m_55	
 
 
   
cee GGM r   