void Start() { startingAge = Map.minutes; _tracker = chromosome.MutationTracker; _viewChromosome = chromosome.ToString(); cells = new CellsStructure(); bodyEnergy = OrganismSetter.BodyEnergy(chromosome); orderOfGrowth = CellsCreator.GetOrderOfGrowth(this); OrganismSetter.SetNeuralNetwork(this); OrganismSetter.SetDrag(this); OrganismSetter.SetName(this); OrganismCellsGrowth.GrowCell(this, false); if (fullyGrow) { OrganismCellsGrowth.FullyGrow(this); } InvokeRepeating("Routine", 2f, 5f); InvokeRepeating("Think", 2f, SimulationParameters.brainTick); InvokeRepeating("Physics", 2f, SimulationParameters.physics); }
private void ComputeEfficiency() { CellsStructure cells = GetComponentInParent <Organism>().cells; efficiency = 1; for (int i = 0; i <= 2; i++) { for (int j = -1; j <= 1; j++) { Vector3 vect = attributes.angle * -(new Vector3(j, -i)); vect = new Vector3((float)Math.Round(vect.x, 3), (float)Math.Round(vect.y, 3)); CellAttributes cell = cells.GetCell(attributes.relativePosition + vect); if (cell != null && cell.alive && !(cell.type.GetComponent <Cell>() is FlagelloCell)) { Vector3 v = new Vector3(j * 7f, -i); efficiency -= 1 / (v.magnitude); } } } if (efficiency < 0) { efficiency = 0; } }
private static void KillNotVisited(CellsStructure cells, HashSet <Vector3> visitedCells) { foreach (CellAttributes cell in cells) { if ((!visitedCells.Contains(cell.relativePosition)) && cell.alive) { cell.instance.Die(false); } } }
public static bool AreValidCellsAlive(CellsStructure cells) { //Check that there is at least one cell that can live alone foreach (CellAttributes cell in cells) { if (cell.alive && cell.type.GetComponent <Cell>().CanLiveAlone()) { return(true); } } //if reach here all alive cells are not valid return(false); }
private static Vector3 GetMinNumber(CellsStructure cells) { int minNumber = int.MaxValue; Vector3 cellPosition = new Vector3(0, 0); foreach (CellAttributes cell in cells) { if (cell.alive && cell.number < minNumber) { minNumber = cell.number; cellPosition = cell.relativePosition; } } return(cellPosition); }
public static LinkedList <KeyValuePair <CellAttributes, CellAttributes> > GetOrderOfGrowth(Organism organism) { Chromosome chromosome = organism.chromosome; LinkedList <KeyValuePair <CellAttributes, CellAttributes> > orderOfGrowth = new LinkedList <KeyValuePair <CellAttributes, CellAttributes> >();//key= starting cell, value = new cell CellsStructure createdCells = new CellsStructure(); foreach (Gene gene in chromosome.Genes) { if (!orderOfGrowth.Any()) { CellAttributes firstCell = CreateCell(gene.Type, new Vector3(0, 0), new Quaternion(), gene.Number, organism.organismEnergy); orderOfGrowth.AddLast(new KeyValuePair <CellAttributes, CellAttributes>(null, firstCell)); createdCells.AddCell(firstCell); continue; } if (createdCells.ContainsNumber(gene.StartingCell)) { List <CellAttributes> targetCells = createdCells.GetCellsByNumber(gene.StartingCell); foreach (CellAttributes cell in targetCells) { if (AllowGrowthInDirection(cell, gene.RelativePosition)) { Vector3 pos = gene.RelativePosition; Quaternion angle = ComputeAngle(gene.RelativePosition); CellAttributes cellToGrow = CreateCell(gene.Type, pos, angle, gene.Number, organism.organismEnergy); orderOfGrowth.AddLast(new KeyValuePair <CellAttributes, CellAttributes>(cell, cellToGrow)); createdCells.AddCell(cellToGrow); } } } if (orderOfGrowth.Count >= MAX_NUMBER_OF_CELLS) { return(orderOfGrowth); } } return(orderOfGrowth); }
private static HashSet <Vector3> VisitCells(CellsStructure cells, HashSet <Vector3> cellsToVisit, Vector3 currentPosition) { HashSet <Vector3> visitedCells = new HashSet <Vector3>(); cellsToVisit.Add(currentPosition); Vector3[] vectors = Cell.vectors; while (cellsToVisit.Any()) { currentPosition = cellsToVisit.First(); CellAttributes currentCell = cells.GetCell(currentPosition); for (int i = 0; i < vectors.Length; i++) {//find cells nearby to visit //checks that the direction is allowed by this cell if (CellLink(currentCell, vectors[i])) { Vector3 pos = currentPosition + vectors[i]; if (!visitedCells.Contains(pos)) { CellAttributes cell = cells.GetCell(pos); if (cell != null && cell.alive && CellLink(cell, -vectors[i])) { cellsToVisit.Add(currentPosition + vectors[i]); } } } } visitedCells.Add(currentPosition); cellsToVisit.Remove(currentPosition); } return(visitedCells); }
public static void KillDeatachedCells(CellsStructure cells) { HashSet <Vector3> cellsToVisit = new HashSet <Vector3>(); Vector3 currentPosition = new Vector3(0, 0);//center CellAttributes startingCell = cells.GetCell(currentPosition); if (startingCell == null || startingCell.alive == false) {//find center currentPosition = GetMinNumber(cells); if (currentPosition == new Vector3(0, 0)) { return; //no cells are alive } } HashSet <Vector3> visitedCells = VisitCells(cells, cellsToVisit, currentPosition); //KILL ALL OF THE CELLS THAT WEREN'T VISITED KillNotVisited(cells, visitedCells); }