private void SpawnAnalogyReasonCodeletsRecur(GroupElement ge, Analogy a)
        {
            if (!(ge is Group))
            {
                return;
            }
            GroupReasonAnalogyComponentCodelet gcc = new GroupReasonAnalogyComponentCodelet(100, this, coderack, workspace, slipnet, (Group)ge, a);

            coderack.AddCodelet(gcc);

            foreach (GroupElement ge2 in ((Group)ge).GroupElements)
            {
                SpawnAnalogyReasonCodeletsRecur(ge2, a);
            }
        }
Exemple #2
0
        public override void Run()
        {
            if (group == null)
            {
                // Pick a random analogy and post codelets to work on it.
                analogy = workspace.PickRandomAnalogyByRecency();
                if (analogy == null)
                {
                    return;
                }

                foreach (Group g in analogy.GetAllSubGroups())
                {
                    GroupReasonAnalogyComponentCodelet gcc = new GroupReasonAnalogyComponentCodelet((int)this.rawUrgency * 2, this, coderack, workspace, slipnet, g);
                    coderack.AddCodelet(gcc);
                }

                // If the analogy has a parent group containing the two sides (and nothing else) add it.
                Group parent = analogy.GetParent();
                if (parent != null)
                {
                    GroupReasonAnalogyComponentCodelet gcc = new GroupReasonAnalogyComponentCodelet(100, this, coderack, workspace, slipnet, parent, analogy);
                    coderack.AddCodelet(gcc);
                }

                return;
            }

            if (group == null)
            {
                return;
            }

            if (!workspace.groups.Contains(group))
            {
                return;
            }

            if (!workspace.analogies.Contains(analogy))
            {
                return;
            }

            if (analogy == null)
            {
                // Find the enclosing analogy.
                List <Analogy> enclosingAnalogies = workspace.FindAllEnclosingAnalogies(group);
                analogy = Utilities.PickItem <Analogy>(enclosingAnalogies);
            }

            if (analogy == null)
            {
                return;
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, group.MinLocation, group.MaxLocation);

            // Add the reason, with strength == analogy strength.
            group.AddGroupReason(new GroupReasonAnalogySupport(group, analogy.Strength, analogy));
        }
Exemple #3
0
        /// <summary>
        /// Adds high-level codelets.
        /// This function provides top-down control by finding which structures are missing, and adding codelets
        /// to search for the missing structures.
        /// </summary>
        /// <param name="currentMeasureIndex">The index of the current (newest) measure, useful for focusing our attention</param>
        private static void AddHighLevelCodelets(int currentMeasureIndex)
        {
            // Add old link-breaker codelets for old analogies. This removes links that are irrelevant, where those measures
            // are already connected by anologies with good link/relationships.
            foreach (Analogy a in workspace.analogies)
            {
                if (a.Strength > 80 || a.MaxLocation < currentMeasureIndex - 4)
                {
                    for (int j = 0; j < 5; j++)
                    {
                        OldMeasureLinkBreakerCodelet obc = new OldMeasureLinkBreakerCodelet(5, null, coderack, workspace, slipnet, a);
                        coderack.AddCodelet(obc);
                    }
                }
            }

            // Get happiness for each measure (taking into account a range before/after the measure.
            // Skip measures too far in past.
            for (int i = Math.Max(0, currentMeasureIndex - 8); i < workspace.measures.Count; i++)
            {
                int    minMeasure;
                int    maxMeasure;
                double happiness = workspace.GetHappinessStandardWindow(i, out minMeasure, out maxMeasure);
                double unhappiness = 100 - happiness;
                int    urgency = (int)unhappiness;
                int    linkUrgency = 0, groupUrgency = 0, analogyUrgency = 0;

                // Look for links at this measure. Do we need to search for more?
                double avgLinkStrength, maxLinkStrength;
                int    numLinks = workspace.CountLinksToMeasure(i, out avgLinkStrength, out maxLinkStrength);

                if (numLinks < 1 || maxLinkStrength < 80 || avgLinkStrength < 50)
                {
                    // Look for links.
                    // TODO: how many to add?
                    linkUrgency = (int)(100 - avgLinkStrength);
                    for (int j = 0; j < 4; j++)
                    {
                        MeasureLinkerCodelet mlc = new MeasureLinkerCodelet(linkUrgency, null, coderack, workspace, slipnet, workspace.measures[i]);
                        coderack.AddCodelet(mlc);
                    }
                }

                // Look for Groups involving this measure. Do we need to search for more?.
                double avgGroupStrength, maxGroupStrength;
                int    numGroups = workspace.CountGroupsInvolvingMeasure(i, out avgGroupStrength, out maxGroupStrength);

                if (numGroups < 1 || maxGroupStrength < 80 || avgGroupStrength < 50)
                {
                    // Look for groups.
                    // TODO: how many to add?
                    for (int j = 0; j < 4; j++)
                    {
                        groupUrgency = Math.Max(5, (int)(100 - avgGroupStrength) - linkUrgency);
                        MeasureSamenessGrouperCodelet msgc = new MeasureSamenessGrouperCodelet(groupUrgency, null, coderack, workspace, slipnet, workspace.PickMeasureLinkInRange(i, i));
                        coderack.AddCodelet(msgc);

                        ProximityGrouperCodelet pgc = new ProximityGrouperCodelet(groupUrgency, null, coderack, workspace, slipnet, workspace.PickGroupElementInRange(i, i));
                        coderack.AddCodelet(pgc);

                        MetaGrouperCodelet mgc = new MetaGrouperCodelet(groupUrgency, null, coderack, workspace, slipnet, workspace.PickGroupInRange(i, i));
                        coderack.AddCodelet(mgc);
                    }
                }

                // Improve existing group scores.
                if (numGroups > 0 && maxGroupStrength < 75)
                {
                    // TODO: how many to add?
                    for (int j = 0; j < numGroups; j++)
                    {
                        Codelet codelet = new GroupReasonAnalogyComponentCodelet((int)(100 - maxGroupStrength), null,
                                                                                 coderack, workspace, slipnet, workspace.PickGroupInRange(i, i));
                        coderack.AddCodelet(codelet);

                        codelet = new GroupReasonComponentsSimilarCodelet((int)(100 - maxGroupStrength), null,
                                                                          coderack, workspace, slipnet, workspace.PickGroupInRange(i, i));
                        coderack.AddCodelet(codelet);

                        codelet = new GroupReasonNumberComponentsCodelet((int)(100 - maxGroupStrength), null,
                                                                         coderack, workspace, slipnet, workspace.PickGroupInRange(i, i));
                        coderack.AddCodelet(codelet);

                        codelet = new GroupReasonRhythmGapCodelet((int)(100 - maxGroupStrength), null,
                                                                  coderack, workspace, slipnet, workspace.PickGroupInRange(i, i));
                        coderack.AddCodelet(codelet);

                        codelet = new GroupPenaltySubcomponentLengthCodelet((int)(100 - maxGroupStrength), null,
                                                                            coderack, workspace, slipnet, workspace.PickGroupInRange(i, i));
                        coderack.AddCodelet(codelet);
                    }
                }

                // Look for Analogies involving this measure. Do we need to search for more?.
                double avgAnalogyStrength, maxAnalogyStrength;
                int    numAnalogies = workspace.CountAnalogiesInvolvingMeasure(i, out avgAnalogyStrength, out maxAnalogyStrength);

                if (numAnalogies < 1 || maxAnalogyStrength < 80 || avgAnalogyStrength < 50)
                {
                    // Look for analogies.
                    // TODO: how many to add?
                    for (int j = 0; j < 4; j++)
                    {
                        analogyUrgency = Math.Max(5, (int)(100 - avgAnalogyStrength) - groupUrgency - linkUrgency);
                        CreateAnalogyCodelet cac = new CreateAnalogyCodelet(analogyUrgency, null, coderack, workspace, slipnet);
                        coderack.AddCodelet(cac);
                    }
                }


                // Add breakers and groupers. Add NUM_CODELETS_TILL_NEW_HIGH_LEVEL if we are super unhappy, less if happier.
                // TODO: how many to add?
                for (int j = 0; j < unhappiness / 100 * Constants.NUM_CODELETS_TILL_NEW_HIGH_LEVEL / 20; j++)
                {
                    GroupBreakerCodelet gbc = new GroupBreakerCodelet(urgency, null, coderack, workspace, slipnet, workspace.PickGroupInRange(minMeasure, maxMeasure));
                    coderack.AddCodelet(gbc);

                    /*MeasureSamenessGrouperCodelet msgc = new MeasureSamenessGrouperCodelet(urgency, null, coderack, workspace, slipnet, workspace.PickMeasureLinkInRange(minMeasure, maxMeasure));
                     * coderack.AddCodelet(msgc);
                     *
                     * ProximityGrouperCodelet pgc = new ProximityGrouperCodelet(urgency, null, coderack, workspace, slipnet, workspace.PickGroupElementInRange(minMeasure, maxMeasure));
                     * coderack.AddCodelet(pgc);
                     *
                     * MetaGrouperCodelet mgc = new MetaGrouperCodelet(urgency, null, coderack, workspace, slipnet, workspace.PickGroupInRange(minMeasure, maxMeasure));
                     * coderack.AddCodelet(mgc);*/

                    //CreateAnalogyCodelet cac = new CreateAnalogyCodelet(urgency, null, coderack, workspace, slipnet);
                    //coderack.AddCodelet(cac);
                }

                //Generate/manage Expectations
            }
        }