public FPGA(Wiring wiring) { this.wiring = wiring; this.network = new List<IFlowGate>(wiring.Gates); for(int i=0; i<wiring.Gates; i++) this.network.Add(new NandGate()); }
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 TestToString() { // rows = input-output+gate string expected_2_1_3 = // 4 rows "2-1-3\n"+ "- - -\n"+ "- - -\n"+ "# - -\n"+ "# # -"; Wiring wiring = new Wiring(2,1,3); Assert.AreEqual(expected_2_1_3,wiring.ToString(), "2-1 (3)"); string expected_3_2_4 = // 5 rows "3-2-4\n"+ "- - - -\n"+ "- - - -\n"+ "- - - -\n"+ "# - - -\n"+ "# # - -"; wiring = new Wiring(3,2,4); Assert.AreEqual(expected_3_2_4,wiring.ToString(), "3-2 (4)"); string expected_4_3_7 = // 6 rows "4-3-7\n"+ "- - - - - - -\n"+ "- - - - - - -\n"+ "- - - - - - -\n"+ "- - - - - - -\n"+ "# - - - - - -\n"+ "# # - - - - -\n"+ "# # # - - - -\n"+ "# # # # - - -"; wiring = new Wiring(4,3,7); Assert.AreEqual(expected_4_3_7,wiring.ToString(), "4-3 (7)"); }
private Wiring CreateXorGateFromGA() { Wiring result = new Wiring(2,1,4); const int IN_A = 0; const int IN_B = 1; const int GATE_1 = 0; const int GATE_2 = 1; const int GATE_3 = 2; const int GATE_4 = 3; result[true,IN_A, GATE_1] = PinWire.Both; result[true,IN_A, GATE_2] = PinWire.B; result[true,IN_A, GATE_4] = PinWire.A; result[true,IN_B, GATE_1] = PinWire.A; result[true,IN_B, GATE_2] = PinWire.Both; result[true,IN_B, GATE_3] = PinWire.B; result[true,IN_B, GATE_4] = PinWire.B; result[false,GATE_1, GATE_2] = PinWire.B; result[false,GATE_2, GATE_3] = PinWire.A; result[false,GATE_2, GATE_4] = PinWire.B; result[false,GATE_3, GATE_4] = PinWire.B; return result; }
private Wiring Create1BitAdder() { Wiring result = new Wiring(3,2,14); const int IN_A = 0; const int IN_B = 1; const int IN_C = 2; const int GATE_1 = 0; const int GATE_2 = 1; const int GATE_3 = 2; const int GATE_4 = 3; const int GATE_5 = 4; const int GATE_6 = 5; const int GATE_7 = 6; const int GATE_8 = 7; const int GATE_9 = 8; const int GATE_10= 9; const int GATE_11 = 10; const int GATE_12 = 11; const int GATE_13 = 12; const int GATE_14 = 13; // 0 mit 56 nand verbinden result[true, IN_B, GATE_1] = PinWire.Both; result[true, IN_C, GATE_2] = PinWire.Both; result[true, IN_B, GATE_3] = PinWire.A; result[true, IN_C, GATE_3] = PinWire.B; result[false,GATE_1, GATE_4] = PinWire.A; result[false,GATE_2, GATE_4] = PinWire.B; result[true, IN_A, GATE_5] = PinWire.A; result[false,GATE_4, GATE_5] = PinWire.B; result[true, IN_A, GATE_6] = PinWire.Both; result[false,GATE_1, GATE_7] = PinWire.A; result[true, IN_C, GATE_7 ] = PinWire.B; result[true, IN_B, GATE_8] = PinWire.A; result[false,GATE_2, GATE_8] = PinWire.B; result[false,GATE_4, GATE_9] = PinWire.A; result[false,GATE_3, GATE_9] = PinWire.B; result[false,GATE_7, GATE_10] = PinWire.A; result[false,GATE_8, GATE_10] = PinWire.B; result[true, IN_A, GATE_11] = PinWire.A; result[false,GATE_9, GATE_11] = PinWire.B; result[false,GATE_6, GATE_12] = PinWire.A; result[false,GATE_10, GATE_12] = PinWire.B; result[false,GATE_3, GATE_13] = PinWire.A; result[false,GATE_5, GATE_13] = PinWire.B; result[false,GATE_12, GATE_14] = PinWire.A; result[false,GATE_11, GATE_14] = PinWire.B; return result; }
public void TestWireing() { // rows = input-output+gate Wiring wiring = new Wiring(2,1,3); string expected = // 4 rows "2-1-3\n"+ "A B -\n"+ "- - *\n"+ "# - -\n"+ "# # A"; wiring[true,0,0]=PinWire.A; wiring[true,0,1]=PinWire.B; wiring[true,1,2]=PinWire.Both; wiring[false,0,0]=PinWire.Both; wiring[false,1,2]=PinWire.A; Assert.AreEqual(expected,wiring.ToString()); }