Esempio n. 1
0
        public override IChromosome Clone()
        {
            BitArrayChromosome clone = new BitArrayChromosome(_length)
            {
                Value   = new BitArray(Value),
                fitness = fitness
            };

            return(clone);
        }
Esempio n. 2
0
        public double Evaluate(IChromosome chromosome)
        {
            BitArrayChromosome bitArrayChromosome = (BitArrayChromosome)chromosome;

            if (_problem.IsSatisfiedSafe(bitArrayChromosome.Value))
            {
                return(_problem.GetWeightSafe(bitArrayChromosome.Value) + 100);
            }

            double percentageOfSatisfiedClausulesSafe = _problem.GetPercentageOfSatisfiedClausulesSafe(bitArrayChromosome.Value);

            if (Math.Abs(percentageOfSatisfiedClausulesSafe - 0) < 0.001)
            {
                return(1);
            }

            return(percentageOfSatisfiedClausulesSafe);
        }
Esempio n. 3
0
        public override void Crossover(IChromosome pair)
        {
            BitArrayChromosome p = (BitArrayChromosome)pair;

            if (p != null && p._length == _length)
            {
                int      crossoverPoint = Random.Next(_length);
                BitArray mask1          = new BitArray(_length, false);
                BitArray mask2          = new BitArray(_length, true);
                for (int i = crossoverPoint; i < _length; i++)
                {
                    mask1[i] = true;
                    mask2[i] = false;
                }
                BitArray newOne    = Value.And(mask1).Or(p.Value.And(mask2));
                BitArray newSecond = p.Value.And(mask1).Or(Value.And(mask2));

                Value   = newOne;
                p.Value = newSecond;
            }
        }