Пример #1
0
        public static void PreTrainACS(BPNetwork idn)
        {
            Console.Write("Pre-training ACS...");

            pABEBlackGun = abeBlackGun + (((r.NextDouble() * 2) - 1) * abeMaxTemp);
            pABEWhiteGun = abeWhiteGun + (((r.NextDouble() * 2) - 1) * abeMaxTemp);

            List <ActivationCollection> dataSets = new List <ActivationCollection>();

            List <DeclarativeChunk> primes = new List <DeclarativeChunk>();

            primes.AddRange(white_faces);
            primes.AddRange(black_faces);

            List <DeclarativeChunk> targets = new List <DeclarativeChunk>();

            targets.AddRange(guns);
            targets.AddRange(tools);

            foreach (DeclarativeChunk p in primes)
            {
                foreach (DeclarativeChunk t in targets)
                {
                    ActivationCollection ds = ImplicitComponentInitializer.NewDataSet();
                    ds.AddRange(p, 1);
                    ds.AddRange(t, 1);

                    dataSets.Add(ds);
                }
            }

            ImplicitComponentInitializer.Train(idn, trainer, numIterations: numTrainingTrials, randomTraversal: true, dataSets: dataSets.ToArray());

            Console.WriteLine("Finished");
        }
Пример #2
0
        static void SetupBPNetwork(Agent reasoner)
        {
            //Chunks for the whales, tuna, and bears
            DeclarativeChunk TunaChunk  = World.NewDeclarativeChunk("Tuna");
            DeclarativeChunk WhaleChunk = World.NewDeclarativeChunk("Whale");
            DeclarativeChunk BearChunk  = World.NewDeclarativeChunk("Bear");

            //The 2 properties (as DV pairs)
            DimensionValuePair livesinwater = World.NewDimensionValuePair("lives in", "water");
            DimensionValuePair eatsfish     = World.NewDimensionValuePair("eats", "fish");

            //The BP network to be used in the bottom level of the NACS
            BPNetwork net = AgentInitializer.InitializeAssociativeMemoryNetwork(reasoner, BPNetwork.Factory);

            //Adds the properties (as inputs) and chunks (as outputs) to the BP network
            net.Input.Add(livesinwater);
            net.Input.Add(eatsfish);
            net.Output.Add(TunaChunk);
            net.Output.Add(WhaleChunk);
            net.Output.Add(BearChunk);

            reasoner.Commit(net);

            //Adds the chunks to the GKS
            reasoner.AddKnowledge(TunaChunk);
            reasoner.AddKnowledge(WhaleChunk);
            reasoner.AddKnowledge(BearChunk);

            //Initializes a trainer to use to train the BP network
            GenericEquation trainer = ImplicitComponentInitializer.InitializeTrainer(GenericEquation.Factory, (Equation)trainerEQ);

            //Adds the properties (as inputs) and chunks (as outputs) to the trainer
            trainer.Input.Add(livesinwater);
            trainer.Input.Add(eatsfish);
            trainer.Output.Add(TunaChunk);
            trainer.Output.Add(WhaleChunk);
            trainer.Output.Add(BearChunk);

            trainer.Commit();

            //Sets up data sets for each of the 2 properties
            List <ActivationCollection> sis = new List <ActivationCollection>();
            ActivationCollection        si  = ImplicitComponentInitializer.NewDataSet();

            si.Add(livesinwater, 1);
            sis.Add(si);

            si = ImplicitComponentInitializer.NewDataSet();
            si.Add(eatsfish, 1);
            sis.Add(si);

            Console.Write("Training AMN...");
            //Trains the BP network to report associative knowledge between the properties and the chunks
            ImplicitComponentInitializer.Train(net, trainer, sis, ImplicitComponentInitializer.TrainingTerminationConditions.SUM_SQ_ERROR);
            Console.WriteLine("Finished!");
        }
Пример #3
0
        /// <summary>
        /// Performs reasoning using a "noisy" input based on each pattern
        /// </summary>
        /// <param name="reasoner">The reasoner who is performing the reasoning</param>
        static void DoReasoning(Agent reasoner)
        {
            int correct = 0;

            //Iterates through each pattern
            foreach (DeclarativeChunk dc in chunks)
            {
                //Gets an input to use for reasoning. Note that the World.GetSensoryInformation method can also be used here
                ActivationCollection si = ImplicitComponentInitializer.NewDataSet();

                int count = 0;

                //Sets up the input
                foreach (DimensionValuePair dv in dvs)
                {
                    if (((double)count / (double)dc.Count < (1 - noise)))
                    {
                        if (dc.Contains(dv))
                        {
                            si.Add(dv, 1);
                            ++count;
                        }
                        else
                        {
                            si.Add(dv, 0);
                        }
                    }
                    else
                    {
                        si.Add(dv, 0);      //Zeros out the dimension-value pair if "above the noise level"
                    }
                }

                Console.WriteLine("Input to reasoner:\r\n" + si);

                Console.WriteLine("Output from reasoner:");

                //Performs reasoning based on the input. The conclusions returned from this method will be in the form of a
                //collection of "Chunk Tuples." A chunk tuple is simply just a chunk combined with its associated activation.
                var o = reasoner.NACS.PerformReasoning(si);

                //Iterates through the conclusions from reasoning
                foreach (var i in o)
                {
                    Console.WriteLine(i.CHUNK);
                    if (i.CHUNK == dc)
                    {
                        correct++;
                    }
                }
            }
            Console.WriteLine("Retrieval Accuracy: " +
                              (int)(((double)correct / (double)chunks.Count) * 100) + "%");
        }
Пример #4
0
        public static void Main()
        {
            char repeat;

            do
            {
                Console.Write("Load Serialization Data (y/n)?");
                load = (Console.ReadKey().KeyChar != 'n');
                Console.WriteLine();

                Console.WriteLine("World Serialization/Pre-Training Demonstration");
                SerializeWorld();

                World.Destroy();
                World.Initialize();
                ImplicitComponentInitializer.ClearRanges();


                Console.WriteLine("Agent Serialization/Pre-Training Demonstration");
                SerializeAgent();

                World.Destroy();
                World.Initialize();
                ImplicitComponentInitializer.ClearRanges();

                Console.WriteLine("Drive Serialization/Pre-Training Demonstration");
                SerializeDrive();

                World.Destroy();
                World.Initialize();
                ImplicitComponentInitializer.ClearRanges();

                Console.WriteLine("Drive Component Serialization/Pre-Training Demonstration");
                SerializeDriveComponent();

                World.Destroy();
                World.Initialize();
                ImplicitComponentInitializer.ClearRanges();

                Console.Write("Continue (y/n)?");
                repeat = Console.ReadKey().KeyChar;
                Console.WriteLine();
            } while (repeat != 'n');
        }
Пример #5
0
        static void DoTraining(BPNetwork target, FoodDrive foodDr)
        {
            DriveEquation trainer = ImplicitComponentInitializer.InitializeTrainer(DriveEquation.Factory, foodDr);

            trainer.Commit();

            List <ActivationCollection> data = new List <ActivationCollection>();

            data.Add(ImplicitComponentInitializer.NewDataSet());

            foreach (var i in foodDr.Input)
            {
                ImplicitComponentInitializer.AddRange(i.WORLD_OBJECT, 0, 1, .25);
                data[0].Add(i);
            }

            Console.WriteLine("Performing Pre-Training (see the trace log for results)");

            ImplicitComponentInitializer.Train(target, trainer, data, ImplicitComponentInitializer.TrainingTerminationConditions.BOTH, numTrials);
        }
Пример #6
0
        /// <summary>
        /// Encodes the patterns into the specified Hopfield network and then tests to make sure they have been successfully encoded
        /// </summary>
        /// <remarks>
        /// <note type="implementnotes">Most of the work that is done by this method is actually also performed by the implicit component initializer's
        /// <see cref="ImplicitComponentInitializer.Encode{T}(T, ImplicitComponentInitializer.EncodeTerminationConditions, int, ActivationCollection[])">
        /// Encode</see> method. However, we must separate the "encode" and "recall" phases in this example since we are using a different
        /// <see cref="HopfieldNetwork.TransmissionOptions">transmission option</see> between these encoding process.</note>
        /// </remarks>
        /// <param name="net">the network where the patterns are to be encoded</param>
        static void EncodeHopfieldNetwork(HopfieldNetwork net)
        {
            //Tracks the accuracy of correctly encoded patterns
            double accuracy = 0;

            //Continue encoding until all of the patterns are successfully recalled
            do
            {
                //Specifies to use the "N spins" transmission option during the encoding phase
                net.Parameters.TRANSMISSION_OPTION =
                    HopfieldNetwork.TransmissionOptions.N_SPINS;

                List <ActivationCollection> sis = new List <ActivationCollection>();
                foreach (DeclarativeChunk dc in chunks)
                {
                    //Gets a new "data set" object (to be used by the Encode method to encode the pattern)
                    ActivationCollection si = ImplicitComponentInitializer.NewDataSet();

                    //Sets up the pattern
                    si.AddRange(dc, 1);

                    sis.Add(si);
                }

                //Encodes the pattern into the Hopfield network
                ImplicitComponentInitializer.Encode(net, sis);


                //Specifies to use the "let settle" transmission option during the testing phase
                net.Parameters.TRANSMISSION_OPTION =
                    HopfieldNetwork.TransmissionOptions.LET_SETTLE;

                //Tests the net to see if it has learned the patterns
                accuracy = ImplicitComponentInitializer.Encode(net, sis, testOnly: true);

                Console.WriteLine(((int)accuracy * 100) + "% of the patterns were successfully recalled.");
            } while (accuracy < 1);
        }
Пример #7
0
        static void DoReasoning(Agent reasoner)
        {
            double act1 = 0;
            double act2 = 0;

            //Gets an input to use for reasoning. Note that the World.GetSensoryInformation method can also be used here
            ActivationCollection hiprhi = ImplicitComponentInitializer.NewDataSet();
            ActivationCollection hipham = ImplicitComponentInitializer.NewDataSet();

            //Sets up the input
            foreach (DimensionValuePair dv in dvs)
            {
                if (chunks[1].Contains(dv) || chunks[2].Contains(dv))
                {
                    hiprhi.Add(dv, 1);
                }
                if (chunks[1].Contains(dv) || chunks[3].Contains(dv))
                {
                    hipham.Add(dv, 1);
                }
                if (!chunks[1].Contains(dv) && !chunks[2].Contains(dv) && !chunks[3].Contains(dv))
                {
                    hiprhi.Add(dv, 0);
                    hipham.Add(dv, 0);
                }
            }

            Console.WriteLine("Using the features for \"hippo\" and \"rhino\" as input into reasoner:\r\n" + hiprhi);
            Console.WriteLine();
            Console.WriteLine("Output from reasoner:");

            //Performs reasoning based on the input
            var o = reasoner.NACS.PerformReasoning(hiprhi);

            //Iterates through the conclusions from reasoning
            foreach (var i in o)
            {
                //Checks to see if it has the right chunk (i.e., mammals)
                if (i.CHUNK == chunks[0])
                {
                    Console.WriteLine(i.CHUNK);
                    Console.WriteLine("The activation of the \"mammal\" chunk based on \"hippo\" and \"rhino\" is: " + Math.Round(i.ACTIVATION, 2));
                    act1 = i.ACTIVATION;
                }
                else
                {
                    continue;
                }
                Console.WriteLine();
            }

            Console.WriteLine("Using the features for \"hippo\" and \"hamster\" as input into reasoner:\r\n" + hiprhi);
            Console.WriteLine();
            Console.WriteLine("Output from reasoner:");

            //Performs reasoning based on the input. The conclusions returned from this method will be in the form of a
            //collection of "Chunk Tuples." A chunk tuple is simply just a chunk combined with its associated activation.
            var k = reasoner.NACS.PerformReasoning(hipham);

            //Iterates through the conclusions from reasoning
            foreach (var i in k)
            {
                if (i.CHUNK == chunks[0])
                {
                    Console.WriteLine(i.CHUNK);
                    Console.WriteLine("The activation of the \"mammal\" chunk based on \"hippo\" and \"hamster\" is: " + Math.Round(i.ACTIVATION, 2));
                    act2 = i.ACTIVATION;
                }
                else
                {
                    continue;
                }
                Console.WriteLine();
            }

            Console.WriteLine("Which animal combination is a stronger representation of a mammal?");
            if (act1 > act2)
            {
                Console.WriteLine("A hippo and rhino, because they activate the mammal chunk more");
            }
            else
            {
                Console.WriteLine("A hippo and hamster, because they activate the mammal chunk more");
            }
        }
Пример #8
0
        static void DoReasoning(Agent reasoner)
        {
            //Gets an input to use for reasoning. Note that the World.GetSensoryInformation method can also be used here
            ActivationCollection si = ImplicitComponentInitializer.NewDataSet();

            //activation values
            double act1 = 0;
            double act2 = 0;

            //Sets up the input
            foreach (DimensionValuePair dv in dvs)
            {
                if (chunks[0].Contains(dv))
                {
                    si.Add(dv, 1);
                }
                else
                {
                    si.Add(dv, 0);
                }
            }

            Console.WriteLine("Using the features for \"robin\" as input to reasoner:\r\n" + si);
            Console.WriteLine();
            Console.WriteLine("Output from reasoner:");

            //Performs reasoning based on the input
            var o = reasoner.NACS.PerformReasoning(si);

            //Iterates through the conclusions from reasoning
            foreach (var i in o)
            {
                //If it is the robin chunk, skip over
                if (i.CHUNK == chunks[0])
                {
                    continue;
                }
                else
                {
                    Console.WriteLine(i.CHUNK);
                    //If it is the sparrow chunk, set act1
                    if (i.CHUNK == chunks[1])
                    {
                        act1 = i.ACTIVATION;
                    }
                    //Otherwise it is the goose chunk
                    else
                    {
                        act2 = i.ACTIVATION;
                    }
                    Console.WriteLine("Activation of \"" + i.CHUNK.LabelAsIComparable + "\" chunk based on \"robin\" input: " + Math.Round(i.ACTIVATION, 2));
                }
                Console.WriteLine();
            }

            Console.WriteLine("Which animal is most similar to a robin?");
            if (act1 > act2)
            {
                Console.WriteLine("A sparrow because its chunk activation is higher (" + Math.Round(act1, 2) + " vs. " + Math.Round(act2, 2) + ")");
            }
            else
            {
                Console.WriteLine("A goose because its chunk activation is higher (" + Math.Round(act2, 2) + " vs. " + Math.Round(act1, 2) + ")");
            }
        }
Пример #9
0
        static void DoReasoning(Agent reasoner)
        {
            //Gets an input to use for reasoning. Note that the World.GetSensoryInformation method can also be used here
            ActivationCollection si = ImplicitComponentInitializer.NewDataSet();

            //activation values
            double act1 = 0;
            double act2 = 0;

            //Sets up the input
            foreach (DimensionValuePair dv in dvs)
            {
                if (chunks[0].Contains(dv))
                {
                    si.Add(dv, 1);
                }
            }

            Console.WriteLine("Using features for \"bird\" as input to reasoner:\r\n" + si);
            Console.WriteLine();
            Console.WriteLine("Output from reasoner:");

            //Performs reasoning based on the input.
            var o = reasoner.NACS.PerformReasoning(si);

            //Iterates through the conclusions from reasoning
            foreach (var i in o)
            {
                // if the bird chunk, skip
                if (i.CHUNK == chunks[0])
                {
                    continue;
                }
                else
                {
                    Console.WriteLine(i.CHUNK);
                    //if the robin chunk, set the first activation value
                    if (i.CHUNK == chunks[1])
                    {
                        act1 = i.ACTIVATION;
                    }
                    //otherwise, set the second activation value for penguins
                    else
                    {
                        act2 = i.ACTIVATION;
                    }
                    Console.WriteLine("Activation of \"" + i.CHUNK.LabelAsIComparable + "\" chunk based on \"bird\" features: " + Math.Round(i.ACTIVATION, 2));
                }
                Console.WriteLine();
            }

            Console.WriteLine("Which is more typical of a bird?");

            if (act1 > act2)
            {
                Console.WriteLine("A robin because its chunk activation is higher (" + Math.Round(act1, 2) + " vs. " + Math.Round(act2, 2) + ")");
            }
            else
            {
                Console.WriteLine("A penguin because its chunk activation is higher (" + Math.Round(act2, 2) + " vs. " + Math.Round(act1, 2) + ")");
            }
        }
Пример #10
0
        public void Run()
        {
            foreach (Tasks t in Enum.GetValues(typeof(Tasks)))
            {
                int max_i = ((t == Tasks.PERSON) ? 2 * numTestTrials : numTestTrials);
                foreach (Groups g in Enum.GetValues(typeof(Groups)))
                {
                    Console.WriteLine("Running Group " + g + " through task " + t);
                    for (int r = 0; r < numRepeats; r++)
                    {
                        Console.Write("Participant #" + r + " is performing the task          ");
                        double currentW = rand.Next(12);
                        double lastP    = rand.Next(12);
                        Initialize(g);

                        ActivationCollection irlSI = ImplicitComponentInitializer.NewDataSet();
                        var irlVars = (from a in As
                                       select from b in Bs
                                       select from c in Cs
                                       select
                                       new
                        {
                            A = World.GetDimensionValuePair("A", a),
                            B = World.GetDimensionValuePair("B", b),
                            C = World.GetDimensionValuePair("C", c)
                        }).SelectMany(k => k).SelectMany(k => k);
                        foreach (var k in irlVars)
                        {
                            irlSI.Add(k.A, 1);
                            irlSI.Add(k.B, 1);
                            irlSI.Add(k.C, 1);
                        }

                        DimensionValuePair targetDV = World.GetDimensionValuePair("Target P", target);

                        GenerateIRLRuleSet(IRL_Rule_Sets.ONE);
                        SensoryInformation si = null;
                        SensoryInformation prevSI;

                        for (int i = 0; i < max_i; i++)
                        {
                            int shift = 10 - (int)Math.Round(10 * ((double)i / (double)max_i));
                            Console.CursorLeft -= shift;
                            Console.Write(".");
                            for (int s = 0; s < shift - 1; s++)
                            {
                                Console.Write(" ");
                            }
                            if ((from a in John.GetInternals(Agent.InternalContainers.ACTION_RULES) where a is IRLRule select a).Count() == 0)
                            {
                                GenerateIRLRuleSet(IRL_Rule_Sets.TWO);
                            }

                            prevSI = si;

                            si = World.NewSensoryInformation(John);

                            foreach (var s in irlSI)
                            {
                                si.Add(s);
                            }

                            si.Add(targetDV, 1);
                            si.Add(World.GetActionChunk(currentW), 1);
                            lastP = FactoryOutput(lastP, currentW);
                            si.Add(World.GetDimensionValuePair("Current P", lastP), 1);

                            if (Math.Abs(lastP - target) < double.Epsilon)
                            {
                                if ((t != Tasks.PERSON || (t == Tasks.PERSON && i >= numTestTrials)))
                                {
                                    results[(int)t, (int)g, r]++;
                                }

                                if (prevSI != null)
                                {
                                    John.ReceiveFeedback(prevSI, 1);
                                }
                            }
                            else
                            {
                                if (prevSI != null)
                                {
                                    John.ReceiveFeedback(prevSI, 0);
                                }
                            }

                            John.Perceive(si);

                            currentW = (double)John.GetChosenExternalAction(si).LabelAsIComparable;
                        }
                        Console.WriteLine();
                        Console.WriteLine("Participant #" + r + " is finished performing the task and hit the target " +
                                          results[(int)t, (int)g, r] + " times out of " + max_i);

                        Console.WriteLine("At the end of the task, the participant had the following rules: ");
                        foreach (var ar in John.GetInternals(Agent.InternalContainers.ACTION_RULES))
                        {
                            Console.WriteLine(ar);
                        }
                        John.Die();
                        World.Remove(John);
                    }
                }
                Console.WriteLine("Tabular results for the " + t + " task:");
                Console.WriteLine("Group\tParticipant\tHits");
                foreach (Groups g in Enum.GetValues(typeof(Groups)))
                {
                    for (int i = 0; i < numRepeats; i++)
                    {
                        Console.WriteLine(g + "\t" + i + "\t" + results[(int)t, (int)g, i]);
                    }
                }
            }
        }