浏览代码

add option. fix EndEpisode bug. Fix reset m_reward bug in agent

/develop/cc-teammanager
Ruo-Ping Dong 4 年前
当前提交
0a2b5c5f
共有 4 个文件被更改,包括 58 次插入32 次删除
  1. 4
      Project/Assets/ML-Agents/Examples/PushBlock/Prefabs/2v3ZombiePushBlockCollabArea.prefab
  2. 13
      Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockTeamManager.cs
  3. 72
      Project/Assets/ML-Agents/Examples/PushBlock/Scripts/ZombiePushBlockDeathEnvController.cs
  4. 1
      com.unity.ml-agents/Runtime/Agent.cs

4
Project/Assets/ML-Agents/Examples/PushBlock/Prefabs/2v3ZombiePushBlockCollabArea.prefab


m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4721449031078373098}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:

UseRandomAgentPosition: 1
UseRandomBlockRotation: 1
UseRandomBlockPosition: 1
UseTeamManager: 0
UseTeamReward: 0
--- !u!1 &4721473865174714786
GameObject:
m_ObjectHideFlags: 0

13
Project/Assets/ML-Agents/Examples/PushBlock/Scripts/PushBlockTeamManager.cs


foreach (var doneAgent in m_AgentDoneState.Keys)
{
doneAgent.SendDoneToTrainer();
m_AgentDoneState[doneAgent] = false;
}
}
public void ResetAgent(Agent agent)
{
if (!m_AgentDoneState.ContainsKey(agent))
{
RegisterAgent(agent);
}
else
{
m_AgentDoneState[agent] = false;
}
}

72
Project/Assets/ML-Agents/Examples/PushBlock/Scripts/ZombiePushBlockDeathEnvController.cs


using System.Collections;
using System.Collections.Generic;
using Unity.MLAgents;
using Unity.MLAgents.Extensions.Teams;
using UnityEngine;
public class ZombiePushBlockDeathEnvController : MonoBehaviour

public bool UseRandomAgentPosition = true;
public bool UseRandomBlockRotation = true;
public bool UseRandomBlockPosition = true;
public bool UseTeamManager = true;
public bool UseTeamReward = true;
private PushBlockTeamManager m_TeamManager;
private BaseTeamManager m_TeamManager;
void Start()
{

item.Rb = item.T.GetComponent<Rigidbody>();
}
// Initialize TeamManager
m_TeamManager = new PushBlockTeamManager();
if (UseTeamManager)
{
if (UseTeamReward)
{
m_TeamManager = new PushBlockTeamManager();
}
else
{
m_TeamManager = new BaseTeamManager();
}
}
foreach (var item in AgentsList)
{
item.StartingPos = item.Agent.transform.position;

//Kill/disable an agent
public void KillAgent(Collision col, Transform t)
{
print($"Zombie {t.gameObject.GetInstanceID()} ate Agent {col.gameObject.GetInstanceID()}");
// print($"Zombie {t.gameObject.GetInstanceID()} ate Agent {col.gameObject.GetInstanceID()}");
//End Episode
foreach (var item in AgentsList)
{
if (!item.Agent)
{
return;
}
// item.Agent.EndEpisode();
}
//Disable killed Agent
foreach (var item in AgentsList)

// col.gameObject.SetActive(false);
//Give Agent Rewards
// foreach (var item in AgentsList)
// {
// if (item.Agent.gameObject.activeInHierarchy)
// {
// print($"{item.Agent.name} scored");
// item.Agent.AddReward(score);
// }
// }
m_TeamManager.AddTeamReward(score);
if (UseTeamManager && UseTeamReward)
{
m_TeamManager.AddTeamReward(score);
}
else
{
foreach (var item in AgentsList)
{
if (item.Agent.gameObject.activeInHierarchy)
{
// print($"{item.Agent.name} scored");
item.Agent.AddReward(score);
}
}
}
// Swap ground material for a bit to indicate we scored.
StartCoroutine(GoalScoredSwapGroundMaterial(m_PushBlockSettings.goalScoredMaterial, 0.5f));

public void ZombieTouchedBlock()
{
//Give Agent Rewards
// foreach (var item in AgentsList)
// {
// item.Agent.AddReward(-1);
// }
m_TeamManager.AddTeamReward(-1);
if (UseTeamManager && UseTeamReward)
{
m_TeamManager.AddTeamReward(-1);
}
else
{
foreach (var item in AgentsList)
{
item.Agent.AddReward(-1);
}
}
// Swap ground material for a bit to indicate we scored.
StartCoroutine(GoalScoredSwapGroundMaterial(m_PushBlockSettings.failMaterial, 0.5f));
ResetScene();

item.Rb.velocity = Vector3.zero;
item.Rb.angularVelocity = Vector3.zero;
item.Agent.gameObject.SetActive(true);
m_TeamManager.ResetAgent(item.Agent);
}
//Reset Blocks

1
com.unity.ml-agents/Runtime/Agent.cs


// We request a decision so Python knows the Agent is done immediately
m_Brain?.RequestDecision(m_Info, sensors);
ResetSensors();
m_Reward = 0f;
}
/// <summary>

正在加载...
取消
保存