public BackPackIndividual BruteForce(BackPackEnvironment environment)
        {
            var possibleIndividuals = new List<BackPackIndividual>();

            int genomeSize = environment.ComputeGenomeSize();

            var chromosome = new int[genomeSize];
            var posssibilitiesPerInt = environment.AvailableItems.Count > 31 ?
                uint.MaxValue :
                (uint)1 << environment.AvailableItems.Count; // 2^n
            
            for (uint i = 0; i < posssibilitiesPerInt; i++)
            {
                for (int j = 0; j < genomeSize; j++)
                {
                    chromosome[j] = (int)i;
                    var individual = new BackPackIndividual(environment);
                    individual.Genome.Chromosome = (int[])chromosome.Clone();

                    if (environment.Validate(individual))
                    {
                        possibleIndividuals.Add(individual);
                    }
                }
            }

            return possibleIndividuals.Aggregate((a, b) => environment.RateFitness(a) > environment.RateFitness(b) ? a : b);
        }
        public void CrossIndividuals()
        {
            var backPack = new BackPack(volume: 1000);
            var items = BackPackEnvironmentTest.RandomItems(8, backPack.Volume, 100);
            var environment = new BackPackEnvironment(backPack, items);

            var a = new BackPackIndividual(environment) 
                { Genome = new BackPackIndividualGenome(new[]{0xF0}) }; //0b11110000
            var b = new BackPackIndividual(environment) 
                
                { Genome = new BackPackIndividualGenome(new[]{0xF}) }; //0b00001111
            
            var child = new BackPackIndividual(environment) 
                { Genome = new BackPackIndividualGenome(new[]{0xF7}) }; //0b11110111

            Assert.AreEqual(child.Genome.Chromosome, a.Cross(b).Genome.Chromosome);
        }
        public void ValidateIndividual()
        {
            var backPack = new BackPack(volume: 1000);
            var items = new List<BackPackItem>(
                new [] { 
                new BackPackItem(200, 20),
                new BackPackItem(300, 8),
                new BackPackItem(500, 30),
                new BackPackItem(150, 10)
            });

            var environment = new BackPackEnvironment(backPack, items);

            var invalidIndividual = new BackPackIndividual(environment);
            invalidIndividual.Genome.Chromosome = new[]{0xF}; //0xF=15=0b1111

            var validIndividual = new BackPackIndividual(environment);
            validIndividual.Genome.Chromosome = new[]{0x7}; //0x7=0b0111

            Assert.IsFalse(environment.Validate(invalidIndividual));
            Assert.IsTrue(environment.Validate(validIndividual));
        }
        public void RateFitnessOfRandomIndividuals()
        {
            const int tests = 3;

            for (int n = 0; n < tests; n++)
            {
                var backPack = new BackPack(volume: random.Next(10000));
                var items = RandomItems(random.Next(512), backPack.Volume, 100);

                var environment = new BackPackEnvironment(backPack, items);

                for (int i = 0; i <= items.Count; i++)
                {
                    // compute chromosome
                    var chromosome = new int[environment.ComputeGenomeSize()];
                        // set every bit that encodes an Item at an index j < i
                    for (int j = 0; j < i; j++)
                    {
                        ChromosomeIndex index = environment.ComputeChromosomeIndex(j);
                        IntBitHelper.Set(ref chromosome[index.IntIndex], index.BitIndex);
                    }

                    var individual = new BackPackIndividual(environment) 
                        { Genome = new BackPackIndividualGenome(chromosome) };

                    // compute fitness with Item-List
                    double fitness = 0.0;
                    for (int j = 0; j < i; j++)
                    {
                        fitness += items[j].Value;
                    }

                    Assert.AreEqual(fitness, environment.RateFitness(individual));
                }
            }
        }