public void TestXorEvolution() { Func<BitArray,double> fitness = (chromosome) => { Wiring wiring = new Wiring(2,1,4, chromosome); FPGA xorGate = new FPGA(wiring); double total = 0.0; if(!xorGate.Output(CreateInput(false,false))[0]) total += 1.0; if(!xorGate.Output(CreateInput(true,true))[0]) total += 1.0; if(xorGate.Output(CreateInput(true,false))[0]) total += 1.0; if(xorGate.Output(CreateInput(false,true))[0]) total += 1.0; if(total == 0.0) return 0.0; return 4.0 / total; }; Random rnd = new Random(); Func<BitArray> init = () => { BitArray result = new BitArray(Wiring.CalcLength(2,1,4)); for(int i=0; i<result.Length; i++) { if(rnd.NextDouble()<0.5) result[i]=true; } return result; }; Evolution evo = new Evolution(fitness,0.001, new int[]{ 0,2,4,6,8,10,12,14, 16,19,22,25, 28,30,32, 34,36, 38}); int n = 100; IList<BitArray> population = new List<BitArray>(n); for(int i=0; i<n; i++) population.Add(init()); population = evo.Run(population, (generation,error) => { return (generation > 1000); // || (error > 0.99); }); foreach(BitArray wiring in population.Take(10)) { Wiring temp = new Wiring(2,1,4,wiring); Console.WriteLine (temp.ToString()); Console.WriteLine (fitness(wiring)); } }
public void TestSimple1BitAdder() { // 2 Input's = 1 Output = 1 Gate NAND // muss identisch zu einzelnen NAND Gate sein Wiring wiring = Create1BitAdder(); Assert.AreEqual(ONE_BIT_ADDER, wiring.ToString()); FPGA oneBitAdder = new FPGA(wiring); BitArray result; // 0 + 0 + (0) = 0 (0) result = oneBitAdder.Output(CreateInput(false,false,false)); Assert.AreEqual(2,result.Length); Assert.IsFalse(result[0]); Assert.IsFalse(result[1]); // 1 + 0 + (0) = 1 (0) result = oneBitAdder.Output(CreateInput(true,false,false)); Assert.AreEqual(2,result.Length); Assert.IsTrue(result[0]); Assert.IsFalse(result[1]); Check(oneBitAdder, "000", "00"); Check(oneBitAdder, "001", "01"); Check(oneBitAdder, "010", "01"); Check(oneBitAdder, "011", "10"); Check(oneBitAdder, "100", "01"); Check(oneBitAdder, "101", "10"); Check(oneBitAdder, "110", "10"); Check(oneBitAdder, "111", "11"); /* Check(oneBitAdder, 0, 0); Check(oneBitAdder, 1, 1); Check(oneBitAdder, 2, 1); Check(oneBitAdder, 3, 2); Check(oneBitAdder, 4, 1); Check(oneBitAdder, 5, 2); Check(oneBitAdder, 6, 2); Check(oneBitAdder, 7, 3); */ }
public void TestXorGate() { Wiring wiring = CreateXorGate(); Assert.AreEqual(XOR_GATE, wiring.ToString()); FPGA xorGate = new FPGA(wiring); Func<bool,bool,bool> calc = (x,y) => { BitArray result = xorGate.Output(CreateInput(x,y)); Assert.AreEqual(1,result.Count); return result[0]; }; Assert.IsFalse(calc(false,false)," 0 xor 0 = 0"); Assert.IsTrue(calc(false,true)," 0 xor 1 = 1"); Assert.IsTrue(calc(true,false)," 1 xor 0 = 1"); Assert.IsFalse(calc(true,true)," 1 xor 1 = 0"); }
public void TestXorGA() { Wiring wiring = CreateXorGateFromGA(); Console.WriteLine (wiring.ToString()); FPGA xorGate = new FPGA(wiring); Func<bool,bool,bool> calc = (x,y) => { BitArray result = xorGate.Output(CreateInput(x,y)); Assert.AreEqual(1,result.Count); return result[0]; }; Assert.IsFalse(calc(false,false)," 0 xor 0 = 0"); Assert.IsTrue(calc(false,true)," 0 xor 1 = 1"); Assert.IsTrue(calc(true,false)," 1 xor 0 = 1"); Assert.IsFalse(calc(true,true)," 1 xor 1 = 0"); /* * B - A A * B B # B - - # # A B # # # B */ }