浏览代码
[cherry-pick] Integrate Group Manager to soccer/retrain with POCA (#5115) (#5121)
[cherry-pick] Integrate Group Manager to soccer/retrain with POCA (#5115) (#5121)
* Integrate Group Manager to soccer/retrain with POCA (#5115) * Add Soccer env to changelog Co-authored-by: andrewcoh <54679309+andrewcoh@users.noreply.github.com>/release_15_branch
GitHub
4 年前
当前提交
d2ee2e6f
共有 19 个文件被更改,包括 1509 次插入 和 1226 次删除
-
139Project/Assets/ML-Agents/Examples/Soccer/Prefabs/SoccerFieldTwos.prefab
-
236Project/Assets/ML-Agents/Examples/Soccer/Prefabs/StrikersVsGoalieField.prefab
-
57Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
-
12Project/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerBallController.cs
-
2com.unity.ml-agents/CHANGELOG.md
-
2docs/Learning-Environment-Design-Agents.md
-
6docs/Learning-Environment-Examples.md
-
2config/poca/SoccerTwos.yaml
-
4config/poca/StrikersVsGoalie.yaml
-
139Project/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerEnvController.cs
-
11Project/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerEnvController.cs.meta
-
1001Project/Assets/ML-Agents/Examples/Soccer/TFModels/SoccerTwos.onnx
-
14Project/Assets/ML-Agents/Examples/Soccer/TFModels/SoccerTwos.onnx.meta
-
13Project/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerFieldArea.cs.meta
-
85Project/Assets/ML-Agents/Examples/Soccer/Scripts/SoccerFieldArea.cs
-
11Project/Assets/ML-Agents/Examples/Soccer/TFModels/SoccerTwos.nn.meta
-
1001Project/Assets/ML-Agents/Examples/Soccer/TFModels/SoccerTwos.nn
-
0/config/poca/SoccerTwos.yaml
-
0/config/poca/StrikersVsGoalie.yaml
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using Unity.MLAgents; |
|||
using UnityEngine; |
|||
|
|||
public class SoccerEnvController : MonoBehaviour |
|||
{ |
|||
[System.Serializable] |
|||
public class PlayerInfo |
|||
{ |
|||
public AgentSoccer Agent; |
|||
[HideInInspector] |
|||
public Vector3 StartingPos; |
|||
[HideInInspector] |
|||
public Quaternion StartingRot; |
|||
[HideInInspector] |
|||
public Rigidbody Rb; |
|||
} |
|||
|
|||
|
|||
/// <summary>
|
|||
/// Max Academy steps before this platform resets
|
|||
/// </summary>
|
|||
/// <returns></returns>
|
|||
[Header("Max Environment Steps")] public int MaxEnvironmentSteps = 25000; |
|||
|
|||
/// <summary>
|
|||
/// The area bounds.
|
|||
/// </summary>
|
|||
|
|||
/// <summary>
|
|||
/// We will be changing the ground material based on success/failue
|
|||
/// </summary>
|
|||
|
|||
public GameObject ball; |
|||
[HideInInspector] |
|||
public Rigidbody ballRb; |
|||
Vector3 m_BallStartingPos; |
|||
|
|||
//List of Agents On Platform
|
|||
public List<PlayerInfo> AgentsList = new List<PlayerInfo>(); |
|||
|
|||
private SoccerSettings m_SoccerSettings; |
|||
|
|||
|
|||
private SimpleMultiAgentGroup m_BlueAgentGroup; |
|||
private SimpleMultiAgentGroup m_PurpleAgentGroup; |
|||
|
|||
private int m_ResetTimer; |
|||
|
|||
void Start() |
|||
{ |
|||
|
|||
m_SoccerSettings = FindObjectOfType<SoccerSettings>(); |
|||
// Initialize TeamManager
|
|||
m_BlueAgentGroup = new SimpleMultiAgentGroup(); |
|||
m_PurpleAgentGroup = new SimpleMultiAgentGroup(); |
|||
ballRb = ball.GetComponent<Rigidbody>(); |
|||
m_BallStartingPos = new Vector3(ball.transform.position.x, ball.transform.position.y, ball.transform.position.z); |
|||
foreach (var item in AgentsList) |
|||
{ |
|||
item.StartingPos = item.Agent.transform.position; |
|||
item.StartingRot = item.Agent.transform.rotation; |
|||
item.Rb = item.Agent.GetComponent<Rigidbody>(); |
|||
if (item.Agent.team == Team.Blue) |
|||
{ |
|||
m_BlueAgentGroup.RegisterAgent(item.Agent); |
|||
} |
|||
else |
|||
{ |
|||
m_PurpleAgentGroup.RegisterAgent(item.Agent); |
|||
} |
|||
} |
|||
ResetScene(); |
|||
} |
|||
|
|||
void FixedUpdate() |
|||
{ |
|||
m_ResetTimer += 1; |
|||
if (m_ResetTimer >= MaxEnvironmentSteps && MaxEnvironmentSteps > 0) |
|||
{ |
|||
m_BlueAgentGroup.GroupEpisodeInterrupted(); |
|||
m_PurpleAgentGroup.GroupEpisodeInterrupted(); |
|||
ResetScene(); |
|||
} |
|||
} |
|||
|
|||
|
|||
public void ResetBall() |
|||
{ |
|||
var randomPosX = Random.Range(-2.5f, 2.5f); |
|||
var randomPosZ = Random.Range(-2.5f, 2.5f); |
|||
|
|||
ball.transform.position = m_BallStartingPos + new Vector3(randomPosX, 0f, randomPosZ); ; |
|||
ballRb.velocity = Vector3.zero; |
|||
ballRb.angularVelocity = Vector3.zero; |
|||
|
|||
} |
|||
|
|||
public void GoalTouched(Team scoredTeam) |
|||
{ |
|||
if (scoredTeam == Team.Blue) |
|||
{ |
|||
m_BlueAgentGroup.AddGroupReward(1 - m_ResetTimer / MaxEnvironmentSteps); |
|||
m_PurpleAgentGroup.AddGroupReward(-1); |
|||
} |
|||
else |
|||
{ |
|||
m_PurpleAgentGroup.AddGroupReward(1 - m_ResetTimer / MaxEnvironmentSteps); |
|||
m_BlueAgentGroup.AddGroupReward(-1); |
|||
} |
|||
m_PurpleAgentGroup.EndGroupEpisode(); |
|||
m_BlueAgentGroup.EndGroupEpisode(); |
|||
ResetScene(); |
|||
|
|||
} |
|||
|
|||
|
|||
public void ResetScene() |
|||
{ |
|||
m_ResetTimer = 0; |
|||
|
|||
//Reset Agents
|
|||
foreach (var item in AgentsList) |
|||
{ |
|||
var randomPosX = Random.Range(-5f, 5f); |
|||
var newStartPos = item.Agent.initialPos + new Vector3(randomPosX, 0f, 0f); |
|||
var rot = item.Agent.rotSign * Random.Range(80.0f, 100.0f); |
|||
var newRot = Quaternion.Euler(0, rot, 0); |
|||
item.Agent.transform.SetPositionAndRotation(newStartPos, newRot); |
|||
|
|||
item.Rb.velocity = Vector3.zero; |
|||
item.Rb.angularVelocity = Vector3.zero; |
|||
} |
|||
|
|||
//Reset Ball
|
|||
ResetBall(); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 4e397bc3ae78c466a8d44400f5b68e38 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
1001
Project/Assets/ML-Agents/Examples/Soccer/TFModels/SoccerTwos.onnx
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: 8cd4584c2f2cb4c5fb51675d364e10ec |
|||
ScriptedImporter: |
|||
internalIDToNameTable: [] |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|||
script: {fileID: 11500000, guid: 683b6cb6d0a474744822c888b46772c9, type: 3} |
|||
optimizeModel: 1 |
|||
forceArbitraryBatchSize: 1 |
|||
treatErrorsAsWarnings: 0 |
|||
importMode: 1 |
|
|||
fileFormatVersion: 2 |
|||
guid: efd705d0a5b1e405eb1869b7cbe47dda |
|||
timeCreated: 1511497566 |
|||
licenseType: Free |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using Unity.MLAgents; |
|||
using UnityEngine; |
|||
using UnityEngine.Serialization; |
|||
|
|||
[System.Serializable] |
|||
public class PlayerState |
|||
{ |
|||
public int playerIndex; |
|||
[FormerlySerializedAs("agentRB")] |
|||
public Rigidbody agentRb; |
|||
public Vector3 startingPos; |
|||
public AgentSoccer agentScript; |
|||
public float ballPosReward; |
|||
} |
|||
|
|||
public class SoccerFieldArea : MonoBehaviour |
|||
{ |
|||
public GameObject ball; |
|||
[FormerlySerializedAs("ballRB")] |
|||
[HideInInspector] |
|||
public Rigidbody ballRb; |
|||
public GameObject ground; |
|||
public GameObject centerPitch; |
|||
SoccerBallController m_BallController; |
|||
public List<PlayerState> playerStates = new List<PlayerState>(); |
|||
[HideInInspector] |
|||
public Vector3 ballStartingPos; |
|||
public GameObject goalTextUI; |
|||
[HideInInspector] |
|||
public bool canResetBall; |
|||
|
|||
EnvironmentParameters m_ResetParams; |
|||
|
|||
void Awake() |
|||
{ |
|||
canResetBall = true; |
|||
if (goalTextUI) { goalTextUI.SetActive(false); } |
|||
ballRb = ball.GetComponent<Rigidbody>(); |
|||
m_BallController = ball.GetComponent<SoccerBallController>(); |
|||
m_BallController.area = this; |
|||
ballStartingPos = ball.transform.position; |
|||
|
|||
m_ResetParams = Academy.Instance.EnvironmentParameters; |
|||
} |
|||
|
|||
IEnumerator ShowGoalUI() |
|||
{ |
|||
if (goalTextUI) goalTextUI.SetActive(true); |
|||
yield return new WaitForSeconds(.25f); |
|||
if (goalTextUI) goalTextUI.SetActive(false); |
|||
} |
|||
|
|||
public void GoalTouched(AgentSoccer.Team scoredTeam) |
|||
{ |
|||
foreach (var ps in playerStates) |
|||
{ |
|||
if (ps.agentScript.team == scoredTeam) |
|||
{ |
|||
ps.agentScript.AddReward(1 + ps.agentScript.timePenalty); |
|||
} |
|||
else |
|||
{ |
|||
ps.agentScript.AddReward(-1); |
|||
} |
|||
ps.agentScript.EndEpisode(); //all agents need to be reset
|
|||
|
|||
if (goalTextUI) |
|||
{ |
|||
StartCoroutine(ShowGoalUI()); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public void ResetBall() |
|||
{ |
|||
ball.transform.position = ballStartingPos; |
|||
ballRb.velocity = Vector3.zero; |
|||
ballRb.angularVelocity = Vector3.zero; |
|||
|
|||
var ballScale = m_ResetParams.GetWithDefault("ball_scale", 0.015f); |
|||
ballRb.transform.localScale = new Vector3(ballScale, ballScale, ballScale); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b0a629580a0ab48a5a774f90ff1fb48b |
|||
ScriptedImporter: |
|||
fileIDToRecycleName: |
|||
11400000: main obj |
|||
11400002: model data |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|||
script: {fileID: 11500000, guid: 19ed1486aa27d4903b34839f37b8f69f, type: 3} |
1001
Project/Assets/ML-Agents/Examples/Soccer/TFModels/SoccerTwos.nn
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
撰写
预览
正在加载...
取消
保存
Reference in new issue