Currently, most of our example agents define behavior that runs the agent when the file that contains the agent's code is run using python -m agent_main.py
. For example, see:
|
if __name__ == "__main__": |
|
"""Create a mouse agent and see what it learns as its best guess of the |
|
size of cookies it is seeing.""" |
|
import argparse |
|
|
|
parser = argparse.ArgumentParser( |
|
description=( |
|
"Run a MouseAgent that learns by looking at cookies " |
|
"using Friston's Free Energy principle. This agent " |
|
"is an implementation of the tutorial by Rafal Bogacz at " |
|
"https://sciencedirect.com/science/article/pii/S0022249615000759" |
|
) |
|
) |
|
parser.add_argument("--max-iters", type=int, default=150) |
|
parser.add_argument("-p", "--plot-results", action="store_true") |
|
args = parser.parse_args() |
|
print(f"Running mouse agent for {args.max_iters} steps...") |
|
print("------------------------------------------------") |
|
agentos.run_agent(Mouse, CookieSensorEnv, max_iters=args.max_iters) |
|
if args.plot_results: |
|
plt.figure(figsize=(15, 10)) |
|
for k, v in mouse_stats.items(): |
|
if k != "belief_light_var" and k != "belief_size_var": |
|
plt.plot(v, label=k) |
|
|
|
for k, v in env_stats.items(): |
|
plt.plot(v, label=k) |
|
|
|
plt.legend() |
|
plt.title("Mouse beliefs over time") |
|
plt.show() |
And also:
|
if __name__ == "__main__": |
|
import argparse |
|
from gym.envs.classic_control import CartPoleEnv |
|
|
|
parser = argparse.ArgumentParser( |
|
description="Run reinforce with a simple TF policy on gym CartPole. " |
|
"One rollout per call to agent.advance(), " |
|
"200 steps per rollout.", |
|
) |
|
parser.add_argument( |
|
"max_iters", |
|
type=int, |
|
metavar="MAX_ITERS", |
|
help="How many times to call advance() on agent.", |
|
) |
|
parser.add_argument("--rollouts_per_iter", type=int, default=1) |
|
parser.add_argument("--max_steps_per_rollout", type=int, default=200) |
|
parser.add_argument("--discount_rate", type=float, default=0.9) |
|
args = parser.parse_args() |
|
agentos.run_agent( |
|
ReinforceAgent, |
|
CartPoleEnv, |
|
max_iters=args.max_iters, |
|
rollouts_per_iter=args.rollouts_per_iter, |
|
max_steps_per_rollout=args.max_steps_per_rollout, |
|
discount_rate=args.discount_rate, |
|
) |
Notice that they have similar structure and contain a decent amount of boilerplate code.
Also notice that they use the standard python convention:
if __name__ == "__main__":
# parse command line args that define agent run behavior
# use agentos.run() to actually run the agent.
Our test cases execute these files via the pytest virtual-env
fixture's virtualenv.run()
which in turn calls subprocess.Popen
.
Since many agents will likely have similar command line args, it would be useful if we could make a utility function that handles defining command-line arguments, parses those same arguments, and runs the agent.
This would then provide a 3rd way to run an agent. That is, in addition to the options of (1) using an Agent Directory (which itself is an MLflow project), and (2) using the agentos CLI command agentos run
, one could now just directly run the agent python file (e.g., python -m /my/agent/main.py
).
The tricky part is keep the utility function super intuitive and easy to use.
One idea would be to define a function in agentos/core.py
:
def setup_agent_main(agent_class, env_classes=[CartPoleEnv]):
```
Set up a main function for an agent file, including accepting common
command line arguments. To use this in an agent file to make the agent
easy to execute directly as a python script.
# code to parse args
# code to run agent via agentos.run_agent()
This would be used from inside your agent_main.py
file as follows:
if __name__ == "__main__":
from agentos.core import setup_agent_main
setup_agent_main(YourAgentClass)
This would make example_agents easier to read and test, and would hopefully make agent development more approachable.
Agent Directories already provides a well defined way to specify agent params (in the MLProject
file), so perhaps this could be written to play nicely with those conventions.
Note that this whole idea might not be necessary since the agent dev could just use the CLI or an Agent Directory to run/test their agent. **And perhaps we should instead be discouraging agent developers from making their agent files directly runnable as python scripts (i.e., via python -m
).