V2.5 Change reward function
Add chart Toggle to turn on reward chart real time in game. Add enemy num changer, now can change enemy num in game. Change facing to enemy reward function, while facing center line far from enemy will not get reward any more.
This commit is contained in:
@@ -29,6 +29,7 @@ public class AgentWithGun : Agent
|
||||
public GameObject SceneBlockContainerObj;
|
||||
public GameObject EnvironmentUIControlObj;
|
||||
public GameObject TargetControllerObj;
|
||||
public GameObject HUDObj;
|
||||
public Camera thisCam;
|
||||
|
||||
[Header("GetAxis() Simulate")]
|
||||
@@ -52,7 +53,6 @@ public class AgentWithGun : Agent
|
||||
// environment
|
||||
private int shoot = 0;
|
||||
private float lastShootTime = 0.0f;
|
||||
private int nowEnemyNum = 0;
|
||||
private int enemyKillCount = 0;
|
||||
private Vector3 killEnemyPosition;
|
||||
private int step = 0;
|
||||
@@ -60,6 +60,7 @@ public class AgentWithGun : Agent
|
||||
public bool defaultTPCamera = true;
|
||||
private bool gunReadyToggle = true;
|
||||
private string myTag = "";
|
||||
private float lastEnemyFacingDistance = 0f; // record last enemy facing minimum distance
|
||||
// scripts
|
||||
private RaySensors rayScript;
|
||||
private CharacterController PlayerController;
|
||||
@@ -68,6 +69,7 @@ public class AgentWithGun : Agent
|
||||
private SceneBlockContainer blockContainer;
|
||||
private EnemyContainer eneContainer;
|
||||
private TargetController targetCon;
|
||||
private HUDController hudController;
|
||||
|
||||
[System.NonSerialized] public int finishedState;
|
||||
|
||||
@@ -78,13 +80,14 @@ public class AgentWithGun : Agent
|
||||
blockContainer = SceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
||||
EnvUICon = EnvironmentUIControlObj.GetComponent<EnvironmentUIControl>();
|
||||
targetCon = TargetControllerObj.GetComponent<TargetController>();
|
||||
hudController = HUDObj.GetComponent<HUDController>();
|
||||
rayScript = GetComponent<RaySensors>();
|
||||
PlayerController = this.transform.GetComponent<CharacterController>();
|
||||
// Environment parameters
|
||||
lockMouse = paramContainer.lockMouse;
|
||||
Damage = paramContainer.Damage;
|
||||
fireRate = paramContainer.fireRate;
|
||||
enemyNum = paramContainer.enemyNum;
|
||||
enemyNum = hudController.enemyNum;
|
||||
lockCameraX = paramContainer.lockCameraX;
|
||||
lockCameraY = paramContainer.lockCameraY;
|
||||
//initialize remainTime
|
||||
@@ -291,6 +294,7 @@ public class AgentWithGun : Agent
|
||||
{
|
||||
float thisReward = 0;
|
||||
bool isFacingtoEnemy = false;
|
||||
float enemyFacingDistance = 0f;
|
||||
Ray ray = thisCam.ScreenPointToRay(new Vector3(thisCam.pixelWidth / 2, thisCam.pixelHeight / 2, 0));
|
||||
if (targetCon.targetTypeInt == (int)TargetController.Targets.Free)
|
||||
{
|
||||
@@ -320,10 +324,21 @@ public class AgentWithGun : Agent
|
||||
// Debug.DrawRay(transform.position, projection, Color.blue);
|
||||
// Debug.DrawRay(thisEnemy.transform.position, verticalToRay, Color.magenta);
|
||||
}
|
||||
enemyFacingDistance = projectionDis.Min();
|
||||
if(enemyFacingDistance <= lastEnemyFacingDistance)
|
||||
{
|
||||
// closing to enemy
|
||||
thisReward = 1 / MathF.Sqrt(paramContainer.facingInviewEnemyDisCOEF * enemyFacingDistance + 0.00001f);
|
||||
}
|
||||
else
|
||||
{
|
||||
thisReward = 0;
|
||||
}
|
||||
// enemy in view Reward
|
||||
thisReward = 1 / MathF.Sqrt(paramContainer.facingInviewEnemyDisCOEF* projectionDis.Min()+0.00001f);
|
||||
lastEnemyFacingDistance = enemyFacingDistance;
|
||||
if (thisReward >= paramContainer.facingReward) thisReward = paramContainer.facingReward; // limit
|
||||
Debug.Log("ninimum = " + thisReward);
|
||||
if (thisReward <= -paramContainer.facingReward) thisReward = -paramContainer.facingReward; // limit
|
||||
// Debug.Log("ninimum = " + thisReward);
|
||||
}
|
||||
}
|
||||
else if(targetCon.targetTypeInt == (int)TargetController.Targets.Attack)
|
||||
@@ -343,41 +358,6 @@ public class AgentWithGun : Agent
|
||||
}
|
||||
return thisReward;
|
||||
}
|
||||
|
||||
// getEnemyNum 获取现场除了自己以外的敌人数量
|
||||
int getEnemyNum()
|
||||
{
|
||||
int enemyNum = 0;
|
||||
GameObject[] EnemyGameObjs;
|
||||
EnemyGameObjs = GameObject.FindGameObjectsWithTag("Enemy");
|
||||
//遍历所有Enemy
|
||||
foreach (GameObject EnemyObj in EnemyGameObjs)
|
||||
{
|
||||
Vector3 thisEnemyPosition = EnemyObj.transform.localPosition;
|
||||
Vector3 thisEnemyScale = EnemyObj.transform.localScale;
|
||||
Vector3 MyselfPosition = transform.localPosition;
|
||||
|
||||
//探测到Agent为自己时的处理
|
||||
if (thisEnemyPosition == MyselfPosition)
|
||||
{
|
||||
//Debug.Log("OH It's me");
|
||||
}
|
||||
else
|
||||
{
|
||||
enemyNum += 1;
|
||||
}
|
||||
}
|
||||
return enemyNum;
|
||||
}
|
||||
|
||||
// enemyNumDiff 获取与上一把相比敌人数量的区别
|
||||
int enemyNumDiff()
|
||||
{
|
||||
int diff = 0;
|
||||
int nowEnemyNum = getEnemyNum();
|
||||
diff = enemyNum - nowEnemyNum;
|
||||
return diff;
|
||||
}
|
||||
|
||||
// ------------Reward--------------
|
||||
// rewardCalculate 计算本动作的Reward
|
||||
@@ -433,9 +413,8 @@ public class AgentWithGun : Agent
|
||||
//thisAgentObj.name = thisAgentObj.GetInstanceID().ToString();
|
||||
targetCon.rollNewScene();
|
||||
paramContainer.resetTimeBonusReward();
|
||||
nowEnemyNum = getEnemyNum(); // Reset Enemy number
|
||||
// give default Reward to Reward value will be used.
|
||||
if (paramContainer.chartOn)
|
||||
if (hudController.chartOn)
|
||||
{
|
||||
EnvUICon.initChart();
|
||||
}
|
||||
@@ -491,10 +470,14 @@ public class AgentWithGun : Agent
|
||||
float endReward = 0f;
|
||||
(finishedState, sceneReward, endReward) = targetCon.checkOverAndRewards();
|
||||
float thisRoundReward = rewardCalculate(sceneReward+ endReward,Mouse_X);
|
||||
if (paramContainer.chartOn)
|
||||
if (hudController.chartOn)
|
||||
{
|
||||
EnvUICon.updateChart(thisRoundReward);
|
||||
}
|
||||
else
|
||||
{
|
||||
EnvUICon.removeChart();
|
||||
}
|
||||
//Debug.Log("reward = " + thisRoundReward);
|
||||
if (finishedState != (int)TargetController.EndType.Running)
|
||||
{
|
||||
|
||||
@@ -12,6 +12,7 @@ public class EnvironmentUIControl : MonoBehaviour
|
||||
public GameObject ParameterContainerObj;
|
||||
public GameObject GroundCanvasObj;
|
||||
public GameObject chartObj;
|
||||
public GameObject HUDObj;
|
||||
public TextMeshProUGUI remainTimeText;
|
||||
public TextMeshProUGUI targetTypeText;
|
||||
public TextMeshProUGUI winLoseText;
|
||||
@@ -21,6 +22,7 @@ public class EnvironmentUIControl : MonoBehaviour
|
||||
private LineChart realTimeRewardChart = null;
|
||||
private TargetController targetController;
|
||||
private ParameterContainer paramContainer;
|
||||
private HUDController hudController;
|
||||
private Image gaugeImg;
|
||||
private float overTime = 0f;
|
||||
private int step = 0;
|
||||
@@ -32,6 +34,7 @@ public class EnvironmentUIControl : MonoBehaviour
|
||||
targetController = TargetControllerObj.GetComponent<TargetController>();
|
||||
paramContainer = ParameterContainerObj.GetComponent<ParameterContainer>();
|
||||
gaugeImg = gaugeImgObj.GetComponent<Image>();
|
||||
hudController = HUDObj.GetComponent<HUDController>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@@ -51,6 +54,10 @@ public class EnvironmentUIControl : MonoBehaviour
|
||||
}
|
||||
public void updateChart(float reward)
|
||||
{
|
||||
if (hudController.chartOn && realTimeRewardChart == null)
|
||||
{
|
||||
initChart();
|
||||
}
|
||||
step += 1;
|
||||
realTimeRewardChart.AddXAxisData(Convert.ToString(step));
|
||||
realTimeRewardChart.AddData(0, reward);
|
||||
@@ -58,7 +65,7 @@ public class EnvironmentUIControl : MonoBehaviour
|
||||
|
||||
public void initChart()
|
||||
{
|
||||
if (paramContainer.chartOn && realTimeRewardChart == null)
|
||||
if (hudController.chartOn && realTimeRewardChart == null)
|
||||
{
|
||||
Vector3 chartPos = new Vector3(-210f, 90f, 0f) * GroundCanvasObj.transform.localScale.x;
|
||||
realTimeRewardChart = chartObj.AddComponent<LineChart>();
|
||||
@@ -68,6 +75,13 @@ public class EnvironmentUIControl : MonoBehaviour
|
||||
realTimeRewardChart.AddSerie<Line>("Rewards");
|
||||
}
|
||||
|
||||
public void removeChart()
|
||||
{
|
||||
|
||||
GameObject.Destroy(realTimeRewardChart);
|
||||
realTimeRewardChart = null;
|
||||
}
|
||||
|
||||
// show result in UI
|
||||
public void showResult(int resultState)
|
||||
{
|
||||
|
||||
@@ -17,11 +17,9 @@ public class ParameterContainer : MonoBehaviour
|
||||
public bool lockMouse = false;
|
||||
public float Damage = 50; // damage to enemy
|
||||
public float fireRate = 0.5f;
|
||||
public int enemyNum = 3;
|
||||
public int timeLimit = 30;
|
||||
public bool lockCameraX = false;
|
||||
public bool lockCameraY = true;
|
||||
public bool chartOn = false;
|
||||
public bool spawnAgentInAllMap = false;
|
||||
public int spinRecordMax = 20;
|
||||
public float spinPenaltyThreshold = 50;
|
||||
|
||||
@@ -9,6 +9,7 @@ public class TargetController : MonoBehaviour
|
||||
{
|
||||
public GameObject EnvironmentObj;
|
||||
public GameObject AgentObj;
|
||||
public GameObject HUDObj;
|
||||
public GameObject SceneBlockContainerObj;
|
||||
public GameObject EnemyContainerObj;
|
||||
public GameObject ParameterContainerObj;
|
||||
@@ -61,6 +62,7 @@ public class TargetController : MonoBehaviour
|
||||
private ParameterContainer paramCon;
|
||||
private CharacterController agentCharaCon;
|
||||
private WorldUIController worldUICon;
|
||||
private HUDController hudCon;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
@@ -81,6 +83,7 @@ public class TargetController : MonoBehaviour
|
||||
agentCharaCon = AgentObj.GetComponent<CharacterController>();
|
||||
paramCon = ParameterContainerObj.GetComponent<ParameterContainer>();
|
||||
worldUICon = WorldUIObj.GetComponent<WorldUIController>();
|
||||
hudCon = HUDObj.GetComponent<HUDController>();
|
||||
freeProb = 1 - attackProb - gotoProb - defenceProb;
|
||||
targetNum = (int)Targets.Num;
|
||||
if (freeProb < 0)
|
||||
@@ -128,7 +131,7 @@ public class TargetController : MonoBehaviour
|
||||
blockCont.destroyBlock();
|
||||
blockCont.createNewBlock(targetTypeInt, randBlockType, targetPosition, group1Tag, group2Tag);
|
||||
enemyCont.destroyAllEnemys();
|
||||
enemyCont.randomInitEnemysExcept(paramCon.enemyNum, targetPosition, sceneSize);
|
||||
enemyCont.randomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize);
|
||||
blockCont.thisBlock.initBlock(EnvironmentObj);
|
||||
// set startDistance
|
||||
firstRewardFlag = true;
|
||||
@@ -150,7 +153,7 @@ public class TargetController : MonoBehaviour
|
||||
blockCont.destroyBlock();
|
||||
blockCont.createNewBlock(targetTypeInt, randBlockType, targetPosition, group1Tag, group2Tag);
|
||||
enemyCont.destroyAllEnemys();
|
||||
enemyCont.randomInitEnemysExcept(paramCon.enemyNum, targetPosition, sceneSize);
|
||||
enemyCont.randomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize);
|
||||
blockCont.thisBlock.initBlock(EnvironmentObj);
|
||||
// set startDistance
|
||||
firstRewardFlag = true;
|
||||
@@ -173,7 +176,7 @@ public class TargetController : MonoBehaviour
|
||||
blockCont.destroyBlock();
|
||||
blockCont.createNewBlock(targetTypeInt, randBlockType, targetPosition, group1Tag, group2Tag);
|
||||
enemyCont.destroyAllEnemys();
|
||||
enemyCont.randomInitEnemysExcept(paramCon.enemyNum, targetPosition, sceneSize);
|
||||
enemyCont.randomInitEnemysExcept(hudCon.enemyNum, targetPosition, sceneSize);
|
||||
blockCont.thisBlock.initBlock(EnvironmentObj);
|
||||
// set startDistance
|
||||
firstRewardFlag = true;
|
||||
@@ -183,7 +186,7 @@ public class TargetController : MonoBehaviour
|
||||
Debug.Log("Free");
|
||||
targetTypeInt = (int)Targets.Free;
|
||||
enemyCont.destroyAllEnemys();
|
||||
enemyCont.randomInitEnemys(paramCon.enemyNum);
|
||||
enemyCont.randomInitEnemys(hudCon.enemyNum);
|
||||
moveAgentToSpwanArea();
|
||||
blockCont.destroyBlock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user