예제 #1
0
        public override void Initialize()
        {
            pCount = 0;
            sCount = 0;
            cCount = 0;

            GetSegmentsFromUKS();

            ModuleUKSN UKSn = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKSn == null)
            {
                return;
            }
            while (UKSSegments.Count > 0)
            {
                UKSn.DeleteThing(UKSSegments[0]);
            }

            while (UKSPoints.Count > 0)
            {
                UKSn.DeleteThing(UKSPoints[0]);
            }

            na.GetNeuronAt(0, 0).Label = "New";
            na.GetNeuronAt(1, 0).Label = "Change";
            AddLabel("Obstacle");
            UpdateDialog();
        }
예제 #2
0
        public Thing CreateLandmark(List <Thing> near)
        {
            //a landmark is a collection of nearby segments
            //these must be cloned so they can be fixed in position rather than being adjusted relative to Sallie's position
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS is null)
            {
                return(null);
            }
            Thing newLandmark = UKS.AddThing("Lm" + landmarkCount++.ToString(), "Landmark");

            foreach (Thing t in near)
            {
                Segment s  = Module2DModel.SegmentFromUKSThing(t);
                Thing   P1 = UKS.AddThing("lmp" + pointCount++.ToString(), "Point"); //These points are not included in the model and do don't move with poin of view
                P1.V = s.P1.Clone();
                Thing P2 = UKS.AddThing("lmp" + pointCount++.ToString(), "Point");
                P2.V = s.P2.Clone();
                Thing S = UKS.AddThing("l" + t.Label.ToString(), "SSegment");
                S.AddReference(P1);
                S.AddReference(P2);
                S.AddReference(t.References[2].T);//the color
                newLandmark.AddReference(S);
            }

            return(newLandmark);
        }
예제 #3
0
        Thing MostLikelyEvent(Thing currentEvent)
        {
            Thing      retVal = null;
            ModuleUKSN UKS    = (ModuleUKSN)FindModleu(typeof(ModuleUKSN));

            if (UKS == null)
            {
                return(retVal);
            }

            Segment       s1     = Module2DModel.SegmentFromUKSThing(currentEvent.References[0].T);
            IList <Thing> events = UKS.Labeled("Event").Children;

            foreach (Thing t in events)
            {
                if (t.References.Count > 0)
                {
                    Thing lm = t.References[0].T;
                    if (lm.References.Count > 0)
                    {
                        Thing   lms = lm.References[0].T;
                        Segment s2  = Module2DModel.SegmentFromUKSThing(lms);

                        if (s1.MidPoint.Near(s2.MidPoint, 0.2f))
                        {
                            retVal = t;
                        }
                    }
                }
            }
            return(retVal);
        }
예제 #4
0
        //this is asking can the entity directly see a target object or are there obstacles
        public PointPlus CanISGoStraightTo(PointPlus midPoint, out Segment obstacle)
        {
            obstacle = null;
            float      closestDistance = 100;
            ModuleUKSN UKS             = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS == null)
            {
                return(null);
            }
            bool ok = true;

            foreach (Thing t in UKSSegments)
            {
                Segment s = SegmentFromUKSThing(t);
                Utils.FindIntersection(new Point(0, 0), midPoint.P, s.P1.P, s.P2.P,
                                       out bool lines_intersect, out bool segments_intersect,
                                       out Point intersection, out Point close_p1, out Point closep2, out double collisionAngle);
                if (segments_intersect)
                {
                    ok = false;
                    Utils.FindDistanceToSegment(new Point(0, 0), s.P1.P, s.P1.P, out Point closest);
                    if (((Vector)closest).Length < closestDistance)
                    {
                        closestDistance = (float)((Vector)closest).Length;
                        obstacle        = s;
                    }
                }
            }
            if (ok)
            {
                return(midPoint);
            }
            return(null);
        }
예제 #5
0
        public override void Initialize()
        {
            //scanning = false;
            orienting = false;
            auto      = false;

            lastLandmark    = null;
            currentLandmark = null;
            currentEvent    = null;
            ClearNeurons();

            //randomize
            rand = new Random((int)DateTime.Now.Ticks);

            AddLabel("Auto");
            AddLabel("Found");

            //this is the first test of building a sequence of behaviors
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS != null)
            {
                Thing t = UKS.AddThing("LTurnS", "Action");
                t = UKS.AddThing("RTurnS", "Action");
                t = UKS.AddThing("UTurnS", "Action");
                t = UKS.AddThing("GoS", "Action");
            }
        }
예제 #6
0
        //maintain a list of objects in the current visual field
        public void FireVisibleObjects()
        {
            ModuleView naUKS = theNeuronArray.FindAreaByLabel("Module2DUKS");

            if (naUKS == null)
            {
                return;
            }
            ModuleUKSN UKS = (ModuleUKSN)naUKS.TheModule;

            if (UKSSegments == null)
            {
                return;
            }
            //Thing tVisible = UKS.Labeled("Visible");

            //clear all visiblility references
            //for (int i = 0; i < UKSSegments.Count; i++)
            //    UKSSegments[i].RemoveReference(tVisible);

            ModuleView naVision = theNeuronArray.FindAreaByLabel("Module2DVision");

            if (naVision == null)
            {
                return;
            }
            int   possibleViewAngles = naVision.Width;
            float deltaTheta         = Module2DVision.fieldOfView / possibleViewAngles;

            for (int i = 0; i < possibleViewAngles; i++)
            {
                float     theta = (float)-Module2DVision.fieldOfView / 2 + (i * deltaTheta);
                PointPlus P     = new PointPlus {
                    R = 10, Theta = theta
                };
                foreach (Thing t in UKSSegments)
                {
                    Segment s = SegmentFromUKSThing(t);
                    if (s == null)
                    {
                        continue;
                    }
                    Utils.FindIntersection(new Point(0, 0), P.P, s.P1.P, s.P2.P,
                                           out bool lines_intersect, out bool segments_intersect,
                                           out Point intersection, out Point close_p1, out Point closep2, out double collisionAngle);
                    if (segments_intersect)
                    {
                        //TODO...only fire the closest at each point
                        UKS.Fire(t);
                        //                        t.AddReference(tVisible);
                    }
                }
            }
        }
예제 #7
0
        //this returns a list of things directly connected to the given target
        private List <Thing> FindGoal(Thing target)
        {
            if (target == null)
            {
                return(null);
            }
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            return(UKS.Labeled("Event").Children.FindAll(
                       t => t.Children.Find(u => u.References[1].T == target) != null));
        }
예제 #8
0
        public override void Initialize()
        {
            ModuleView naKB = theNeuronArray.FindAreaByLabel("ModuleUKS");

            if (naKB != null)
            {
                if (naKB.TheModule != null)
                {
                    KB = (ModuleUKSN)naKB.TheModule;
                }
            }
        }
 private void AddChildren(Thing t, TreeViewItem tvi, int depth)
 {
     foreach (Thing child in t.Children)
     {
         string header = child.Label + ":" + child.useCount;
         if (child.References.Count > 0)
         {
             header += " (";
             ModuleUKSN parent = (ModuleUKSN)base.ParentModule;
             Thing      best   = parent.FindBestReference(child);
             foreach (Link L in child.References)
             {
                 if (L.T == best)
                 {
                     header += "*";
                 }
                 if (L.weight < 0)
                 {
                     header += "-";
                 }
                 header += L.T.Label + ", ";
             }
             header  = header.Substring(0, header.Length - 2);
             header += ")";
         }
         if (child.V != null)
         {
             if (child.V is int i)
             {
                 header += " : " + i.ToString("X");
             }
             else
             {
                 header += " : " + child.V.ToString();
             }
         }
         TreeViewItem tviChild = new TreeViewItem {
             Header = header
         };
         if (expandedItems.Contains(LeftOfColon(header)))
         {
             tviChild.IsExpanded = true;
         }
         tvi.Items.Add(tviChild);
         tviChild.MouseRightButtonDown += Tvi_MouseRightButtonDown;
         if (depth < maxDepth)
         {
             AddChildren(child, tviChild, depth + 1);
             AddReferences(child, tviChild);
             AddReferencedBy(child, tviChild);
         }
     }
 }
예제 #10
0
        private static void FindBestLandmarkMatch(ModuleUKSN UKS, ref Thing best, ref float bestDist, List <Thing> near)
        {
            //searching each spatial Event
            best     = null;
            bestDist = 1000;
            if (UKS == null)
            {
                return;
            }
            if (UKS.Labeled("Landmark") == null)
            {
                return;
            }

            //landmark segments are sorted, closest segment first
            //closer segments are more important
            List <Thing> landmarks = UKS.Labeled("Landmark").Children;

            foreach (Thing landmark in landmarks)
            {
                float totalDist = 0;
                //each reference in that landmark
                int foundCount = 0;  //must match several items to count as a hit
                int linkNumber = 0;
                foreach (Link L1 in landmark.References)
                {
                    Segment s1         = Module2DModel.SegmentFromUKSThing(L1.T);
                    float   d1         = (float)Utils.FindDistanceToSegment(s1);
                    int     nearNumber = 0;
                    foreach (Thing t1 in near)
                    {
                        //does it match one of the segments presently near me?
                        Segment s2 = Module2DModel.SegmentFromUKSThing(t1);
                        float   d2 = (float)Utils.FindDistanceToSegment(s2);
                        if (s1.theColor == s2.theColor)
                        {
                            foundCount++;
                            float dist = (float)Abs(d1 - d2);
                            dist      *= Abs(nearNumber - linkNumber) + 1;
                            totalDist += dist;
                        }
                        nearNumber++;
                    }
                    linkNumber++;
                }
                if (totalDist < bestDist && foundCount == near.Count)
                {
                    best     = landmark;
                    bestDist = totalDist;
                }
            }
        }
예제 #11
0
        public void AddOutcomePair(Thing Event, Thing theAction, Thing theOutcome)
        {
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS is null)
            {
                return;
            }
            Thing newOutcomePair = UKS.AddThing("x" + pairCount++, Event); //creates new thing as it as a child

            newOutcomePair.AddReference(theAction);
            newOutcomePair.AddReference(theOutcome);
        }
예제 #12
0
        public Thing CreateEvent(Thing newLandmark)
        {
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS is null)
            {
                return(null);
            }
            Thing retVal = UKS.AddThing("E" + eventCount++.ToString(), "Event");

            retVal.AddReference(newLandmark);
            return(retVal);
        }
예제 #13
0
        public void GetSegmentsFromUKS()
        {
            ModuleUKSN nmUKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (nmUKS is ModuleUKS UKS)
            {
                UKSSegments = UKS.Labeled("Segment").Children;
                UKSPoints   = new List <Thing>();
                if (UKS.Labeled("ModelThing") != null)
                {
                    UKSPoints = UKS.Labeled("ModelThing").Children;
                }
            }
        }
예제 #14
0
        private void CheckForTrainingResult()
        {
            if (actionTrainingState == -1)
            {
                return;
            }
            ModuleUKSN nmUKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (nmUKS == null)
            {
                return;
            }
            actionTrainingState--;
            if (actionTrainingState <= 4 || !na.GetNeuronAt("Train").Fired())
            {
                return;
            }
            List <Thing> actions    = nmUKS.GetChildren(nmUKS.Labeled("Action"));
            bool         bResponded = false;

            foreach (Thing action in actions)
            {
                if (nmUKS.Fired(action, 2, false))
                {
                    if (action.Label == trainingCase[1])
                    {
                        SimulatePhrase("good");
                        bResponded = true;
                        break;
                    }
                    else
                    {
                        SimulatePhrase("no");
                        bResponded = true;
                        break;
                    }
                }
            }
            if (bResponded)
            {
                actionTrainingState = 5;
                return;
            }
            ////we "timed out" with no response to the stimulus
            //if (actionTrainingDelay == 4)
            //{
            //    SimulatePhrase("no");
            //}
        }
예제 #15
0
        public override void Fire()
        {
            Init();  //be sure to leave this here
            if (words.Count > 0)
            {
                string word = words[0];
                na.BeginEnum();
                for (Neuron n = na.GetNextNeuron(); n != null; n = na.GetNextNeuron())
                {
                    if (n.Label == word)
                    {
                        n.SetValue(1);
//                        n.CurrentCharge = 1;
                        break;
                    }
                    else if (n.Label == "")
                    {
                        //the word has not been heard before, add it
                        n.Label         = word;
                        n.CurrentCharge = 1;
                        //connection to UKS
                        ModuleUKSN nmUKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));
                        if (nmUKS != null)
                        {
                            List <Thing> words = nmUKS.Labeled("Word").Children;
                            Thing        w     = nmUKS.Valued(word, words);
                            if (w == null)
                            {
                                string label = "w" + char.ToUpper(word[0]) + word.Substring(1);
                                w = nmUKS.AddThing(label, new Thing[] { nmUKS.Labeled("Word") }, word);
                            }
                            Neuron n1 = nmUKS.GetNeuron(w);
                            if (n1 != null)
                            {
                                n.AddSynapse(n1.Id, 1);
                            }
                            //TODO: add a synapse to the speakwords module as well
                        }
                        break;
                    }
                }
                words.RemoveAt(0);
            }
        }
예제 #16
0
        void SetCenterColor()
        {
            int        c1    = GetNeuronValueInt(null, na.Width / 2, 0);
            int        c2    = GetNeuronValueInt(null, na.Width / 2, 1);
            ModuleUKSN nmUKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (nmUKS != null && nmUKS.Labeled("Color") != null)
            {
                List <Thing> colors = nmUKS.Labeled("Color").Children;
                //if (c1 != 0)
                {
                    nmUKS.Fire(nmUKS.Valued(c1, colors));
                }
                //if (c2 != 0)
                {
                    nmUKS.Fire(nmUKS.Valued(c2, colors));
                }
            }
        }
예제 #17
0
        public override void Fire()
        {
            Init();  //be sure to leave this here
            if (synth == null)
            {
                return;
            }

            if (!na.GetNeuronAt(0).Fired())
            {
                return;
            }

            ModuleUKSN nmKB = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (nmKB == null)
            {
                return;
            }
            List <Thing> words  = nmKB.GetChildren(nmKB.Labeled("Word"));
            bool         paused = true;

            //TODO: replace this direct access into the KB with synapses...then we can eliminate the storage of the words in the things values.
            foreach (Thing word in words)
            {
                if (nmKB.Fired(word, 2, false))
                {
                    paused         = false;
                    phraseToSpeak += " " + word.V.ToString();
                }
            }
            if (paused && phraseToSpeak != "")
            {
                ModuleSpeechIn msi = (ModuleSpeechIn)FindModuleByType(typeof(ModuleSpeechIn));
                if (msi != null)
                {
                    msi.PauseRecognition(); //if there is a recognizer active
                }
                synth.SpeakAsync(phraseToSpeak + ".");
                phraseToSpeak = "";
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string     root   = textBox1.Text;
            ModuleUKSN parent = (ModuleUKSN)base.ParentModule;

            expandedItems.Clear();
            FindExpandedItems(theTreeView.Items, root);
            theTreeView.Items.Clear();
            List <Thing> KB = parent.GetTheKB();
            Thing        t  = parent.Labeled(root);

            if (t != null)
            {
                TreeViewItem tvi = new TreeViewItem {
                    Header = t.Label
                };
                tvi.IsExpanded = true; //always expand the top-level item
                theTreeView.Items.Add(tvi);
                tvi.MouseRightButtonDown += Tvi_MouseRightButtonDown;
                AddChildren(t, tvi, 0);
            }
        }
예제 #19
0
        //This returns the action needed at the currentEvent in order to move toward the goal
        public Thing GoToGoal(Thing goal)
        {
            if (goal == null)
            {
                return(null);
            }
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            //trivial case, you can go straight to goal
            Thing pair = currentEvent.Children.Find(t => t.References[1].T == goal);

            if (pair != null)
            {
                return(pair.References[0].T);
            }

            List <Thing> placesToTry = FindGoal(goal);

            //extend the list of connections to existing nodes until we reach our current position
            for (int i = 0; i < placesToTry.Count; i++)
            {
                List <Thing> newPlacesToTry = FindGoal(placesToTry[i]);
                if (newPlacesToTry.Contains(currentEvent))
                {
                    Thing target = placesToTry[i]; //target is the intermediate target
                    pair = currentEvent.Children.Find(t => t.References[1].T == target);
                    return(pair.References[0].T);  //return the action of the outcome pair
                }
                foreach (Thing t in newPlacesToTry)
                {
                    if (!placesToTry.Contains(t))
                    {
                        placesToTry.Add(t);
                    }
                }
            }
            return(null);
        }
예제 #20
0
        private void CheckNeurons(ModuleUKSN UKS)
        {
            //check for operating mode
            auto = (GetNeuronValue(null, "Auto") == 1) ? true : false;

            //check for a goal selection
            foreach (Neuron n in mv.Neurons)
            {
                if (n.Fired() && n.Label.IndexOf("c") == 0 && n.Model == Neuron.modelType.IF)
                {
                    Thing newTarget = UKS.Labeled(n.Label);
                    if (newTarget == currentTarget)
                    {
                    }  // currentTarget = null;
                    else
                    {
                        currentTarget = newTarget;
                    }
                    currentTargetReached = null;
                    break;
                }
            }
        }
예제 #21
0
        public override void Fire()
        {
            Init();  //be sure to leave this here
            if (synth == null)
            {
                return;
            }

            bool paused = true;

            for (int i = 1; i < na.NeuronCount; i++)
            {
                Neuron n = na.GetNeuronAt(i);
                if (n.Fired())
                {
                    if (n.Label.Length == 1)
                    {
                        phraseToSpeak += n.Label;
                        paused         = false;
                    }
                    if (n.Synapses.Count == 0)
                    {
                        //if a neuron fired and it has no connection, connect it to the knowledge store
                        //connection to KB
                        ModuleUKSN nmKB = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));
                        if (nmKB != null)
                        {
                            string       label    = "pn" + n.Label;
                            List <Thing> phonemes = nmKB.Labeled("Phoneme").Children;
                            Thing        pn       = nmKB.Labeled(label, phonemes);
                            if (pn == null) //this should always be null
                            {
                                pn = nmKB.AddThing(label, new Thing[] { nmKB.Labeled("Phoneme") }, pn);
                            }
                            Neuron n1 = nmKB.GetNeuron(pn);
                            if (n1 != null)
                            {
                                n.AddSynapse(n1.Id, 1);
                                n1.SetValue(1);
                            }
                        }
                    }
                }
            }
            if (phonemesToFire != "")
            {
                char c     = phonemesToFire[0];
                bool fired = false;
                for (int i = 0; i < na.NeuronCount; i++)
                {
                    Neuron n = na.GetNeuronAt(i);
                    if (n.Label == c.ToString())
                    {
                        n.SetValue(1);
                        fired = true;
                        break;
                    }
                }
                if (!fired)
                {
                    Utils.Noop();
                }
                phonemesToFire = phonemesToFire.Substring(1);
            }

            if (paused && phraseToSpeak != "")
            {
                if (dlg != null)
                {
                    ((ModuleSpeakPhonemesDlg)dlg).SetLabel(phraseToSpeak);
                }

                if (na.GetNeuronAt("Enable").Fired())
                {
                    ModuleSpeechIn msi = (ModuleSpeechIn)FindModuleByType(typeof(ModuleSpeechIn));
                    if (msi != null)
                    {
                        msi.PauseRecognition(); //if there is a recognizer active
                    }
                    //synth.SpeakAsync(phraseToSpeak + ".");
                    //phraseToSpeak = "";

                    PromptBuilder pb1 = new PromptBuilder();
                    if (typedIn)
                    {
                        pb1.StartVoice("Microsoft David Desktop");
                        pb1.StartStyle(new PromptStyle(PromptRate.Medium));
                    }
                    else
                    {
                        pb1.StartVoice("Microsoft Zira Desktop");
                        pb1.StartStyle(new PromptStyle(PromptRate.ExtraSlow));
                    }

                    pb1.AppendTextWithPronunciation("not used", phraseToSpeak);
                    pb1.EndStyle();
                    pb1.EndVoice();
                    string x = pb1.ToXml();
                    Debug.WriteLine(debugString(phraseToSpeak));
                    //synth.Speak(pb1);
                    synth.SpeakAsync(pb1);
                }
                //string heard = GetPronunciationFromText("", phraseToSpeak); //it would be nice to hear what was said but it doesn't work with this engine
                phraseToSpeak = "";
                typedIn       = false;
            }
        }
예제 #22
0
        public override void Fire()
        {
            Init();  //be sure to leave this here

            //get the external references
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS == null)
            {
                return;
            }
            Module2DModel nmModel = (Module2DModel)FindModuleByType(typeof(Module2DModel));

            if (nmModel == null)
            {
                return;
            }
            ModuleBehavior nmBehavior = (ModuleBehavior)FindModuleByType(typeof(ModuleBehavior));

            if (nmBehavior == null)
            {
                return;
            }
            ModuleEvent mEvent = (ModuleEvent)FindModuleByType(typeof(ModuleEvent));

            if (mEvent == null)
            {
                return;
            }


            //check on the various input neurons...
            //check for a goal selection
            foreach (Neuron n in na.Neurons())
            {
                if (n.Fired() && n.Label.IndexOf("c") == 0 && n.Model == Neuron.modelType.Std)
                {
                    Thing newTarget = UKS.Labeled(n.Label);
                    if (newTarget == currentTarget)
                    {
                        currentTarget = null;
                    }
                    else
                    {
                        currentTarget = newTarget;
                    }
                    currentTargetReached = null;
                    break;
                }
            }
            //check for operating mode
            auto = (GetNeuronValue(null, "Auto") == 1) ? true : false;

            //don't do anything while a behavior is in progress
            if (GetNeuronValue("ModuleBehavior", "Done") == 0)
            {
                return;
            }

            //is there an existing landmark? Do I recognize I'm near a spot I was before?
            //this only calculates "R" so it is rotation-independent
            Thing best     = null;
            float bestDist = 1000;

            List <Thing> near = nmModel.NearbySegments(3);

            FindBestLandmarkMatch(UKS, ref best, ref bestDist, near);

            nmModel.FireVisibleObjects(); //not really needed

            if (bestDist < .2f)           //are we near a landmark we've been at before?
            {
                SetNeuronValue(null, "Found", 1);

                //yse, we have returned to a landmark we've been at before
                currentLandmark = best;
                currentEvent    = currentLandmark.ReferencedBy[0].T;

                //we need to reorient ourselves to face the same way as we did before (set a flag)
                if (lastLandmark != currentLandmark)
                {
                    lastLandmark = currentLandmark;
                    orienting    = true;
                }
            }
            else
            {
                //we're not near an existing landmark
                currentLandmark = null;
                currentEvent    = null;
                lastLandmark    = null;
            }

            //this is on arrival back at a landmark
            if (orienting)
            {
                Angle totalAngleError = GetTotalAngleError(near);
                if (Abs(totalAngleError) > PI / 2 - .1f)
                {
                    //turn in large increments until the angular error gets small
                    Angle angle = (float)PI / 2;
                    nmBehavior.TurnTo(angle);
                    return;
                }
                //this corrects for roundoff errors
                CorrectModelPosition(nmModel, nmBehavior, best, near);
                orienting = false;
                return;
            }

            //we are in going-to-goal mode
            if (currentTarget != null)    //goingToGoal)
            {
                if (currentEvent != null) //currentEvent means we're at a decision point...check for the decision and execute it
                {
                    Thing action = GoToGoal(currentTarget);
                    if (action != null)
                    {
                        float angle = GetAngleFromAction(action);
                        if (angle != 0)
                        {
                            nmBehavior.TurnTo(angle);
                        }
                        nmBehavior.MoveTo(1);
                        currentEvent = null;
                        return;
                    }
                }
            }

            //we are in exploration mode
            //We're not at a known landmark
            //decide which way to turn at an obstacle
            //If I am up to an obstacle...is there a decision or can I only go one way
            float distAhead  = nmModel.GetDistanceAtDirection(0);
            float distLeft   = nmModel.GetDistanceAtDirection((float)PI / 2);
            float distRight  = nmModel.GetDistanceAtDirection((float)-PI / 2);
            bool  canGoAhead = distAhead > 1;
            bool  canGoLeft  = distLeft > 1;
            bool  canGoRight = distRight > 1;
            int   options    = (canGoAhead ? 1 : 0) + (canGoRight ? 1 : 0) + (canGoLeft ? 1 : 0);

            //First determine if there is a decision to be made or if there is only one option
            if (options == 1 && auto)
            {
                //we have no choice but to follow the path
                if (canGoAhead)
                {
                    nmBehavior.MoveTo(1);
                    return;
                }
                if (canGoLeft)
                {
                    nmBehavior.TurnTo((float)-PI / 2);
                    nmBehavior.MoveTo(1);
                    return;
                }
                if (canGoRight)
                {
                    nmBehavior.TurnTo((float)PI / 2);
                    nmBehavior.MoveTo(1);
                    return;
                }
            }
            else if (options == 0 && auto)
            {
                //we're trapped...note the color ahead and turn around
                Thing thingAhead = nmModel.GetNearestThing();
                currentTargetReached = thingAhead.References[2].T;
                if (mostRecentDecisionPoint != null)
                {
                    mEvent.AddOutcomePair(mostRecentDecisionPoint, mostRecentAction, currentTargetReached);
                    mostRecentAction        = null;
                    mostRecentDecisionPoint = null;
                }

                Neuron n1 = null;
                if (currentTargetReached != null)
                {
                    n1 = na.GetNeuronAt(currentTargetReached.Label);
                }

                //color a new goal neuron
                if (n1 == null && currentTargetReached != null)
                {
                    n1       = AddLabel(currentTargetReached.Label + " ");
                    n1.Model = Neuron.modelType.Color;
                    n1.SetValueInt((int)currentTargetReached.V);
                    na.GetNeuronLocation(n1, out int X, out int Y);
                    Neuron n2 = na.GetNeuronAt(X + 1, Y);
                    if (n2 != null)
                    {
                        n2.Label = currentTargetReached.Label;
                    }
                }

                if (currentTargetReached == currentTarget)
                {
                    //if we're looking for a goal, we've reached it, so stop
                    //currentTarget = null;
                    currentEvent = null;
                }
                else
                {
                    //make a U-Turn and start backtracking
                    nmBehavior.TurnTo((float)-PI);
                }
            }
            else if (auto)
            {
                //we have a choice...
                //if the current landmark is null, create a new landmark & Event
                if (currentLandmark == null)
                {
                    //Create new Landmark...it clones the points so they are not modified by the model module
                    Thing newLandmark = mEvent.CreateLandmark(near);
                    currentEvent    = mEvent.CreateEvent(newLandmark);
                    currentLandmark = newLandmark;
                    if (mostRecentDecisionPoint != null)
                    {
                        mEvent.AddOutcomePair(mostRecentDecisionPoint, mostRecentAction, currentEvent);
                    }
                }
                else
                {
                    if (mostRecentDecisionPoint != null && mostRecentDecisionPoint.Children.Find(t1 => t1.References[0].T == mostRecentAction) == null)
                    {
                        mEvent.AddOutcomePair(mostRecentDecisionPoint, mostRecentAction, currentEvent);
                    }
                }

                //TODO improve the method of finding something not tried before
                //Decide which untried path to take
                //priorities...1)continue ahead 2)left 3)right or randomized (depending on comment below)
                Thing        newAction       = UKS.Labeled("NoAction");
                List <Thing> possibleActions = new List <Thing>();
                if (currentEvent.Children.Find(t => t.References[0].T.Label == "GoS") == null && canGoAhead)
                {
                    possibleActions.Add(UKS.Labeled("GoS"));
                }
                if (currentEvent.Children.Find(t => t.References[0].T.Label == "LTurnS") == null && canGoLeft)
                {
                    possibleActions.Add(UKS.Labeled("LTurnS"));
                }
                if (currentEvent.Children.Find(t => t.References[0].T.Label == "RTurnS") == null && canGoRight)
                {
                    possibleActions.Add(UKS.Labeled("RTurnS"));
                }
                if (possibleActions.Count == 0 && currentEvent.Children.Find(t => t.References[0].T.Label == "UTurnS") == null)
                {
                    newAction = UKS.Labeled("UTurnS");
                }
                else if (possibleActions.Count == 1)
                {
                    newAction = possibleActions[0];
                }
                else if (possibleActions.Count > 0)
                {
                    //for debugging, eliminate the ransomization by alternately commenting the 2 stmts below
                    //    newAction = possibleActions[0];
                    newAction = possibleActions[rand.Next(possibleActions.Count)];
                }

                if (newAction.Label != "NoAction")
                {
                    mostRecentAction        = newAction;
                    mostRecentDecisionPoint = currentEvent;

                    Angle angle = GetAngleFromAction(newAction);
                    nmBehavior.TurnTo(angle);
                    nmBehavior.MoveTo(1);
                }
                else
                {
                    //TODO: all actions at the current Event have been tried, is there another Event which hasn't been exhausted?
                }
                lastLandmark = currentLandmark;
                return;
            }
        }
예제 #23
0
        public override void Fire()
        {
            Init();  //be sure to leave this here

            if (GetNeuronValue("Auto") == 0)
            {
                return;
            }
            goToGoal = GetNeuronValue("Goal") == 1;

            ModuleBehavior mBehavior = (ModuleBehavior)FindModleu(typeof(ModuleBehavior));

            if (mBehavior == null)
            {
                return;
            }
            Module2DModel mModel = (Module2DModel)FindModleu(typeof(Module2DModel));

            if (mModel is null)
            {
                return;
            }
            ModuleEvent mEvent = (ModuleEvent)FindModleu(typeof(ModuleEvent));

            if (mEvent == null)
            {
                return;
            }
            ModuleUKSN UKS = (ModuleUKSN)FindModleu(typeof(ModuleUKSN));

            if (UKS == null)
            {
                return;
            }

            if (GetNeuronValue("ModuleBehavior", "Done") == 0)
            {
                return;
            }


            mModel.GetSegmentsFromUKS();
            IList <Thing> segments = mModel.GetUKSSegments();

            if (segments.Count == 0)
            {
                return;
            }
            Thing t = segments[0]; //TODO: this only works with just a single item in the UKS

            //figure out if any motion occured
            Segment s = Module2DModel.SegmentFromUKSThing(t);

            Module2DModel.OrderSegment(s);

            if (GetNeuronValue("E0") == 1)
            {
                GoToLandmark(UKS.Labeled("E0").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }
            if (GetNeuronValue("E1") == 1)
            {
                GoToLandmark(UKS.Labeled("E1").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }
            if (GetNeuronValue("E2") == 1)
            {
                GoToLandmark(UKS.Labeled("E2").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }


            if (doPush != 0)
            {
                if (doPush == 2)
                {
                    Push(s);
                }
                doPush--;
                return;
            }
            if (doFaceSegment)
            {
                DoFaceSegment(s);
                doFaceSegment = false;
                return;
            }

            Segment s1;

            if (lastPosition == null) //create objects to keep track of the target and last position
            {
                s1                 = s.Clone();
                lastPosition       = mModel.AddSegmentToUKS(s1);
                lastPosition.Label = "LastPosition";
                lastPosition.RemoveParent(UKS.Labeled("Segment"));
                lastPosition.AddParent(UKS.Labeled("SSegment"));
                Module2DSim mSim = (Module2DSim)FindModleu(typeof(Module2DSim));
                if (mSim is null)
                {
                    return;
                }
                Segment motionTarget = mSim.GetMotionTarget();
                if (motionTarget == null)
                {
                    motionTarget          = new Segment();
                    motionTarget.P1       = new PointPlus(4, 1.5f);
                    motionTarget.P2       = new PointPlus(4, -2.5f);
                    motionTarget.theColor = (ColorInt)0xff;
                }
                endTarget       = mModel.AddSegmentToUKS(motionTarget);
                endTarget.Label = "EndTarget";
                //endTarget.RemoveParent(UKS.Labeled("Segment"));
                //endTarget.AddParent(UKS.Labeled("SSegment"));
            }
            else
            {
                s1 = Module2DModel.SegmentFromUKSThing(lastPosition);
            }

            //get motion from subtracting and then updating last position
            Angle rotation = s.Angle - s1.Angle;

            if (rotation > PI / 2)
            {
                rotation = PI - rotation;
            }
            if (rotation < -PI / 2)
            {
                rotation = PI + rotation;
            }
            Motion motion = new Motion()
            {
                P        = (Point)s.MidPoint.V - s1.MidPoint.V,
                rotation = rotation,
            };

            lastPosition.References[0].T.V = s.P1.Clone();
            lastPosition.References[1].T.V = s.P2.Clone();

            if (Abs(motion.R) > .01 || Abs(motion.rotation) > .05 && !goToGoal && !doBackOff)
            {
                //check for existing Event
                Thing currentEvent = MostLikelyEvent(t);
                if (currentEvent == null)
                {                //add new Event
                    Thing lm1 = mEvent.CreateLandmark(new List <Thing>()
                    {
                        t
                    });
                    Thing t1 = mEvent.CreateEvent(lm1);
                    Thing t3 = UKS.AddThing("m" + motionCount++, UKS.Labeled("Motion"), motion);
                    mEvent.AddOutcomePair(t1, UKS.GetOrAddThing("Push", "Action"), t3);
                }
                return;
            }

            if (doBackOff)
            {
                DoBackOff(s);
                doBackOff     = false;
                doFaceSegment = true;
                return;
            }

            if (goToGoal)
            {
                if (endTarget != null)
                {
                    Segment s2 = Module2DModel.SegmentFromUKSThing(endTarget);
                    GoToGoal(s, s2);
                    return;
                }
            }

            Explore(s);
        }
예제 #24
0
        void GoToGoal(Segment sCurrent, Segment sTarget)
        {
            //instead of working with endpoints, we'll just work with midpoint and rotation because the length can't change
            Seg pCurrent = new Seg {
                P = sCurrent.MidPoint.P, rotation = sCurrent.Angle
            };
            Seg pTarget = new Seg {
                P = sTarget.MidPoint.P, rotation = sTarget.Angle
            };
            Motion neededMotion = DistanceFromTarget(pTarget, pCurrent);

            //there are two exit cases...here we test for absolute closeness, below we test for the best action makes thing further
            if (Abs(neededMotion.rotation) < .05 && Abs(neededMotion.X) < .1 && Abs(neededMotion.Y) < .1)
            {
                endTarget = null;
            }
            else
            {
                ModuleUKSN UKS       = (ModuleUKSN)FindModleu(typeof(ModuleUKSN));
                Thing      bestEvent = null;
                if (neededMotion.R > .2f)
                {
                    //create a temp distination which is slightly offset
                    ModuleBehavior mBehavior  = (ModuleBehavior)FindModleu(typeof(ModuleBehavior));
                    Seg            tempTarget = new Seg()
                    {
                        P = sTarget.MidPoint.P + new PointPlus {
                            R = .15f, Theta = sTarget.Angle - PI / 2
                        }.V, rotation = 0
                    };
                    neededMotion = DistanceFromTarget(tempTarget, pCurrent);
                    bestEvent    = UKS.Labeled("E0");
                    if (neededMotion.Theta < -.05)
                    {
                        bestEvent = UKS.Labeled("E2");
                    }
                    if (neededMotion.Theta > .05)
                    {
                        bestEvent = UKS.Labeled("E1");
                    }
                }
                else if (neededMotion.R > .01)
                {
                    if (neededMotion.rotation < -.02)
                    {
                        bestEvent = UKS.Labeled("E2");
                    }
                    else
                    {
                        bestEvent = UKS.Labeled("E1");
                    }
                }
                if (bestEvent != null)
                {
                    Motion m            = (Motion)bestEvent.Children[0].References[1].T.V;
                    Seg    nextPosition = NextPosition(pCurrent, (Motion)bestEvent.Children[0].References[1].T.V);
                    Motion next         = DistanceFromTarget(pTarget, nextPosition);
                    //if (next.R < neededMotion.R || Abs(next.rotation) > Utils.Rad(5))
                    {
                        GoToLandmark(bestEvent.References[0].T, sCurrent);
                        doPush    = 2;
                        doBackOff = true;
                    }
                    ////                    else
                    //                    {
                    //                        endTarget = null;
                    //                    }
                }
                //find the event with the most desireable outcome and then go the the landmark.
                //Thing bestEvent = null;
                //List<Thing> events = UKS.Labeled("Event").Children;
                //Seg[] distances = new Seg[events.Count];

                ////first time through loop initialize array and take first action
                //for (int i = 0; i < events.Count; i++)
                //{
                //    distances[i] = new Seg() { P = neededMotion.P, rotation = neededMotion.rotation };
                //    Thing tEvent = events[i];
                //    Motion motion = (Motion)tEvent.Children[0].References[1].T.V;
                //    distances[i] = NextPosition(distances[i], motion);
                //}

                ////second time through loop, add to array
                //for (int j = 0; j < 3; j++) //three steps
                //{
                //    for (int i = 0; i < events.Count; i++)
                //    {
                //        for (int k = 0; k < events.Count; k++)
                //        {
                //            Thing tEvent = events[k];
                //            Motion motion = (Motion)tEvent.Children[0].References[1].T.V;

                //            Seg newS = NextPosition(distances[i], motion);
                //            if (newS.Val < distances[i].Val)
                //            { distances[i] = newS; }
                //        }
                //    }
                //}

                //float bestDist = float.MaxValue;
                //for (int i = 0; i < events.Count; i++) //last time through loop, find best
                //{
                //    if (distances[i].Val < bestDist)
                //    {
                //        bestDist = distances[i].Val;
                //        bestEvent = events[i];
                //    }
                //}

                //Motion motion1 = (Motion)bestEvent.Children[0].References[1].T.V;
                //Seg newS1 = NextPosition(pCurrent, motion1);
                //if (newS1.Val < pTarget.Val)
                //{
                //    GoToLandmark(bestEvent.References[0].T, sCurrent);
                //    doPush = 2;
                //    doBackOff = true;
                //}
                //else //our best move makes things worse
                //{
                //    endTarget = null;
                //}
            }
        }
예제 #25
0
        //TODO: rewrite again
        public Thing AddSegmentFromVision(PointPlus P1, PointPlus P2, ColorInt theColor, bool moved)
        {
            Thing retVal = null;

            Debug.WriteLine("AddSegment: " + P1 + P2 + theColor);
            //determine if the segment is already in the UKS.
            //Correct it if it is there, add it if it is not.
            //FUTURE: detect motion
            if (theColor == 0)
            {
                return(null);
            }
            Segment newSegment = new Segment()
            {
                P1 = P1, P2 = P2, theColor = theColor
            };
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            GetSegmentsFromUKS();
            if (UKS != null)
            {
                //it's easier if we sort by theta
                OrderSegment(newSegment);
                retVal = MostLikelySegment(newSegment);
                if (retVal != null)
                {
                    //UKS.Fire(match);
                    //OrderSegment(match);
                    //Segment s = SegmentFromUKSThing(match);
                    //float newVisualWidth = newSegment.VisualWidth();
                    //float matchVisualWidth = s.VisualWidth();
                    ////if the newVisualWidth is bigger, an adjustment is needed
                    ////this happens if the initial view was occluded but now it is less
                    //Thing match1 = MostLikelyPoint(newSegment.P1, newSegment.theColor);
                    //Thing match2 = MostLikelyPoint(newSegment.P2, newSegment.theColor);
                    //if (match1 != null && match2 != null)
                    //{
                    //    if (newSegment.P1.Conf < s.P1.Conf)
                    //    {
                    //        s.P1.Conf = newSegment.P1.Conf;
                    //        s.P1.R = newSegment.P1.R;
                    //        s.P1.Theta = newSegment.P1.Theta;
                    //    }
                    //    if (newSegment.P2.Conf < s
                    //        .P2.Conf)
                    //    {
                    //        s.P2.Conf = newSegment.P2.Conf;
                    //        s.P2.R = newSegment.P2.R;
                    //        s.P2.Theta = newSegment.P2.Theta;
                    //    }
                    //}

                    ////there is a significant point mismatch...
                    //else
                    //{
                    //    if (match1 == null && newSegment.P1.R > s.P1.R)
                    //    {
                    //        s.P1.Conf = newSegment.P1.Conf;
                    //        s.P1.R = newSegment.P1.R;
                    //        s.P1.Theta = newSegment.P1.Theta;
                    //    }
                    //    if (match2 == null && newSegment.P2.R > s.P2.R)
                    //    {
                    //        s.P2.Conf = newSegment.P2.Conf;
                    //        s.P2.R = newSegment.P2.R;
                    //        s.P2.Theta = newSegment.P2.Theta;
                    //    }
                    //}
                }
                else
                {
                    retVal = AddSegmentToUKS(P1, P2, theColor);
                    UKS.Fire(retVal);
                }
                UpdateDialog();
            }
            return(retVal);
        }
예제 #26
0
        //get input from touch... accurate locations, no color
        public bool AddSegmentFromTouch(PointPlus P1, PointPlus P2, PointPlus motion, int arm)
        {
            //if conf=0, it's a known endpoint. conf=1, not an endpoint
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS is null)
            {
                return(false);
            }
            if (UKSSegments is null)
            {
                return(false);
            }
            if (imagining)
            {
                return(false);
            }

            ColorInt theColor   = Utils.ColorToInt(Colors.Wheat);
            Segment  newSegment = new Segment()
            {
                P1 = P1, P2 = P2, theColor = theColor
            };
            //OrderSegment(newSegment);

            Thing t1 = GetNearestThing(newSegment.MidPoint.Theta, out float dist1);

            //TODO: for motion testing
            t1 = UKS.Labeled("s0");
            if (t1 == null)
            {
                //TODO: merge mutliple segments
                //                AddSegmentToUKS(P1, P2, theColor, motion); //don't store motion with the segment (yet)
                AddSegmentToUKS(P1, P2, theColor);
            }

            else if (dist1 < 1)
            {
                Segment   prevSegment  = SegmentFromUKSThing(t1);
                PointPlus prevMidpoint = prevSegment.MidPoint;
                Angle     oldM         = prevSegment.Angle;
                Angle     newM         = newSegment.Angle;
                PointPlus offset       = new PointPlus()
                {
                    R = prevSegment.Length, Theta = newM
                };
                if (P1.Conf == 0 && P2.Conf == 0) //we're given both endpoints
                {
                    prevSegment.P1.P = P1.P; prevSegment.P1.Conf = 0;
                    prevSegment.P2.P = P2.P; prevSegment.P2.Conf = 0;
                }
                else if (P1.Conf == 0)
                {
                    prevSegment.P1.P = P1.P; prevSegment.P1.Conf = 0;
                    prevSegment.P2.P = P1.P - offset.V;
                }
                else if (P2.Conf == 0)
                {
                    prevSegment.P1.P = P2.P; prevSegment.P2.Conf = 0;
                    prevSegment.P2.P = P2.P + offset.V;
                }
                else
                {
                    //we're not near an endpoint--match the modpoint as close as possible & preserve length
                    //make the segment match the two points
                    PointPlus newMidpoint1 = new PointPlus()
                    {
                        P = (Point)GetClosestPointOnLine(P1.V, P2.V, prevMidpoint.V),
                    };
                    //offset is the dietance from the midpoint to each end
                    offset.R = offset.R / 2;
                    PointPlus newP1 = new PointPlus()
                    {
                        P = newMidpoint1.P + offset.V
                    };
                    PointPlus newP2 = new PointPlus()
                    {
                        P = newMidpoint1.P - offset.V
                    };
                    prevSegment.P1.R = newP1.R; prevSegment.P1.Theta = newP1.Theta;
                    prevSegment.P2.R = newP2.R; prevSegment.P2.Theta = newP2.Theta;
                }
                PointPlus newMidpoint = prevSegment.MidPoint;
                newMidpoint.P = newMidpoint.P - prevMidpoint.V;
                if (newMidpoint.R > 0.01 && motion.R != 0)
                {
                    if (prevSegment.Motion == null)
                    {
                        prevSegment.Motion = new PointPlus();
                        Thing tMotion = UKS.AddThing("m" + mCount++, UKS.Labeled("Point"));
                        tMotion.V = prevSegment.Motion;
                        t1.AddReference(tMotion);
                    }
                    prevSegment.Motion.R     = motion.R;
                    prevSegment.Motion.Theta = motion.Theta;
                    prevSegment.Motion.Conf  = newM - oldM;
                }
            }

            UpdateDialog();
            return(false);
        }