Example #1
0
 private void Awake()
 {
     // get the car controller
     m_Car       = GetComponent <CarController>();
     nn          = new NeuralNetwork(0.5, new int[] { m_Car.Sensor().Length, 5, 2 });
     nnSwitch    = false;
     trainSwitch = false;
 }
Example #2
0
        private void Update()
        {
            // Switch from AI control or manuel control
            if (Input.GetKeyDown(KeyCode.Space))
            {
                nnSwitch = !nnSwitch;
            }

            // AI Training
            if (Input.GetKeyDown(KeyCode.T))
            {
                trainSwitch = !trainSwitch;
            }

            // Record traindata
            if (Input.GetKeyDown(KeyCode.R))
            {
                collectData = !collectData;
            }

            // Save AI
            if (Input.GetKeyDown(KeyCode.O))
            {
                NeuralNetwork.Save("save", nn);
                Debug.Log("Saved");
            }

            // Load AI
            if (Input.GetKeyDown(KeyCode.L))
            {
                nn = NeuralNetwork.Read("save");
                Debug.Log("Loaded");
            }

            // Delete AI
            if (Input.GetKeyDown(KeyCode.P))
            {
                File.Delete("save");
            }

            // Reset AI
            if (Input.GetKeyDown(KeyCode.C))
            {
                nn = new NeuralNetwork(0.5, new int[] { m_Car.Sensor().Length, 5, 2 });
                Debug.Log("ResetAI");
            }

            // Save train data
            if (Input.GetKeyDown(KeyCode.I))
            {
                BinaryFormatter bf   = new BinaryFormatter();
                FileStream      file = File.Create("trainData");
                bf.Serialize(file, trainset);
                file.Close();
                Debug.Log("TrainData Saved");
            }

            // Load train data
            if (Input.GetKeyDown(KeyCode.K))
            {
                trainset = null;
                if (File.Exists("trainData"))
                {
                    BinaryFormatter bf   = new BinaryFormatter();
                    FileStream      file = File.Open("trainData", FileMode.Open);
                    trainset = (List <double[][]>)bf.Deserialize(file);
                    file.Close();
                }
                trainsetCount = trainset.Count;
                Debug.Log("TrainData Loaded");
            }

            // Delete train data
            if (Input.GetKeyDown(KeyCode.U))
            {
                File.Delete("trainData");
            }

            // Reset training data
            if (Input.GetKeyDown(KeyCode.V))
            {
                trainset = new List <double[][]>();
                Debug.Log("ResetData");
            }
        }