void FixedUpdate() { if (ballCtrl.GetPosition().y - diskCtrl.GetPosition().y < -5 || Mathf.Abs(ballCtrl.GetPosition().x - diskCtrl.GetPosition().x) > 5 || Mathf.Abs(ballCtrl.GetPosition().z - diskCtrl.GetPosition().z) > 5) { Restart(); } switch (mode) { case EvaluationMode.Neural: OutputData neuralOutput = Neural.Evaluate(PrepareData()); diskCtrl.SetAngularVelocity(FloatToVector3(neuralOutput.outputDiskRotationSpeedVector)); break; case EvaluationMode.Fuzzy: OutputData fuzzyOutput = FuzzyLogic.Evaluate(PrepareData()); // fuzzyOutput.outputDiskRotationSpeedVector[2] = -fuzzyOutput.outputDiskRotationSpeedVector[2]; diskCtrl.SetAngularVelocity(FloatToVector3(fuzzyOutput.outputDiskRotationSpeedVector)); break; /* case EvaluationMode.TrainNeural: * OutputData neuralOutputExp = new OutputData(Vector3ToFloat(diskCtrl.GetAngularVelocity())); * if (diskCtrl.GetAngularVelocity() != Vector3.zero) * Neural.Train(PrepareData(), neuralOutputExp); * break;*/ } }
void Start() { diskCtrl = disk.GetComponent <diskController>(); ballCtrl = ball.GetComponent <ballController>(); manualDiskCtrl = disk.GetComponent <manualDiskControl>(); manualBallCtrl = ball.GetComponent <manualBallControl>(); diskStartPosition = diskCtrl.GetPosition(); ballStartPosition = ballCtrl.GetPosition(); CreateLineMaterial(); // Neural.learnRate = learnRate; List <float[, ]> weights = new List <float[, ]>(); weights.Add(new float[, ] { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0f }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0f }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }); weights.Add(new float[, ] { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } }); NewNet("Empty", new int[] { 3 * 4, 4, 3 }, weights); weights = new List <float[, ]>(); weights.Add(new float[, ] { { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0.0f }, { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0 }, { 0, 0, -0.3f, -0.0f, 0, 0, 0, 0, 0, 1, 0, 0, 0.0f }, { -0.3f, 0, 0, 0, 0, 0.0f, 0, 0, 0, 0, 0, -1, 0 } }); weights.Add(new float[, ] { { 0, 0.6f, 0, 1f, 0 }, { 0, 0, 0, 0, 0 }, { 0.6f, 0, 1f, 0, 0 } }); NewNet("Centering", new int[] { 3 * 4, 4, 3 }, weights); weights = new List <float[, ]>(); weights.Add(new float[, ] { { 0, 0, 0, 0, 0, 0, 0.8f, 0, 0, 0, 0, 0.4f, 0.0f }, { 0, 0, 0, 0, 0, 0, 0, 0, -0.8f, 0.4f, 0, 0, 0 }, { 0, 0, -0.3f, -0.0f, 0, 0, 0, 0, 0, 0, 0, 0, 0.0f }, { -0.3f, 0, 0, 0, 0, 0.0f, 0, 0, 0, 0, 0, 0, 0 } }); weights.Add(new float[, ] { { 0, .5f, 0, 1f, 0 }, { 0, 0, 0, 0, 0 }, { .5f, 0, 1f, 0, 0 } }); NewNet("Looping", new int[] { 3 * 4, 4, 3 }, weights); weights = new List <float[, ]>(); weights.Add(new float[, ] { { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1.0f }, { 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 1.0f }, { 0, 0, -0.3f, -0.0f, 0, 0, 0, 0, 0, 1, 0, 0, 0.5f }, { -0.3f, 0, 0, 0, 0, 0.0f, 0, 0, 0, 0, 0, -1, 0.5f } }); weights.Add(new float[, ] { { 0, 0.6f, 0, 1f, 0 }, { 0, 0, 0, 0, 0 }, { 0.6f, 0, 1f, 0, 0 } }); NewNet("OnEdge", new int[] { 3 * 4, 4, 3 }, weights); ChangeNet(true); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 0, 0, 1, 0, 0, 0, 10, 0, 0, 18, 0, 0 }, weight = 0.2f }, true); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 0, 0, 3, 0, 0, 0, 10, 0, 0, 16, 0, 0 }, weight = -0.2f }, true); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 3, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 18 }, weight = -0.2f }, false); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 16 }, weight = 0.2f }, false); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 0, 0, 1, 0, 0, 0, 13, 0, 0, 18, 0, 0 }, weight = 0.2f }, true); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 0, 0, 3, 0, 0, 0, 11, 0, 0, 16, 0, 0 }, weight = -0.2f }, true); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 3, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 18 }, weight = -0.2f }, false); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 16 }, weight = 0.2f }, false); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 0, 0, 1, 0, 0, 0, 14, 0, 0, 18, 0, 0 }, weight = 2.9f }, true); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 0, 0, 3, 0, 0, 0, 10, 0, 0, 16, 0, 0 }, weight = -2.9f }, true); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 3, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 18 }, weight = -2.9f }, false); FuzzyLogic.AddRule(new Rule { usedFunctions = new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 16 }, weight = 2.9f }, false); return; Color[] colors = new Color[] { Color.red, Color.yellow, Color.gray, Color.blue, Color.green }; for (int z = 0; z < 4; z++) { for (int i = -50; i < 50; i++) { for (int j = 0; j < 5; j++) { //if (j % 2 == 0) continue; float i2 = i / 4f; float x = MembershipFunctions.functionCollection[j + 5 * z](i2), x2 = MembershipFunctions.functionCollection[j + 5 * z](i2 - 1 / 4f); Debug.DrawLine(Vector3.up * (1.5f * z + 3 + x) + Vector3.right * i2 / 10f, Vector3.up * (1.5f * z + 3 + x2) + Vector3.right * (i2 / 10f - 1 / 40f), colors[j], 100); } } } }