public bool FF(double[] input, bool bp, bool good)//Feed Forward { double[] answer = nn.FeedForward(input); bool correct = false; double actual = (good) ? 1 : -1; double[] error = { actual - answer[0], -actual - answer[1] }; if (good) { if (answer[0] > answer[1]) { correct = true; } else { correct = false; } } else { if (answer[0] < answer[1]) { correct = true; } else { correct = false; } } if (bp) { nn.BackPropagation(error); } return(correct); }
public void ponder() { distToCentre = (new Vector(0, 0) - spacials.pos).Length; ins [(int)nnInputs.leftEyeR] = leftEyeSense [0]; ins [(int)nnInputs.leftEyeG] = leftEyeSense [1]; ins [(int)nnInputs.leftEyeB] = leftEyeSense [2]; ins [(int)nnInputs.rightEyeR] = rightEyeSense [0]; ins [(int)nnInputs.rightEyeG] = rightEyeSense [1]; ins [(int)nnInputs.rightEyeB] = rightEyeSense [2]; ins [(int)nnInputs.pressrue] = pressure; ins [(int)nnInputs.distToCentre] = distToCentre; ins [(int)nnInputs.feedback0] = outs[(int)nnOutputs.feedback0]; ins [(int)nnInputs.feedback1] = outs[(int)nnOutputs.feedback1]; ins [(int)nnInputs.feedback2] = outs[(int)nnOutputs.feedback2]; ins [(int)nnInputs.feedback3] = outs[(int)nnOutputs.feedback3]; ins [(int)nnInputs.thrusterA0] = thrusters [0, 0]; ins [(int)nnInputs.thrusterA1] = thrusters [1, 0]; ins [(int)nnInputs.thrusterA2] = thrusters [2, 0]; ins [(int)nnInputs.thrusterA3] = thrusters [3, 0]; outs = nn.FeedForward(ins); }
void FixedUpdate() { if (!collided) { for (int i = 0; i < 5; i++) { Vector3 newVector = Quaternion.AngleAxis(i * 45 - 90, new Vector3(0, 1, 0)) * transform.right; RaycastHit hit; Ray Ray = new Ray(transform.position, newVector); Debug.DrawRay(transform.position, newVector * rayDistance); if (Physics.Raycast(Ray, out hit, rayDistance, raycastMask)) { input[i] = (10 - hit.distance) / 10; } } float[] output = network.FeedForward(input); transform.Rotate(0, output[0] * rotation, 0, Space.World); transform.position += transform.right * output[1] * speed; } }
void FixedUpdate() { float vision = 5f + attackSkill; float[] inputs = new float[inputsCount]; Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, vision); // количество соседних объектов float[] neighboursCount = new float[4]; // вектара к центрам масс еды, красного, зеленого и синего Vector3[] vectors = new Vector3[4]; for (int i = 0; i < 4; i++) { neighboursCount[i] = 0; vectors[i] = new Vector3(0f, 0f, 0f); } for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject == gameObject) { continue; } if (colliders[i].gameObject.name == "food") { neighboursCount[0]++; vectors[0] += colliders[i].gameObject.transform.position - transform.position; } else if (colliders[i].gameObject.name == "bacterium") { AI ai = colliders[i].gameObject.GetComponent <AI>(); neighboursCount[1] += ai.attackSkill / 3f; vectors[1] += (colliders[i].gameObject.transform.position - transform.position) * ai.attackSkill; neighboursCount[2] += ai.foodSkill / 3f; vectors[2] += (colliders[i].gameObject.transform.position - transform.position) * ai.foodSkill; neighboursCount[3] += ai.defSkill / 3f; vectors[3] += (colliders[i].gameObject.transform.position - transform.position) * ai.defSkill; } } for (int i = 0; i < 4; i++) { if (neighboursCount[i] > 0) { vectors[i] /= neighboursCount[i] * vision; inputs[i] = vectors[i].magnitude; } else { inputs[i] = 0f; } } float[] outputs = nn.FeedForward(inputs); Vector2 target = new Vector2(0, 0); for (int i = 0; i < 4; i++) { if (neighboursCount[i] > 0) { Vector2 dir = new Vector2(vectors[i].x, vectors[i].y); dir.Normalize(); target += dir * outputs[i]; } } if (target.magnitude > 1f) { target.Normalize(); } Vector2 velocity = rb.velocity; velocity += target * (0.25f + attackSkill * 0.05f); velocity *= 0.98f; rb.velocity = velocity; float antibiotics = 1f; // концентрация антибиотиков // if(transform.position.x < -39) antibiotics = 4; // else if(transform.position.x < -20) antibiotics = 3; // else if(transform.position.x < -1) antibiotics = 2; // antibiotics = Mathf.Max(1f, antibiotics - defSkill); energy -= Time.deltaTime * antibiotics * antibiotics; if (energy < 0f) { Kill(); } }
void FixedUpdate() { float vision = 5f + attackSkill; float[] inputs = new float[inputsCount]; Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, vision); // количество соседних объектов float[] neighboursCount = new float[4]; // Вектора к центрам масс еды, красного, зеленого и синего Vector3[] vectors = new Vector3[4]; for (int i = 0; i < 4; i++) { neighboursCount[i] = 0; vectors[i] = new Vector3(0f, 0f, 0f); } for (int i = 0; i < colliders.Length; i++) { if (colliders[i].gameObject == gameObject) { continue; } if (colliders[i].gameObject.name == "food") { neighboursCount[0]++; vectors[0] += colliders[i].gameObject.transform.position - transform.position; } else if (colliders[i].gameObject.name.Contains("bacterium")) { BacteriaAgent ai = colliders[i].gameObject.GetComponent <BacteriaAgent>(); neighboursCount[1] += ai.attackSkill / 3f; vectors[1] += (colliders[i].gameObject.transform.position - transform.position) * ai.attackSkill; neighboursCount[2] += ai.foodSkill / 3f; vectors[2] += (colliders[i].gameObject.transform.position - transform.position) * ai.foodSkill; neighboursCount[3] += ai.defSkill / 3f; vectors[3] += (colliders[i].gameObject.transform.position - transform.position) * ai.defSkill; } } for (int i = 0; i < 4; i++) { if (neighboursCount[i] > 0) { vectors[i] /= neighboursCount[i] * vision; inputs[i] = vectors[i].magnitude; } else { inputs[i] = 0f; } } float[] outputs = nn.FeedForward(inputs); Vector2 target = new Vector2(0, 0); for (int i = 0; i < 4; i++) { if (neighboursCount[i] > 0) { Vector2 dir = new Vector2(vectors[i].x, vectors[i].y); dir.Normalize(); target += dir * outputs[i]; } } if (target.magnitude > 1f) { target.Normalize(); } Vector2 velocity = rb.velocity; velocity += target * (0.25f + attackSkill * 0.05f); velocity *= 0.98f; rb.velocity = velocity; energy -= (Time.deltaTime * 0.5f); allSkills = new int[] { foodSkill, attackSkill, defSkill }; if (energy < 0f) { Kill(); } }
private void Brain(float[] inputs) { float[] outputs = NN.FeedForward(inputs); Move(outputs[0], outputs[1]); }