Exemplo n.º 1
0
        public void AddKerbal(KerbalExt kerbal)
        {
            if (kerbal[name] != null)
            {
                // already added via an initialization race
                Debug.LogFormat("[Genome] AddKerbal: double add {0}",
                                kerbal.kerbal.name);
                return;
            }
            var genes = new GenePair[traits.Length];

            kerbal[name] = genes;
            if (kerbal.kerbal.name != null)
            {
                RebuildGenes(kerbal.kerbal, genes);
            }
            else
            {
                if (loading_kerbals == null)
                {
                    loading_kerbals = new List <KerbalExt> ();
                }
                loading_kerbals.Add(kerbal);
            }
        }
Exemplo n.º 2
0
        public static GenePair Combine(GenePair g1, GenePair g2)
        {
            uint  a, b;
            Trait trait = g1.trait;

            if (UnityEngine.Random.Range(0, 2) != 0)
            {
                a = g1.a;
            }
            else
            {
                a = g1.b;
            }

            if (UnityEngine.Random.Range(0, 2) != 0)
            {
                b = g2.a;
            }
            else
            {
                b = g2.b;
            }

            if (UnityEngine.Random.Range(0, 2) != 0)
            {
                return(new GenePair(trait, a, b));
            }
            else
            {
                return(new GenePair(trait, b, a));
            }
        }
Exemplo n.º 3
0
        public static GenePair[] Combine(GenePair[] kerbal1, GenePair[] kerbal2)
        {
            var genes = new GenePair[traits.Length];

            for (int i = 0; i < genes.Length; i++)
            {
                genes[i] = GenePair.Combine(kerbal1[i], kerbal2[i]);
            }
            return(genes);
        }
Exemplo n.º 4
0
        public static GenePair[] ReadGenes(ConfigNode node)
        {
            var pairs = node.values;

            GenePair[] genes = new GenePair[traits.Length];
            for (int i = 0; i < pairs.Count; i++)
            {
                var trait_name  = pairs[i].name;
                var trait_value = pairs[i].value;
                if (trait_map.ContainsKey(trait_name))
                {
                    var ind   = trait_map[trait_name];
                    var trait = traits[ind];
                    genes[ind] = new GenePair(trait, trait_value);
                }
            }
            return(genes);
        }
Exemplo n.º 5
0
        ContinuousDistribution ChooseDistribution(GenePair gene)
        {
            uint index = (gene.a & 1) + (gene.b & 1);

            return(distributions[index]);
        }
Exemplo n.º 6
0
        public string CreateValue(GenePair gene)
        {
            ContinuousDistribution dist = ChooseDistribution(gene);

            return(dist.Value(UnityEngine.Random.Range(0, 1f)).ToString("G9"));
        }
Exemplo n.º 7
0
        DiscreteDistribution ChooseDistribution(GenePair gene)
        {
            int index = CountBits(gene.a & 7) + CountBits(gene.b & 7);

            return(distributions[index]);
        }
Exemplo n.º 8
0
        public string CreateValue(GenePair gene)
        {
            DiscreteDistribution dist = ChooseDistribution(gene);

            return((dist.Value(UnityEngine.Random.Range(0, 1f)) > 0).ToString());
        }
Exemplo n.º 9
0
        static void Main()
        {
            var dist = new ContinuousDistribution(pdf3, 0, 1, cdf3);

            for (int i = 0; i < 11; i++)
            {
                float x = i / 10f;
                Console.WriteLine(String.Format("{0}; {1} {2} {3} {4}", x, pdf3(x), dist.Density(x), cdf3(x), dist.Cumulation(x)));
            }
            int [] counts = new int [11];
            for (int i = 0; i < 10000; i++)
            {
                float x = dist.Value(Random.Range(0, 1f));
                counts[(int)(x * 10 + 0.5)]++;
            }
            for (int i = 0; i < 11; i++)
            {
                Console.WriteLine(String.Format("{0}; {1}", i / 10f, counts[i]));
            }

            var ddist = new DiscreteDistribution(new float[] { 1, 2, 3, 1 });

            int [] dcounts = new int [4];
            for (int i = 0; i < 10000; i++)
            {
                int v = ddist.Value(Random.Range(0, 1f));
                dcounts[v]++;
            }
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(String.Format("{0}; {1}", i, dcounts[i]));
            }

            Trait badass = new BadAss();

            Console.WriteLine("BadAss True");
            for (int i = 0; i < 20; i++)
            {
                GenePair gene = badass.CreateGene("True");
                Console.WriteLine(String.Format("{0}, {1}: {2}", gene.a, gene.b, badass.CreateValue(gene)));
            }
            Console.WriteLine("BadAss False");
            for (int i = 0; i < 20; i++)
            {
                GenePair gene = badass.CreateGene("False");
                Console.WriteLine(String.Format("{0}, {1}: {2}", gene.a, gene.b, badass.CreateValue(gene)));
            }

            Trait courage = new Courage();

            Console.WriteLine("Courage 0..1");
            for (int i = 0; i < 11; i++)
            {
                float    c    = i / 10f;
                GenePair gene = courage.CreateGene(c.ToString("G9"));
                Console.WriteLine(String.Format("{0}: {1}, {2}: {3}", c, gene.a, gene.b, courage.CreateValue(gene)));
            }

            Trait stupidity = new Stupidity();

            Console.WriteLine("Stupidity 0..1");
            for (int i = 0; i < 11; i++)
            {
                float    c    = i / 10f;
                GenePair gene = stupidity.CreateGene(c.ToString("G9"));
                Console.WriteLine(String.Format("{0}: {1}, {2}: {3}", c, gene.a, gene.b, stupidity.CreateValue(gene)));
            }

            Trait gender = new Gender();

            Console.WriteLine("Gender");
            {
                GenePair gene = gender.CreateGene("M");
                Console.WriteLine(String.Format("{0}: {1}, {2}: {3}", "M", gene.a, gene.b, gender.CreateValue(gene)));
                gene = gender.CreateGene("F");
                Console.WriteLine(String.Format("{0}: {1}, {2}: {3}", "F", gene.a, gene.b, gender.CreateValue(gene)));
            }
        }
Exemplo n.º 10
0
        public string CreateValue(GenePair gene)
        {
            uint index = (gene.a & 1) + (gene.b & 1);

            return(genders[index]);
        }
Exemplo n.º 11
0
 public GenePair(GenePair gene)
 {
     trait = gene.trait;
     a     = gene.a;
     b     = gene.b;
 }