private static void FindScriptableObject()
    {
        string levelId = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
        Regex  regex   = new Regex(@"\d+");
        Match  match   = regex.Match(levelId);

        levelId = match.Value;
        string name = "BestAIRun" + levelId;

        bestAIRun = Resources.Load <BestAIRun> (name);
    }
Ejemplo n.º 2
0
    private void Start()
    {
        m_carControl = FindObjectOfType <UnityStandardAssets.Vehicles.Car.CarUserControl> ();
        m_waypoints  = FindObjectsOfType <Waypoint> ().ToList();
        m_raycaster  = m_carControl.GetComponent <Eyes> ();

        SceneManager.sceneLoaded += OnSceneChanged;

        if (m_geneticAlg == null)
        {
            m_geneticAlg = new GeneticAlgorithm();

            int totalWeights = 0;
            for (int i = 0; i < m_neuralTopology.Length - 1; i++)
            {
                totalWeights += m_neuralTopology [i] * m_neuralTopology [i + 1];
            }
            m_geneticAlg.GeneratePopulation(m_populationCount, totalWeights);

            m_neuralNet = new NeuralNetwork();
            if (loadBestGenome == false)
            {
                GeneticAlgorithm.Genome genome = m_geneticAlg.GetNextGenome();
                m_currentGenomeIndex = m_geneticAlg.GetCurrentGenomeIndex();
                m_neuralNet.CreateNetFromGenome(genome, m_neuralTopology);
            }
            else
            {
                BestAIRun bestRun = SaveLoad.LoadRun();
                GeneticAlgorithm.Genome genome = bestRun.genomeToSave;
                if (genome.weights.Count == 0)
                {
                    Debug.LogError("There are nothing to load. Uncheck 'Load Best Genome' option.");
                    Debug.Break();
                    return;
                }
                m_neuralNet.CreateNetFromGenome(genome, m_neuralTopology);

                m_currentGenerationIndex = bestRun.generationNumber;
                m_currentGenomeIndex     = bestRun.genomeNumber;
                m_bestFitness            = (int)genome.fitness;
            }

            m_defaultPosition = transform.position;
            m_defaultRotation = transform.rotation;
        }

        StartCoroutine(WaitGenomeStart());
    }