Exemplo n.º 1
0
        public void HandleAsynchronousDialogueItemCompleted(object sender, AsynchronousDialogueItemEventArgs e)
        {
            string targetContext = e.TargetContext;
            string targetID      = e.TargetID;

            // Here, set the targetID ONLY if
            //
            // (i)  the agent will change its context anyway upon completion of the asynchronous dialogue item, i.e. if
            //      the targetContext is different from the original context, or
            //
            // (ii) the agent aims to stay in the same context (upon completion of the asynchronous dialogue item) but
            //      has been forced to change context for some other reason (e.g. user action). In this case, the
            //      agent will ignore the targedID suggested by the asynchronous dialogue item.
            //
            //
            if (e.TargetContext != e.OriginalContext)
            {
                workingMemory.CurrentContext = e.TargetContext;
            }
            else
            {
                if (targetContext == workingMemory.CurrentContext)
                {
                    workingMemory.CurrentID = targetID;
                }
            }

            // MW ToDo: Old, remove
            //  if (targetContext != workingMemory.CurrentContext) { workingMemory.CurrentContext = targetContext; } // Triggers the ContextChangedEvent in working memory
            //  else { workingMemory.CurrentID = targetID; } // Triggers the IDChangedEvent in working memory
        }
        protected override void RunLoop(List <object> parameterList)
        {
            //pops out the remaining items
            MemoryItem origSought = ItemHandler.PopItem(ownerAgent, AgentConstants.QUERY_TAG_3);
            MemoryItem destSought = ItemHandler.PopItem(ownerAgent, AgentConstants.QUERY_TAG_4);

            string originalContext  = ownerAgent.WorkingMemory.CurrentContext;
            int    totalWaitingTime = 0;
            bool   hasExpired       = false;


            ownerAgent.SendExpression("squint"); //tells the agent to really think

            string nextID = OutputAction.TargetID;

            while (!(mapControl.LoadState == MapControl.LOADSTATE.DestinationComplete || mapControl.LoadState == MapControl.LOADSTATE.LocationComplete) && !hasExpired)
            {
                totalWaitingTime += step;
                hasExpired        = totalWaitingTime >= millisecondMaxWaitingTime;
                System.Threading.Thread.Sleep(step);
            }

            if (hasExpired)
            {
                ownerAgent.SendSpeechOutput("Failed to search for the trip");
            }
            else if ((mapControl.LoadState == MapControl.LOADSTATE.DestinationComplete) && !hasExpired && mapControl.DepartureTimeListBox.Items.Count > 0)
            {
                ownerAgent.SendSpeechOutput("I found your trip, the transport departs from " + mapControl.Departure + " at " + mapControl.DepartureTimeListBox.Items[0] + " o' clock");

                ownerAgent.SendExpression("nodhead");
                if (origSought != null && destSought != null)
                {
                    string orig = origSought.GetContent().ToString();
                    string dest = destSought.GetContent().ToString();

                    //tries to remember the gathered position
                    ItemHandler.StoreShortcut(ownerAgent, orig, mapControl.Departure, false);
                    ItemHandler.StoreShortcut(ownerAgent, dest, mapControl.Arrival, false);
                }
            }
            else if (((mapControl.LoadState == MapControl.LOADSTATE.LocationComplete)))
            {
                ownerAgent.SendExpression("nodhead");
                if (mapControl.LocationsOfInterest.Count > 0)
                {
                    ownerAgent.SendSpeechOutput("I found " + mapControl.AddressesOfInterest.Count + " locations nearby, " + mapControl.LocationsOfInterest[0] + " is closest. Do you want to go?");
                    nextID = interestID;


                    ItemHandler.StoreTermOnTag(ownerAgent, AgentConstants.QUERY_TAG_3, origSought.GetContent().ToString());
                }
                else
                {
                    ownerAgent.SendSpeechOutput("I could not find any " + destSought.GetContent().ToString() + " places near you.");
                }
            }
            else
            {
                ownerAgent.SendExpression("shakehead");
                ownerAgent.SendSpeechOutput("Uh oh. Something went wrong.");
            }
            AsynchronousDialogueItemEventArgs e = new AsynchronousDialogueItemEventArgs(originalContext, outputAction.TargetContext, nextID);

            OnRunCompleted(e);
        }
Exemplo n.º 3
0
        protected override void RunLoop(List <object> parameterList)
        {
            string     originalContext = ownerAgent.WorkingMemory.CurrentContext;
            string     currentUser     = "";
            MemoryItem itemSought      = ownerAgent.WorkingMemory.GetLastItemByTag(inputQueryTagList[0]);

            if (itemSought != null)  // 20171201
            {
                currentUser = (string)itemSought.GetContent();
            }

            // Recommend!

            // Get similarity of currentUser to all other users-------------------------------------------------

            // create ratingTable
            List <string> userList  = new List <string> {
            };
            List <string> movieList = new List <string> {
            };

            userList.Add(currentUser);
            foreach (var rating in _ultraManager.RatingList)
            {
                if (!userList.Exists(x => string.Equals(x, rating.UserName, StringComparison.OrdinalIgnoreCase)))
                {
                    userList.Add(rating.UserName);
                }
            }
            foreach (var rating in _ultraManager.RatingList)
            {
                if (!movieList.Exists(x => string.Equals(x, rating.MovieTitle, StringComparison.OrdinalIgnoreCase)))
                {
                    movieList.Add(rating.MovieTitle);
                }
            }

            int nbrOfUsers          = userList.Count;
            int userInsertionIndex  = 0;
            int nbrOfMovies         = movieList.Count;
            int movieInsertionIndex = 0;

            double[,] ratingTable = new double[nbrOfUsers, nbrOfMovies];

            foreach (var rating in _ultraManager.RatingList)
            {
                userInsertionIndex  = userList.FindIndex(x => x.StartsWith(rating.UserName));
                movieInsertionIndex = movieList.FindIndex(x => x.StartsWith(rating.MovieTitle));
                ratingTable[userInsertionIndex, movieInsertionIndex] = rating.RatingValue;
            }

            // calculate mean rating for each user
            double[] meanRatings = new double[nbrOfUsers];
            for (int i = 0; i < nbrOfUsers; ++i)
            {
                double ratingSum  = 0;
                int    nbrOfRated = 0;
                for (int j = 0; j < nbrOfMovies; ++j)
                {
                    if (ratingTable[i, j] > 0)
                    {
                        ratingSum = ratingSum + ratingTable[i, j];
                        ++nbrOfRated;
                    }
                }
                if (ratingSum > 0 && nbrOfRated > 0)
                {
                    meanRatings[i] = ratingSum / nbrOfRated;
                }
            }

            // calculate Pearson correlation
            List <double> simToUser = new List <double> {
                0
            };                                               // TODO: use dictionary

            for (int i = 1; i < nbrOfUsers; ++i)
            {
                List <int> setRatedBoth = new List <int> {
                };
                for (int j = 0; j < nbrOfMovies; ++j)
                {
                    if (ratingTable[0, j] > 0 && ratingTable[i, j] > 0)
                    {
                        setRatedBoth.Add(j);
                    }
                }

                double sum1 = 0;
                double sum2 = 0;
                double sum3 = 0;
                for (int k = 0; k < setRatedBoth.Count; ++k)
                {
                    sum1 = sum1 + (ratingTable[0, setRatedBoth[k]] - meanRatings[0]) * (ratingTable[i, setRatedBoth[k]] - meanRatings[i]);
                    sum2 = sum2 + Math.Pow(ratingTable[0, setRatedBoth[k]] - meanRatings[0], 2);
                    sum3 = sum3 + Math.Pow(ratingTable[i, setRatedBoth[k]] - meanRatings[i], 2);
                }
                simToUser.Add(sum1 / (Math.Sqrt(sum2 * sum3)));
            }

            // Get most similar user
            int mostSimilarUser = simToUser.IndexOf(simToUser.Max());

            Debug.WriteLine(userList[mostSimilarUser]);

            // Return random unseen movie
            List <string> unseenMovies = new List <string> {
            };

            for (int j = 0; j < nbrOfMovies; ++j)
            {
                if (ratingTable[0, j] == 0 && ratingTable[mostSimilarUser, j] > 5)
                {
                    unseenMovies.Add(movieList[j]);
                }
            }
            Boolean noMoreRecommendations = false;

            if (unseenMovies.Count > 0)
            {
                int              randomIndex    = ownerAgent.RandomNumberGenerator.Next(0, unseenMovies.Count);
                string           recommendation = unseenMovies[randomIndex];
                StringMemoryItem rateMemoryItem = new StringMemoryItem();
                rateMemoryItem.TagList = new List <string>()
                {
                    outputQueryTag
                };
                rateMemoryItem.SetContent(recommendation);
                ownerAgent.WorkingMemory.AddItem(rateMemoryItem);
            }
            else
            {
                noMoreRecommendations = true;
            }


            AsynchronousDialogueItemEventArgs e = new AsynchronousDialogueItemEventArgs(originalContext, outputAction.TargetContext, outputAction.TargetID);

            OnRunCompleted(e);
        }