예제 #1
0
        public void InvalidChromosome()
        {
            var alleles = ImmutableSeq.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            var gene    = new EnumGene <int>(3, alleles);
            var genes   = MutableSeq.OfLength <EnumGene <int> >(10).Fill(() => gene).ToImmutableSeq();

            var chromosome = new PermutationChromosome <int>(genes);

            Assert.False(chromosome.IsValid);
        }
예제 #2
0
 public static IAlterer <TGene, TAllele> Of <TGene, TAllele>(params IAlterer <TGene, TAllele>[] alterers)
     where TGene : IGene <TGene>
     where TAllele : IComparable <TAllele>, IConvertible
 {
     return(alterers.Length == 0
         ? new DefaultAlterer <TGene, TAllele>()
         : alterers.Length == 1
             ? alterers[0]
             : alterers.Length == 1
                 ? alterers.First()
                 : new CompositeAlterer <TGene, TAllele>(ImmutableSeq.Of(alterers)));
 }
예제 #3
0
            private int Mutate(IList <IChromosome <TGene> > c, int i, double p)
            {
                var           chromosome = c[i];
                IList <TGene> genes      = new List <TGene>(chromosome.ToSeq());

                var mutations = Mutate(genes, p);

                if (mutations > 0)
                {
                    c[i] = chromosome.NewInstance(ImmutableSeq.Of((IEnumerable <TGene>)genes));
                }
                return(mutations);
            }
예제 #4
0
        public static void Main()
        {
            const double maxPricePerUniqueSong = 2.5;

            var springsteen = new Springsteen(
                ImmutableSeq.Of(
                    new Record("Record1", 25, ImmutableSeq.Of("Song1", "Song2", "Song3", "Song4", "Song5", "Song6")),
                    new Record("Record2", 15, ImmutableSeq.Of("Song2", "Song3", "Song4", "Song5", "Song6", "Song7")),
                    new Record("Record3", 35, ImmutableSeq.Of("Song5", "Song6", "Song7", "Song8", "Song9", "Song10")),
                    new Record("Record4", 17,
                               ImmutableSeq.Of("Song9", "Song10", "Song12", "Song4", "Song13", "Song14")),
                    new Record("Record5", 29,
                               ImmutableSeq.Of("Song1", "Song2", "Song13", "Song14", "Song15", "Song16")),
                    new Record("Record6", 5, ImmutableSeq.Of("Song18", "Song20", "Song30", "Song40"))
                    ),
                maxPricePerUniqueSong
                );

            var engine = Engine.Engine.Builder(springsteen).Build();

            var result = springsteen.Codec().Decoder()(
                engine.Stream()
                .Take(10)
                .ToBestGenotype()
                );

            var cost = result
                       .Select(r => r.Price)
                       .Sum();

            var uniqueSongCount = result
                                  .Select(r => r.Songs)
                                  .ToHashSet()
                                  .Count;

            //var pricePerUniqueSong = cost / uniqueSongCount;

            Console.WriteLine("Overall cost:  " + cost);
            Console.WriteLine("Unique songs:  " + uniqueSongCount);
            Console.WriteLine("Cost per song: " + cost / uniqueSongCount);
            Console.WriteLine("Records:       " + string.Join(", ", result));
        }
예제 #5
0
 public static IntegerChromosome Of(params IntegerGene[] genes)
 {
     return(new IntegerChromosome(ImmutableSeq.Of(genes)));
 }
예제 #6
0
 public static DoubleChromosome Of(params DoubleGene[] genes)
 {
     return(new DoubleChromosome(ImmutableSeq.Of(genes)));
 }
예제 #7
0
        public static IEnumerable <object[]> CrossoverParameters()
        {
            return(new[]
            {
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Empty <int>(),
                    "0123456789", "ABCDEFGHIJ"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0),
                    "ABCDEFGHIJ", "0123456789"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(1),
                    "0BCDEFGHIJ", "A123456789"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(2),
                    "01CDEFGHIJ", "AB23456789"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(3),
                    "012DEFGHIJ", "ABC3456789"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(8),
                    "01234567IJ", "ABCDEFGH89"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(9),
                    "012345678J", "ABCDEFGHI9"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(10),
                    "0123456789", "ABCDEFGHIJ"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1),
                    "A123456789", "0BCDEFGHIJ"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(3, 5),
                    "012DE56789", "ABC34FGHIJ"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2),
                    "A1CDEFGHIJ", "0B23456789"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3),
                    "A1C3456789", "0B2DEFGHIJ"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3, 4),
                    "A1C3EFGHIJ", "0B2D456789"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5),
                    "A1C3E56789", "0B2D4FGHIJ"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6),
                    "A1C3E5GHIJ", "0B2D4F6789"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6, 7),
                    "A1C3E5G789", "0B2D4F6HIJ"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6, 7, 8),
                    "A1C3E5G7IJ", "0B2D4F6H89"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
                    "A1C3E5G7I9", "0B2D4F6H8J"
                },
                new object[]
                {
                    "0123456789", "ABCDEFGHIJ",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                    "A1C3E5G7I9", "0B2D4F6H8J"
                },

                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Empty <int>(),
                    "012345678", "ABCDEFGHI"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0),
                    "ABCDEFGHI", "012345678"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(1),
                    "0BCDEFGHI", "A12345678"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(2),
                    "01CDEFGHI", "AB2345678"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(3),
                    "012DEFGHI", "ABC345678"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(8),
                    "01234567I", "ABCDEFGH8"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(9),
                    "012345678", "ABCDEFGHI"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1),
                    "A12345678", "0BCDEFGHI"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(3, 5),
                    "012DE5678", "ABC34FGHI"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2),
                    "A1CDEFGHI", "0B2345678"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2, 3),
                    "A1C345678", "0B2DEFGHI"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2, 3, 4),
                    "A1C3EFGHI", "0B2D45678"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5),
                    "A1C3E5678", "0B2D4FGHI"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6),
                    "A1C3E5GHI", "0B2D4F678"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6, 7),
                    "A1C3E5G78", "0B2D4F6HI"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6, 7, 8),
                    "A1C3E5G7I", "0B2D4F6H8"
                },
                new object[]
                {
                    "012345678", "ABCDEFGHI",
                    ImmutableSeq.Of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
                    "A1C3E5G7I", "0B2D4F6H8"
                },

                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Empty <int>(),
                    "0123456789", "ABCDEF"
                },
                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Of(0),
                    "ABCDEF6789", "012345"
                },
                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Of(1),
                    "0BCDEF6789", "A12345"
                },
                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Of(2),
                    "01CDEF6789", "AB2345"
                },
                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Of(3),
                    "012DEF6789", "ABC345"
                },
                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Of(5),
                    "01234F6789", "ABCDE5"
                },
                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Of(6),
                    "0123456789", "ABCDEF"
                },
                new object[]
                {
                    "0123456789", "ABCDEF",
                    ImmutableSeq.Of(1, 3),
                    "0BC3456789", "A12DEF"
                }
            });
        }