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);
        }
Beispiel #2
0
        public ExpectedAnalogy MakeNewExpectedAnalogy(Group group, ExpectedGroup expectedGroup, float expectationStrength, int level)
        {
            ExpectedAnalogy a = new ExpectedAnalogy(group, expectedGroup, workspace, expectationStrength, level);

            analogies.Add(a);
            return(a);
        }
Beispiel #3
0
        private ExpectedGroup MakeNewExpectedGroup(int minLocation, int maxlocation, int level, double strength)
        {
            ExpectedGroup g = new ExpectedGroup(workspace, minLocation, maxlocation, level, (float)strength);

            groups.Add(g);                      // TODO: deal with any existing conflicts............???
            return(g);
        }
Beispiel #4
0
        public ExpectedGroup GenerateExpectedGroupBasedOn(Group group, int offset, float expectationStrength, bool compete)
        {
            // First check other competing expectation groups. Delete them and add this one if we win.
            // Note that ALL other expectation groups are considered "competing". i.e. only 1 set of expectations can be
            // active at one time.

            double str = group.ComputeStrength();

            // Look for conflicts and resolve.

            if (compete)
            {
                /*
                 * foreach (Group g2 in groups) {
                 *      if (Utilities.FightItOut(g2.ComputeStrength(), str, workspace.Temperature)) {
                 *              // Lost fight.
                 *              return null;
                 *      }
                 * }*/
                ExpectedGroup eGroup = FindLargestExpectationGroup();
                if (eGroup != null)
                {
                    if (Utilities.FightItOut(eGroup.ComputeStrength(), str, workspace.Temperature))
                    {
                        // Lost fight.
                        return(null);
                    }
                }

                // Won all fights. Clean out expectations.
                RemoveAllExpectations();
                this.offset      = offset;
                this.sourceGroup = group;
            }


            // Copy the group structure, recursively.
            ExpectedGroup expectedGroup = MakeNewExpectedGroup(group.MinLocation + offset, group.MaxLocation + offset, group.Level, group.ComputeStrength());


            foreach (GroupElement e in group.GroupElements)
            {
                if (e is Measure)
                {
                    GenerateExpectedMeasureBasedOn((Measure)e, offset, group.ComputeStrength());
                }
                else
                {
                    GenerateExpectedGroupBasedOn((Group)e, offset, expectationStrength, false);
                }
            }

            return(expectedGroup);
        }
Beispiel #5
0
        private List <ExpectedGroup> FindExpectedGroupsAbove(ExpectedGroup eg)
        {
            List <ExpectedGroup> above = new List <ExpectedGroup>();

            foreach (ExpectedGroup eg2 in workspace.expectations.groups)
            {
                if (eg2.Level > eg.Level && eg2.MinLocation == eg.MinLocation)
                {
                    above.Add(eg2);
                }
            }
            return(above);
        }
Beispiel #6
0
        public ExpectedGroup FindLargestExpectationGroup()
        {
            int           maxSize = -1;
            ExpectedGroup argmax  = null;

            foreach (ExpectedGroup g2 in groups)
            {
                int size = g2.LengthInMeasures;
                if (size > maxSize)
                {
                    maxSize = size;
                    argmax  = g2;
                }
            }
            return(argmax);
        }
Beispiel #7
0
 public GroupReasonExpected(Group group, double reasonStrength, ExpectedGroup expectedGroup)
     : base(group, reasonStrength)
 {
     this.reasonWeight  = Constants.WEIGHT_REASON_EXPECTED;
     this.expectedGroup = expectedGroup;
 }
Beispiel #8
0
        public override void Run()
        {
            if (eg == null)
            {
                // Pick a 1st level group starting now.
                eg = workspace.expectations.PickRandomGroupWithConditions(workspace.measures.Count - 1, 1);
            }

            if (eg == null)
            {
                return;
            }

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

            // 2 conditions: expected group starts now, or it started earlier.

            // Case 1:
            if (eg.MinLocation == workspace.measures.Count - 1)
            {
                // Check level. Level 1, or metagroup?
                if (eg.Level == 1)
                {
                    // Suggest a group that matches the expected group.
                    TemporaryGroup tmpG = new TemporaryGroup(workspace, workspace.measures[workspace.measures.Count - 1]);
                    tmpG.AddGroupReason(new GroupReasonExpected(tmpG, eg.ComputeStrength(), eg));
                    Group newGroup = workspace.AddGroup(tmpG);
                    if (newGroup == null)
                    {
                        return;
                    }

                    // TODO

                    /*
                     * //Also add any metagroups above this one.
                     * List<ExpectedGroup> metagroups = new List<ExpectedGroup>();
                     * metagroups = FindExpectedGroupsAbove(eg);
                     * Group child = newGroup;
                     * foreach (ExpectedGroup meta in metagroups) {
                     *      tmpG = new TemporaryGroup(workspace, child);
                     *      tmpG.AddGroupReason(new GroupReasonExpected(tmpG, meta.ComputeStrength(), meta));
                     *      newGroup = workspace.AddGroup(tmpG);
                     *      if (newGroup == null)
                     *              return;
                     *      child = newGroup;
                     * }
                     */
                }
                else
                {
                    // Metagroup.
                    // TODO.
                }
            }
            else
            {
                // Case 2: expected group started earlier.

                // Check level.
                // TODO.
            }



            /*
             * double r = Utilities.rand.NextDouble() * 100;
             *
             * // TODO! Score group strenght reasonably.
             * // TODODODODODODOODODODODODODO
             *
             * // Make tmp group.
             * float score = 75 / (1 + (Math.Abs(g2.GroupElements.Count - g1.GroupElements.Count))); //MetaGroupStrength;
             *
             *
             *
             * // Group if the score is strong enough.
             * if (r < score) {
             *      workspace.AddGroup(tmpG);
             *
             *      // TODO: add reasons....
             * }
             */
        }
Beispiel #9
0
 /// <summary>
 /// Use this constructer to tell the codelet which group 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 SuggestGroupFromExpectationCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, ExpectedGroup eg)
     : base("Suggest Group From Expectation", urgency, parent, coderack, workspace, slipnet)
 {
     this.eg = eg;
 }
        public override void Run()
        {
            double weight;

            if (group == null)
            {
                // Pick the *largest* group ending in the final measure and expect it to repeat,
                // with probability based on group strength and hierarchy strength.
                List <Utilities.ObjectValuePair> pairs = new List <Utilities.ObjectValuePair>();
                foreach (Group g in workspace.groups)
                {
                    // Skip groups that don't end on the final measure.
                    if (g.MaxLocation != workspace.MaxLocation)
                    {
                        continue;                         // TODO test
                    }
                    double grpWeight = ComputeWeight(g);

                    Utilities.ObjectValuePair pair = new Utilities.ObjectValuePair(g, grpWeight);
                    pairs.Add(pair);
                }

                //Utilities.ObjectValuePair p = Utilities.PickItemWeightedReturnPair(pairs);
                Utilities.ObjectValuePair p = Utilities.PickLargestItemReturnPair(pairs);
                group  = (Group)p.obj;
                weight = p.value;
            }
            else
            {
                if (!workspace.groups.Contains(group))
                {
                    return;
                }
                weight = ComputeWeight(group);
            }

            if (group == null)
            {
                return;
            }

            // We have an existing group. Try to make an expectation.
            // Note: It will have to compete with other existing expectations  (handle hierarchy differences)
            int startExpectationLocation = workspace.MaxLocation + 1;
            int offset = startExpectationLocation - group.MinLocation;

            // TODO expectation strength is higher for stronger groups, for now.
            // Set the max strength to be the group strength.
            // Decrease strength below this amount for non-well-placed groups with respect to barlines
            float expectationStrength = (float)group.ComputeStrength();

            // Find the length of the group, and find the typical length of a group starting/ending at the barline we're starting on in the expectation.
            int groupLength = group.LengthInMeasures;

            // Don't generate expectations if we dont' have barlines in place yet.
            if (startExpectationLocation >= workspace.barlines.Count)
            {
                return;
            }

            int barlineHeight = workspace.barlines[startExpectationLocation];

            int[] barlineDistances = workspace.FindPreviousBarlineDistances(startExpectationLocation);
            // Fails if barlines don't all exist yet.
            if (barlineDistances == null)
            {
                return;
            }
            // Find the distance closest to an existing distance.
            int min    = Int32.MaxValue;
            int argmin = -1;

            for (int h = 0; h < barlineDistances.Length; h++)
            {
                // Ignore "-1" (unused heights)
                if (barlineDistances[h] == -1)
                {
                    continue;
                }
                int dist = Math.Abs(barlineDistances[h] - groupLength);
                if (dist < min)
                {
                    min    = dist;
                    argmin = h;
                }
            }
            int expectedHeight = argmin;

            // Penalize if the barline is too short for this group length.
            if (barlineHeight < expectedHeight)
            {
                expectationStrength *= 0.7f;
            }
            // Penalize if the group length is a weird number.
            if (min > 0)
            {
                expectationStrength *= (0.7f * ((float)min / barlineDistances[argmin]));
            }


            ExpectedGroup expectedGroup = workspace.expectations.GenerateExpectedGroupBasedOn(group, offset, expectationStrength, true);

            if (expectedGroup != null)
            {
                // Also make a large-scale analogy between the group and the expected groups.
                workspace.expectations.MakeNewExpectedAnalogy(group, expectedGroup, expectationStrength, group.Level + 1);
                // And add supporting analogies between the top-level components of the group and expected group.
                for (int i = 0; i < group.GroupElements.Count; i++)
                {
                    if (group.GroupElements[i] is Group)
                    {
                        Group subgroup      = (Group)group.GroupElements[i];
                        int   startLocation = subgroup.MinLocation;
                        int   endLocation   = subgroup.MaxLocation;
                        // Find expected subgroup for this subgroup.
                        // Note: we can't assume expected groups have elements; they just contain a start and endpoint. So we have to search.
                        ExpectedGroup expectedSubgroup = null;
                        foreach (ExpectedGroup eg in workspace.expectations.groups)
                        {
                            if (eg.MinLocation == startLocation + offset && eg.MaxLocation == endLocation + offset)
                            {
                                expectedSubgroup = eg;
                                break;
                            }
                        }
                        if (expectedSubgroup != null)
                        {
                            workspace.expectations.MakeNewExpectedAnalogy(subgroup,
                                                                          expectedSubgroup,
                                                                          expectationStrength, subgroup.Level + 1);
                        }
                    }
                }


                // Now, add expected within-future analogies and links, simply copies of existing analogies and relationships, shifted by offset.
                // Search for all analogies and relationships which take place within the timespan of the original group.

                // Links.
                foreach (Relationship rel in workspace.relationships)
                {
                    // Skip relationships between non-measures for now.
                    if (!(rel.LHS is Measure && rel.RHS is Measure))
                    {
                        continue;
                    }
                    int idx1 = rel.LHS.Location;
                    int idx2 = rel.RHS.Location;

                    if (group.IncludesLocation(idx1) && group.IncludesLocation(idx2))
                    {
                        workspace.expectations.MakeNewExpectedMeasureLink(idx1, idx2, offset, rel.Strength);
                    }
                }

                // Analogies.
                foreach (Analogy analogy in workspace.analogies)
                {
                    int idxLHS1 = analogy.LHS.MinLocation;
                    int idxLHS2 = analogy.LHS.MaxLocation;
                    int idxRHS1 = analogy.RHS.MinLocation;
                    int idxRHS2 = analogy.RHS.MaxLocation;

                    if (group.IncludesLocation(idxLHS1) && group.IncludesLocation(idxLHS2) &&
                        group.IncludesLocation(idxRHS1) && group.IncludesLocation(idxRHS2))
                    {
                        // Find the future LHS and RHS groups. We have to search for each.
                        ExpectedGroup futureLHS = null, futureRHS = null;

                        foreach (ExpectedGroup eg in workspace.expectations.groups)
                        {
                            if (eg.MinLocation == idxLHS1 + offset && eg.MaxLocation == idxLHS2 + offset)
                            {
                                futureLHS = eg;
                            }
                            else if (eg.MinLocation == idxRHS1 + offset && eg.MaxLocation == idxRHS2 + offset)
                            {
                                futureRHS = eg;
                            }
                            if (futureLHS != null && futureRHS != null)
                            {
                                break;
                            }
                        }
                        if (futureLHS != null && futureRHS != null)
                        {
                            workspace.expectations.MakeNewExpectedAnalogy(futureLHS, futureRHS, analogy.Strength, analogy.Level);
                        }
                    }
                }
            }

            // Add to attention history.
            workspace.RecordCodeletAttentionHistory(this, group.MinLocation, group.MaxLocation);
            workspace.RecordCodeletAttentionHistory(this, group.MinLocation + offset, group.MaxLocation + offset);
        }
Beispiel #11
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);
                }
            }
        }
Beispiel #12
0
 /// <summary>
 /// Use this constructer to tell the codelet which group 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="notesStrengthenExpectationCodeletparam>
 public StrengthenExpectationCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                                     ExpectedGroup group)
     : base("Strengthen Expectation", urgency, parent, coderack, workspace, slipnet)
 {
     this.group = group;
 }