/// <summary> /// /// </summary> private void CombineHalfRandom(IDNAF dna1, IDNAF dna2) { for (int i = 0; i < _dnaLength; i++) { _genes[i] = Random.Range(0, 10) < 5 ? dna1.Genes[i] : dna2.Genes[i]; } }
/// <summary> /// /// </summary> private void CombineHalf(IDNAF dna1, IDNAF dna2) { for (int i = 0; i < _dnaLength; i++) { _genes[i] = i < (_dnaLength / 2) ? dna1.Genes[i] : dna2.Genes[i]; } }
/// <summary> /// Create child dna by breeding two parents from the mating pool /// </summary> /// <param name="dna1"></param> /// <param name="dna2"></param> /// <returns></returns> private IDNAF Breed() { IDNAF child = new DNAF(); IDNAF parent1 = _matingPool[UnityEngine.Random.Range(0, _matingPool.Count)]; IDNAF parent2 = _matingPool[UnityEngine.Random.Range(0, _matingPool.Count)]; child.Crossover(parent1, parent2); return(child); }
/// <summary> /// /// </summary> private void Start() { _model = GetComponent <StackModel>(); _analyser = GetComponent <StackAnalyser>(); _dna = _model.Stack.DNA; /* * instructionSetArray = new GOLInstructionSet[5]; * * instructionSetArray[0] = _instSetMO1; * instructionSetArray[1] = _instSetMO2; * instructionSetArray[2] = _instSetMO3; * instructionSetArray[3] = _instSetMO5; * instructionSetArray[4] = _instSetMO8; */ }
/// <summary> /// /// </summary> /// <param name="dna"></param> public void SetDNA(IDNAF dna) { _dna = dna; }
/// <summary> /// /// </summary> private void Awake() { _dna = new DNAF(); InitializeCells(); }
/// <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"); } }
/// <summary> /// /// </summary> /// <param name="dna1"></param> /// <param name="dna2"></param> public void Crossover(IDNAF dna1, IDNAF dna2) { CombineHalfRandom(dna1, dna2); Mutate(); }