/// <summary>
            ///
            /// </summary>
            private void Update()
            {
                if (_pause == false)
                {
                    //check if stack is finished building

                    //if build not complete, leave function
                    if (_model.BuildComplete == false)
                    {
                        return;
                    }

                    //if stack building is complete, get fitness / update
                    if (_model.BuildComplete == true)
                    {
                        //calculate fitness
                        _analyser.Fitness();

                        //add stack to current generation
                        _currentStack.SetName("GEN " + generations + "_STACK " + _curCount);
                        _currentStack.UpdateDataText(); //updates the datatext

                        //add the stack to the generation
                        AddStackToGeneration(_currentStack);

                        _curCount++;

                        //if count == popsize recalculate the mating pool
                        if (_curCount == _genSize)
                        {
                            //add generation to the population history
                            AddGenToPopulation(_currentGenerationData);

                            //run natural selection to generate breeding pool - threshold value: include best performers above threshhold value (between 0,1))
                            UpdateMatingPool(0.5f);

                            SerializeData(_currentGenerationData);

                            //reset current population array
                            _currentGenerationData = new CellStackData[_genSize];

                            //reset popcounter
                            _curCount = 0;
                            generations++;
                        }

                        //breed new dna from mating pool
                        IDNAI childdna = Breed();

                        //reset the stack and insert new dna
                        _currentStack.Restore();
                        _currentStack.SetDNA(childdna);
                        _model.Stack = _currentStack;

                        //ONLY IF USING GENES FOR PARTIAL IMAGE SEEDS - synthesize 4 images from child genes - don't use this if you dont have genes for image textures
                        if (_SeedFromGenes == true)
                        {
                            Texture2D texture1 = _seeds[childdna.GetGene(0)];
                            Texture2D texture2 = _seeds[childdna.GetGene(1)];
                            Texture2D texture3 = _seeds[childdna.GetGene(2)];
                            Texture2D texture4 = _seeds[childdna.GetGene(3)];
                            Texture2D combined = ImageSynthesizer.CombineFour(texture1, texture2, texture3, texture4, _currentStack.RowCount, _currentStack.ColumnCount);

                            //place the synthesized image into the stack
                            _currentStack.SetSeed(combined);

                            //resets/initializes the model using the synthesized image
                            _model.ResetModel(combined);
                            _analyser.ResetAnalysis();
                        }

                        else
                        {
                            //resets/initializes the model using original seedimage
                            _model.ResetModel();
                            _analyser.ResetAnalysis();
                        }
                    }
                }
            }
Beispiel #2
0
            /// <summary>
            ///
            /// </summary>
            private void Update()
            {
                //check if stack is finished building

                //if build not complete, leave function
                if (_model.BuildComplete == false)
                {
                    return;
                }

                //if stack building is complete, get fitness / update
                if (_model.BuildComplete == true)
                {
                    //calculate fitness
                    _analyser.Fitness();

                    //move the stack position
                    var     generations = _population.Population.Count + 1;
                    Vector3 vector      = new Vector3(1.5f * (_currentStack.RowCount) * (_curCount - 1), 0, 1.5f * (_currentStack.ColumnCount));
                    _currentStack.transform.localPosition = vector;
                    _currentStack.transform.parent        = gameObject.transform;

                    //add stack to current generation
                    AddStackToGeneration(_currentStack);

                    _curCount++;

                    //if count == popsize recalculate the mating pool
                    if (_curCount == _genSize)
                    {
                        //add generation to the population history
                        AddGenToPopulation(_currentGeneration);

                        //run natural selection to generate breeding pool
                        UpdateMatingPool();

                        //reset current population array
                        _currentGeneration = new CellStack[_genSize];

                        //reset popcounter
                        _curCount = 0;

                        //move population
                        Vector3 vec = new Vector3(0, 0, 1.5f * (_currentStack.ColumnCount));
                        foreach (var stack in _population.Population)
                        {
                            stack.transform.localPosition += vec;
                        }
                    }

                    //breed new dna from mating pool
                    IDNAF childdna = Breed();

                    //turn off/deactivate the stack
                    _currentStack.gameObject.SetActive(false);

                    Debug.Log("Generation: " + generations + ", Stack: " + _curCount + " | Fitness= " + _currentStack.Fitness);

                    //reset the stack and insert new dna
                    _currentStack = Instantiate(_stackPrefab);
                    _currentStack.SetDNA(childdna);
                    _model.SetStack(_currentStack);

                    //synthesize 4 images from child gene
                    Texture2D texture1 = _seeds[Mathf.RoundToInt(childdna.GetGene(0))];
                    Texture2D texture2 = _seeds[Mathf.RoundToInt(childdna.GetGene(1))];
                    Texture2D texture3 = _seeds[Mathf.RoundToInt(childdna.GetGene(2))];
                    Texture2D texture4 = _seeds[Mathf.RoundToInt(childdna.GetGene(3))];
                    Texture2D combined = ImageSynthesizer.CombineFour(texture1, texture2, texture3, texture4, _currentStack.RowCount, _currentStack.ColumnCount);

                    //place the synthesized image into the stack
                    _currentStack.SetSeed(combined);

                    //resets/initializes the model using the synthesized image
                    _model.ResetModel(combined);
                    //_model.ResetModel();
                    //Debug.Log("I reseted the model");
                }
            }