public static AI_Config LoadIntelligence(string fName) { string data; // using (StreamReader sR = new StreamReader(fName)) { data = sR.ReadToEnd(); } AI_Config conf = new AI_Config(fName, data); return(conf); }
public static void ConvertFightWeights(AI_Config conf, StateData state, out float[] o, out float[] hL, out float[][] wHL, out float[][] wO) { // get a ref to how many inputs will we be using for the attack output network int inpCount = state.attState.Length; // ref to number of output nodes for attack network int numOutputNodes = 3; int nodePer1 = conf.attNodePer1; // ref to output nodes o = new float[numOutputNodes]; // ref to hidden layer nodes hL = new float[numOutputNodes * nodePer1]; // ref to hidden layer node weights wHL = new float[numOutputNodes * nodePer1][]; // ref to output layer weights wO = new float[numOutputNodes][]; // ref to the when the hidden layer weights end int lastHLW = numOutputNodes * nodePer1 * inpCount; // init our hidden layer nodes wieghts for (int i = 0; i < wHL.Length; i++) { wHL[i] = new float[inpCount]; } // init our output nodes wieghts for (int i = 0; i < wO.Length; i++) { wO[i] = new float[hL.Length]; } // convert config string NN into 12 float arrays for (int j = 0; j < wHL.Length; j++) { for (int i = 0; i < inpCount; i++) { wHL[j][i] = float.Parse(conf.attackNN[(inpCount * j) + (i)]); } } // convert the remaining config string NN into 3 float arrays for (int j = 0; j < wO.Length; j++) { for (int i = 0; i < wHL.Length; i++) { wO[j][i] = float.Parse(conf.attackNN[lastHLW + (wHL.Length * j) + i]); } } }
public Fighter(GameObject gO, int myTurn, int mapSize, AI_Config aI) { _obj = gO; this.mapSize = mapSize; _myTurn = myTurn; _config = aI; // _anim = _obj.GetComponentInChildren <Animator>(); // get a ref to all Sprite Renderers (WARNING: THIS BIT IS A BIT OF A MESS) SpriteRenderer[] allSR = gO.GetComponentsInChildren <SpriteRenderer>(); // _hpHolder = allSR[2].transform.parent; // _runIcons = new SpriteRenderer[] { allSR[5], allSR[7], allSR[9], allSR[11], allSR[13], }; // DisableAllRunIcons(); // _stunSpr = gO.GetComponentsInChildren <SpriteRenderer>()[4]; TextMeshPro[] grabTMP = gO.GetComponentsInChildren <TextMeshPro>(); // _dmgTxt = grabTMP[0]; // grabTMP[1].text = gO.name; // _strTxt = grabTMP[2]; // _powerUp = gO.GetComponentsInChildren <ParticleSystem>()[0]; // _powerDown = gO.GetComponentsInChildren <ParticleSystem>()[1]; // _display = gO.GetComponentsInChildren <MeshRenderer>()[6]; }
/// <summary> /// this takes in state data, & spits out a prediction on the best move to make /// </summary> /// <param name="inp"></param> /// <returns></returns> public static OutputMove CalculateOutput(Fighter fighter) { float d = UnityEngine.Random.value; float aX = UnityEngine.Random.Range(-1f, 1f); float aY = UnityEngine.Random.Range(-1f, 1f); AI_Config conf = fighter.config; StateData state = fighter.stateData; int nodePer1 = conf.attNodePer1; // if (conf.neuralNetwork[1]) { // get a ref to how many inputs will we be using for the move output network int inpCount = state.fullState.Length; // ref to number of output nodes for move network int numOutputNodes = 3; // ref to output nodes float[] o = new float[numOutputNodes]; // ref to hidden layer nodes float[] hL = new float[numOutputNodes * nodePer1]; // ref to hidden layer node weights float[][] wHL = new float[numOutputNodes * nodePer1][]; // ref to output layer weights float[][] wO = new float[numOutputNodes][]; // ref to the when the hidden layer weights end int lastHLW = numOutputNodes * nodePer1 * inpCount; // init our hidden layer nodes wieghts for (int i = 0; i < wHL.Length; i++) { wHL[i] = new float[inpCount]; } // init our output nodes wieghts for (int i = 0; i < wO.Length; i++) { wO[i] = new float[hL.Length]; } // convert config string NN into 12 float arrays for (int j = 0; j < wHL.Length; j++) { for (int i = 0; i < inpCount; i++) { wHL[j][i] = float.Parse(conf.movementNN[(inpCount * j) + (i)]); } } // convert the remaining config string NN into 3 float arrays for (int j = 0; j < wO.Length; j++) { for (int i = 0; i < wHL.Length; i++) { wO[j][i] = float.Parse(conf.movementNN[lastHLW + (wHL.Length * j) + i]); } } // calculate the hidden layer nodes output for (int i = 0; i < hL.Length; i++) { hL[i] = AI.ReLu(AI.Σ(state.attState, wHL[i]) + 0); } // calculate the output nodes output for (int i = 0; i < o.Length; i++) { o[i] = AI.Σ(hL, wO[i]); } // squash & set the the final values for returning d = AI.Sigmoid(o[0]); aX = AI.Tanh(o[1]); aY = AI.Tanh(o[2]); Debug.Log($"{fighter.name} dXY: {d},{aX},{ aY}"); } else { float dice = UnityEngine.Random.value; d = dice < conf.movementTrad ? 0 : 1; } return(new OutputMove(d, aX, aY)); }
public static OutputMakeMove CalculateNormal(Fighter fighter) { float ret = UnityEngine.Random.value; AI_Config conf = fighter.config; StateData state = fighter.stateData; // when fighter is stunned, it has to stay put if (fighter.isStunned) { return(new OutputMakeMove(0)); } // if (conf.neuralNetwork[0]) { // get a ref to how many inputs will we be using for the stay output network int inpCount = state.fullState.Length; // ref to number of output nodes for stay network int numOutputNodes = 1; int nodePer1 = conf.attNodePer1; // ref to output nodes float[] o = new float[numOutputNodes]; // ref to hidden layer nodes float[] hL = new float[numOutputNodes * nodePer1]; // ref to hidden layer node weights float[][] wHL = new float[numOutputNodes * nodePer1][]; // ref to output layer weights float[][] wO = new float[numOutputNodes][]; // ref to the when the hidden layer weights end int lastHLW = numOutputNodes * nodePer1 * inpCount; // init our hidden layer nodes wieghts for (int i = 0; i < wHL.Length; i++) { wHL[i] = new float[inpCount]; } // init our output nodes wieghts for (int i = 0; i < wO.Length; i++) { wO[i] = new float[hL.Length]; } // convert config string NN into 12 float arrays for (int j = 0; j < wHL.Length; j++) { for (int i = 0; i < inpCount; i++) { wHL[j][i] = float.Parse(conf.stayNN[(inpCount * j) + (i)]); } } // convert the remaining config string NN into 3 float arrays for (int j = 0; j < wO.Length; j++) { for (int i = 0; i < wHL.Length; i++) { //Debug.Log($"{lastHLW + (wHL.Length * j) + i}"); wO[j][i] = float.Parse(conf.stayNN[lastHLW + (wHL.Length * j) + i]); } } // calculate the hidden layer nodes output for (int i = 0; i < hL.Length; i++) { hL[i] = AI.ReLu(AI.Σ(state.attState, wHL[i]) + 0); } // calculate the output nodes output for (int i = 0; i < o.Length; i++) { o[i] = AI.Σ(hL, wO[i]); } // squash & set the the final values for returning ret = AI.Sigmoid(o[0]); Debug.Log($"{fighter.name} Stay: {ret}"); } else { float dice = UnityEngine.Random.value; ret = dice < conf.stayTrad ? 1 : 0; } return(new OutputMakeMove(ret)); }
public static OutputAttack CalculateOutput(bool stunned, Fighter fighter) { float a = UnityEngine.Random.value; float d = UnityEngine.Random.Range(0, 1 - a); float t = 1 - (a + d); float[] o = new float[0], hL = new float[0]; float[][] wHL = new float[0][], wO = new float[0][]; AI_Config conf = fighter.config; StateData state = fighter.stateData; float greedy = UnityEngine.Random.value; NNState nn = new NNState(); string dType = null; // if (conf.usingAttackNN) { ConvertFightWeights(conf, state, out o, out hL, out wHL, out wO); //Debug.Log($"FORW: {wHL.Length} * {wHL[0].Length} = {(wHL.Length * wHL[0].Length)}"); //Debug.Log($"FORW: {wO.Length} * {wO[0].Length} = {(wO.Length * wO[0].Length)}"); //Debug.Log($"\t\tFORW: {(wHL.Length * wHL[0].Length)} + {(wO.Length * wO[0].Length)} = {(wHL.Length * wHL[0].Length) + (wO.Length * wO[0].Length)}"); // calculate the hidden layer nodes output for (int i = 0; i < hL.Length; i++) { hL[i] = AI.ReLu(AI.Σ(state.attState, wHL[i]) + 0); } // calculate the output nodes output for (int i = 0; i < o.Length; i++) { o[i] = AI.Σ(hL, wO[i]); } // ship this. nn = new NNState(o, hL, wO, wHL); bool useNN = greedy < conf.showoff; dType = useNN ? "nn" : "r"; // if (useNN) { // squash & set the the final values for returning a = AI.Sigmoid(o[0]); d = AI.Sigmoid(o[1]); t = AI.Sigmoid(o[2]); //Debug.Log($"{fighter.name}: Combat Exploit"); } else { //Debug.Log($"{fighter.name}: Combat Explore"); a = UnityEngine.Random.Range(0f, 1f); d = UnityEngine.Random.Range(0f, 1f); t = UnityEngine.Random.Range(0f, 1f); } } else { //Debug.Log($"{fighter.name}: Combat Traditional"); a = UnityEngine.Random.Range(0, conf.attackTrad); d = UnityEngine.Random.Range(0, conf.defendTrad); t = UnityEngine.Random.Range(0, conf.tauntTrad); } // MOVE IS STUNNED FUNCT INTO HERE INSTEAD, PASS IN BOOL if (stunned) { // check if defense is higher than taunt if (d > t) { a = 0; d = 1; t = 0; } // else that means that taunt is higher than defense, & we can set taunt else { a = 0; d = 0; t = 1; } return(new OutputAttack(a, d, t, nn, dType)); } // return(new OutputAttack(a, d, t, nn, dType)); }