示例#1
0
文件: Organism.cs 项目: ernado/Owl
        public List<GrayCode> GenerateFactors()
        {
            try
            {
                if (_factors.Count == 0) throw new ArgumentOutOfRangeException();

                var factors = new List<GrayCode>();
                var code = new List<bool>();
                code.AddRange(Alleles);
                foreach (GrayCode factor in _factors)
                {
                    uint bits = factor.BitAccuracy;
                    var newFactor = new GrayCode(0, factor.Config);
                    newFactor.BooleanCode = code.GetRange(0, (int) bits);
                    factors.Add(newFactor);
                    code.RemoveRange(0, (int) bits);
                }

                if (code.Count == 0)
                    return factors;

                throw new ArgumentOutOfRangeException();
            }
            catch (Exception e)
            {
                throw new Exception("Cant generate factors", e);
            }
        }
示例#2
0
 public void GrayCodeConstructorTest()
 {
     double value = 0.2;
     double min = 0F;
     double max = 2;
     uint bitAccuracy = 8;
     GrayCodeConfig config = new GrayCodeConfig(min, max, bitAccuracy);
     GrayCode target = new GrayCode(value, config);
 }
示例#3
0
 public void BinaryGTest()
 {
     double value = 0.5;
     double min = -1.0;
     double max = 1.0;
     uint bitAccuracy = 8;
     GrayCodeConfig config = new GrayCodeConfig(min, max, bitAccuracy);
     GrayCode target = new GrayCode(value, config);
     string expected = "11100000";
     string actual;
     //target.StringCode = expected;
     actual = target.StringCode;
     Assert.AreEqual(expected, actual);
 }
示例#4
0
 /// <summary>
 /// Генерирует фактор со случайным значением
 /// </summary>
 /// <param name="config">Конфигурация фактора</param>
 /// <returns>Случайный фактор</returns>
 private GrayCode GenerateRandomFactor(GrayCodeConfig config)
 {
     try
     {
         var randomFactor = new GrayCode(0, config);
         var decimalNumber = (uint) _random.Next(0, (int) (Math.Pow(2, config.Accuracy) - 1));
         randomFactor.DecimalCode = decimalNumber;
         return randomFactor;
     }
     catch (Exception e)
     {
         throw new Exception("Cant generate random factor", e);
     }
 }
示例#5
0
 public void ValueUintGrayCodeTest()
 {
     double value = 0.5;
     double min = -1.0;
     double max = 1.0;
     uint bitAccuracy = 8;
     GrayCodeConfig config = new GrayCodeConfig(min,max,bitAccuracy);
     GrayCode target = new GrayCode(value, config);
     uint expected = 224;
     uint actual = target.DecimalCode;
     Assert.AreEqual(expected, actual);
 }
示例#6
0
 public void ValueTest()
 {
     double value = 1.0;
     double min = 0.5;
     double max = 4;
     uint bitAccuracy = 8;
     GrayCodeConfig config = new GrayCodeConfig(min, max, bitAccuracy);
     GrayCode target = new GrayCode(value, config);
     double expected = value;
     double actual;
     actual = target.Value;
     Assert.IsTrue(Math.Abs(actual - expected) <= 2/(Math.Pow(2,bitAccuracy)-1));
 }
示例#7
0
文件: Organism.cs 项目: ernado/Owl
 public void AddFactorAndCode(GrayCode factor)
 {
     AddFactor(factor);
     AddGenes(factor.BooleanCode);
 }
示例#8
0
文件: Organism.cs 项目: ernado/Owl
 private void AddFactor(GrayCode factor)
 {
     _factors.Add(factor);
 }