internal static LogicalNetworEntity Create(
            IEnumerable<LogicalNetworkGene> dominantGeneSequence,
            GateTypeEvolutionMethod evolutionMethod,
            TruthTable truthTableToSolve,
            LogicGateTypes gateTypeRestrictions)
        {
            Contract.Requires(dominantGeneSequence != null);
            Contract.Requires(truthTableToSolve != null);
            Contract.Requires(gateTypeRestrictions != null);

            var dna = new DNA<LogicalNetworkGene>(dominantGeneSequence.ToList());
            return Create(dna, dna.Genes[0], evolutionMethod, truthTableToSolve, gateTypeRestrictions);
        }
        internal static LogicalNetworEntity Create(
            DNA<LogicalNetworkGene> dna,
            IEnumerable<LogicalNetworkGene> dominantGeneSequence,
            GateTypeEvolutionMethod evolutionMethod,
            TruthTable truthTableToSolve,
            LogicGateTypes gateTypeRestrictions)
        {
            Contract.Requires(dna != null);
            Contract.Requires(dominantGeneSequence != null);
            Contract.Requires(truthTableToSolve != null);
            Contract.Requires(gateTypeRestrictions != null);
            
            LogicalNetworkFactory factory;
            if (evolutionMethod == GateTypeEvolutionMethod.Restrict)
            {
                factory = new LogicalNetworkFactory(truthTableToSolve.InputInterfaceLength, truthTableToSolve.OutputInterfaceLength, gateTypeRestrictions);
            }
            else // Evolve
            {
                factory = new LogicalNetworkFactory(truthTableToSolve.InputInterfaceLength, truthTableToSolve.OutputInterfaceLength);
            }

            var network = LogicalNetworkDNADecoder.CreateNetwork(factory, dominantGeneSequence);

            int numOfNAGates = 0;
            if (evolutionMethod == GateTypeEvolutionMethod.Evolve && gateTypeRestrictions != null)
            {
                foreach (var entry in network.EntryArray)
                {
                    var gate = entry.NodeEntry.Node as LogicGate;
                    if (gate != null)
                    {
                        var type = new LogicGateType(gate.Operation, entry.UpperConnectionEntryArray.Length);
                        if (!gateTypeRestrictions.Contains(type)) numOfNAGates++;
                    }
                }
            }

            int errors = new TruthTableComputation(network).ComputeError(truthTableToSolve);

            return new LogicalNetworEntity(dna, network, errors, numOfNAGates);
        }
Ejemplo n.º 3
0
        private static void Begin()
        {
/*
0	0	0	0	0
0	1	0	0	1
1	0	0	0	1
1	1	0	1	0
0	0	1	0	1
0	1	1	1	0
1	0	1	1	0
1	1	1	1	1
*/

            var table = new TruthTable(
                3, 2,
                new[] 
                { 
                    new TruthTableEntry(new[] { false, false, false }, new[] { false, false }),
                    new TruthTableEntry(new[] { false, true, false }, new[] { false, true }),
                    new TruthTableEntry(new[] { true, false, false }, new[] { false, true }),
                    new TruthTableEntry(new[] { true, true, false }, new[] { true, false }),
                    new TruthTableEntry(new[] { false, false, true }, new[] { false, true }),
                    new TruthTableEntry(new[] { false, true, true }, new[] { true, false }),
                    new TruthTableEntry(new[] { true, false, true }, new[] { true, false }),
                    new TruthTableEntry(new[] { true, true, true }, new[] { true, true }),
                });

            var restrict = new LogicGateTypes(new[] { LogicGateType.NAND(2), LogicGateType.NOT() });

            var factory = new LNGAEntityFactory(table, restrict);

            factory.MaxIndex = 20;
            factory.ValidDNALenghtRange = IntRange.CreateInclusive(200, 250);
            factory.CrossoverChunkSize = IntRange.CreateExclusive(1, factory.ValidDNALenghtRange.MaxValue);
            factory.StoreParentSequences = false;
            factory.GateTypeEvolutionMethod = GateTypeEvolutionMethod.Restrict;

            factory.MutationParameters.MutationChunkSize = IntRange.CreateExclusive(1, factory.ValidDNALenghtRange.MaxValue);

            factory.MutationParameters.PointMutationChance = 0.01;

            factory.MutationParameters.DeletionMutationChance = 0.01;
            factory.MutationParameters.DuplicationMutationChance = 0.01;
            factory.MutationParameters.InsertionMutationChance = 0.01;
            factory.MutationParameters.TranslocationMutationChance = 0.01;
            factory.MutationParameters.InversionMutationChance = 0.01;

            var epoch = new LogicalGAEpoch(factory, 50, 25);

            epoch.NumberOfParentsRange = IntRange.CreateFixed(4);
            epoch.OffspringMovingChance = 0.1;
            epoch.BestSelectStdDev = 0.7;
            epoch.WorstSelectStdDev = 0.01;

            //var factory = new LNStatisticalEntityFactory(
            //    table,
            //    180,
            //    40,
            //    10,
            //    0.00,
            //    0.25,
            //    GateTypeEvolutionMethod.Evolve,
            //    restrict);

            //var epoch = new LogicalSAEpoch(factory, 50, 25, 5);

            bool done = false;

            LogicalNetworEntity best = null;

            while (!done)
            {
                epoch.Step();

                var cbest = (LogicalNetworEntity)epoch.BestEntity;

                if (cbest != null)
                {
                    if (best == null || cbest.CompareTo(best) < 0)
                    {
                        best = cbest;
                        Dump(epoch, best);
                    }
                    else if (epoch.CurrentIteration % 100 == 0)
                    {
                        Dump(epoch, best);
                    }
                }

                //if (Console.KeyAvailable)
                //{
                //    Console.ReadKey();
                //    done = true;
                //}
            }
        }