1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| // l'initialisation.
override protected void Start () {
base.Start();
MN = GetComponent<MonoNeuralNet>();
H3 = GetComponent<Hero3Owner>();
Tasks = new List<string>();
Tasks.Add("DodgeTask");
Tasks.Add("LauncherTask");
Tasks.Add("Hero3AttackChargeTask");
Tasks.Add("Hero3AttackJump");
Tasks.Add("Hero3Attack1");
Tasks.Add("Hero3Attack2");
Tasks.Add("DashTask");
Tasks.Add("Hero3MagicTask");
Tasks.Add("Hero3AuraTask");
Tasks.Add("MoveTask");
Tasks.Add("JumpTask");
}
// les inputs pour le neural network (normaliser)
private float[] GetInputs()
{
return new float[] { (this.transform.position.x + 5000f) / 10000f, (Target.transform.position.x + 5000f) / 10000f, (HH.transform.localScale.x + 1f) / 2f,
(Target.transform.localScale.x + 1f) / 2f, GetComponent<Stats>().CurLife / GetComponent<Stats>().MaxLife,
Target.GetComponent<Stats>().CurLife / Target.GetComponent<Stats>().MaxLife, GetComponent<Stats>().CurrentEnergy / GetComponent<Stats>().MaxEnergy,
Target.GetComponent<Stats>().CurrentEnergy / Target.GetComponent<Stats>().MaxEnergy };
}
// la fonction d'update
override protected void Update () {
base.Update();
if (HH.GetComponent<Stats>().IsDead()) // si je suis mort
{
float[] outtt = new float[11];
for (int i = 0; i < 11; ++i)
{
if (i == LastOut)
outtt[i] = -1; // on met une reward negative
else
outtt[i] = 0;
}
for (int j = 0; j < 10000; ++j)
MN.Train(GetInputs(), outtt);
return;
}
if (Target.GetComponent<Stats>().IsDead()) // si mon adversaire est mort
{
float[] outtt = new float[11];
for (int i = 0; i < 11; ++i)
{
if (i == LastOut)
outtt[i] = 1; // on met une reward positive
else
outtt[i] = 0;
}
for (int j = 0; j < 10000; ++j)
MN.Train(GetInputs(), outtt);
return;
}
float[] ret = null;
MN.Calculate(GetInputs(), out ret);
int maxaction = 0;
float maxval = - 100000;
for (int i = 0; i < 11; ++i)
{
if (ret[i] > maxval)
{
maxval = ret[i];
maxaction = i;
}
}
LastOut = maxaction;
string keytask = Tasks[maxaction];
Debug.Log(keytask);
ExecTask(keytask);
} |
Partager