Views
No views yet
1# Core PPO Settings
2batch_size: 2048
3buffer_size: 20480
4learning_rate: 3e-4
5learning_rate_schedule: linear
6epsilon: 0.2
7beta: 5e-4
8lambd: 0.95
9num_epoch: 3
10
11# Network Architecture
12hidden_units: 512
13num_layers: 2
14normalize: true
15vis_encode_type: simple
16
17# Training Schedule
18max_steps: 50000000
19time_horizon: 1000
20summary_freq: 120001import onnxruntime as ort
2import numpy as np
3
4# Load the ONNX model
5model_path = "SoccerTwos.onnx"
6session = ort.InferenceSession(model_path)
7
8# Get input/output names
9input_name = session.get_inputs()[0].name
10output_names = [output.name for output in session.get_outputs()]
11
12# Run inference
13def predict_action(observation):
14 observation = np.array(observation, dtype=np.float32)
15 observation = observation.reshape(1, -1) # Batch dimension
16
17 outputs = session.run(output_names, {input_name: observation})
18 actions = outputs[0][0] # Extract actions from batch
19
20 return actions1// Unity C# script example
2using Unity.MLAgents;
3using Unity.MLAgents.Sensors;
4using Unity.MLAgents.Actuators;
5
6public class SoccerAgent : Agent
7{
8 [SerializeField] private string modelPath = "SoccerTwos.onnx";
9
10 public override void OnActionReceived(ActionBuffers actionBuffers)
11 {
12 // Extract continuous actions
13 float moveX = actionBuffers.ContinuousActions[0];
14 float moveZ = actionBuffers.ContinuousActions[1];
15 float rotate = actionBuffers.ContinuousActions[2];
16
17 // Apply actions to agent
18 ApplyMovement(moveX, moveZ, rotate);
19 }
20}1# Evaluation with metrics tracking
2class SoccerEvaluator:
3 def __init__(self, model_path):
4 self.session = ort.InferenceSession(model_path)
5 self.reset_metrics()
6
7 def reset_metrics(self):
8 self.goals_scored = 0
9 self.goals_conceded = 0
10 self.ball_touches = 0
11 self.episode_length = 0
12
13 def evaluate_episode(self, observations, actions, rewards):
14 # Run full episode evaluation
15 total_reward = sum(rewards)
16 win_rate = 1.0 if self.goals_scored > self.goals_conceded else 0.0
17
18 return {
19 'total_reward': total_reward,
20 'goals_scored': self.goals_scored,
21 'goals_conceded': self.goals_conceded,
22 'win_rate': win_rate,
23 'ball_touches': self.ball_touches
24 }1# Multi-episode evaluation
2def evaluate_model(model_path, num_episodes=100):
3 evaluator = SoccerEvaluator(model_path)
4 results = []
5
6 for episode in range(num_episodes):
7 # Run episode
8 episode_result = evaluator.run_episode()
9 results.append(episode_result)
10
11 # Aggregate results
12 avg_reward = np.mean([r['total_reward'] for r in results])
13 win_rate = np.mean([r['win_rate'] for r in results])
14 avg_goals = np.mean([r['goals_scored'] for r in results])
15
16 return {
17 'average_reward': avg_reward,
18 'win_rate': win_rate,
19 'average_goals_per_episode': avg_goals,
20 'total_episodes': num_episodes
21 }1# Custom training configuration
2from mlagents_envs.environment import UnityEnvironment
3from mlagents.trainers.settings import TrainerSettings
4
5# Environment setup
6env = UnityEnvironment(file_name="SoccerTwos")
7trainer_config = TrainerSettings(
8 trainer_type="ppo",
9 hyperparameters={
10 "batch_size": 2048,
11 "buffer_size": 20480,
12 "learning_rate": 3e-4,
13 "beta": 5e-4,
14 "epsilon": 0.2,
15 "lambd": 0.95,
16 "num_epoch": 3,
17 "learning_rate_schedule": "linear"
18 }
19)1@misc{ml_agents_soccer_twos_2025,
2 title={ML-Agents SoccerTwos: Multi-Agent Soccer AI},
3 author={Adilbai},
4 year={2025},
5 publisher={Hugging Face},
6 url={https://huggingface.co/Adilbai/ML-Agents-SoccerTwos},
7 note={Unity ML-Agents trained model for 2v2 soccer simulation}
8}multi-agent reinforcement-learning unity-ml-agents soccer cooperative-ai competitive-ai onnx game-ai emergent-behavior team-coordination