예제 #1
0
        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));
            }
        }
예제 #2
0
파일: TestWiring.cs 프로젝트: stefc/evogate
        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)");
        }
예제 #3
0
파일: TestWiring.cs 프로젝트: stefc/evogate
        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());
        }