// select function by Roulette public static GAObject[] RouletteSelector(GAObject[] pGAObject, float pDyingRate, Type pGAObjectType) { int nowLength = (int)(pGAObject.Length * pDyingRate); if (nowLength == pGAObject.Length && nowLength != 0) { nowLength--; } GAObject[] nowObject = new GAObject[nowLength]; int index = 0; int RSIndex = UnityEngine.Random.Range(0, pGAObject.Length); float maxfitness = -1.0f; float minfitness = 999.0f; for (int i = 0; i < pGAObject.Length; i++) { if (pGAObject [i].mfitness > maxfitness) { maxfitness = pGAObject [i].mfitness; } if (pGAObject [i].mfitness < minfitness) { minfitness = pGAObject [i].mfitness; } } List <int> succeedIndex = new List <int> (); while (index < nowLength) { if (succeedIndex.Contains(RSIndex)) { RSIndex++; RSIndex = RSIndex % pGAObject.Length; continue; } float trate = (pGAObject [RSIndex].mfitness - minfitness) / (maxfitness - minfitness); if (UnityEngine.Random.Range(0.0f, 1.0f) < trate) { nowObject [index++] = pGAObject [RSIndex]; succeedIndex.Add(RSIndex); RSIndex++; RSIndex = RSIndex % pGAObject.Length; } else { RSIndex++; RSIndex = RSIndex % pGAObject.Length; } } //Debug.Log (s); return(nowObject); }
// add a object new public void Add(SimpleAI pSimpleAI) { GAObject[] tGAObjects = new GAObject[mGAObjectSet.Length + 1]; SimpleAI[] tSimpleAIs = new SimpleAI[mRTObjectSet.Length + 1]; for (int i = 0; i < mGAObjectSet.Length; i++) { tGAObjects [i] = mGAObjectSet [i]; tSimpleAIs [i] = mRTObjectSet [i]; } tGAObjects [tGAObjects.Length - 1] = pSimpleAI.mCharacter; tSimpleAIs [tGAObjects.Length - 1] = pSimpleAI; mGAObjectSet = tGAObjects; mRTObjectSet = tSimpleAIs; }
// destroy a AI should be delete in GAPopulation public void Delete(SimpleAI pSimpleAI) { GAObject[] tGAObjects = new GAObject[mGAObjectSet.Length - 1]; SimpleAI[] tSimpleAIs = new SimpleAI[mRTObjectSet.Length - 1]; int index = 0; for (int i = 0; i < mGAObjectSet.Length; i++) { if (mRTObjectSet [i] == pSimpleAI) { continue; } tGAObjects [index] = mGAObjectSet [i]; tSimpleAIs [index] = mRTObjectSet [i]; index++; } mGAObjectSet = tGAObjects; mRTObjectSet = tSimpleAIs; // Debug.Log (mRTObjectSet.Length); }
// breed by single point insert public static GAObject[] SinglePointInsertBreedor(GAObject[] pGAObject, float pIncreseseRate, Type pGAObjectType) { int nowLength = (int)(pGAObject.Length * pIncreseseRate); if (nowLength % 2 == 1) { nowLength++; } GAObject[] nowObject = new GAObject[nowLength]; for (int i = 0; i < nowLength; i++) { nowObject [i] = (GAObject)pGAObjectType.Assembly.CreateInstance(pGAObjectType.FullName); } int index = 0; while (index < nowLength) { GAObject mother = pGAObject[UnityEngine.Random.Range(0, pGAObject.Length)]; GAObject father = pGAObject[UnityEngine.Random.Range(0, pGAObject.Length)]; bool[] bitbunch1 = new bool[mother.mbitbunch.Length]; bool[] bitbunch2 = new bool[father.mbitbunch.Length]; for (int i = 0; i < bitbunch1.Length; i++) { if (i < bitbunch1.Length / 2) { bitbunch1 [i] = mother.mbitbunch [i]; bitbunch2 [i] = father.mbitbunch [i]; } else { bitbunch1 [i] = father.mbitbunch [i]; bitbunch2 [i] = mother.mbitbunch [i]; } } nowObject [index].mbitbunch = bitbunch1; nowObject [index + 1].mbitbunch = bitbunch2; index = index + 2; } return(nowObject); }