/// <summary> /// /// </summary> private void CombineHalf(IDNAI dna1, IDNAI dna2) { for (int i = 0; i < _dnaLength; i++) { _genes[i] = i < (_dnaLength / 2) ? dna1.Genes[i] : dna2.Genes[i]; } }
/// <summary> /// /// </summary> private void Awake() { _properties = new MaterialPropertyBlock(); _dna = new DNAI(); InitializeCells(); SetupUIText(); }
/// <summary> /// /// </summary> private void CombineHalfRandom(IDNAI dna1, IDNAI dna2) { for (int i = 0; i < _dnaLength; i++) { _genes[i] = Random.Range(0, 10) < 5 ? 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 IDNAI Breed() { IDNAI child = new DNAI(); IDNAI parent1 = _matingPool[UnityEngine.Random.Range(0, _matingPool.Count)]; IDNAI parent2 = _matingPool[UnityEngine.Random.Range(0, _matingPool.Count)]; child.Crossover(parent1, parent2); return(child); }
public void ResetStackFromStackData() { OpenFileDialog choofdlog = new OpenFileDialog(); choofdlog.Filter = "All Files (*.*)|*.*"; choofdlog.FilterIndex = 1; choofdlog.Multiselect = true; if (choofdlog.ShowDialog() == DialogResult.OK) { string sFileName = choofdlog.FileName; string[] arrAllFiles = choofdlog.FileNames; //used when Multiselect = true CellStackData stackdata = Interop.DeserializeBinary <CellStackData>(sFileName); if (stackdata.ColumnCount != _rowCount || stackdata.RowCount != _columnCount || stackdata.LayerCount != _layerCount) { _columnCount = stackdata.ColumnCount; _rowCount = stackdata.RowCount; _layerCount = stackdata.LayerCount; InitializeCells(); } //set stack values _name = stackdata.Name; _meanStackDensity = stackdata.MeanStackDensity; _maxLayerDensity = stackdata.MaxLayerDensity; _minLayerDensity = stackdata.MinLayerDensity; _maxAge = stackdata.MaxAge; _avgAge = stackdata.AvgAge; _fitness = stackdata.Fitness; _dna = DNAI.CreateFromGenes(stackdata.DNAGENES); UpdateDataText(); for (int z = 0; z < _layerCount; z++) { CellLayer layer = _layers[z]; //set layer values layer.Density = stackdata.LayerDensities[z]; layer.AvgAge = stackdata.LayerAvgAges[z]; layer.MaxAge = stackdata.LayerMaxAges[z]; for (int x = 0; x < _rowCount; x++) { for (int y = 0; y < _columnCount; y++) { Cell cell = layer.Cells[x, y]; cell.State = 0; //set cell values cell.Age = stackdata.CellAges[x, y, z]; cell.State = stackdata.CellStates[x, y, z]; } } } } }
/// <summary> /// /// </summary> /// <param name="dna"></param> public void SetDNA(IDNAI dna) { _dna = dna; }
/// <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(); } } } }
/// <summary> /// /// </summary> /// <param name="dna1"></param> /// <param name="dna2"></param> public void Crossover(IDNAI dna1, IDNAI dna2) { CombineHalfRandom(dna1, dna2); Mutate(); }
/// <summary> /// /// </summary> /// <param name="i"></param> /// <param name="j"></param> /// <param name="current"></param> /// <returns></returns> public int NextState(Index2 index, int[,] current) { if (_dna != _model.Stack.DNA) { _dna = _model.Stack.DNA; } //get current state int state = current[index.I, index.J]; //get local neighborhood data int sumMO = GetNeighborSum(index, current, Neighborhoods.MooreR1); int sumVNPair = GetNeighborSum(index, current, Neighborhoods.VonNeumannPair1); //choose an instruction set GOLInstructionSet instructionSet = _instSetMO1; // collect relevant analysis results CellLayer[] layers = _model.Stack.Layers; int currentLayer = _model.CurrentLayer; float prevLayerDensity; int prevCellAge; // get attributes of corresponding cell on the previous layer (if it exists) if (currentLayer > 0) { var prevLayer = layers[currentLayer - 1]; prevLayerDensity = prevLayer.Density; prevCellAge = prevLayer.Cells[index.I, index.J].Age; } else { prevLayerDensity = 1.0f; prevCellAge = 0; } /* * if (currentlayerdensity < .17) * { * instructionSet = _instSetMO3; * } * * if (currentlayerdensity >= .17 && currentlayerdensity<.2) * { * instructionSet = _instSetMO1; * } * * if (currentlayerdensity >.2) * { * instructionSet = _instSetMO2; * } */ /* * if(state==0 && sumVNPair == 2) * { * return 1; * } * * if (state == 1 && sumVNPair == 2) * { * return 0; * } */ //example of using Gene from DNA if (currentLayer <= _dna.GetGene(4)) { instructionSet = _instSetMO2; } float threshholdValue = (float)_dna.GetGene(5) / 100f; if (prevLayerDensity <= threshholdValue) { instructionSet = _instSetMO2; } int output = 0; //if current state is "alive" if (state == 1) { if (sumMO < instructionSet.getInstruction(0)) { output = 0; } if (sumMO >= instructionSet.getInstruction(0) && sumMO <= instructionSet.getInstruction(1)) { output = 1; } if (sumMO > instructionSet.getInstruction(1)) { output = 0; } } //if current state is "dead" if (state == 0) { if (sumMO >= instructionSet.getInstruction(2) && sumMO <= instructionSet.getInstruction(3)) { output = 1; } else { output = 0; } } return(output); }
/// <summary> /// /// </summary> private void Start() { _model = GetComponent <StackModelManager>(); _analyser = GetComponent <StackAnalyser>(); _dna = _model.Stack.DNA; }
public FitnessDNA(CellStackData data) { fitness = data.Fitness; DNA = DNAI.CreateFromGenes(data.DNAGENES); }