public static float[] ToVector(R2Tensor Tensor)
 {
     if (Tensor.row != 1 & Tensor.col != 1)
     {
         throw new MismatchedDimensionException("The matrix must be a row or column matrix");
     }
     if (Tensor.row == 1)
     {
         float[] vector_out = new float[Tensor.col];
         for (int i = 0; i < Tensor.col; i++)
         {
             vector_out[i] = Tensor.tensor[0, i];
         }
         return(vector_out);
     }
     if (Tensor.col == 1)
     {
         float[] vector_out = new float[Tensor.row];
         for (int i = 0; i < Tensor.row; i++)
         {
             vector_out[i] = Tensor.tensor[i, 0];
         }
         return(vector_out);
     }
     else
     {
         return(null);
     }
 }
    private void Start()
    {
        Manager2 manager = FindObjectOfType <Manager2>();

        max_v        = manager.max_v;
        acceleration = manager.acceleration;
        dampspeed    = manager.dampspeed;
        maxhealth    = manager.maxhealth;
        inman        = FindObjectOfType <InputManager>();
        currhealth   = maxhealth;

        //Load brain
        int      inputNeuron  = nn.Input;
        int      hiddenNeuron = nn.Hidden;
        int      outputNeuron = nn.Output;
        R2Tensor ihWeight     = new R2Tensor(hiddenNeuron, inputNeuron, nn.ih_weight);
        R2Tensor ihBias       = new R2Tensor(hiddenNeuron, 1, nn.ih_bias);
        R2Tensor ohWeight     = new R2Tensor(outputNeuron, hiddenNeuron, nn.ho_weight);
        R2Tensor ohBias       = new R2Tensor(outputNeuron, 1, nn.ho_bias);

        brain = new NeuralNetwork(ihWeight, ihBias, ohWeight, ohBias, inputNeuron, hiddenNeuron, outputNeuron);

        //Particles
        if (particleson)
        {
            part = Instantiate(particlesystem, Vector3.zero, Quaternion.identity, gameObject.transform).GetComponent <ParticleSystem>();
        }
    }
Example #3
0
 private static R2Tensor Sign(R2Tensor Tensor)
 {
     float[,] tensor_out = new float[Tensor.row, Tensor.col];
     for (int i = 0; i < Tensor.row; i++)
     {
         tensor_out[i, 0] = Mathf.Sign(Tensor.tensor[i, 0]);
     }
     return(new R2Tensor(tensor_out));
 }
Example #4
0
 public NeuralNetwork(R2Tensor ihWeight, R2Tensor ihBias, R2Tensor hoWeight, R2Tensor hoBias, int InputLayer, int HiddenLayer, int OutputLayer)
 {
     input     = InputLayer;
     hidden    = HiddenLayer;
     output    = OutputLayer;
     ih_weight = ihWeight.Copy();
     ih_bias   = ihBias.Copy();
     ho_weight = hoWeight.Copy();
     ho_bias   = hoBias.Copy();
 }
Example #5
0
 public NeuralNetwork(int InputLayer, int HiddenLayer, int OutputLayer)
 {
     input     = InputLayer;
     hidden    = HiddenLayer;
     output    = OutputLayer;
     ih_weight = new R2Tensor(HiddenLayer, InputLayer);
     ih_bias   = new R2Tensor(HiddenLayer, 1);
     ho_weight = new R2Tensor(OutputLayer, HiddenLayer);
     ho_bias   = new R2Tensor(OutputLayer, 1);
 }
Example #6
0
    private static R2Tensor Softmax(R2Tensor Tensor)
    {
        float[,] tensor_out = new float[Tensor.row, Tensor.col];
        float total = 0;

        for (int i = 0; i < Tensor.row; i++)
        {
            total           += Mathf.Exp(Tensor.tensor[i, 0]);
            tensor_out[i, 0] = Mathf.Exp(Tensor.tensor[i, 0]);
        }
        return(new R2Tensor(tensor_out).Scale(1 / total));
    }
Example #7
0
    public R2Tensor ForwardPropagation(R2Tensor Input, bool norm = true)
    {
        R2Tensor Hidden = Tanh(R2Tensor.Add(R2Tensor.MatMul(ih_weight, Input), ih_bias));

        if (!norm)
        {
            return(Hidden);
        }
        R2Tensor Output = R2Tensor.Add(R2Tensor.MatMul(ho_weight, Hidden), ho_bias);

        return(Sign(Output));
    }
Example #8
0
    public string PrintNeuron(R2Tensor tensor)
    {
        string string_out = "";

        for (int y = 0; y < tensor.row; y++)
        {
            string_out = string_out + " | ";
            for (int x = 0; x < tensor.col; x++)
            {
                string_out = string_out + Truncate(tensor.tensor[y, x]) + " | ";
            }
            string_out = string_out + "\n";
        }
        return(string_out);
    }
 public static R2Tensor HadamardMul(R2Tensor A, R2Tensor B)
 {
     if (A.row != B.row | A.col != B.col)
     {
         throw new MismatchedDimensionException("Both Matrix must have the same dimension in order to be multiplied together");
     }
     float[,] tensor_out = new float[A.row, A.col];
     for (int y = 0; y < A.row; y++)
     {
         for (int x = 0; x < A.col; x++)
         {
             tensor_out[y, x] = A.tensor[y, x] * B.tensor[y, x];
         }
     }
     return(new R2Tensor(tensor_out));
 }
 public static R2Tensor MatMul(R2Tensor A, R2Tensor B)
 {
     if (A.col != B.row)
     {
         throw new MismatchedDimensionException("Number of column of the first matrix must match the number of row of the second matrix");
     }
     float[,] tensor_out = new float[A.row, B.col];
     for (int y = 0; y < A.row; y++)
     {
         for (int x = 0; x < B.col; x++)
         {
             tensor_out[y, x] = Dot(A.GetRow(y), B.GetCol(x));
         }
     }
     return(new R2Tensor(tensor_out));
 }
Example #11
0
    private int Max(R2Tensor tensor)
    {
        int   intout = 0;
        float max    = 0;

        for (int i = 0; i < tensor.row; i++)
        {
            int currin = intout;
            if (tensor.tensor[i, 0] > max)
            {
                currin = i;
            }
            intout = currin;
        }
        return(intout);
    }
Example #12
0
 public void Mutate(R2Tensor Tensor, float mutationrate, float magnitude, float clamping)
 {
     for (int i = 0; i < Tensor.row; i++)
     {
         for (int j = 0; j < Tensor.col; j++)
         {
             if (mutationrate > Random.Range(0f, 1f))
             {
                 Tensor.tensor[i, j] += Random.Range(-1f * magnitude, magnitude);
             }
             if (Tensor.tensor[i, j] > clamping)
             {
                 Tensor.tensor[i, j] = clamping;
             }
             if (Tensor.tensor[i, j] < -1 * clamping)
             {
                 Tensor.tensor[i, j] = -1 * clamping;
             }
         }
     }
 }
Example #13
0
    public R2Tensor ForwardPropagation(R2Tensor Input, bool norm = true)
    {
        R2Tensor Hidden = Tanh(R2Tensor.Add(R2Tensor.MatMul(ih_weight, Input), ih_bias));

        if (!norm)
        {
            return(Hidden);
        }
        R2Tensor Output = R2Tensor.Add(R2Tensor.MatMul(ho_weight, Hidden), ho_bias);

        switch (outtype)
        {
        case "Basisdirection":
            return(Tanh(Output));

        case "Direction":
            int     maxind     = Max(Output);
            float[] tensor_out = new float[9];
            for (int i = 0; i < 9; i++)
            {
                if (i == maxind)
                {
                    tensor_out[i] = 1; continue;
                }
                tensor_out[i] = 0;
            }
            return(R2Tensor.ToMatrix(tensor_out, "col"));

        case "Keystroke":
            return(Sign(Output));

        case "Debug":
            return(Output);

        default:
            return(Output);
        }
    }
Example #14
0
    private void Update()
    {
        if (networkvisual == null)
        {
            networkvisual = GameObject.Find("Network");
        }
        if (afkenable)
        {
            if (speedup * deathtimer >= afk & !inlas & !dead)
            {
                FindObjectOfType <Holocaust_2>().ImDeadLOL(prisoner_number);
                Destroy(gameObject);
                dead = true;
            }
            if (velocity.x == 0 & velocity.y == 0)
            {
                deathtimer += Time.deltaTime;
            }
            else
            {
                deathtimer = 0;
            }
        }
        if (singafkenable)
        {
            if (speedup * deathtimer2 >= singafk & !inlas & !dead)
            {
                FindObjectOfType <Holocaust_2>().ImDeadLOL(prisoner_number);
                Destroy(gameObject);
            }
            if (velocity.x == 0 & xory == 1)
            {
                deathtimer2 += Time.deltaTime;
            }
            else
            {
                if (velocity.y == 0 & xory == 0)
                {
                    deathtimer2 += Time.deltaTime;
                }
                else
                {
                    deathtimer2 = 0; xory = 1 - xory;
                }
            }
        }

        #region Think
        float[] input_array        = inman.GetInput(gameObject);
        int     input_array_length = input_array.Length;
        input_array[input_array_length - 4] = Tanh(obj.position.x, 4f);
        input_array[input_array_length - 3] = Tanh(obj.position.y, 9f / 4f);
        input_array[input_array_length - 2] = Tanh(velocity.x, max_v.x / 2f);
        input_array[input_array_length - 1] = Tanh(velocity.y, max_v.y / 2f);

        R2Tensor finproc = brain.ForwardPropagation(R2Tensor.ToMatrix(input_array, "col"));
        float[]  thought = R2Tensor.ToVector(finproc);

        if (networkvisual != null)
        {
            if (networkvisual.activeSelf & prisoner_number == best_n - 1)
            {
                inputactivation.text  = PrintNeuron(R2Tensor.ToMatrix(input_array, "col"));
                hiddenactivation.text = PrintNeuron(brain.ForwardPropagation(R2Tensor.ToMatrix(input_array, "col"), false));
                outputactivation.text = PrintNeuron(finproc);
            }
        }

        //R2Tensor.ToMatrix(thought, "row").PrintMatrix();
        int vert = 0;
        int hori = 0;
        switch (output)
        {
        case "Basisdirection":
            if (thought[0] > 0.5f)
            {
                hori = 1;
            }
            if (thought[0] < -0.5f)
            {
                hori = -1;
            }
            if (thought[1] > 0.5f)
            {
                vert = 1;
            }
            if (thought[1] < -0.5f)
            {
                vert = -1;
            }
            break;

        case "Direction":
            if (thought[0] == 1 | thought[4] == 1 | thought[5] == 1)
            {
                vert = 1;
            }
            if (thought[1] == 1 | thought[6] == 1 | thought[7] == 1)
            {
                vert = -1;
            }
            if (thought[3] == 1 | thought[5] == 1 | thought[7] == 1)
            {
                hori = 1;
            }
            if (thought[2] == 1 | thought[4] == 1 | thought[6] == 1)
            {
                hori = -1;
            }
            break;

        case "Keystroke":
            if (thought[0] == 1)
            {
                vert += 1;
            }
            if (thought[1] == 1)
            {
                vert -= 1;
            }
            if (thought[2] == 1)
            {
                hori -= 1;
            }
            if (thought[3] == 1)
            {
                hori += 1;
            }
            break;
        }
        #endregion
        #region Translation
        if (firstbump_h & Mathf.Abs(obj.position.x) > 8.25f)
        {
            velocity    = new Vector2(0, velocity.y);
            firstbump_h = false;
        }
        if (!firstbump_h & Mathf.Abs(obj.position.x) < 8.25f)
        {
            firstbump_h = true;
        }
        if (firstbump_v & Mathf.Abs(obj.position.y) > 4.37f)
        {
            velocity    = new Vector2(velocity.x, 0);
            firstbump_v = false;
        }
        if (!firstbump_v & Mathf.Abs(obj.position.y) < 4.37f)
        {
            firstbump_v = true;
        }

        if ((hori == -1 & obj.position.x > -8.25f) | (hori == 1 & obj.position.x < 8.25f))
        {
            if (hori * velocity.x < max_v.x)
            {
                if (hori != 0)
                {
                    velocity += new Vector2(hori * acceleration * Time.deltaTime * speedup, 0);
                }
            }
        }
        else
        {
            if (Mathf.Abs(velocity.x) < 0.001)
            {
                velocity = new Vector2(0, velocity.y);
            }
            else
            {
                velocity -= new Vector2(dampspeed * velocity.x * Time.deltaTime * speedup, 0);
            }
        }
        if ((vert == 1 & obj.position.y < 4.37f) | (vert == -1 & obj.position.y > -4.37f))
        {
            if (vert * velocity.y < max_v.y)
            {
                if (vert != 0)
                {
                    velocity += new Vector2(0, vert * acceleration * Time.deltaTime * speedup);
                }
            }
        }
        else
        {
            if (Mathf.Abs(velocity.y) < 0.001)
            {
                velocity = new Vector2(velocity.x, 0);
            }
            else
            {
                velocity -= new Vector2(0, dampspeed * velocity.y * Time.deltaTime * speedup);
            }
        }

        obj.position += new Vector3(velocity.x * speedup, velocity.y * speedup, 0);
        if (obj.position.x < -8.25f)
        {
            obj.position = new Vector3(-8.25f, obj.position.y, 0); velocity.x = 0;
        }
        if (obj.position.x > 8.25f)
        {
            obj.position = new Vector3(8.25f, obj.position.y, 0); velocity.x = 0;
        }
        if (obj.position.y > 4.37f)
        {
            obj.position = new Vector3(obj.position.x, 4.37f, 0); velocity.y = 0;
        }
        if (obj.position.y < -4.37f)
        {
            obj.position = new Vector3(obj.position.x, -4.37f, 0); velocity.y = 0;
        }
        #endregion
        #region Rotation
        float anglef = ((Mathf.Atan2(velocity.y, velocity.x) * 180 / Mathf.PI + 270) % 360);
        if (velocity.y != 0 | velocity.x != 0)
        {
            obj.eulerAngles = new Vector3(0, 0, anglef);
        }
        #endregion
    }
    private void Update()
    {
        #region Think
        float[]  input_array = inman.GetInput(obj, velocity);
        R2Tensor finproc     = brain.ForwardPropagation(R2Tensor.ToMatrix(input_array, "col"));
        float[]  thought     = R2Tensor.ToVector(finproc);

        int vert = 0;
        int hori = 0;
        if (thought[0] == 1)
        {
            vert += 1;
        }
        if (thought[1] == 1)
        {
            vert -= 1;
        }
        if (thought[2] == 1)
        {
            hori -= 1;
        }
        if (thought[3] == 1)
        {
            hori += 1;
        }
        #endregion
        #region Translation
        if (firstbump_h & Mathf.Abs(obj.position.x) > 8.25f)
        {
            velocity    = new Vector2(0, velocity.y);
            firstbump_h = false;
        }
        if (!firstbump_h & Mathf.Abs(obj.position.x) < 8.25f)
        {
            firstbump_h = true;
        }
        if (firstbump_v & Mathf.Abs(obj.position.y) > 4.37f)
        {
            velocity    = new Vector2(velocity.x, 0);
            firstbump_v = false;
        }
        if (!firstbump_v & Mathf.Abs(obj.position.y) < 4.37f)
        {
            firstbump_v = true;
        }

        if ((hori == -1 & obj.position.x > -8.25f) | (hori == 1 & obj.position.x < 8.25f))
        {
            if (hori * velocity.x < max_v.x)
            {
                if (hori != 0)
                {
                    velocity += new Vector2(hori * acceleration * Time.deltaTime, 0);
                }
            }
        }
        else
        {
            if (Mathf.Abs(velocity.x) < 0.001)
            {
                velocity = new Vector2(0, velocity.y);
            }
            else
            {
                velocity -= new Vector2(dampspeed * velocity.x * Time.deltaTime, 0);
            }
        }
        if ((vert == 1 & obj.position.y < 4.37f) | (vert == -1 & obj.position.y > -4.37f))
        {
            if (vert * velocity.y < max_v.y)
            {
                if (vert != 0)
                {
                    velocity += new Vector2(0, vert * acceleration * Time.deltaTime);
                }
            }
        }
        else
        {
            if (Mathf.Abs(velocity.y) < 0.001)
            {
                velocity = new Vector2(velocity.x, 0);
            }
            else
            {
                velocity -= new Vector2(0, dampspeed * velocity.y * Time.deltaTime);
            }
        }

        obj.position += new Vector3(velocity.x, velocity.y, 0);
        if (obj.position.x < -8.25f)
        {
            obj.position = new Vector3(-8.25f, obj.position.y, 0); velocity.x = 0;
        }
        if (obj.position.x > 8.25f)
        {
            obj.position = new Vector3(8.25f, obj.position.y, 0); velocity.x = 0;
        }
        if (obj.position.y > 4.37f)
        {
            obj.position = new Vector3(obj.position.x, 4.37f, 0); velocity.y = 0;
        }
        if (obj.position.y < -4.37f)
        {
            obj.position = new Vector3(obj.position.x, -4.37f, 0); velocity.y = 0;
        }
        #endregion
        #region Rotation
        float anglef = ((Mathf.Atan2(velocity.y, velocity.x) * 180 / Mathf.PI + 270) % 360);
        if (velocity.y != 0 | velocity.x != 0)
        {
            obj.eulerAngles = new Vector3(0, 0, anglef);
        }
        #endregion
    }
Example #16
0
    private void Start()
    {
        speedup       = parameters.SpeedUp;
        hidden        = parameters.HiddenLayer;
        output        = parameters.output.ToString();
        afk           = parameters.afk;
        afkenable     = parameters.afk_kill;
        singafk       = parameters.singlelineafk;
        singafkenable = parameters.singlelineafkkill;
        invert        = parameters.Invert;
        // BestPrison stuff
        BestPrison camp = FindObjectOfType <BestPrison>();

        networkvisual = GameObject.Find("Network");
        if (networkvisual != null)
        {
            inputactivation    = GameObject.Find("InputText").GetComponent <Text>();
            hiddenactivation   = GameObject.Find("HiddenText").GetComponent <Text>();
            outputactivation   = GameObject.Find("OutputText").GetComponent <Text>();
            inputhiddenweight  = GameObject.Find("InputHiddenWeight").GetComponent <Text>();
            inputhiddenbias    = GameObject.Find("InputHiddenBias").GetComponent <Text>();
            hiddenoutputweight = GameObject.Find("HiddenOutputWeight").GetComponent <Text>();
            hiddenoutputbias   = GameObject.Find("HiddenOutputBias").GetComponent <Text>();
        }
        //load brain
        if (parameters.PlayMode.ToString() == "Load")
        {
            NetData  loadedBrain  = DataManager.LoadBrain(parameters.LoadIndex);
            int      inputNeuron  = loadedBrain.Input;
            int      hiddenNeuron = loadedBrain.Hidden;
            int      outputNeuron = loadedBrain.Output;
            R2Tensor ihWeight     = new R2Tensor(hiddenNeuron, inputNeuron, loadedBrain.ih_weight);
            R2Tensor ihBias       = new R2Tensor(hiddenNeuron, 1, loadedBrain.ih_bias);
            R2Tensor ohWeight     = new R2Tensor(outputNeuron, hiddenNeuron, loadedBrain.ho_weight);
            R2Tensor ohBias       = new R2Tensor(outputNeuron, 1, loadedBrain.ho_bias);
            brain = new NeuralNetwork(ihWeight, ihBias, ohWeight, ohBias, inputNeuron, hiddenNeuron, outputNeuron, loadedBrain.outputType);
        }
        else
        {
            int      inputNeuron  = bestbrain.Input;
            int      hiddenNeuron = bestbrain.Hidden;
            int      outputNeuron = bestbrain.Output;
            R2Tensor ihWeight     = new R2Tensor(hiddenNeuron, inputNeuron, bestbrain.ih_weight);
            R2Tensor ihBias       = new R2Tensor(hiddenNeuron, 1, bestbrain.ih_bias);
            R2Tensor ohWeight     = new R2Tensor(outputNeuron, hiddenNeuron, bestbrain.ho_weight);
            R2Tensor ohBias       = new R2Tensor(outputNeuron, 1, bestbrain.ho_bias);
            brain = new NeuralNetwork(ihWeight, ihBias, ohWeight, ohBias, inputNeuron, hiddenNeuron, outputNeuron, bestbrain.outputType);
        }

        if (networkvisual != null)
        {
            if (networkvisual.activeSelf)
            {
                inputhiddenweight.text  = PrintNeuron(brain.ih_weight);
                inputhiddenbias.text    = PrintNeuron(brain.ih_bias);
                hiddenoutputweight.text = PrintNeuron(brain.ho_weight);
                hiddenoutputbias.text   = PrintNeuron(brain.ho_bias);
            }
        }

        Manager2 manager = FindObjectOfType <Manager2>();

        max_v        = manager.max_v;
        acceleration = manager.acceleration;
        dampspeed    = manager.dampspeed;
        maxhealth    = parameters.maxhealth;
        inman        = FindObjectOfType <InputManager>();
        currhealth   = maxhealth;

        //Particles
        if (particleson)
        {
            part = Instantiate(particlesystem, Vector3.zero, Quaternion.identity, gameObject.transform).GetComponent <ParticleSystem>();
        }
    }