Beispiel #1
0
    public Flower(Gene[][] genesPoss, Fraction[] genesProbs, FlowerType type, FlowerColour colour)
    {
        this.genesPoss  = genesPoss;
        this.genesProbs = genesProbs;
        this.type       = type;
        this.colour     = colour;

        Validate();
    }
Beispiel #2
0
    public Flower(string[] geneCodes, Fraction[] genesProbs, FlowerType type, FlowerColour colour)
    {
        //convert the string gene codes into arrays of Gene objects e.g. 210 to [Gene(TT), Gene(TF) , Gene(FF)].
        genesPoss = geneCodes.Select(x => x.Select(g => new Gene(g)).ToArray()).ToArray();

        this.genesProbs = genesProbs;
        this.type       = type;
        this.colour     = colour;

        Validate();
    }
Beispiel #3
0
    /// <summary>
    /// Gets a generuc flower of a given type and colour.
    /// All possible genetics codes have equal probablity to start with.
    /// </summary>
    /// <param name="type"></param>
    /// <param name="colour"></param>
    /// <returns>Flower object of colour and type</returns>
    public Flower GetFlowerWithColour(FlowerType type, FlowerColour colour)
    {
        if (type is FlowerType.NONE || colour is FlowerColour.NONE)
        {
            return(PredefinedFlowers.noFlower);
        }

        string[] possibleGenes = geneLookup[type][colour].ToArray();

        Fraction[] probabilities = new Fraction[possibleGenes.Length];
        probabilities = probabilities.Select(x => new Fraction(1, possibleGenes.Length)).ToArray();

        return(new Flower(possibleGenes, probabilities, type, colour));
    }
Beispiel #4
0
    /// <summary>
    /// Reads the CSV files found in the FlowerData folder within the base lookup folder.
    /// Loads them into dictionaries to allow searches:
    ///     - 1: for possible genes based on colours and flower type
    ///     - 2: for the expressed colour based on genes and flower type
    /// </summary>
    private void InitialiseLookups()
    {
        lookup = this;

        foreach (FlowerType flowerType in Enum.GetValues(typeof(FlowerType)))
        {
            if (flowerType.ToString() == "NONE")
            {
                continue;
            }

            string csvName = Path.Combine(FLOWER_FOLDER, flowerType.ToString() + ".csv");

            var flowerColLookup  = new Dictionary <string, FlowerColour>();
            var flowerGeneLookup = new Dictionary <FlowerColour, List <string> >();

            try
            {
                using (StreamReader reader = new StreamReader(File.OpenRead(csvName)))
                {
                    while (!reader.EndOfStream)
                    {
                        string   line   = reader.ReadLine();
                        string[] values = line.Split(',');

                        string[] genes  = values.Take(values.Length - 1).ToArray();
                        string   geneID = String.Join("", genes);

                        FlowerColour colour = (FlowerColour)Enum.Parse(typeof(FlowerColour), values[values.Length - 1]);

                        flowerColLookup[geneID] = colour;

                        if (!flowerGeneLookup.ContainsKey(colour))
                        {
                            flowerGeneLookup[colour] = new List <string>();
                        }
                        flowerGeneLookup[colour].Add(geneID);
                    }
                }
                colourLookup[flowerType] = flowerColLookup;
                geneLookup[flowerType]   = flowerGeneLookup;
            }
            catch (Exception e)
            {
                Debug.Log("ERROR " + csvName + " " + e.ToString());
            }
        }
    }
Beispiel #5
0
    public bool AddOffspring(FlowerType type, FlowerColour colour, int x, int y)
    {
        if (GetFlower(x, y).type != FlowerType.NONE)
        {
            return(false);
        }

        Dictionary <(int, int, int, int), int> possibleParents = GetPossibleParents(type, x, y);

        if (possibleParents.Count == 0)
        {
            return(false);
        }

        Dictionary <(int, int, int, int), Fraction>           parentLiklihoods = GetLiklihoodsOfParents(possibleParents, colour);
        Dictionary <(int, int), List <(int, int, Fraction)> > combosPerFlower  = GetCombosForEachFlower(parentLiklihoods);

        // get the possible genes of this offspring and add it to the grid
        var genesOffspring = new Dictionary <String, Fraction>();

        foreach ((int x1, int y1, int x2, int y2) in parentLiklihoods.Keys)
        {
            Flower offspring = GetFlower(x1, y1);
            if (x1 != x2 || y1 != y2)
            {
                offspring = GetFlower(x1, y1).BreedWith(GetFlower(x2, y2));
            }

            for (int i = 0; i < offspring.genesPoss.Count(); i++)
            {
                string offspringGeneCode = Gene.getString(offspring.genesPoss[i]);
                if (FlowerColourLookup.lookup.colourLookup[type][offspringGeneCode] == colour)
                {
                    if (!genesOffspring.ContainsKey(offspringGeneCode))
                    {
                        genesOffspring[offspringGeneCode] = new Fraction(0, 0);
                    }
                    genesOffspring[offspringGeneCode] += offspring.genesProbs[i] * parentLiklihoods[(x1, y1, x2, y2)];