/// <summary>
 /// Use this constructer to tell the codelet which measure 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 AnalogyFinderCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                             MeasureLink link1, MeasureLink link2)
     : base("Analogy Scout", urgency, parent, coderack, workspace, slipnet)
 {
     this.link1 = link1;
     this.link2 = link2;
 }
        public override void Run()
        {
            double r;

            // If the analogy was specified, examine a link inside it.
            if (analogy != null)
            {
                // Pick links that start and end in the time span delimeted by the LHS of RHS of the analogy
                List <MeasureLink> links = new List <MeasureLink>();

                foreach (MeasureLink link2 in workspace.measureLinks)
                {
                    // Does it start in the LHS or RHS?
                    if (analogy.IncludesLocation(link2.m1.Location) && analogy.IncludesLocation(link2.m2.Location))
                    {
                        links.Add(link2);
                    }
                }
                MeasureLink linkToDelete = Utilities.PickItem <MeasureLink>(links);

                if (linkToDelete == null)
                {
                    return;
                }
                r = Utilities.rand.NextDouble() * 100;

                if (r > linkToDelete.strength / 2)
                {
                    workspace.BreakMeasureLink(linkToDelete);
                }

                return;
            }
            else if (link == null)
            {
                link = workspace.PickRandomMeasureLinkByOldest();
            }

            // Make sure we have a link, and make sure it's not too recent.
            if (link == null || link.MostRecentMeasure.Location + 4 > workspace.measures.Count)
            {
                return;
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, link.m1.Location);
            workspace.RecordCodeletAttentionHistory(this, link.m2.Location);


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

            if (r > link.strength)
            {
                workspace.BreakMeasureLink(link);
            }
        }
        public override void Run()
        {
            if (link == null)
            {
                link = workspace.PickRandomMeasureLinkByRecencyAndStrength();
            }

            if (link == null)
            {
                return;
            }

            Measure m1 = link.m1;
            Measure m2 = link.m2;

            // Check if the measures are adjacent. Otherwise, we can't group.
            if (Math.Abs(m1.number - m2.number) != 1)
            {
                return;
            }

            // Check if either measure is in another group. If so, we can't group.
            //if (m1.hasParent || m2.hasParent)
            //	return;

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, m1.Location);
            workspace.RecordCodeletAttentionHistory(this, m2.Location);

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

            // Try to group if the link is strong enough.
            if (r < link.strength)
            {
                //Group g = workspace.CreateAndAddGroup(m1, m2);

                TemporaryGroup tmpG = new TemporaryGroup(workspace, m1, m2);

                // Record the grouping reason.
                GroupReason reason;
                // Are measures identical?
                if (m1.IsExactlyEqualTo(m2))
                {
                    reason = new GroupReasonComponentsIdentical(tmpG);
                }
                else
                {
                    reason = new GroupReasonComponentsSimilar(tmpG, link.strength);
                }
                tmpG.AddGroupReason(reason);

                // Try to add!
                workspace.AddGroup(tmpG);
            }
        }
Exemple #4
0
        public ExpectedMeasureLink MakeNewExpectedMeasureLink(MeasureLink sourceLink, int offset, float strength)
        {
            int location1 = sourceLink.m1.number;
            int location2 = sourceLink.m2.number;

            ExpectedMeasure em1 = workspace.expectations.GetExpectedMeasure(location1 + offset);
            ExpectedMeasure em2 = workspace.expectations.GetExpectedMeasure(location2 + offset);

            ExpectedMeasureLink expectedLink = new ExpectedMeasureLink(sourceLink, em1, em2, strength);

            links.Add(expectedLink);
            return(expectedLink);
        }
        public override void Run()
        {
            if (link == null)
            {
                link = workspace.PickRandomMeasureLinkByRecency();
            }

            if (link == null)
            {
                return;
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, link.m1.Location);
            workspace.RecordCodeletAttentionHistory(this, link.m2.Location);

            // Compare with other links from each measure.  Both measures need to have other alternate links, or else we leave alone.
            List <MeasureLink> links1 = workspace.GetOtherLinksForMeasure(link.m1, link);
            List <MeasureLink> links2 = workspace.GetOtherLinksForMeasure(link.m2, link);

            if (links1.Count == 0 && links2.Count == 0)
            {
                return;
            }

            float minStrength1 = GetMinStrength(links1);
            float minStrength2 = GetMinStrength(links2);

            float strengthToBeat = Math.Min(minStrength1, minStrength2);

            // If this link's strength is weaker than the "weakest link", try to destroy.
            if (link.strength <= strengthToBeat)
            {
                double r = Utilities.rand.NextDouble() * 100;

                if (r > link.strength)
                {
                    workspace.BreakMeasureLink(link);
                }
            }
        }
        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);
                }
            }
        }
        private void MakeLinkedMeasureLabel(Measure m, MeasureLink selectedLink)
        {
            // Check the original measure for a label. If it has none, assign it and this measure at the same time.
            Measure other = selectedLink.Other(m);

            if (other.FormLabel == null)
            {
                SetMeasureLabel(other, DEFAULT_LABEL_STRENGTH, workspace.GetNextFormLabel(0));
            }

            // Make a prime label.
            string label;

            if (other.FormLabel[other.FormLabel.Length - 1] == '\'')
            {
                label = "(" + other.FormLabel + ")'";
            }
            else
            {
                label = other.FormLabel + '\'';
            }
            TryToSetMeasureLabel(m, selectedLink.strength, label);
        }
 public ExpectedMeasureLink(MeasureLink sourceMeasureLink, ExpectedMeasure expectedMeasure1, ExpectedMeasure expectedMeasure2, float expectationStrength)
     : base(expectedMeasure1, expectedMeasure2, expectationStrength)
 {
     this.expectationStrength = expectationStrength;
 }
 /// <summary>
 /// Use this constructer to tell the codelet which measure 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 MeasureLinkBreakerCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, MeasureLink link)
     : base("Measure Link Breaker", urgency, parent, coderack, workspace, slipnet)
 {
     this.link = link;
 }
        public override void Run()
        {
            if (e == null)
            {
                e = workspace.PickRandomGroupElementByRecency();
            }

            if (e == null)
            {
                return;
            }

            if (!workspace.GroupElements.Contains(e))
            {
                return;
            }

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

            // Find previous elements linking to this object.

            if (e is Measure)
            {
                Measure m = (Measure)e;

                // Special case: measure 1. Just call it "a".
                if (m.number == 0)
                {
                    //				TryToSetMeasureLabel(m, DEFAULT_LABEL_STRENGTH, workspace.GetNextFormLabel(0));
                    m.FormLabel         = "a";
                    m.FormLabelStrength = 100;
                    return;
                }


                // Find previous measures linking to this measure.
                List <Utilities.ObjectValuePair> pairs = new List <Utilities.ObjectValuePair>();
                for (int i = 0; i < m.number; i++)
                {
                    Measure m2 = workspace.measures[i];
                    // Find links.
                    foreach (MeasureLink link in workspace.measureLinks)                       // TODO: speed up link lookup.
                    {
                        if ((link.m1 == m && link.m2 == m2) || link.m1 == m2 && link.m2 == m)
                        {
                            // If there is a 100% strength link, just use it and copy the label.
                            if (link.strength > 99.999)
                            {
                                TryToSetMeasureLabel(m, 100, m2.FormLabel);
                                return;
                            }


                            // Match.
                            Utilities.ObjectValuePair pair = new Utilities.ObjectValuePair(link, link.strength);
                            pairs.Add(pair);
                        }
                    }
                    // If pairs is empty, try to make a new label of default strength, and quit.
                    // If a label exists, we fight.
                    if (pairs.Count == 0)
                    {
                        TryToMakeNewMeasureLabel(m, DEFAULT_LABEL_STRENGTH);
                        return;
                    }

                    // Pick a link probabilistically based on weights.
                    MeasureLink selectedLink = (MeasureLink)Utilities.PickItemWeighted(pairs);

                    float similarity = m.ComputeRhythmicSimilarity(m2);

                    if (similarity > 99.9)
                    {
                        // Use same label.
                        TryToSetMeasureLabel(m, 100, m2.FormLabel);
                        return;
                    }

                    // Decide whether or not to try to add label based on this link, probabilitically.
                    double r = Utilities.rand.NextDouble() * 100;
                    if (selectedLink.strength > 75 && r < selectedLink.strength)
                    {
                        // We're going to try to label. Should it be a duplicate of the former label or a "prime" version" or compeltely new?
                        // If it's a different measure at all, don't use the origianl label. If it's different, decide based on strength of link whether to make ' version.


                        // Make a prime version.
                        MakeLinkedMeasureLabel(m, selectedLink);
                        return;
                    }
                    else
                    {
                        //Link was too weak; just assign a new label.
                        TryToMakeNewMeasureLabel(m, DEFAULT_LABEL_STRENGTH);
                        //return;
                    }
                }
            }
            else
            {
                // Group case.
            }
        }
Exemple #11
0
 public GroupReasonExpectationMeasureLink(Group group, double reasonStrength, MeasureLink link)
     : base(group, reasonStrength)
 {
     this.reasonWeight = Constants.WEIGHT_REASON_ANALOGY;
     this.link         = link;
 }
        // Creates group scout codelets for a group starting near position, of given size.
        private void SendGroupScoutsToArea(int position, int size)
        {
            if (size < 3)
            {
                if (size < 2)
                {
                    return;
                }
                if (position < 0 || position + 1 >= workspace.measures.Count)
                {
                    return;
                }

                ProximityGrouperCodelet pgc = new ProximityGrouperCodelet(
                    40, this, coderack, workspace, slipnet,
                    workspace.measures[position],
                    workspace.measures[position + 1]);
                coderack.AddCodelet(pgc);

                // Try to find measure link.
                MeasureLink link = workspace.FindMeasureLink(position, position + 1);
                if (link != null)
                {
                    MeasureSamenessGrouperCodelet mgc = new MeasureSamenessGrouperCodelet(
                        60, this, coderack, workspace, slipnet,
                        link);
                    coderack.AddCodelet(mgc);
                }

                return;
            }


            int range = size / 2;

            for (int pos = position - range; pos <= position + range; pos++)
            {
                // Set urgency higher for an exact fit.
                int u = 30;
                if (pos == position)
                {
                    u = 100;
                }

                Group g1 = null, g2 = null;
                bool  foundBoth = false;
                // Try to divide into 2 subelements: find existing groups in range.
                for (int split = pos + 2; split <= pos + size - 2; split++)
                {
                    foundBoth = TryToFindGroups(pos, split - 1, split, pos + size - 1, out g1, out g2);

                    if (!foundBoth && pos == position)
                    {
                        if (g1 == null)
                        {
                            SendGroupScoutsToArea(pos, split - pos);
                        }
                        else
                        {
                            SendGroupScoutsToArea(split, pos + size - split + 1);
                        }
                    }

                    if (foundBoth)
                    {
                        break;
                    }
                }

                if (!foundBoth)
                {
                    continue;
                }

                MetaGrouperCodelet mgc = new MetaGrouperCodelet(u, this, coderack, workspace, slipnet, g1, g2);
                coderack.AddCodelet(mgc);
            }
        }
Exemple #13
0
        public override void Run()
        {
            if (group == null)
            {
                group = workspace.expectations.FindLargestExpectationGroup();
            }

            if (group == null)
            {
                return;
            }

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

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

            // Pick a measure in the group, and look for a link to the measure with inspired this expectation.
            for (int i = group.MinLocation; i <= group.MaxLocation; i++)
            {
                int idxFirst = i - workspace.expectations.Offset;
                if (idxFirst < 0 || idxFirst >= workspace.measures.Count || i >= workspace.measures.Count)
                {
                    continue;
                }
                MeasureLink theLink = null;
                foreach (MeasureLink link in workspace.measureLinks)
                {
                    if (link.m1.Location == i - workspace.expectations.Offset && link.m2.Location == i)
                    {
                        theLink = link;
                        break;
                    }
                }

                if (theLink == null)
                {
                    // If no link, try to form one.
                    MeasureLinkerCodelet mlc = new MeasureLinkerCodelet((int)this.rawUrgency, this, coderack, workspace, slipnet,
                                                                        workspace.measures[idxFirst], workspace.measures[i]);
                    coderack.AddCodelet(mlc);
                    continue;
                }

                // We have a link.
                // Make sure it's not already listed
                bool found = false;
                foreach (GroupReason gr in group.Reasons)
                {
                    if (gr is GroupReasonExpectationMeasureLink)
                    {
                        if (((GroupReasonExpectationMeasureLink)gr).link == theLink)
                        {
                            found = true;
                            break;
                        }
                    }
                }
                if (found)
                {
                    continue;
                }
                if (theLink.strength > 75)
                {
                    GroupReasonExpectationMeasureLink grm = new GroupReasonExpectationMeasureLink(group, theLink.strength, theLink);
                    group.Reasons.Add(grm);
                }
            }
        }
 /// <summary>
 /// Use this constructer to tell the codelet which measure 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 ExamineLinkedPairsCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, MeasureLink link)
     : base("Examine Linked Pairs", urgency, parent, coderack, workspace, slipnet)
 {
     this.link = link;
 }