Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soccer refactor (release) #3331

Merged
merged 32 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b59fec2
removed roles/modified reward soccer
andrewcoh Jan 29, 2020
d082707
updated config
andrewcoh Jan 29, 2020
3dc2ef5
observation stacking in soccer
andrewcoh Jan 29, 2020
d634e39
Merge branch 'master' into develop-soccer-refactor
andrewcoh Jan 29, 2020
97637c5
removing net rule from tennis
andrewcoh Jan 29, 2020
7193f93
removed intermediate reward soccer
andrewcoh Jan 29, 2020
301e1b6
branched actions soccer
andrewcoh Jan 30, 2020
99fd879
add timestep penalty tennis
andrewcoh Jan 30, 2020
8f89b9c
increased drag soccer ball/soccer brain
andrewcoh Jan 30, 2020
f6bc5b1
cleaned commented code tennis
andrewcoh Jan 30, 2020
2453e3b
Merge branch 'master' into develop-soccer-refactor
andrewcoh Jan 30, 2020
7f407a6
added heuristic and more obs raycast to soccer
andrewcoh Jan 30, 2020
c907b0b
testing tennis 0.0 prob play self
andrewcoh Jan 30, 2020
cc47674
new soccer policy
andrewcoh Jan 31, 2020
d492497
revert changes to Tennis
andrewcoh Jan 31, 2020
b4066aa
Merge branch 'master' into develop-soccer-refactor
andrewcoh Jan 31, 2020
73dddf5
adding policy meta file
andrewcoh Jan 31, 2020
b8cd85e
TFModels meta
andrewcoh Jan 31, 2020
f561488
removed collision layers that are no longer used
andrewcoh Jan 31, 2020
755a46f
reduced kick power
andrewcoh Jan 31, 2020
ed0bbe6
increasing kickpower and drag
andrewcoh Feb 3, 2020
f9b3ed7
running soccer 50 million timesteps
andrewcoh Feb 4, 2020
18b9391
Merge branch 'master' into develop-soccer-refactor
andrewcoh Feb 4, 2020
9724bff
reduced lateral speed
andrewcoh Feb 4, 2020
5b99aaa
added ray perception backwards
andrewcoh Feb 4, 2020
fa54ddf
Merge branch 'master' into develop-soccer-refactor
andrewcoh Feb 4, 2020
193c4f7
updated soccer brain
andrewcoh Feb 5, 2020
0e2b785
fixed learning environment docs and updated soccer image
andrewcoh Feb 5, 2020
1761aa1
new tennis image
andrewcoh Feb 5, 2020
24a9117
x offset
andrewcoh Feb 5, 2020
20882fa
new Soccer brain
andrewcoh Feb 5, 2020
cc726b5
compare against enum
andrewcoh Feb 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,308 changes: 1,623 additions & 1,685 deletions Project/Assets/ML-Agents/Examples/Soccer/Prefabs/SoccerFieldTwos.prefab

Large diffs are not rendered by default.

171 changes: 79 additions & 92 deletions Project/Assets/ML-Agents/Examples/Soccer/Scripts/AgentSoccer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEngine;
using MLAgents;

Expand All @@ -12,17 +13,12 @@ public class AgentSoccer : Agent
// * opposing player
public enum Team
{
Purple,
Blue
}
public enum AgentRole
{
Striker,
Goalie
Blue = 0,
Purple = 1
}

[HideInInspector]
public Team team;
public AgentRole agentRole;
float m_KickPower;
int m_PlayerIndex;
public SoccerFieldArea area;
Expand All @@ -31,39 +27,23 @@ public enum AgentRole
public Rigidbody agentRb;
SoccerSettings m_SoccerSettings;
Renderer m_AgentRenderer;
BehaviorParameters m_BP;
Vector3 m_Transform;

public void ChooseRandomTeam()
public override void InitializeAgent()
{
team = (Team)Random.Range(0, 2);
if (team == Team.Purple)
base.InitializeAgent();
m_BP = gameObject.GetComponent<BehaviorParameters>();
if (m_BP.m_TeamID == (int)Team.Blue)
{
JoinPurpleTeam(agentRole);
team = Team.Blue;
m_Transform = new Vector3(transform.position.x - 4f, .5f, transform.position.z);
}
else
{
JoinBlueTeam(agentRole);
team = Team.Purple;
m_Transform = new Vector3(transform.position.x + 4f, .5f, transform.position.z);
}
}

public void JoinPurpleTeam(AgentRole role)
{
agentRole = role;
team = Team.Purple;
m_AgentRenderer.material = m_SoccerSettings.purpleMaterial;
tag = "purpleAgent";
}

public void JoinBlueTeam(AgentRole role)
{
agentRole = role;
team = Team.Blue;
m_AgentRenderer.material = m_SoccerSettings.blueMaterial;
tag = "blueAgent";
}

public override void InitializeAgent()
{
base.InitializeAgent();
m_AgentRenderer = GetComponentInChildren<Renderer>();
m_SoccerSettings = FindObjectOfType<SoccerSettings>();
agentRb = GetComponent<Rigidbody>();
Expand All @@ -87,53 +67,43 @@ public void MoveAgent(float[] act)

var action = Mathf.FloorToInt(act[0]);

// Goalies and Strikers have slightly different action spaces.
if (agentRole == AgentRole.Goalie)
m_KickPower = 0f;

var forwardAxis = (int)act[0];
var rightAxis = (int)act[1];
var rotateAxis = (int)act[2];

switch (forwardAxis)
{
m_KickPower = 0f;
switch (action)
{
case 1:
dirToGo = transform.forward * 1f;
m_KickPower = 1f;
break;
case 2:
dirToGo = transform.forward * -1f;
break;
case 4:
dirToGo = transform.right * -1f;
break;
case 3:
dirToGo = transform.right * 1f;
break;
}
case 1:
dirToGo = transform.forward * 1f;
m_KickPower = 1f;
break;
case 2:
dirToGo = transform.forward * -1f;
break;
}
else

switch (rightAxis)
{
m_KickPower = 0f;
switch (action)
{
case 1:
dirToGo = transform.forward * 1f;
m_KickPower = 1f;
break;
case 2:
dirToGo = transform.forward * -1f;
break;
case 3:
rotateDir = transform.up * 1f;
break;
case 4:
rotateDir = transform.up * -1f;
break;
case 5:
dirToGo = transform.right * -0.75f;
break;
case 6:
dirToGo = transform.right * 0.75f;
break;
}
case 1:
dirToGo = transform.right * 0.3f;
break;
case 2:
dirToGo = transform.right * -0.3f;
break;
}

switch (rotateAxis)
{
case 1:
rotateDir = transform.up * -1f;
break;
case 2:
rotateDir = transform.up * 1f;
break;
}

transform.Rotate(rotateDir, Time.deltaTime * 100f);
agentRb.AddForce(dirToGo * m_SoccerSettings.agentRunSpeed,
ForceMode.VelocityChange);
Expand All @@ -142,18 +112,42 @@ public void MoveAgent(float[] act)
public override void AgentAction(float[] vectorAction)
{
// Existential penalty for strikers.
if (agentRole == AgentRole.Striker)
AddReward(-1f / 3000f);
MoveAgent(vectorAction);
}

public override float[] Heuristic()
{
var action = new float[3];
//forward
if (Input.GetKey(KeyCode.W))
{
AddReward(-1f / 3000f);
action[0] = 1f;
}
// Existential bonus for goalies.
if (agentRole == AgentRole.Goalie)
if (Input.GetKey(KeyCode.S))
{
AddReward(1f / 3000f);
action[0] = 2f;
}
MoveAgent(vectorAction);
//rotate
if (Input.GetKey(KeyCode.A))
{
action[2] = 1f;
}
if (Input.GetKey(KeyCode.D))
{
action[2] = 2f;
}
//right
if (Input.GetKey(KeyCode.E))
{
action[1] = 1f;
}
if (Input.GetKey(KeyCode.Q))
{
action[1] = 2f;
}
return action;
}

/// <summary>
/// Used to provide a "kick" to the ball.
/// </summary>
Expand All @@ -170,22 +164,15 @@ void OnCollisionEnter(Collision c)

public override void AgentReset()
{
if (m_SoccerSettings.randomizePlayersTeamForTraining)
{
ChooseRandomTeam();
}

if (team == Team.Purple)
{
JoinPurpleTeam(agentRole);
transform.rotation = Quaternion.Euler(0f, -90f, 0f);
}
else
{
JoinBlueTeam(agentRole);
transform.rotation = Quaternion.Euler(0f, 90f, 0f);
}
transform.position = area.GetRandomSpawnPos(agentRole, team);
transform.position = m_Transform;
agentRb.velocity = Vector3.zero;
agentRb.angularVelocity = Vector3.zero;
SetResetParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ public class SoccerBallController : MonoBehaviour
{
[HideInInspector]
public SoccerFieldArea area;
public AgentSoccer lastTouchedBy; //who was the last to touch the ball
public string agentTag; //will be used to check if collided with a agent
public string purpleGoalTag; //will be used to check if collided with red goal
public string purpleGoalTag; //will be used to check if collided with purple goal
public string blueGoalTag; //will be used to check if collided with blue goal

void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag(purpleGoalTag)) //ball touched red goal
if (col.gameObject.CompareTag(purpleGoalTag)) //ball touched purple goal
{
area.GoalTouched(AgentSoccer.Team.Blue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ public class SoccerFieldArea : MonoBehaviour

SoccerSettings m_SoccerSettings;

public IEnumerator GoalScoredSwapGroundMaterial(Material mat, float time)
{
m_GroundRenderer.material = mat;
yield return new WaitForSeconds(time);
m_GroundRenderer.material = m_GroundMaterial;
}

void Awake()
{
m_SoccerSettings = FindObjectOfType<SoccerSettings>();
Expand All @@ -62,94 +55,32 @@ IEnumerator ShowGoalUI()
if (goalTextUI) goalTextUI.SetActive(false);
}

public void AllPlayersDone(float reward)
{
foreach (var ps in playerStates)
{
if (ps.agentScript.gameObject.activeInHierarchy)
{
if (reward != 0)
{
ps.agentScript.AddReward(reward);
}
ps.agentScript.Done();
}
}
}

public void GoalTouched(AgentSoccer.Team scoredTeam)
{
foreach (var ps in playerStates)
{
if (ps.agentScript.team == scoredTeam)
{
RewardOrPunishPlayer(ps, m_SoccerSettings.strikerReward, m_SoccerSettings.goalieReward);
ps.agentScript.AddReward(1);
}
else
{
RewardOrPunishPlayer(ps, m_SoccerSettings.strikerPunish, m_SoccerSettings.goaliePunish);
}
if (m_SoccerSettings.randomizePlayersTeamForTraining)
{
ps.agentScript.ChooseRandomTeam();
ps.agentScript.AddReward(-1);
}
ps.agentScript.Done(); //all agents need to be reset

if (scoredTeam == AgentSoccer.Team.Purple)
{
StartCoroutine(GoalScoredSwapGroundMaterial(m_SoccerSettings.purpleMaterial, 1));
}
else
{
StartCoroutine(GoalScoredSwapGroundMaterial(m_SoccerSettings.blueMaterial, 1));
}
if (goalTextUI)
{
StartCoroutine(ShowGoalUI());
}
}
}

public void RewardOrPunishPlayer(PlayerState ps, float striker, float goalie)
{
if (ps.agentScript.agentRole == AgentSoccer.AgentRole.Striker)
{
ps.agentScript.AddReward(striker);
}
if (ps.agentScript.agentRole == AgentSoccer.AgentRole.Goalie)
{
ps.agentScript.AddReward(goalie);
}
ps.agentScript.Done(); //all agents need to be reset
}

public Vector3 GetRandomSpawnPos(AgentSoccer.AgentRole role, AgentSoccer.Team team)
{
var xOffset = 0f;
if (role == AgentSoccer.AgentRole.Goalie)
{
xOffset = 13f;
}
if (role == AgentSoccer.AgentRole.Striker)
{
xOffset = 7f;
}
if (team == AgentSoccer.Team.Blue)
{
xOffset = xOffset * -1f;
}
var randomSpawnPos = ground.transform.position +
new Vector3(xOffset, 0f, 0f)
+ (Random.insideUnitSphere * 2);
randomSpawnPos.y = ground.transform.position.y + 2;
return randomSpawnPos;
}

public Vector3 GetBallSpawnPosition()
{
var randomSpawnPos = ground.transform.position +
new Vector3(0f, 0f, 0f)
+ (Random.insideUnitSphere * 2);
randomSpawnPos.y = ground.transform.position.y + 2;
new Vector3(0f, 0f, 0f);
randomSpawnPos.y = ground.transform.position.y + .5f;
return randomSpawnPos;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,5 @@ public class SoccerSettings : MonoBehaviour
public Material purpleMaterial;
public Material blueMaterial;
public bool randomizePlayersTeamForTraining = true;

public float agentRunSpeed;

public float strikerPunish; //if opponents scores, the striker gets this neg reward (-1)
public float strikerReward; //if team scores a goal they get a reward (+1)
public float goaliePunish; //if opponents score, goalie gets this neg reward (-1)
public float goalieReward; //if team scores, goalie gets this reward (currently 0...no reward. can play with this later)
}
8 changes: 8 additions & 0 deletions Project/Assets/ML-Agents/Examples/Soccer/TFModels.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Loading