예제 #1
0
    public static Epoch LoadFile(string Data)
    {
        int _ExpectedLength = 8;

        // Expect 2 variables
        if (Data.Length < _ExpectedLength)
        {
            Debug.LogError("File too small (a)");
            return(null);
        }

        // File index
        int FileIndex = 0;

        // Important Data from start of file
        int hidden_layers = (int)FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));

        FileIndex += 4;
        Debug.Log("hidden layers: " + hidden_layers);

        int hidden_layers_size = (int)FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));

        FileIndex += 4;
        Debug.Log("hidden layer sizes: " + hidden_layers_size);

        // Expect (hidden_layers * hidden_layers_size * 4)
        // Expect 2 more variables (output size + output weight count)
        _ExpectedLength += (hidden_layers * hidden_layers_size * 4) + 8;
        if (Data.Length < _ExpectedLength)
        {
            Debug.LogError("File too small (b)");
            return(null);
        }

        List <int> hidden_layer_weight_count = new List <int>();

        for (int i = 0; i < hidden_layers; i++)
        {
            for (int j = 0; j < hidden_layers_size; j++)
            {
                hidden_layer_weight_count.Add((int)FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4)));
                FileIndex += 4;
                //Debug.Log("Hidden Layer: " + (i + 1) + ", Perceptron: " + (j + 1) + ", Size: " + hidden_layer_weight_count[j]);
            }
        }

        int output_layer_size = (int)FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));

        FileIndex += 4;
        Debug.Log("output layer size: " + output_layer_size);

        int output_layer_weightcount = (int)FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));

        FileIndex += 4;
        Debug.Log("Output Layer, Perceptron Size: " + output_layer_weightcount);

        Debug.Log("~~~");

        // Check if file is correct size

        // Expect
        for (int i = 0; i < hidden_layers; i++)
        {
            for (int j = 0; j < hidden_layers_size; j++)
            {
                _ExpectedLength += 4;
                _ExpectedLength += hidden_layer_weight_count[j] * 4;
            }
        }
        for (int i = 0; i < output_layer_size; i++)
        {
            _ExpectedLength += 4;
            _ExpectedLength += output_layer_weightcount * 4;
        }
        for (int i = 0; i < TetrisBoard.GetDataSize(); i++)
        {
            _ExpectedLength += 8;
        }
        if (Data.Length < _ExpectedLength)
        {
//#if UNITY_EDITOR
//            string Message = "File Too Small\n";
//            Message += "File Count: " + Data.Length.ToString() + "\n";
//            Message += "Expected Count: " + _ExpectedLength.ToString() + "\n";
//            EditorUtility.DisplayDialog("File Size", Message, "Continue");
//#endif
            return(null);
        }
        else if (Data.Length > _ExpectedLength)
        {
//#if UNITY_EDITOR
//            string Message = "File Too Big\n";
//            Message += "File Count: " + Data.Length.ToString() + "\n";
//            Message += "Expected Count: " + _ExpectedLength.ToString() + "\n";
//            EditorUtility.DisplayDialog("File Size", Message, "Continue");
//#endif
            return(null);
        }
//#if UNITY_EDITOR
//        else
//        {
//            EditorUtility.DisplayDialog("File Size", "Correct Size", "Continue");
//        }
//#endif


        // Read Hidden Layers
        List <List <Perceptron> > All_HiddenLayer = new List <List <Perceptron> >();

        for (int i = 0; i < hidden_layers; i++)
        {
            List <Perceptron> HiddenLayer = new List <Perceptron>();
            for (int j = 0; j < hidden_layers_size; j++)
            {
                // Get Bias
                float bias = FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));
                FileIndex += 4;
                //Debug.Log("Layer: " + (i + 1) + ", Perceptron: " + (j + 1) + ", Bias: " + bias);
                // Get all weights
                float[] weights = new float[hidden_layer_weight_count[j]];
                for (int k = 0; k < hidden_layer_weight_count[j]; k++)
                {
                    weights[k] = FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));
                    FileIndex += 4;
                    //Debug.Log("Layer: " + (i + 1) + ", Perceptron: " + (j + 1) + ", Weight[" + (k) + "]: " + weights[k]);
                }
                Perceptron P = new Perceptron(weights, bias);
                HiddenLayer.Add(P);
            }
            All_HiddenLayer.Add(HiddenLayer);
        }

        // Read Output Layer
        List <Perceptron> OutputLayer = new List <Perceptron>();

        for (int i = 0; i < output_layer_size; i++)
        {
            // Get Bias
            float bias = FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));
            FileIndex += 4;
            //Debug.Log("Output Perceptron: " + (i + 1) + ", Bias: " + bias);
            // Get all weights
            float[] weights = new float[output_layer_weightcount];
            for (int k = 0; k < output_layer_weightcount; k++)
            {
                weights[k] = FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));
                FileIndex += 4;
                //Debug.Log("Output Perceptron: " + (i + 1) + ", Weight[" + (k) + "]: " + weights[k]);
            }
            Perceptron P = new Perceptron(weights, bias);
            OutputLayer.Add(P);
        }

        // Read Input Layer
        List <Perceptron> InputLayer = new List <Perceptron>();

        for (int i = 0; i < TetrisBoard.GetDataSize(); i++)
        {
            // Get Bias
            float bias = FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));
            FileIndex += 4;
            //Debug.Log("Output Perceptron: " + (i + 1) + ", Bias: " + bias);
            // Get all weights
            float[] weights = new float[1];
            weights[0] = FloatByteConverter.CharStringOfBytesToFloat(Data.Substring(FileIndex, 4));
            FileIndex += 4;

            Perceptron P = new Perceptron(weights, bias);
            InputLayer.Add(P);
        }

        // Created Epoch
        Epoch NewEpoch = new Epoch(TetrisBoard.GetDataSize(), hidden_layers, hidden_layers_size, (int)Tetris.Move.TOTAL);

        NewEpoch.SetHiddenLayerData(All_HiddenLayer);
        NewEpoch.SetOutputLayerData(OutputLayer);
        NewEpoch.SetInputLayerData(InputLayer);

        return(NewEpoch);
    }