void b_Click(object sender, EventArgs e) { // Find this button's data. foreach (ButtonAndGroup bag in buttonData) { if (sender == bag.button) { // List group reasons. StringBuilder sb = new StringBuilder(); sb.Append("Group: "); sb.AppendLine(GroupString(bag.group, true)); sb.AppendLine(); sb.AppendLine("Scoring:"); sb.Append("Age bonus: "); sb.AppendLine(bag.group.GetAgeStrengthBonus().ToString()); sb.AppendLine(); foreach (GroupReason r in bag.group.Reasons) { sb.AppendLine(r.ToString()); } sb.AppendLine("Penalties:"); foreach (GroupPenaltyReason p in bag.group.PenaltyReasons) { sb.AppendLine(p.ToString()); } sb.AppendLine(); sb.Append("# subcomponents: "); sb.AppendLine(bag.group.GroupElements.Count.ToString()); for (int i = 0; i < bag.group.GroupElements.Count; i++) { GroupElement ge = bag.group.GroupElements[i]; sb.Append("Component #"); sb.Append(i + 1); sb.Append(": "); if (ge is Group) { sb.AppendLine(GroupString((Group)ge, false)); } else { sb.Append("m."); sb.AppendLine((ge.Location + 1).ToString()); } //sb.AppendLine(); } MessageBox.Show(sb.ToString()); } } }
public Sequence(Workspace workspace, GroupElement ge1, GroupElement ge2, GroupElement ge3, double score) : base(workspace) { SequenceElements = new List <GroupElement>(); SequenceElements.Add(ge1); SequenceElements.Add(ge2); SequenceElements.Add(ge3); AddGroupElement(ge1); AddGroupElement(ge2); AddGroupElement(ge3); this.AddGroupReason(new GroupReasonSequence(this, score)); }
/// <summary> /// Generate a contour from a Group /// </summary> /// <param name="group"></param> public MelodyContour(GroupElement ge) { transitions = new List <ContourTransition>(); // Get list of notes in group, removing all rests. // Flatten group. List <Measure> measures = ge.Measures; List <Note> notes = Measure.FlattenToNotesNoRests(measures); for (int i = 0; i < notes.Count - 1; i++) { AddTransition(notes[i], notes[i + 1]); } }
public override void Run() { if (relationship == null) { relationship = workspace.PickRandomRelationshipByRecencyAndStrength(); } if (relationship == null) { return; } GroupElement ge1 = relationship.LHS; GroupElement ge2 = relationship.RHS;
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); } }
private void AddAllSubelements(GroupElement ge, List <GroupElement> subelements) { if (ge is Group) { Group g = (Group)ge; subelements.Add(g); foreach (GroupElement ge2 in g.GroupElements) { AddAllSubelements(ge2, subelements); } } else { subelements.Add(ge); } }
public bool Overlaps(GroupElement ge2) { if (this.MinLocation == ge2.MinLocation) { return(true); } if (this.MinLocation < ge2.MinLocation) { return(this.MaxLocation >= ge2.MinLocation); } else { return(ge2.MaxLocation >= this.MinLocation); } }
private bool ContainsSubGroupRecur(GroupElement ge, GroupElement subgroup) { if (!(ge is Group)) { return(false); } foreach (GroupElement ge2 in ((Group)ge).GroupElements) { if (ge2 == subgroup) { return(true); } if (ContainsSubGroupRecur(ge2, subgroup)) { return(true); } } return(false); }
private GroupElement ProcessAnalogyComponent(GroupElement component, out bool foundExisting, List <Group> conflicts) { GroupElement processedComponent; if (component is Group) { // Look for existing LHS. Group foundLHSGroup = workspace.FindEquivalentGroup((Group)component, false); if (foundLHSGroup != null) { processedComponent = foundLHSGroup; foundExisting = true; } else { // Copy substructure recursively... but don't add groups to workspace yet; we'll do that atomically later. processedComponent = TemporaryCopyExistingGroupOrFindExisting((Group)component); foundExisting = false; } // Look for conflicts and add to list. if (!foundExisting) { List <Group> conflictsHere = workspace.FindConflictingGroups((Group)component); // Add conflists to list, skipping duplicates. foreach (Group gConflict in conflictsHere) { if (!conflicts.Contains(gConflict)) { conflicts.Add(gConflict); } } } } else { // For a measure, no need to clone. processedComponent = component; foundExisting = true; } return(processedComponent); }
private GroupElement SearchForEquivalentGroupElement(GroupElement target, GroupElement parent) { if (target.MatchesRangeOf(parent)) { return(parent); } if (parent is Group) { Group p = (Group)parent; foreach (GroupElement ge in p.GroupElements) { GroupElement result = SearchForEquivalentGroupElement(target, ge); if (result != null) { return(result); } } } return(null); }
public override void Run() { if (eanalogy == null) { // Pick an expected analogy where the RHS is not in the future. eanalogy = workspace.expectations.PickRandomAnalogyWithMaxEndTime(workspace.measures.Count - 1); } if (eanalogy == null) { return; } // Add to attention history. workspace.RecordCodeletAttentionHistory(this, eanalogy.LHS.MinLocation, eanalogy.LHS.MaxLocation); workspace.RecordCodeletAttentionHistory(this, eanalogy.RHS.MinLocation, eanalogy.RHS.MaxLocation); // Look for LHS and RHS. GroupElement LHS = workspace.FindGroupElement(eanalogy.LHS.MinLocation, eanalogy.LHS.MaxLocation); if (LHS == null) { return; } GroupElement RHS = workspace.FindGroupElement(eanalogy.RHS.MinLocation, eanalogy.RHS.MaxLocation); if (RHS == null) { return; } // Try to add an analogy. CreateAnalogyCodelet cac = new CreateAnalogyCodelet((int)this.rawUrgency, this, coderack, workspace, slipnet, LHS, RHS); coderack.AddCodelet(cac); }
/// <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 FindSequenceCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, GroupElement ge1) : base("Find Sequence", urgency, parent, coderack, workspace, slipnet) { this.ge1 = ge1; }
/// <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="notes"></param> public MetaGrouperCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, GroupElement g1) : base("Meta Grouper", urgency, parent, coderack, workspace, slipnet) { if (!(g1 is Group)) { return; } this.g1 = (Group)g1; }
/// <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 ExpectedAnalogy(GroupElement LHS, GroupElement RHS, Workspace workspace, float expectationStrength, int level) : base(LHS, RHS, workspace) { this.expectationStrength = expectationStrength; this.level = level; }
public TemporaryGroup(Workspace workspace, GroupElement ge1, GroupElement ge2) : base(workspace) { groupElements.Add(ge1); // add done without affecting ge1.parentGroup. groupElements.Add(ge2); // add done without affecting ge2.parentGroup. }
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. } }
public override void Run() { if (ge1 == null) { ge1 = workspace.PickRandomGroupElementByRecencyAndStrength(); } if (ge1 == null) { return; } if (ge2 == null) { // pick one adjacent to g1.... ge2 = workspace.PickRandomGroupElementAdjacentTo(ge1); } if (ge2 == null || ge1 == ge2) { return; } if (!workspace.GroupElements.Contains(ge1) || !workspace.GroupElements.Contains(ge2)) { return; } if (ge1.MinLocation > ge2.MinLocation) { GroupElement tmp = ge1; ge1 = ge2; ge2 = tmp; } // Check if the group elements are adjacent. Otherwise, we can't group. int m1 = ge1.MaxLocation; int m2 = ge2.MinLocation; if (m2 - m1 != 1) { return; } //Make sure they are both of same level, or we can't group. if (ge1.Level != ge2.Level) { return; } // Add to attention history. workspace.RecordCodeletAttentionHistory(this, ge1.MinLocation, ge1.MaxLocation); workspace.RecordCodeletAttentionHistory(this, ge2.MinLocation, ge2.MaxLocation); // Check if group already exists! // TODO> double r = Utilities.rand.NextDouble() * 100; // TODO! Score group strenght reasonably. // TODODODODODODOODODODODODODO // Make tmp group. TemporaryGroup tmpG = new TemporaryGroup(workspace, ge1, ge2); float score = 50; // 75 / (1 + (Math.Abs(ge2.GroupElements.Count - ge1.GroupElements.Count))); //MetaGroupStrength; tmpG.AddGroupReason(new GroupReasonNumberOfSubelements(tmpG, score)); // Group if the score is strong enough. if (r < score) { // Check for conflicts, and decide whether to kill conflicting. workspace.AddGroup(tmpG); // TODO: add reasons.... } }
/// <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 ProximityGrouperCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, GroupElement ge1, GroupElement ge2) : base("Proximity Grouper", urgency, parent, coderack, workspace, slipnet) { this.ge1 = ge1; this.ge2 = ge2; }
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 element 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 FormLabelAssignerCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, GroupElement e) : base("Form Label Assigner", urgency, parent, coderack, workspace, slipnet) { this.e = e; }
public void AddGroupElement(GroupElement ge) { ge.parentGroup = this; groupElements.Add(ge); }
public override void Run() { if (ge1 == null) { ge1 = workspace.PickRandomGroupElementByRecencyAndStrength(); } if (ge1 == null) { return; } // Pick 2 more elements to the right if we can. // TODO: be smarter about adjacent picking -- go for same-level/same-size groups? GroupElement ge2 = workspace.PickRandomGroupElementRightOf(ge1); if (ge2 == null) { return; } GroupElement ge3 = workspace.PickRandomGroupElementRightOf(ge2); if (ge3 == null) { return; } if (!workspace.GroupElements.Contains(ge1) || !workspace.GroupElements.Contains(ge2) || !workspace.GroupElements.Contains(ge3)) { return; } //Make sure they are both of same level, or we can't group. // TODO: relax this appropriately for ge3 if (ge1.Level != ge2.Level) { return; } if (Math.Abs(ge2.Level - ge3.Level) > 1) { return; } // Add to attention history. workspace.RecordCodeletAttentionHistory(this, ge1.MinLocation, ge3.MaxLocation); // Check if sequence already exists! foreach (Group g in workspace.groups) { if (g is Sequence) { Sequence s = (Sequence)g; if (s.SequenceElements.Contains(ge1) && s.SequenceElements.Contains(ge2) && s.SequenceElements.Contains(ge3)) { return; // already exists. } } } // Estimate the sequence's score. MelodyContour mc1 = new MelodyContour(ge1); MelodyContour mc2 = new MelodyContour(ge2); MelodyContour mc3 = new MelodyContour(ge3); // Need exact matches for first 2 elements. string contourStart = mc1.ToStringSimple(); if (contourStart != mc2.ToStringSimple()) { return; } // Need initial match for elements 2 and 3 (in case 3 is longer). if (!mc3.ToStringSimple().StartsWith(contourStart)) { return; } // Strength based on # elements. double score = Math.Min(100, contourStart.Length * 30); double r = Utilities.rand.NextDouble() * 100; if (r < score) { Sequence s = new Sequence(workspace, ge1, ge2, ge3, score); workspace.AddSequence(s, score); // TODO: hack: forcing score in.... but should be computed from reasons... } }
/// <summary> /// Returns true if this groupelement has the excact same range (in measures) of the given groupelement. /// </summary> /// <param name="parent"></param> /// <returns></returns> public abstract bool MatchesRangeOf(GroupElement target);
public bool LinksTheseTwo(GroupElement ge1, GroupElement ge2) { return((LHS == ge1 && RHS == ge2) || (LHS == ge2 && RHS == ge1)); }
public Group(Workspace workspace, GroupElement ge) : base(workspace) { Init(); AddGroupElement(ge); }
public RelationshipParameter(GroupElement LHS, GroupElement RHS, float strength) : base(LHS, RHS, strength, false) { }
public RelationshipAntecedentConsequentTonality(GroupElement LHS, GroupElement RHS, float strength) : base(LHS, RHS, strength, false) { }
public RelationshipIdentical(GroupElement LHS, GroupElement RHS, float strength) : base(LHS, RHS, strength, true) { }