Пример #1
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!");
        }
Пример #2
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) + "%");
        }
Пример #3
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");
            }
        }
Пример #4
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) + ")");
            }
        }
Пример #5
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) + ")");
            }
        }
Пример #6
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]);
                    }
                }
            }
        }