Пример #1
0
    // Combine 2 parents' DNA using crossover & apply mutations
    NN_Data CrossOver(NN_Data[] parents)
    {
        float[,] ih_w = new float[parents[0].ih_w.GetLength(0), parents[0].ih_w.GetLength(1)];
        float[] ih_b = new float[parents[0].ih_b.Length];
        float[,] ho_w = new float[parents[0].ho_w.GetLength(0), parents[0].ho_w.GetLength(1)];
        float[] ho_b = new float[parents[0].ho_b.Length];


        for (int row = 0; row < ih_w.GetLength(0); row++)
        {
            for (int col = 0; col < ih_w.GetLength(1); col++)
            {
                ih_w[row, col] = parents[Random.Range(0, 2)].ih_w[row, col];
                if (mutationPercentage > Random.value)
                {
                    ih_w[row, col] += (Random.value - .5f) * mutationScale;
                }
            }
        }

        for (int element = 0; element < ih_b.Length; element++)
        {
            ih_b[element] = parents[Random.Range(0, 2)].ih_b[element];
            if (mutationPercentage > Random.value)
            {
                ih_b[element] += (Random.value - .5f) * mutationScale;
            }
        }

        for (int row = 0; row < ho_w.GetLength(0); row++)
        {
            for (int col = 0; col < ho_w.GetLength(1); col++)
            {
                ho_w[row, col] = parents[Random.Range(0, 2)].ho_w[row, col];
                if (mutationPercentage > Random.value)
                {
                    ho_w[row, col] += (Random.value - .5f) * mutationScale;
                }
            }
        }
        for (int element = 0; element < ho_b.Length; element++)
        {
            ho_b[element] = parents[Random.Range(0, 2)].ho_b[element];
            if (mutationPercentage > Random.value)
            {
                ho_b[element] += (Random.value - .5f) * mutationScale;
            }
        }

        NN_Data childDNA = new NN_Data();

        childDNA.UpdateData(ih_w, ih_b, ho_w, ho_b);

        return(childDNA);
    }
        // Get saved NN data from a playerID
        public static NN_Data ReadData(int playerID)
        {
            if (File.Exists(path + "Runner" + playerID.ToString() + ".txt"))
            {
                string[] lines = File.ReadAllLines(path + "Runner" + playerID.ToString() + ".txt");


                string[] ih_w_rows = lines[0].Split(':');
                float[,] ih_w = new float[ih_w_rows.Length, ih_w_rows[0].Split(',').Length];
                for (int row = 0; row < ih_w.GetLength(0); row++)
                {
                    string[] ih_w_cols = ih_w_rows[row].Split(',');
                    for (int col = 0; col < ih_w.GetLength(1); col++)
                    {
                        //File.WriteAllText(path + "Debug.txt", ih_w_cols[col]);
                        ih_w[row, col] = float.Parse(ih_w_cols[col]);
                    }
                }

                string[] ih_b_elements = lines[1].Split(',');
                float[]  ih_b          = new float[ih_b_elements.Length];
                for (int element = 0; element < ih_b_elements.Length; element++)
                {
                    ih_b[element] = float.Parse(ih_b_elements[element]);
                }


                string[] ho_w_rows = lines[2].Split(':');
                float[,] ho_w = new float[ho_w_rows.Length, ho_w_rows[0].Split(',').Length];
                for (int row = 0; row < ho_w.GetLength(0); row++)
                {
                    string[] ho_w_cols = ho_w_rows[row].Split(',');
                    for (int col = 0; col < ho_w.GetLength(1); col++)
                    {
                        ho_w[row, col] = float.Parse(ho_w_cols[col]);
                    }
                }

                string[] ho_b_elements = lines[3].Split(',');
                float[]  ho_b          = new float[ho_b_elements.Length];
                for (int element = 0; element < ho_b_elements.Length; element++)
                {
                    ho_b[element] = float.Parse(ho_b_elements[element]);
                }

                NN_Data readData = new NN_Data();
                readData.UpdateData(ih_w, ih_b, ho_w, ho_b);

                return(readData);
            }
            else
            {
                return(null);
            }
        }