# Train

## AMESA Train API Documentation

### Overview

The AMESA Train API provides the training infrastructure for agents using Ray for distributed computing. It handles the complete training lifecycle, including algorithm configuration, resource management, checkpointing, and deployment preparation.

### Trainer API

The `Trainer` class is the main interface for training agents.

#### Basic Usage

```python
from composabl import Trainer
from composabl_core.config.trainer_config import (
    BenchmarkConfig,
    RecordConfig,
    PostProcessingConfig,
)

# Create trainer with configuration
trainer = Trainer({
    "target": {
        "local": {"address": "localhost:1337"}
    },
    "env": {
        "name": "my-environment"
    }
})

# Train agent
trainer.train(agent, train_cycles=100)

# Evaluate performance
results = trainer.postprocess(
        agent,
        postprocess_config=PostProcessingConfig(
            file_path="model_files/",
            record=RecordConfig(
                avi_file_name="output.avi",
                gif_file_name="output.gif",
                max_frames=24 * 5,
            ),
            benchmark=BenchmarkConfig(
                num_episodes_per_scenario=2,
                file_name="benchmark.json",
            ),
        ),
    )

# Package for deployment
deployed_agent = trainer.package(agent)

# Clean up resources
trainer.close()
```

### Training Configuration

#### Complete Configuration Example

```python
config = {
    # License (required)
    "license": "your-license-key",
    
    # Training target (required)
    "target": {
        # See Training Targets section for options
    },
    
    # Environment configuration
    "env": {
        "name": "environment-id",
        "init": {
            "param1": "value1",
            "difficulty": "medium"
        }
    }
}
```

### Training Targets

#### Local Target

Train with a simulator running locally:

```python
config = {
    "target": {
        "local": {
            "address": "localhost:1337"
        }
    }
}
```

#### Docker Target

Train with simulators in Docker containers:

```python
config = {
    "target": {
        "docker": {
            "image": "composabl/sim-reactor:latest"
        }
    }
}
```

### Benchmarking

#### Benchmarking

For every scenario on the top-level skill, this will perform \`num\_episodes\_per\_scenario\` amount of inference episodes.

```python
# Evaluate trained agent
eval_results = trainer.postprocess(
        agent,
        postprocess_config=PostProcessingConfig(
            file_path="model_files/",
            benchmark=BenchmarkConfig(
                num_episodes_per_scenario=2,
                file_name="benchmark.json",
            ),
        ),
    )
```

#### Recording

```python
# Evaluation with recording
eval_results = trainer.postprocess(
        agent,
        postprocess_config=PostProcessingConfig(
            file_path="model_files/",
            record=RecordConfig(
                avi_file_name="output.avi",
                gif_file_name="output.gif",
                max_frames=24 * 5,
            ),
        ),
    )
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.amesa.com/reference/sdk-reference/train.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
