Exemple #1
0
    private static GeneticCode DuplicateGeneticCode(GeneticCode oGeneticCode)
    {
        GeneticCode newGeneticCode     = new GeneticCode();
        float       mutationChance     = oGeneticCode.GetMutationChance();
        float       noise              = oGeneticCode.GetNoise();
        float       noiseChance        = oGeneticCode.GetNoiseChance();
        List <byte> newGeneticCodeList = new List <byte>();
        List <byte> oldGeneticCodeList = oGeneticCode.GetGeneticCodeList();

        //Debug.Log("Mutation Chance " + mutationChance);

        int randomInsert = Random.Range(0, oldGeneticCodeList.Count);
        //randomInsert = 1;

        //Debug.Log("Random Insert at = " + randomInsert);
        byte randomValue = (byte)(Random.Range(0, 255));

        randomValue = 10;
        //Debug.Log("Random Value = " + randomValue);


        for (int i = 0; i < oldGeneticCodeList.Count; i++)
        {
            bool addNoise = false;
            if (Random.Range(0f, 1f) > noiseChance)
            {
                addNoise = true;
            }

            if (Random.Range(0f, 1f) < mutationChance)
            {
                Debug.Log("Random Insertion @ " + i);
                newGeneticCodeList.Add((byte)(Random.Range(0, 255)));
                Debug.Log("Safely inserted");
            }

            byte byteToAdd = oldGeneticCodeList[i];

            if (addNoise)
            {
                //Debug.Log("Adding noise to " + byteToAdd);
                byteToAdd = (byte)(byteToAdd * Random.Range(-noise, +noise));
                //Debug.Log("Added noise : " + byteToAdd);
            }

            newGeneticCodeList.Add(oldGeneticCodeList[i]);
        }

        Debug.Log("Exited");
        newGeneticCode.SetGeneticCodeList(newGeneticCodeList);
        //newGeneticCodeList = new List<byte>();

        return(newGeneticCode);
    }
Exemple #2
0
    void Start()
    {
        gameObject.AddComponent <MeshFilter>();
        currentStage = CurrentStage.expanding;

        //Debug.Log("Start being called");
        if (geneticCode == null)
        {
            //Debug.Log(gameObject.name + " is creating a genetic code!");
            geneticCode = new GeneticCode();
        }
        else
        {
            //Debug.Log(gameObject.name + " already have a genetic code!");
        }

        parts = new List <GameObject>();



        geneticCodeOperator         = new GeneticCodeOperator();
        geneticBuildingInstructions = new GeneticBuildingInstructions();

        #region neural network tests

        /*
         * neural = new NetworkModel();
         * neural.Layers.Add(new NeuralLayer(4, 1, "INPUT"));
         * neural.Layers.Add(new NeuralLayer(2, 1, "HIDDEN"));
         * neural.Layers.Add(new NeuralLayer(4, 2, "OUTPUT"));
         *
         * neural.Build();
         * //Debug.Log("----Before Training------------");
         * neural.Print();
         *
         * NeuralData X = new NeuralData(4);
         * X.Add(1, 1);
         * X.Add(1, 1);
         * X.Add(1, 0);
         * X.Add(1, 1);
         *
         * NeuralData Y = new NeuralData(4);
         * Y.Add(1);
         * Y.Add(1);
         * Y.Add(0);
         * Y.Add(0);
         *
         * neural.Train(X, Y, iterations: 10, learningRate: 0.1);
         * //List<double> outputs = neural.Process(X);
         *
         */
        #endregion

        //Debug.Log("----After Training------------");

        //for(int i = 0; i < outputs.Count; i++)
        //{
        //    Debug.Log($"Output{i} = {outputs[i]}");
        //}

        //neural.Print();

        //X.Add(2.0f);
        //X.Add(3.0f);

        //List<double> outputs = neural.Train(X);

        //Debug.Log("Output");
        //for (int i = 0; i < outputs.Count; i++)
        //{
        //    Debug.Log($"Out{i} = {outputs[i]}");
        //}


        List <byte> geneticCodeList = geneticCode.GetGeneticCodeList();

        geneticCodeOperator.ProcessGeneticCode(gameObject, ref geneticCodeList, geneticBuildingInstructions.GetBuildingInstructions());

        geneticCode.SetGeneticCodeList(geneticCodeList);

        //read geneticCode
        //operateGeneticCode
    }