|
|
|
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.SceneManagement; |
|
|
|
using Unity.MLAgents; |
|
|
|
using UnityEngine.Serialization; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// An example of how to use ML-Agents without inheriting from the Agent class.
|
|
|
|
|
|
|
{ |
|
|
|
public float timeBetweenDecisionsAtInference; |
|
|
|
float m_TimeSinceDecision; |
|
|
|
[FormerlySerializedAs("m_Position")] |
|
|
|
public int m_Position; |
|
|
|
public int position; |
|
|
|
const int k_SmallGoalPosition = 7; |
|
|
|
const int k_LargeGoalPosition = 17; |
|
|
|
public GameObject largeGoal; |
|
|
|
|
|
|
public void OnEnable() |
|
|
|
{ |
|
|
|
m_Agent = GetComponent<Agent>(); |
|
|
|
m_Position = 10; |
|
|
|
transform.position = new Vector3(m_Position - 10f, 0f, 0f); |
|
|
|
position = 10; |
|
|
|
transform.position = new Vector3(position - 10f, 0f, 0f); |
|
|
|
smallGoal.transform.position = new Vector3(k_SmallGoalPosition - 10f, 0f, 0f); |
|
|
|
largeGoal.transform.position = new Vector3(k_LargeGoalPosition - 10f, 0f, 0f); |
|
|
|
} |
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
m_Position += direction; |
|
|
|
if (m_Position < k_MinPosition) { m_Position = k_MinPosition; } |
|
|
|
if (m_Position > k_MaxPosition) { m_Position = k_MaxPosition; } |
|
|
|
position += direction; |
|
|
|
if (position < k_MinPosition) { position = k_MinPosition; } |
|
|
|
if (position > k_MaxPosition) { position = k_MaxPosition; } |
|
|
|
gameObject.transform.position = new Vector3(m_Position - 10f, 0f, 0f); |
|
|
|
gameObject.transform.position = new Vector3(position - 10f, 0f, 0f); |
|
|
|
if (m_Position == k_SmallGoalPosition) |
|
|
|
if (position == k_SmallGoalPosition) |
|
|
|
{ |
|
|
|
m_Agent.AddReward(0.1f); |
|
|
|
m_Agent.EndEpisode(); |
|
|
|
|
|
|
if (m_Position == k_LargeGoalPosition) |
|
|
|
if (position == k_LargeGoalPosition) |
|
|
|
{ |
|
|
|
m_Agent.AddReward(1f); |
|
|
|
m_Agent.EndEpisode(); |
|
|
|