Example #1
0
    public void MakeNewGeneration()
    {
        currentGeneration++;
        currentSize = 0;
        int poolSize = matingPool.Count;
        List <GameObject> nextGeneration = new List <GameObject>();

        for (int i = 0; i < simManager.generationSize; i++)
        {
            GameObject parent1 = matingPool[Random.Range(0, poolSize - 1)];
            GameObject parent2 = matingPool[Random.Range(0, poolSize - 1)];
            nextGeneration.Add(organisms[i]);

            if (parent1.GetComponent <Organism>().HasTopMarker)
            {
                parent2 = parent1;
            }
            else if (parent2.GetComponent <Organism>().HasTopMarker)
            {
                parent1 = parent2;
            }
            GameObject offspring = PopGen.Reproduce(parent1, parent2, currentGeneration, i);
            nextGeneration[i].GetComponent <Organism>().SetNewOrganism(offspring.GetComponent <Organism>());
            Destroy(offspring);
        }
        organisms = PopGen.SortByFitness(nextGeneration);
        organisms = PopGen.SetMatingChances(organisms, simManager.generationSize);
        UpdateBestOrganism();
        matingPool = PopGen.GenerateMatingPool(organisms);
    }
Example #2
0
        private async void btnGeneratePopulation_Click(object sender, EventArgs e)
        {
            int cycles = Convert.ToInt32(this.numCycles.Value);

            Progress <IDNA> progress = new Progress <IDNA>(world => {
                this.UpdateBestFitnessLabels();
                this.ipStatus.TransformOrigin.X = -PopGen.StatusGraph.GetWidth();
                this.ipStatus.TransformOrigin.Y = -PopGen.StatusGraph.GetHeight();
            });

            this.btnGeneratePopulation.Enabled = false;
            this.btnRestrategize.Enabled       = false;
            this.btnStart.Enabled = false;

            Stopwatch w = Stopwatch.StartNew();
            await Task.Factory.StartNew(() => PopGen.GeneratePopulation(cycles, progress), TaskCreationOptions.LongRunning);

            w.Stop();
            Debug.WriteLine(w.ElapsedMilliseconds);

            this.btnGeneratePopulation.Enabled = true;
            this.btnRestrategize.Enabled       = true;
            this.btnStart.Enabled = true;

            this.UpdateBestFitnessLabels();
            this.ipStrategy.Refresh();
        }
Example #3
0
    public void SetNewOrganism(Organism other)
    {
        this.generation = other.Generation;
        this.id         = other.Id;
        this.name       = other.name;
        this.DNA        = other.DNA;
        Renderer rend = GetComponent <Renderer>();

        rend.material.color = PopGen.ColorAlleleToColor(dna["color"]);
        fitness             = other.fitness;
        parents             = other.parents;
    }
Example #4
0
 private void UpdateUI()
 {
     genLabel.text       = "Current Generation: " + currentGeneration.ToString().PadLeft(5, '0');
     bestOrgName.text    = "Name: ";
     bestOrgId.text      = "ID: ";
     bestOrgColor.text   = "Color Allele: ";
     bestOrgFitness.text = "Fitness: ";
     bestOrgParents.text = "Parents: {";
     if (bestOrganism != null)
     {
         bestOrgName.text    += bestOrganism.name;
         bestOrgId.text      += bestOrganism.Id;
         bestOrgColor.text   += PopGen.AlleleToString(bestOrganism.DNA["color"]);
         bestOrgFitness.text += bestOrganism.Fitness.ToString();
         bestOrgParents.text += bestOrganism.Parents[0] + ", " + bestOrganism.Parents[1];
     }
     bestOrgParents.text += "}";
 }
Example #5
0
    //Generate organisms "DNA" and other attributes
    private Organism SetupOrganism(Organism organism)
    {
        organism.Waypoints            = waypoints;
        organism.DestinationThreshold = simManager.destinationThreshold;
        organism.MoveSpeed            = simManager.moveSpeed;
        organism.WanderWaitTime       = simManager.wanderWaitTime;

        organism.Generation = currentGeneration;
        organism.Id         = PopGen.MakeId(currentGeneration, currentSize);
        organism.name       = PopGen.MakeOrganismName();
        organism.DNA        = PopGen.MakeOrganismDNA();

        Renderer rend = organism.GetComponent <Renderer>();

        rend.material       = new Material(Shader.Find("Standard"));
        rend.material.color = PopGen.ColorAlleleToColor(organism.DNA["color"]);

        organism.Fitness = PopGen.EvaluateFitness("color", organism.DNA["color"]);
        organism.Parents = new string[] { "?????-?????", "?????-?????" };
        return(organism);
    }
Example #6
0
 private void MakeInitPopulation()
 {
     if (currentSize < simManager.generationSize)
     {
         GameObject obj = SpawnOrganism();
         obj.AddComponent <Organism>();
         SetupOrganism(obj.GetComponent <Organism>());
         organisms.Add(obj);
         obj.GetComponent <Organism>().InitOrganism();
         currentSize++;
     }
     else if (currentSize == simManager.generationSize && !doneGenProcessing)
     {
         organisms         = PopGen.SortByFitness(organisms);
         organisms         = PopGen.SetMatingChances(organisms, simManager.generationSize);
         doneGenProcessing = true;
         UpdateBestOrganism();
         matingPool = PopGen.GenerateMatingPool(organisms);
         generationButton.SetActive(true);
         resetButton.SetActive(true);
     }
 }