Пример #1
0
        public override void Run()
        {
            if (a == null)
            {
                a = workspace.PickRandomAnalogyByRecency();
            }

            if (a == null)
            {
                return;
            }

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

            // Examine each relationship
            // TODO: do we have to dig down into subanalogies??????????????????
            foreach (Relationship r in a.relationships)
            {
                // Add to attention history.
                workspace.RecordCodeletAttentionHistory(this, r.LHS.MinLocation, r.LHS.MaxLocation);
                workspace.RecordCodeletAttentionHistory(this, r.RHS.MinLocation, r.RHS.MaxLocation);


                GroupElement right = r.RHS;
                if (right.FormLabelStrength > 99.9999)
                {
                    continue;
                }

                // Grab the left side, and try to make the right side labelled analogously.
                GroupElement left = r.LHS;
                if (left.FormLabelStrength > 0)
                {
                    // If the relationship is exact, just copy the label.
                    if (r.Strength > 99.999)
                    {
                        SetLabel(right, left.FormLabelStrength, left.FormLabel);
                        return;
                    }

                    if (workspace.FightItOut(r.Strength * 2, right.FormLabelStrength))
                    {
                        // Make a prime version.
                        string label;
                        if (left.FormLabel[left.FormLabel.Length - 1] == '\'')
                        {
                            label = "(" + left.FormLabel + ")'";
                        }
                        else
                        {
                            label = left.FormLabel + '\'';
                        }
                        SetLabel(right, 100, label);
                    }
                }
            }
        }
        public override void Run()
        {
            if (analogy == null)
            {
                // Pick a source analogy.
                Group sourceGroup = workspace.expectations.SourceGroup;
                if (sourceGroup == null)
                {
                    return;
                }
                analogy = workspace.PickRandomAnalogyInGroup(sourceGroup);
            }
            if (analogy == null)
            {
                return;
            }

            // We have an existing analogy. Check if its copy already exists in expectations.
            int offset  = workspace.expectations.Offset;
            int idxLHS1 = analogy.LHS.MinLocation + offset;
            int idxLHS2 = analogy.LHS.MaxLocation + offset;
            int idxRHS1 = analogy.RHS.MinLocation + offset;
            int idxRHS2 = analogy.RHS.MaxLocation + offset;


            foreach (ExpectedAnalogy ea in workspace.expectations.analogies)
            {
                int idxLHS1e = ea.LHS.MinLocation;
                int idxLHS2e = ea.LHS.MaxLocation;
                int idxRHS1e = ea.RHS.MinLocation;
                int idxRHS2e = ea.RHS.MaxLocation;

                if (idxLHS1 == idxLHS1e && idxLHS2 == idxLHS2e && idxRHS1 == idxRHS1e && idxRHS2 == idxRHS2e)
                {
                    return;
                }
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, idxLHS1, idxLHS2);
            workspace.RecordCodeletAttentionHistory(this, idxRHS1, idxRHS2);

            // New expected analogy time!  Try to find the necessary expected group elements.
            ExpectedGroup LHSe = workspace.expectations.FindGroupByLocation(idxLHS1, idxLHS2);

            if (LHSe == null)
            {
                return;
            }

            ExpectedGroup RHSe = workspace.expectations.FindGroupByLocation(idxRHS1, idxRHS2);

            if (RHSe == null)
            {
                return;
            }

            // Create.
            workspace.expectations.MakeNewExpectedAnalogy(LHSe, RHSe, analogy.Strength, analogy.Level);
        }
Пример #3
0
        private static string AnalogyString(Analogy a, bool verbose)
        {
            StringBuilder sb = new StringBuilder();

            /*sb.Append("m.");
             * sb.Append(g.MinLocation + 1);
             * sb.Append('-');
             * sb.Append(g.MaxLocation + 1);
             *
             * sb.Append(" (");
             * foreach (Measure m in g.Measures) {
             *      sb.Append(m.FormLabel);
             * }
             * sb.Append(")");*/

            sb.Append(a.ToString());



            //sb.Append(" -- ");
            //if (verbose)
            //	sb.Append("Score: ");
            //sb.Append(g.ComputeStrength());
            return(sb.ToString());
        }
Пример #4
0
 /// <summary>
 /// Use this constructer to tell the codelet which group to examine, with known parent analogies.
 /// Otherwise, it picks one randomly.
 /// </summary>
 /// <param name="urgency"></param>
 /// <param name="parent"></param>
 /// <param name="coderack"></param>
 /// <param name="workspace"></param>
 /// <param name="slipnet"></param>
 /// <param name="notes"></param>
 public GroupReasonAnalogyComponentCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                                           Group group, Analogy analogy)
     : base("Group Reason Analogy Component", urgency, parent, coderack, workspace, slipnet)
 {
     this.group   = group;
     this.analogy = analogy;
 }
Пример #5
0
 public void CloneRelationshipsFrom(Analogy a)
 {
     foreach (Relationship r in a.relationships)
     {
         relationships.Add(r.DeepCopyUsingNewGroupsInAnalogy(r, a));
     }
 }
Пример #6
0
 private void UpdateAnalogy(Analogy toUpdate, Analogy newerAnalogy)
 {
     foreach (Relationship r in newerAnalogy.relationships)
     {
         toUpdate.TryToAddRelationship(r);                       // TODO might destroy old relationships... not sure we want this............ might need to have them compete too...
     }
 }
Пример #7
0
        /// <summary>
        /// Adds a strong analogy to the list, cloning in memory any sub analogies/relationships necessary. i.e. we duplicate the tree structure.
        /// </summary>
        /// <param name="a"></param>
        public void AddAnalogy(Analogy a)
        {
            // First check if it already exists.
            Analogy found = FindAnalogy(a);

            // If it exists, update (by adding relationships, not taking away)
            if (found != null)
            {
                UpdateAnalogy(found, a);
                return;
            }

            // Otherwise, clone and store new version.
            GroupElement lhs = a.LHS.DeepCopy();
            GroupElement rhs = a.RHS.DeepCopy();

            Analogy newAnalogy = new Analogy(lhs, rhs, workspace);

            // Clone analogy relationships.
            newAnalogy.CloneRelationshipsFrom(a);

            analogies.Add(newAnalogy);
            analogyCompetitionStats[newAnalogy] = new CompetitionStats();
            analogyCompetitionStats[newAnalogy].AddWin();               // start with default win.
        }
        public override Relationship DeepCopyUsingNewGroupsInAnalogy(Relationship r, Analogy a)
        {
            GroupElement newLHS, newRHS;

            FindNewLHSRHS(r, a, out newLHS, out newRHS);

            RelationshipAntecedentConsequentTonality rNew = new RelationshipAntecedentConsequentTonality(newLHS, newRHS, a.Strength);

            return(rNew);
        }
Пример #9
0
        public override Relationship DeepCopyUsingNewGroupsInAnalogy(Relationship r, Analogy a)
        {
            GroupElement newLHS, newRHS;

            FindNewLHSRHS(r, a, out newLHS, out newRHS);

            RelationshipTransposition rNew = new RelationshipTransposition(newLHS, newRHS, a.Strength);

            return(rNew);
        }
        public override Relationship DeepCopyUsingNewGroupsInAnalogy(Relationship r, Analogy a)
        {
            GroupElement newLHS, newRHS;

            FindNewLHSRHS(r, a, out newLHS, out newRHS);

            RelationshipParameterIncreasing rNew = new RelationshipParameterIncreasing(newLHS, newRHS, a.Strength);

            return(rNew);
        }
Пример #11
0
 /// <summary>
 /// Use this constructer to tell the codelet which groups to examine.
 /// Otherwise, it picks randomly.
 /// </summary>
 /// <param name="urgency"></param>
 /// <param name="parent"></param>
 /// <param name="coderack"></param>
 /// <param name="workspace"></param>
 /// <param name="slipnet"></param>
 /// <param name="notes"></param>
 public MetaGrouperCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, GroupElement g1, GroupElement g2, Analogy a)
     : base("Meta Grouper", urgency, parent, coderack, workspace, slipnet)
 {
     if (!(g1 is Group && g2 is Group))
     {
         return;
     }
     this.g1 = (Group)g1;
     this.g2 = (Group)g2;
     this.a  = a;
 }
        public override void Run()
        {
            if (analogy == null)
            {
                analogy = workspace.PickRandomAnalogyByRecency();
            }

            if (analogy == null)
            {
                return;
            }

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

            if (!(analogy.LHS is Group && analogy.RHS is Group))
            {
                return;
            }

            List <Group> lhsGroups = ((Group)analogy.LHS).GetAllSubgroups();

            lhsGroups.Add((Group)analogy.LHS);
            List <Group> rhsGroups = ((Group)analogy.RHS).GetAllSubgroups();

            rhsGroups.Add((Group)analogy.RHS);


            // Look for relationships and try to add to analogy.
            foreach (Relationship r in workspace.relationships)
            {
                // Skip rhythm-type relationships.
                if (r.RhythmType)
                {
                    continue;
                }

                if (!(r.LHS is Group && r.RHS is Group))
                {
                    continue;
                }
                if (lhsGroups.Contains((Group)r.LHS) && rhsGroups.Contains((Group)r.RHS))
                {
                    analogy.TryToAddRelationship(r);

                    // Add to attention history.
                    workspace.RecordCodeletAttentionHistory(this, r.LHS.MinLocation, r.LHS.MaxLocation);
                    workspace.RecordCodeletAttentionHistory(this, r.RHS.MinLocation, r.RHS.MaxLocation);
                }
            }
        }
Пример #13
0
 /// <summary>
 /// Checks if analogy exists (in terms of have the same measure span and same (recursive) structure of groups)
 /// </summary>
 /// <param name="a"></param>
 /// <returns></returns>
 public Analogy FindAnalogy(Analogy toFind)
 {
     foreach (Analogy a in analogies)
     {
         // Verify LHS and RHS are the same measures.
         if (Group.VerifySameGroupStructures(toFind.LHS, a.LHS) && Group.VerifySameGroupStructures(toFind.RHS, a.RHS))
         {
             return(a);
         }
     }
     return(null);
 }
Пример #14
0
        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);
            }
        }
Пример #15
0
        /// <summary>
        /// Returns a random analogy from the collection.
        /// Weights it by win/loss stats
        /// </summary>
        /// <returns></returns>
        public Analogy PickAnalogy()
        {
            if (analogies.Count == 0)
            {
                return(null);
            }

            List <Utilities.ObjectValuePair> pairs = new List <Utilities.ObjectValuePair>();

            for (int i = 0; i < analogies.Count; i++)
            {
                Analogy a = analogies[i];

                Utilities.ObjectValuePair pair = new Utilities.ObjectValuePair(analogies[i], analogyCompetitionStats[a].PercentWins);
                pairs.Add(pair);
            }

            return((Analogy)Utilities.PickItemWeighted(pairs));
        }
        public override void Run()
        {
            if (link1 == null)
            {
                link1 = workspace.PickRandomMeasureLinkByRecency();
            }

            if (link1 == null)
            {
                return;
            }

            if (link2 == null)
            {
                link2 = workspace.PickRandomMeasureLinkByRecency();
            }

            if (link2 == null || link1 == link2)
            {
                return;
            }

            // TODO: understand the kind of link involved. For now just use strength score.

            if (Math.Abs(link1.strength - link2.strength) < 10)
            {
                // Try making Analogy.
                Analogy a = new Analogy();
                a.metaLinks.Add(new MetaLink(link1, link2, 100));                       // TODO: metalink strength

                float strength = a.Strength;

                double r = Utilities.rand.NextDouble() * 100;

                if (r < strength)
                {
                    workspace.AddAnalogy(a);
                }
            }
        }
Пример #17
0
        public override void Run()
        {
            if (analogy == null)
            {
                // Pick a strong analogy
                analogy = workspace.PickRandomAnalogyByRecencyAndStrengthAndSize();
            }

            if (analogy == null)
            {
                return;
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, analogy.LHS.MinLocation, analogy.LHS.MaxLocation);
            workspace.RecordCodeletAttentionHistory(this, analogy.RHS.MinLocation, analogy.RHS.MaxLocation);

            // If strong and big enough, add!
            if (analogy.Strength > Constants.MIN_ANALOGY_STRENGTH_LTM && analogy.LengthInMeasures >= Constants.MIN_ANALOGY_LENGTH_LTM)
            {
                workspace.structureCollection.AddAnalogy(analogy);
            }
        }
Пример #18
0
 /// <summary>
 /// Use this constructer to tell the codelet which analogy to examine.
 /// Otherwise, it picks one randomly.
 /// </summary>
 /// <param name="urgency"></param>
 /// <param name="parent"></param>
 /// <param name="coderack"></param>
 /// <param name="workspace"></param>
 /// <param name="slipnet"></param>
 /// <param name="notes"></param>
 public DestroyOldAnalogyrCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, Analogy analogy)
     : base("Destroy Old Analogy", urgency, parent, coderack, workspace, slipnet)
 {
     this.analogy = analogy;
 }
Пример #19
0
        public override void Run()
        {
            if (analogy == null)
            {
                analogy = workspace.PickRandomAnalogyByWeaknessAndAge();
            }

            if (analogy == null)
            {
                return;
            }

            if (analogy.Strength > Constants.DESTROY_ANALOGIES_BELOW_THRESHOLD || analogy.MaxLocation > workspace.CurrentTime - 4)
            {
                return;
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, analogy.LHS.MinLocation);
            workspace.RecordCodeletAttentionHistory(this, analogy.LHS.MaxLocation);
            workspace.RecordCodeletAttentionHistory(this, analogy.RHS.MinLocation);
            workspace.RecordCodeletAttentionHistory(this, analogy.RHS.MaxLocation);


            // verify analogy is unused.
            foreach (Analogy a in workspace.analogies)
            {
                if (analogy == a)
                {
                    continue;
                }
                foreach (Relationship r in a.relationships)
                {
                    if (r is RelationshipAnalogy)
                    {
                        if (((RelationshipAnalogy)r).Analogy == analogy)
                        {
                            // It's used; return.
                            return;
                        }
                    }
                }
            }

            // Verify analogy isn't supporting a group.
            foreach (Group g in workspace.groups)
            {
                foreach (GroupReason gr in g.Reasons)
                {
                    if (gr is GroupReasonAnalogySupport)
                    {
                        if (((GroupReasonAnalogySupport)gr).Analogy == analogy)
                        {
                            return;
                        }
                    }
                }
            }


            double x = Utilities.rand.NextDouble() * 100;

            if (x > analogy.Strength)
            {
                workspace.DropAnalogyNoChecks(analogy);
            }
        }
Пример #20
0
 /// <summary>
 /// Copies the relationship, updating group pointers to use the groups in this analogy.
 /// </summary>
 /// <param name="r"></param>
 /// <param name="a"></param>
 /// <returns></returns>
 public abstract Relationship DeepCopyUsingNewGroupsInAnalogy(Relationship r, Analogy a);
 public GroupReasonAnalogySupport(Group group, double reasonStrength, Analogy a)
     : base(group, reasonStrength)
 {
     this.reasonWeight = Constants.WEIGHT_REASON_ANALOGY;
     this.Analogy      = a;
 }
Пример #22
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));
        }
Пример #23
0
 /*{
  *              GroupElement newLHS, newRHS;
  *
  *              FindNewLHSRHS(r, a, out newLHS, out newRHS);
  *
  *              Relationship rNew = new Relationship(newLHS, newRHS, a.Strength, r.RhythmType);
  *              return rNew;
  *      }
  */
 protected static void FindNewLHSRHS(Relationship r, Analogy a, out GroupElement newLHS, out GroupElement newRHS)
 {
     newLHS = a.FindSubGroupEquivalentToGroup(r.LHS);
     newRHS = a.FindSubGroupEquivalentToGroup(r.RHS);
 }
Пример #24
0
        public override void Run()
        {
            // If groups are not specified already,
            // look for two groups that are linked.
            if (ge1 == null || ge2 == null || !workspace.GroupElements.Contains(ge1) || !workspace.GroupElements.Contains(ge2))
            {
                // Pick a link.
                Relationship r = workspace.PickRandomRelationshipByRecencyAndStrength();
                if (r == null)
                {
                    return;
                }
                ge1 = r.LHS;
                ge2 = r.RHS;

                // We prefer an analogy between parent groups containing the ends of this link.
                //Alternately, just try to find analogies when starting-relatinoships are found/

                // If both have parents, spawn a codelet to look at that!
                // If they don't have parents, try to make groups including them.

                if (ge1.hasParent && ge2.hasParent)
                {
                    if (ge1.parentGroup != ge2.parentGroup)
                    {
                        CreateAnalogyCodelet cac = new CreateAnalogyCodelet((int)this.rawUrgency, this, coderack, workspace, slipnet, ge1.parentGroup, ge2.parentGroup);
                        coderack.AddCodelet(cac);
                    }
                }
                else
                {
                    MetaGrouperCodelet mgc1 = new MetaGrouperCodelet((int)this.rawUrgency, this, coderack, workspace, slipnet, ge1);
                    MetaGrouperCodelet mgc2 = new MetaGrouperCodelet((int)this.rawUrgency, this, coderack, workspace, slipnet, ge2);
                    coderack.AddCodelet(mgc1);
                    coderack.AddCodelet(mgc2);
                }

                // Now try to work with this relationship as well, just in case it's at a good level and has good support.
            }

            if (ge1 == null || ge2 == null || ge1 == ge2 || !workspace.GroupElements.Contains(ge1) || !workspace.GroupElements.Contains(ge2))
            {
                return;
            }

            if (ge1.Location > ge2.Location)
            {
                GroupElement tmp = ge1;
                ge1 = ge2;
                ge2 = tmp;
            }

            // Make analogies between single measures?
            if (Constants.MAKE_SINGLE_MEASURE_ANALOGIES)
            {
                if (!(ge1 is Group && ge2 is Group))
                {
                    return;
                }
            }

            // Check for redundant (identical) analogies!
            foreach (Analogy a2 in workspace.analogies)
            {
                if (a2.LHS == ge1 && a2.RHS == ge2)
                {
                    return;
                }
            }

            // Make sure the 2 sides of the analogy span distinct time intervals (no mapping of m. 1-3 onto m.1-5, for instance)
            if (ge1.MaxLocation >= ge2.MinLocation)
            {
                return;
            }


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

            // So now we have 2 group elements, and we want to consider an analogy between them.
            // Look for a relationship between these elements (if bottom-level) or their subelements (if they have children)
            Analogy a = new Analogy(ge1, ge2, workspace);

            foreach (Relationship r in workspace.relationships)
            {
                // Check if this relatinoship is relevant.
                if (ge1 is Group)
                {
                    Group g1 = (Group)ge1;
                    if (!g1.GroupElements.Contains(r.LHS))
                    {
                        continue;
                    }
                }
                else
                {
                    if (r.LHS != ge1)
                    {
                        continue;
                    }
                }
                if (ge2 is Group)
                {
                    Group g2 = (Group)ge2;
                    if (!g2.GroupElements.Contains(r.RHS))
                    {
                        continue;
                    }
                }
                else
                {
                    if (r.RHS != ge2)
                    {
                        continue;
                    }
                }

                // Inside an analogy, we can only have one relationship (of normal similarity type) for each measure. For multiple ones, have them compete.
                // Compete against each conflicting relationship. Only add if it beats them all.
                bool won = true;
                List <Relationship> conflicting = a.FindConflictingRelationships(r);
                foreach (Relationship r2 in conflicting)
                {
                    if (FightItOut(r, r2) == r2)
                    {
                        won = false;
                        break;
                    }
                }
                if (!won)
                {
                    continue;
                }

                foreach (Relationship r2 in conflicting)
                {
                    a.relationships.Remove(r2);
                }

                a.TryToAddRelationship(r);
            }

            // Make sure we were able to add something.
            if (a.relationships.Count == 0)
            {
                return;
            }


            // Create analogy if it's strong enough. Then other codelets will strengthen it and use it if necessary.. this
            // codelet just starts it up.
            double rnd   = Utilities.rand.NextDouble() * 100;
            double score = a.Strength;

            if (rnd < score)
            {
                if (workspace.AddAnalogy(a))
                {
                    // Spawn more codelets to improve this analogy.
                    AddRelationshipsToAnalogyCodelet arac = new AddRelationshipsToAnalogyCodelet(100, this, coderack, workspace, slipnet, a);

                    // Consider a metagroup if the two items in the analogy are neighbors.
                    if (ge1.MaxLocation + 1 == ge2.MinLocation)
                    {
                        MetaGrouperCodelet mgc = new MetaGrouperCodelet(100, this, coderack, workspace, slipnet, ge1, ge2, a);
                        coderack.AddCodelet(mgc);
                    }

                    // Improve strength of subgroups.
                    SpawnAnalogyReasonCodeletsRecur(ge1, a);
                    SpawnAnalogyReasonCodeletsRecur(ge2, a);

                    // Add contour analysis for LHS-RHS.
                    LookForContourRelationshipCodelet lrc = new LookForContourRelationshipCodelet(100, this, coderack, workspace, slipnet, ge1, ge2);
                    coderack.AddCodelet(lrc);
                    AddRelationshipsToAnalogyCodelet arc = new AddRelationshipsToAnalogyCodelet(50, this, coderack, workspace, slipnet, a);
                    coderack.AddCodelet(arc);
                }
            }
        }
 /// <summary>
 /// Use this constructer to tell the codelet which link to examine.
 /// Otherwise, it picks one randomly.
 /// </summary>
 /// <param name="urgency"></param>
 /// <param name="parent"></param>
 /// <param name="coderack"></param>
 /// <param name="workspace"></param>
 /// <param name="slipnet"></param>
 /// <param name="notes"></param>
 public GenerateExpectedAnalogyCopyCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, Analogy analogy)
     : base("Generate Expected Analogy Copy", urgency, parent, coderack, workspace, slipnet)
 {
     this.analogy = analogy;
 }
 /// <summary>
 /// Use this constructer to tell the codelet which group to examine.
 /// Otherwise, it picks one randoml.y
 /// </summary>
 /// <param name="urgency"></param>
 /// <param name="parent"></param>
 /// <param name="coderack"></param>
 /// <param name="workspace"></param>
 /// <param name="slipnet"></param>
 /// <param name="notes"></param>
 public AddNonRhythmRelationshipsToAnalogyCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                                                  Analogy analogy)
     : base("Add Nonrhythm Relationships To Analogy", urgency, parent, coderack, workspace, slipnet)
 {
     this.analogy = analogy;
 }
Пример #27
0
        public override void Run()
        {
            if (analogy == null)
            {
                analogy = workspace.structureCollection.PickAnalogy();
            }

            if (analogy == null)
            {
                return;
            }

            // Check if it already exists in the workspace.
            if (workspace.FindEquivalentAnalogy(analogy) != null)
            {
                return;
            }

            // Construct the new LHS and RHS, using existing groups in the workspace if they are there. If groups don't exist, try to add.
            // Also find conflicts.
            GroupElement lhs, rhs;
            bool         foundLHS, foundRHS;
            List <Group> conflictGroups = new List <Group>();

            lhs = ProcessAnalogyComponent(analogy.LHS, out foundLHS, conflictGroups);
            rhs = ProcessAnalogyComponent(analogy.RHS, out foundRHS, conflictGroups);

            // Find conflict Analogies.
            List <Analogy> conflictAnalogies = FindConflictAnalogies(conflictGroups);

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, analogy.LHS.MinLocation, analogy.LHS.MaxLocation);
            workspace.RecordCodeletAttentionHistory(this, analogy.RHS.MinLocation, analogy.RHS.MaxLocation);

            // Evaluate current workspaces conflict measures vs. the proposed recussitated version, in terms of happiness.

            // Find conflict measures.
            HashSet <int> conflictMeasures = UnionGroupRanges(conflictGroups);

            // Eval happiness for original version.
            double happinessOld = EvalHappiness(conflictMeasures);

            // Swap out groups/analogies and reeval happiness for new proposed version.
            workspace.DropGroups(conflictGroups);
            workspace.DropAnalogies(conflictAnalogies);

            // Put in the new pile of stuff.
            if (!foundLHS)
            {
                workspace.AddGroupAndSubgroupsNoChecks(lhs);
            }
            if (!foundRHS)
            {
                workspace.AddGroupAndSubgroupsNoChecks(rhs);
            }

            Analogy newAnalogy = new Analogy(lhs, rhs, workspace);

            workspace.AddAnalogy(newAnalogy);
            newAnalogy.CloneRelationshipsFrom(analogy);

            // Eval.
            double happinessNew = EvalHappiness(conflictMeasures);

            if (Utilities.FightItOut(happinessNew, happinessOld, workspace.Temperature))
            {
                // New one won!

                // Update stats.
                workspace.structureCollection.analogyCompetitionStats[analogy].AddWin();

                // Reify all temporary groups.
                if (newAnalogy.LHS is TemporaryGroup)
                {
                    Group realLHS = ((TemporaryGroup)lhs).MakeRealGroup();
                    newAnalogy.LHS = realLHS;
                }

                if (newAnalogy.RHS is TemporaryGroup)
                {
                    Group realRHS = ((TemporaryGroup)rhs).MakeRealGroup();
                    newAnalogy.RHS = realRHS;
                }

                // Remove left-over relationship stuff from dropped /analogoes
                workspace.CompleteDropGroups(conflictGroups);
                workspace.CompleteDropAnalogies(conflictAnalogies);
            }
            else
            {
                // Old one won! revert.
                // Update stats.
                workspace.structureCollection.analogyCompetitionStats[analogy].AddLoss();

                workspace.DropGroupAndSubgroupsNoChecks(lhs);
                workspace.DropGroupAndSubgroupsNoChecks(rhs);
                workspace.DropAnalogyNoChecks(newAnalogy);

                workspace.AddGroupsNoChecks(conflictGroups);
                workspace.AddAnalogiesNoChecks(conflictAnalogies);
            }
        }
Пример #28
0
        public override void Run()
        {
            if (analogy == null)
            {
                analogy = workspace.PickRandomAnalogyByRecencyAndIncompletenessAndSize();
            }

            if (analogy == null)
            {
                return;
            }

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

            // Find unmapped elements on left and right.
            List <GroupElement> unmappedLeft;
            List <GroupElement> unmappedRight;

            analogy.GetUnmappedElements(out unmappedLeft, out unmappedRight);

            // If there were unmapped elements on both sides, look for relationships.
            if (unmappedLeft.Count == 0 || unmappedRight.Count == 0)
            {
                // At least one side is all mapped! Return.
                return;
            }

            // Look for relationships and try to add to analogy.
            foreach (Relationship r in workspace.relationships)
            {
                if (unmappedLeft.Contains(r.LHS) && unmappedRight.Contains(r.RHS))
                {
                    if (analogy.TryToAddRelationship(r))
                    {
                        // Remove from unmapped if adding was successful.
                        unmappedLeft.Remove(r.LHS);
                        unmappedRight.Remove(r.RHS);
                    }
                    // Add to attention history.
                    workspace.RecordCodeletAttentionHistory(this, r.LHS.MinLocation, r.LHS.MaxLocation);
                    workspace.RecordCodeletAttentionHistory(this, r.RHS.MinLocation, r.RHS.MaxLocation);
                }
            }

            // Try again after scouting more, if there are still unmapped pairs.
            if (unmappedLeft.Count == 0 || unmappedRight.Count == 0)
            {
                // At least one side is all mapped! Returned.
                return;
            }

            // Send out scouts for any left over unmapped segments.
            foreach (GroupElement geLeft in unmappedLeft)
            {
                foreach (GroupElement geRight in unmappedRight)
                {
                    LookForRelationshipCodelet lrc = new LookForRelationshipCodelet(90, this, coderack, workspace, slipnet, geLeft, geRight);
                    coderack.AddCodelet(lrc);

                    if (geLeft is Group && geRight is Group)
                    {
                        LookForMetricPositionRelationshipCodelet lmrc = new LookForMetricPositionRelationshipCodelet(90, this, coderack, workspace, slipnet, (Group)geLeft, (Group)geRight);
                        coderack.AddCodelet(lmrc);
                    }
                }
            }

            // Try to add relationships again later (respawn a codelet just like this one).
            AddRelationshipsToAnalogyCodelet arac = new AddRelationshipsToAnalogyCodelet((int)this.rawUrgency, this, coderack, workspace, slipnet, analogy);

            coderack.AddCodelet(arac);
        }
Пример #29
0
 /// <summary>
 /// Use this constructer to tell the codelet which groups to examine.
 /// Otherwise, it picks one randomly.
 /// </summary>
 /// <param name="urgency"></param>
 /// <param name="parent"></param>
 /// <param name="coderack"></param>
 /// <param name="workspace"></param>
 /// <param name="slipnet"></param>
 /// <param name="notes"></param>
 public RecussitateAnalogyCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, Analogy analogy)
     : base("Recussitate Analogy", urgency, parent, coderack, workspace, slipnet)
 {
     this.analogy = analogy;
 }
        public override void Run()
        {
            if (sourceAnalogy == null)
            {
                // Pick an expected group to start from.
                sourceAnalogy = workspace.PickRandomGapAnalogyByRecencyAndStrengthAndSizeAndGapSize();
            }

            if (sourceAnalogy == null)
            {
                return;
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, sourceAnalogy.LHS.MinLocation, sourceAnalogy.LHS.MaxLocation);
            workspace.RecordCodeletAttentionHistory(this, sourceAnalogy.RHS.MinLocation, sourceAnalogy.RHS.MaxLocation);

            // Look in two places for parallel analogies: one crossing the LHS of the given analogy (RHS of new in the gap),
            // and one crossing the RHS (the LHS will be in the gap)
            int sizeRHS = sourceAnalogy.RHS.LengthInMeasures;
            int sizeLHS = sourceAnalogy.LHS.LengthInMeasures;
            int gapMin  = sourceAnalogy.LHS.MaxLocation + 1;
            int gapMax  = sourceAnalogy.RHS.MinLocation - 1;

            // Make sure gap is large enough to hold something of appropriate size.
            if (gapMax - gapMin < Math.Min(sizeLHS, sizeRHS))
            {
                return;
            }

            // First try to find a group inside the gap, of the same size as the RHS.
            GroupElement geGap = FindGroupElementInRange(gapMin, gapMax, sizeRHS);

            if (geGap != null)
            {
                // Now look for an element of same size as LHS, to the left of the original LHS.
                // We find the difference in starting pos between the element in the gap and the RHS, and look at that offset to the left to find an element.
                // If none found, send grouping codelets.
                int          offset = sourceAnalogy.RHS.MinLocation - geGap.MinLocation;
                GroupElement geLHS  = FindGroupElementStartingNearPointWithMax(sourceAnalogy.LHS.MinLocation - offset, sourceAnalogy.LHS.MinLocation - 1, sizeLHS);                     // start point, max, size
                if (geLHS != null)
                {
                    // Try to form analogy.
                    CreateAnalogyCodelet cac = new CreateAnalogyCodelet(100, this, coderack, workspace, slipnet, geLHS, geGap);
                    coderack.AddCodelet(cac);
                }
                else
                {
                    // Send group scouts to find something in that area.
                    SendGroupScoutsToArea(sourceAnalogy.LHS.MinLocation - offset, sizeLHS);                     // min, size
                }
            }
            else
            {
                // Send group scouts to look in the gap.
            }

            // Second try to find a group inside the gap, of the same size as the LHS.
            geGap = FindGroupElementInRange(gapMin, gapMax, sizeLHS);
            if (geGap != null)
            {
                // Now look for an element of same size as RHS, to the right of the original RHS.
                // We find the difference in starting pos between the element in the gap and the RHS, and look at that offset to the left to find an element.
                // If none found, send grouping codelets.
                int          offset = geGap.MinLocation - sourceAnalogy.LHS.MaxLocation;
                GroupElement geRHS  = FindGroupElementStartingNearPointWithMin(sourceAnalogy.RHS.MaxLocation + offset, sourceAnalogy.RHS.MaxLocation + 1, sizeRHS);                     // start point, min, size
                if (geRHS != null)
                {
                    // Try to form analogy.
                    CreateAnalogyCodelet cac = new CreateAnalogyCodelet(100, this, coderack, workspace, slipnet, geGap, geRHS);
                    coderack.AddCodelet(cac);
                }
                else
                {
                    // Send group scouts to find something in that area.
                    SendGroupScoutsToArea(sourceAnalogy.RHS.MaxLocation + offset, sizeRHS);                     // min, size
                }
            }
        }