/// <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 LookForRelationshipCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                                   GroupElement ge1, GroupElement ge2)
     : base("Look for Relationship", urgency, parent, coderack, workspace, slipnet)
 {
     this.ge1 = ge1;
     this.ge2 = ge2;
 }
Example #2
0
 /// <summary>
 /// Remove oldest codelets until the coderack is the maximum size.
 /// </summary>
 public void RemoveOld()
 {
     Log("Number of codelets before killing old: " + codelets.Count);
     lock (CodeletsLock) {
         while (codelets.Count > Constants.MAX_CODERACK_SIZE)
         {
             // Remove the oldest codelet.
             int max    = codelets[0].Age;
             int argmax = 0;
             for (int i = 1; i < codelets.Count; i++)
             {
                 Codelet c = codelets[i];
                 if (c.Age > max)
                 {
                     max    = c.Age;
                     argmax = i;
                 }
             }
             codelets.RemoveAt(argmax);
             _num_codelets_killed++;
         }
     }
     Log("Number of codelets after killing old: " + codelets.Count);
     Log("Max coderack size: " + maxSize.ToString() + " at time: " + totalNumCodeletsRun.ToString());
 }
Example #3
0
        public List <string> GetActiveCodeletNames()
        {
            List <string> names = new List <string>();

            foreach (Assembly asm in assemblies)
            {
                foreach (Type t in asm.GetExportedTypes())
                {
                    foreach (Attribute a in t.GetCustomAttributes(false))
                    {
                        CodeletAttribute ca = a as CodeletAttribute;
                        if (ca != null)
                        {
                            if (ca.Active)
                            {
                                Codelet c = (Codelet)asm.CreateInstance(t.FullName, false, BindingFlags.CreateInstance, null,
                                                                        new object[] { ca.DefaultUrgency, null, this, workspace, slipnet },
                                                                        null, null);
                                names.Add(c.Name);
                            }
                        }
                    }
                }
            }
            return(names);
        }
Example #4
0
 public void AddCodelet(Codelet c, int numCopies)
 {
     for (int i = 0; i < numCopies; i++)
     {
         AddCodelet((Codelet)c.Clone());
     }
 }
 /// <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 LookForMetricPositionRelationshipCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                                                 Group g1, Group g2)
     : base("Look for Mertric Position Relationship", urgency, parent, coderack, workspace, slipnet)
 {
     this.g1 = g1;
     this.g2 = g2;
 }
 /// <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 CreateAnalogyCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                             GroupElement ge1, GroupElement ge2)
     : base("Create Analogy", urgency, parent, coderack, workspace, slipnet)
 {
     this.ge1 = ge1;
     this.ge2 = ge2;
 }
 /// <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;
 }
Example #8
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;
 }
Example #9
0
 /// <summary>
 /// Raise an event to let viewers update their views.
 /// </summary>
 private void SendRunCodeletEvent(Codelet c)
 {
     // Verify we have subsubscribed event handlers.
     if (RunCodeletEvent != null)
     {
         CoderackRunCodeletEventArgs ea = new CoderackRunCodeletEventArgs(c);
         RunCodeletEvent(this, ea);
     }
 }
Example #10
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="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;
 }
Example #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;
 }
Example #12
0
 // Modify the urgency of all codelets of the given type.
 public void ModifyCodeletUrgencies(Codelet c, double urgency)
 {
     lock (CodeletsLock) {
         foreach (Codelet c2 in codelets)
         {
             if (c.GetType() == c2.GetType())
             {
                 c2.Urgency = urgency;
             }
         }
     }
     UpdateView();
 }
Example #13
0
 public void AddCodelet(Codelet c)
 {
     // Set coderack post time.
     c.PostTime = workspace.CurrentTime;
     // Add to coderack.
     lock (CodeletsLock) {
         codelets.Add(c);
     }
     UpdateView();
     if (codelets.Count > maxSize)
     {
         maxSize     = codelets.Count;
         maxSizeTime = totalNumCodeletsRun;
     }
 }
Example #14
0
        public Codelet(string name, double urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet)
        {
            if (urgency < 0 || urgency > 100)
            {
                //	throw new ArgumentOutOfRangeException("Urgency for new codelet invalid");
            }
            this.name       = name;
            this.rawUrgency = urgency;
            this.parent     = parent;
            this.coderack   = coderack;
            this.workspace  = workspace;
            this.slipnet    = slipnet;

            if (parent == null)
            {
                generation = 0;
            }
            else
            {
                generation = parent.generation + 1;
            }
        }
Example #15
0
        public void Populate()
        {
            //Reset();


            if (Constants.SHOW_CODERACK_URGENCIES_CONSOLE)
            {
                Log("Coderack size: " + codelets.Count.ToString());

                // Show % of each type.
                SortedDictionary <string, double> urgencies = new SortedDictionary <string, double>();
                double total = 0;
                foreach (Codelet c in codelets)
                {
                    if (urgencies.ContainsKey(c.Name))
                    {
                        urgencies[c.Name] += c.Urgency;
                    }
                    else
                    {
                        urgencies[c.Name] = c.Urgency;
                    }
                    total += c.Urgency;
                }

                Log("\nCoderack state:");


                foreach (KeyValuePair <string, double> p in urgencies)
                {
                    Console.WriteLine("{0:0.0}%:\t{1}", 100.0 * p.Value / total, p.Key);
                }
            }



            ///


            // Initialize Coderack with the codelets we find via reflection.
            // TODO: Do this at compile-time, with PostSharp
            int numAdded = 0;

            foreach (Assembly asm in assemblies)
            {
                foreach (Type t in asm.GetExportedTypes())
                {
                    foreach (Attribute a in t.GetCustomAttributes(false))
                    {
                        CodeletAttribute ca = a as CodeletAttribute;
                        if (ca != null)
                        {
                            if (ca.Active)
                            {
                                // Add 1 copy of each codelet.
                                for (int i = 0; i < 1; i++)
                                {
                                    lock (CodeletsLock) {
                                        Codelet c = (Codelet)asm.CreateInstance(t.FullName, false, BindingFlags.CreateInstance, null,
                                                                                new object[] { ca.DefaultUrgency, null, this, workspace, slipnet },
                                                                                null, null);
                                        c.PostTime = workspace.CurrentTime;
                                        codelets.Add(c);
                                        numAdded++;
                                    }
                                }
                            }
                            else
                            {
                                //throw new Exception("Deprecated: Codelet disabled in attribute: " + t.FullName);
                            }
                        }
                    }
                }
            }
            Log("Number of codelets added in populate: " + numAdded.ToString());

            UpdateView();
        }
Example #16
0
 public CoderackRunCodeletEventArgs(Codelet codelet)
 {
     this.codelet = codelet;
 }
 /// <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 GroupPenaltySubcomponentLengthCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                                              Group group)
     : base("Group Penalty Subcomponent Length", urgency, parent, coderack, workspace, slipnet)
 {
     this.group = group;
 }
Example #18
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 FindSequenceCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, GroupElement ge1)
     : base("Find Sequence", urgency, parent, coderack, workspace, slipnet)
 {
     this.ge1 = ge1;
 }
 public GroupReasonComponentsSimilarCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet)
     : base("Group Reason Similar Components", urgency, parent, coderack, workspace, slipnet)
 {
 }
 public LookForRelationshipCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet)
     : base("Look for Relationship", urgency, parent, coderack, workspace, slipnet)
 {
 }
Example #21
0
 public MetaGrouperCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet)
     : base("Meta Grouper", urgency, parent, coderack, workspace, slipnet)
 {
 }
 /// <summary>
 /// Use this constructer to tell the codelet which analogy 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 SuggestAnalogyFromExpectationCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, ExpectedAnalogy eanalogy)
     : base("Suggest Analogy From Expectation", urgency, parent, coderack, workspace, slipnet)
 {
     this.eanalogy = eanalogy;
 }
Example #23
0
 /// <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 GroupBreakerCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, Group g)
     : base("Group Breaker", urgency, parent, coderack, workspace, slipnet)
 {
     this.group = g;
 }
Example #24
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 MakeTranspositionGroupCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet, RelationshipTransposition rt)
     : base("Transposition Grouper", urgency, parent, coderack, workspace, slipnet)
 {
     this.rt = rt;
 }
Example #25
0
        /// <summary>
        /// Picks a codelet based on urgency values, and runs it.
        /// Codelet is removed from rack when it starts running.
        /// TODO: could implement more efficiently if we persist the cumulative distribution
        /// values and update when codelets are removed.
        /// TODO: run in parallel instead of serial.
        /// </summary>
        public void RunNextCodelet()
        {
            // Remove any disabled codelet types.
            for (int i = 0; i < codelets.Count; i++)                // codelets.Count must not be optimized away, or this deletion breaks.
            {
                string name = codelets[i].Name;
                if (disabled_codelet_names.Contains(name))
                {
                    lock (CodeletsLock) {
                        codelets.RemoveAt(i);
                    }
                    i--;
                }
            }

            double[] dist = new double[codelets.Count];             // stores the cumulative urgency distribution

            if (codelets.Count == 0)
            {
                Console.WriteLine("Out of Codelets!!!");
                return;
            }

            // Build cumulative prob. distribution.
            dist[0] = codelets[0].Urgency;
            for (int i = 1; i < codelets.Count; i++)
            {
                dist[i] = dist[i - 1] + codelets[i].Urgency;
            }

            double max = dist[codelets.Count - 1];

            // Choose a random-weighted codelet.
            double r = Utilities.rand.NextDouble() * max;               // generates a num from 0 to max

            for (int i = 0; i < codelets.Count; i++)
            {
                if (r < dist[i] + 0.00000001)
                {
                    // Remove the winner from the to-run list.
                    Codelet c = codelets[i];
                    lock (CodeletsLock) {
                        codelets.RemoveAt(i);
                    }
                    UpdateView();

                    // Start timer.
                    DateTime startTime = DateTime.Now;

                    // Run this codelet.
                    SendRunCodeletEvent(c);
                    //Log("Running codelet " + c.ToString() + ". Age = " + c.Age + "Generation = " + c.Generation);

#if DEBUG
                    string groupsBefore = workspace.GetGroupsString();
#endif

                    c.Run();

                    TimeSpan duration = DateTime.Now - startTime;
                    double   durMs    = duration.TotalMilliseconds;
                    totalCodeletRunTimeMs         += durMs;
                    totalCodeletRunTimeMsWindowed += durMs;
                    totalNumCodeletsRun++;
                    totalNumCodeletsRunWindowed++;
                    double avgTimeWindowed = totalCodeletRunTimeMsWindowed / totalNumCodeletsRunWindowed;
                    if (totalNumCodeletsRunWindowed == 40)                              // rewindow every 30 codelets, keeping time of previous 10 to smooth out the average.
                    {
                        totalCodeletRunTimeMsWindowed = 10 * avgTimeWindowed;
                        totalNumCodeletsRunWindowed   = 10;
                        if (Constants.SHOW_CODELET_RUNTIME_CONSOLE)
                        {
                            Console.WriteLine("Avg. codelet running time in ms in prev 30 codelets: {0}", avgTimeWindowed);
                        }
                    }

                    // Add to the completed list.
                    codelets_finished.Add(c);
                    _sum_codelet_age_at_run += c.Age;

                    UpdateView();

                    slipnet.UpdateView();
                    //workspace.UpdateView();

#if DEBUG
                    // Run a workspace consistency check.
                    if (!workspace.verifyConsistent())
                    {
                        Utilities.LogtoFile("\nFailed Consistency Check after codelet:");
                        Utilities.LogtoFile(c.ToString());
                        Utilities.LogtoFile("Groups Before:");
                        Utilities.LogtoFile(groupsBefore);
                        Utilities.LogtoFile("Groups After:");
                        Utilities.LogtoFile(workspace.GetGroupsString());

                        System.Diagnostics.Debugger.Break();
                    }
#endif
                    break;
                }
            }
        }
 /// <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;
 }
Example #27
0
 public MakeTranspositionGroupCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet)
     : base("Transposition Grouper", urgency, parent, coderack, workspace, slipnet)
 {
 }
 /// <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 GroupReasonNumberComponentsCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet,
                                           Group group)
     : base("Group Reason Number Components", urgency, parent, coderack, workspace, slipnet)
 {
     this.group = group;
 }
Example #29
0
 public FindSequenceCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet)
     : base("Find Sequence", urgency, parent, coderack, workspace, slipnet)
 {
 }
 public AnalogyFinderCodelet(int urgency, Codelet parent, Coderack coderack, Workspace workspace, Slipnet slipnet)
     : base("Analogy Scout", urgency, parent, coderack, workspace, slipnet)
 {
 }