예제 #1
0
 public void LoadData(Car2DData data)
 {
     net = data.GetNetwork();
     numberOfSightLines = data.GetSightLines();
     FOV           = data.GetFOV();
     sightDistance = data.GetSightDistance();
 }
    private void Save(Car2DData data)
    {
        BinaryFormatter bf   = new BinaryFormatter();
        FileStream      file = File.Open(Application.persistentDataPath + "/savedCar.car", FileMode.OpenOrCreate);

        bf.Serialize(file, data);
        file.Close();
        Debug.Log("Successfully Saved!");
    }
    public void SaveBest()
    {
        if (currentGeneration < 1)
        {
            Debug.LogError("Cannot save best on generation 0");
            return;
        }
        Car2DData saveData = bestCar;

        Save(saveData);
    }
    // Update is called once per frame
    void Update()
    {
        if (numSimulated == carsPerGeneration && totalFailure)
        {
            smolBrains   = GenerateChildren(brains);
            numSimulated = 0;
            currentGeneration++;
            FindBest();
            Death();
            Cleanup();
            totalFailure = false;
        }
        else if (totalFailure)
        {
            SpawnCars();
            totalFailure = false;
        }
        else
        {
            totalFailure = true;
            // TODO: Optimize me cap'n
            for (int i = 0; i < cars.Length; i++)
            {
                if (cars[i] != null)
                {
                    // Save data and destroy failed cars
                    if (cars[i].GetComponent <Car2DTrainer>().hasFailed)
                    {
                        Car2DTrainer car = cars[i].GetComponent <Car2DTrainer>();

                        // Saving important car info
                        carData[i] = new Car2DData(car.GetNetwork(), car.numberOfSightLines, car.FOV, car.sightDistance);

                        Destroy(cars[i]);
                    }
                }

                if (cars[i] != null)
                {
                    // Update each car's progress along the track
                    TrackProgress(cars[i]);
                    totalFailure = false;

                    if (cars[i].GetComponent <Car2DTrainer>().lap > numberOfLaps)
                    {
                        cars[i].GetComponent <Car2DTrainer>().hasFailed = true;     // Stop a car that has completed all laps
                        Destroy(cars[i]);
                    }
                }
            }
        }
    }
    private bool Load(out Car2DData data)
    {
        if (File.Exists(Application.persistentDataPath + "/savedNetwork.dat"))
        {
            BinaryFormatter bf   = new BinaryFormatter();
            FileStream      file = File.Open(Application.persistentDataPath + "/savedNetwork.dat", FileMode.Open);

            data = (Car2DData)bf.Deserialize(file);
            Debug.Log("Successfully Loaded!");

            return(true);
        }
        else
        {
            Debug.LogError("File " + Application.persistentDataPath + "/savedNetwork.dat" + " could not be found.");

            data = null;
            return(false);
        }
    }
    public void LoadGeneration()
    {
        GenerationData loadData;

        if (LoadGenerationFromFile(out loadData))
        {
            currentGeneration = loadData.currentGeneration;
            for (int i = 0; i < carData.Length; i++)
            {
                brains[i] = loadData.carData[i].GetNetwork();
            }
            carData      = loadData.carData;
            bestCar      = loadData.bestCar;
            totalFailure = true;
            ResetGeneration();
        }
        else
        {
            Debug.LogError("File " + Application.persistentDataPath + "/savedGeneration.gen" + " could not be found.");
        }
    }
    private void FindBest()
    {
        if (currentGeneration < 1)
        {
            Debug.LogError("Cannot find best on generation 0");
            return;
        }

        float fitness = 0;
        int   index   = 0;

        for (int i = 0; i < carData.Length; i++)
        {
            if (fitness < carData[i].GetNetwork().GetFitness())
            {
                fitness = carData[i].GetNetwork().GetFitness();
                index   = i;
            }
        }
        bestCar = carData[index];
    }
예제 #8
0
    public static Car2DData[] LoadRacers(string difficulty)
    {
        string        path     = Application.dataPath + "/AI/" + difficulty;
        DirectoryInfo myDir    = new DirectoryInfo(path);
        int           numFiles = myDir.GetFiles("*.car").Length;

        if (numFiles == 0)
        {
            Debug.LogError("No racers found in " + path);
            return(null);
        }
        Car2DData[] racers = new Car2DData[numFiles];
        for (int i = 0; i < numFiles; i++)
        {
            FileInfo[]      files = myDir.GetFiles("*.car");
            BinaryFormatter bf    = new BinaryFormatter();
            FileStream      file  = File.Open(files[i].FullName, FileMode.Open);
            racers[i] = (Car2DData)bf.Deserialize(file);
        }
        return(racers);
    }
 public GenerationData(Car2DData[] data, int gen, Car2DData best)
 {
     carData           = data;
     currentGeneration = gen;
     bestCar           = best;
 }