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); } }
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); }
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); }
/// <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); } }
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); }
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)); }
public void AddFactorAndCode(GrayCode factor) { AddFactor(factor); AddGenes(factor.BooleanCode); }
private void AddFactor(GrayCode factor) { _factors.Add(factor); }