Ejemplo n.º 1
0
        // Divides the two scores and truncates the result into a double
        public double DividedBy(LayoutScore other)
        {
            ListItemStats <double, double> ourFirstItem = this.components.GetLastValue();

            if (ourFirstItem == null)
            {
                ourFirstItem = new ListItemStats <double, double>(double.NegativeInfinity, 0);
            }
            ListItemStats <double, double> theirFirstItem = other.components.GetLastValue();

            if (theirFirstItem == null)
            {
                theirFirstItem = new ListItemStats <double, double>(double.NegativeInfinity, 0);
            }
            double ourValue   = 0;
            double theirValue = 0;

            if (ourFirstItem.Key > theirFirstItem.Key)
            {
                theirValue = 0;
            }
            else
            {
                theirValue = theirFirstItem.Value;
            }
            if (ourFirstItem.Key < theirFirstItem.Key)
            {
                ourValue = 0;
            }
            else
            {
                ourValue = ourFirstItem.Value;
            }
            return(ourValue / theirValue);
        }
        public ProgressionValue GetValueAt(DateTime when, bool strictlyEarlier)
        {
            ListItemStats <DateTime, WillingnessSummary> stats = this.searchHelper.FindPreviousItem(when, strictlyEarlier);

            if (stats == null)
            {
                return(null);
            }

            // get some statistics
            DateTime latestDate = stats.Key;
            // compute how long ago that rating was given
            TimeSpan duration = when.Subtract(latestDate);
            // create another date that is twice as far in the past
            DateTime earlierDate = latestDate.Subtract(duration);
            // add up everything that occurred between the earlier day and now
            WillingnessSummary sum = this.searchHelper.CombineBetweenKeys(earlierDate, true, when, !strictlyEarlier);
            double             numParticipations = sum.NumUnpromptedParticipations + sum.NumPromptedParticipations;
            double             numSkips          = sum.NumSkips;
            double             mean         = numParticipations / (numParticipations + numSkips);
            double             weight       = numParticipations + numSkips;
            Distribution       distribution = Distribution.MakeDistribution(mean, 0, weight);
            ProgressionValue   result       = new ProgressionValue(when, distribution);

            return(result);
        }
        public ProgressionValue GetValueAt(DateTime when, bool strictlyAfter)
        {
            ListItemStats <DateTime, double> prev = this.searchHelper.FindPreviousItem(when, false);

            if (prev == null)
            {
                return(this.DefaultValue(when));
            }
            ListItemStats <DateTime, double> next = this.searchHelper.FindNextItem(when, false);

            if (next == null)
            {
                return(this.DefaultValue(when));
            }
            double prevDuration = when.Subtract(prev.Key).TotalSeconds;
            double nextDuration = next.Key.Subtract(when).TotalSeconds;
            double totalWeight  = prevDuration + nextDuration;

            if (totalWeight <= 0)
            {
                Distribution avg = new Distribution();
                avg.Add(prev.Value);
                avg.Add(next.Value);
                return(new ProgressionValue(when, avg));
            }
            double       prevWeight = nextDuration / totalWeight;
            double       nextWeight = 1 - prevWeight;
            Distribution average    = new Distribution();

            average.Add(prev.Value, prevWeight);
            average.Add(next.Value, nextWeight);
            return(new ProgressionValue(when, average));
        }
        // returns basically the fraction of the user's time that was spent performing that Doable recently at that date
        public ProgressionValue GetValueAt(DateTime when, bool strictlyEarlier)
        {
            // find the most recent Participation
            ListItemStats <DateTime, Participation> previousStats = this.searchHelper.FindPreviousItem(when, strictlyEarlier);
            DateTime previousEnd;

            // make sure that we have enough data
            if (previousStats == null)
            {
                previousEnd = this.Owner.DiscoveryDate;
            }
            else
            {
                // figure out whether we're in the middle of a participation or not
                Participation previousParticipation = previousStats.Value;
                previousEnd = previousParticipation.EndDate;
            }
            //int index = 2 * this.searchHelper.CountBeforeKey(when, !strictlyEarlier);
            if (previousEnd.CompareTo(when) > 0)
            {
                // we're in the middle of a participation, so the value is zero
                return(new ProgressionValue(when, new Distribution(0, 0, 1)));
            }
            // we're not in the middle of a participation, so the value is the amount of time since the last one ended
            TimeSpan         duration     = when.Subtract(previousEnd);
            Distribution     currentValue = Distribution.MakeDistribution(duration.TotalSeconds, 0, 1);
            ProgressionValue result       = new ProgressionValue(when, currentValue);

            return(result);
        }
Ejemplo n.º 5
0
        // returns basically the average of the recent rating at that date
        public ProgressionValue GetValueAt(DateTime when, bool strictlyEarlier)
        {
            // find the most recent rating before the given date
            ListItemStats <DateTime, Distribution> latestItem = this.searchHelper.FindPreviousItem(when, true);

            if (latestItem == null)
            {
                return(null);
                //ProgressionValue defaultValue = new ProgressionValue(when, new Distribution(0, 0, 0));
                //return defaultValue;
            }
            // get some statistics
            Distribution latestDistribution = latestItem.Value;
            DateTime     latestDate         = latestItem.Key;
            // compute how long ago that rating was given
            TimeSpan duration = when.Subtract(latestDate);
            // create another date that is twice as far in the past
            DateTime earlierDate = latestDate.Subtract(duration);
            // add up everything that occurred between the earlier day and now
            Distribution sum = this.searchHelper.CombineBetweenKeys(earlierDate, true, when, !strictlyEarlier);
            //int previousCount = this.searchHelper.CountBeforeKey(when, strictlyEarlier);
            ProgressionValue result = new ProgressionValue(when, sum);

            return(result);
        }
Ejemplo n.º 6
0
        public Distribution AdaptivePrediction(double input)
        {
            //ListItemStats<double, Distribution> firstItem = this.searchHelper.GetValueAtIndex(0);
            //ListItemStats<double, Distribution> lastItem = this.searchHelper.GetValueAtIndex(this.NumDatapoints - 1);
            //double minXObserved = firstItem.Key;
            //double maxXObserved = lastItem.Key;
            double       minWindowX = this.minXObserved;
            double       maxWindowX = this.maxXObserved;
            double       newX1      = minWindowX;
            double       newX2      = maxWindowX;
            Distribution contents   = this.searchHelper.CombineBetweenKeys(minWindowX, true, maxWindowX, true);

            //while ((this.maxXObserved - this.minXObserved) / (maxWindowX - minWindowX) < (this.maxYObserved - this.minYObserved) / (contents.StdDev / contents.Weight))
            while ((this.maxXObserved - this.minXObserved) * contents.StdDev < (this.maxYObserved - this.minYObserved) * contents.Weight * (maxWindowX - minWindowX) &&
                   contents.Weight > 2)
            {
                // find the rightmost and leftmost datapoints in the domain we're considering
                ListItemStats <double, Distribution> leftStats = this.searchHelper.FindNextItem(newX1, false);
                if (leftStats != null)
                {
                    minWindowX = leftStats.Key;
                }
                else
                {
                    minWindowX = newX1;
                }
                ListItemStats <double, Distribution> rightStats = this.searchHelper.FindPreviousItem(newX2, false);
                if (rightStats != null)
                {
                    maxWindowX = rightStats.Key;
                }
                else
                {
                    maxWindowX = newX2;
                }
                // make the domain a little smaller
                double width = maxWindowX - minWindowX;
                newX1 = input - width / 4;
                newX2 = input + width / 4;
                if (minWindowX == newX1 && maxWindowX == newX2)
                {
                    // if there would be an infinite loop, then quit
                    break;
                }
                //newX1 = (minWindowX * 3 + maxWindowX) / 4;
                //newX2 = (minWindowX + maxWindowX * 3) / 4;
                // find the standard deviation of all points within this domain
                contents = this.searchHelper.CombineBetweenKeys(newX1, true, newX2, true);
            }
            Distribution result = this.searchHelper.CombineBetweenKeys(minWindowX, true, maxWindowX, true);

            return(result);
        }
 public void RemoveAllBefore(DateTime when)
 {
     while (true)
     {
         if (this.searchHelper.NumItems <= 0)
         {
             break;
         }
         ListItemStats <DateTime, double> item = this.searchHelper.GetFirstValue();
         if (item.Key.CompareTo(when) >= 0)
         {
             return;
         }
         this.searchHelper.Remove(item.Key);
     }
 }
        public ProgressionValue GetValueAt(DateTime when, bool strictlyEarlier)
        {
            ListItemStats <DateTime, ActivitySkip> stats = this.searchHelper.FindPreviousItem(when, strictlyEarlier);

            if (stats == null)
            {
                return(null);
            }
            //return new ProgressionValue(when, new Distribution(), -1);
            ActivitySkip skip         = stats.Value;
            Distribution distribution = Distribution.MakeDistribution(when.Subtract(skip.CreationDate).TotalSeconds, 0, 1);
            //ProgressionValue progressionValue = new ProgressionValue(when, distribution, this.searchHelper.CountBeforeKey(when, !strictlyEarlier));
            ProgressionValue progressionValue = new ProgressionValue(when, distribution);

            return(progressionValue);
        }
        private void simulate(Engine engine, StatList <DateTime, double> efficiencies)
        {
            for (int i = 0; i < efficiencies.NumItems; i++)
            {
                DateTime when = efficiencies.GetValueAtIndex(i).Key;
                if (engine.Test_ChooseExperimentOption().HasError)
                {
                    break; // not enough data to run more experiments
                }
                // make a list of experiment options
                List <SuggestedMetric> experimentOptions = engine.ChooseExperimentOptions(when);
                // Skip setting difficulties for now.
                // Task difficulties could be set via something like:
                //   experimentOptions[0].PlannedMetric.DifficultyEstimate.NumEasiers++;
                // Make an experiment
                ExperimentSuggestion suggestion = engine.Experiment(experimentOptions, when);
                if (!suggestion.Experiment.Started)
                {
                    engine.PutExperimentInMemory(suggestion.Experiment);
                }
                // Do the suggestion
                double             efficiency         = efficiencies.FindPreviousItem(when, false).Value;
                double             duration           = 1.0 / efficiency;
                ActivityDescriptor activityDescriptor = suggestion.ActivitySuggestion.ActivityDescriptor;
                Activity           activity           = engine.ActivityDatabase.ResolveDescriptor(activityDescriptor);
                Metric             metric             = activity.DefaultMetric;
                Participation      participation      = new Participation(when, when.AddDays(duration), activityDescriptor);

                participation.EffectivenessMeasurement = new CompletionEfficiencyMeasurement(metric, true, 0);
                participation.EffectivenessMeasurement.DismissedActivity = true;
                RelativeEfficiencyMeasurement measurement = engine.Make_CompletionEfficiencyMeasurement(participation);
                participation.EffectivenessMeasurement.Computation = measurement;

                engine.PutParticipationInMemory(participation);
            }
            engine.FullUpdate();
            DateTime lastDay = efficiencies.GetLastValue().Key;

            for (int i = 1; i < efficiencies.NumItems; i++)
            {
                ListItemStats <DateTime, double> item     = efficiencies.GetValueAtIndex(i - 1);
                ListItemStats <DateTime, double> nextItem = efficiencies.GetValueAtIndex(i);
                Distribution estimatedEfficiency          = engine.EfficiencySummarizer.GetValueDistributionForDates(item.Key, nextItem.Key, true, false);
                System.Diagnostics.Debug.WriteLine("True efficiency at " + item.Key + " = " + item.Value + ", estimated efficiency = " + estimatedEfficiency.Mean);
            }
            System.Diagnostics.Debug.WriteLine("Test done");
        }
Ejemplo n.º 10
0
        private void addComponent(double priority, double weight)
        {
            // workaround for rounding error
            weight = Math.Round(weight, 6);
            this.components.Add(priority, weight);
            ListItemStats <double, double> lastItem = this.components.GetLastValue();

            if (double.IsPositiveInfinity(lastItem.Key))
            {
                if (lastItem.Value > 0)
                {
                    this.CopyFrom(LayoutScore.Maximum);
                }
                else
                {
                    this.CopyFrom(LayoutScore.Minimum);
                }
            }
        }
Ejemplo n.º 11
0
        public int CompareTo(LayoutScore other)
        {
            int ourIndex   = this.components.NumItems - 1;
            int theirIndex = other.components.NumItems - 1;

            // loop over the list of components and once a coordinate differs, use it for the comparison
            while (true)
            {
                if (ourIndex >= 0)
                {
                    if (theirIndex >= 0)
                    {
                        // both coordinates exist
                        ListItemStats <double, double> ourComponent   = this.components.GetValueAtIndex(ourIndex);
                        ListItemStats <double, double> theirComponent = other.components.GetValueAtIndex(theirIndex);
                        // check for the possibility that one coordinate is more important than the other
                        if (ourComponent.Key > theirComponent.Key)
                        {
                            if (ourComponent.Value != 0)
                            {
                                return(ourComponent.Value.CompareTo(0));
                            }
                        }
                        else
                        {
                            if (theirComponent.Key > ourComponent.Key)
                            {
                                if (theirComponent.Value != 0)
                                {
                                    return(-theirComponent.Value.CompareTo(0));
                                }
                            }
                            else
                            {
                                // keys are equal
                                int comparison = ourComponent.Value.CompareTo(theirComponent.Value);
                                if (comparison != 0)
                                {
                                    return(comparison);
                                }
                            }
                        }
                        if (ourComponent.Key >= theirComponent.Key)
                        {
                            ourIndex--;
                        }
                        if (ourComponent.Key <= theirComponent.Key)
                        {
                            theirIndex--;
                        }
                    }
                    else
                    {
                        // only our coordinate exists
                        ListItemStats <double, double> ourComponent = this.components.GetValueAtIndex(ourIndex);
                        if (ourComponent.Value != 0)
                        {
                            return(ourComponent.Value.CompareTo(0));
                        }
                        ourIndex--;
                    }
                }
                else
                {
                    if (theirIndex >= 0)
                    {
                        // only their coordinate exists
                        ListItemStats <double, double> theirComponent = other.components.GetValueAtIndex(theirIndex);
                        if (theirComponent.Value != 0)
                        {
                            return(-theirComponent.Value.CompareTo(0));
                        }
                        theirIndex--;
                    }
                    else
                    {
                        // neither score has any more coordinates
                        return(0);
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public LayoutScore Plus(LayoutScore other)
        {
            int         ourIndex   = 0;
            int         theirIndex = 0;
            LayoutScore sum        = new LayoutScore();

            while (true)
            {
                double priority;
                double weight = 0;
                if (ourIndex < this.components.NumItems)
                {
                    if (theirIndex < other.components.NumItems)
                    {
                        // both coordinates exist
                        ListItemStats <double, double> ourComponent   = this.components.GetValueAtIndex(ourIndex);
                        ListItemStats <double, double> theirComponent = other.components.GetValueAtIndex(theirIndex);
                        priority = Math.Min(ourComponent.Key, theirComponent.Key);
                        if (ourComponent.Key == priority)
                        {
                            weight += ourComponent.Value;
                            ourIndex++;
                        }
                        if (theirComponent.Key == priority)
                        {
                            if (double.IsInfinity(weight) && double.IsInfinity(theirComponent.Value) &&
                                double.IsPositiveInfinity(weight) != double.IsPositiveInfinity(theirComponent.Value))
                            {
                                // Treat negative infinity plus positive infinity as zero
                                weight = 0;
                            }
                            else
                            {
                                weight += theirComponent.Value;
                            }
                            theirIndex++;
                        }
                    }
                    else
                    {
                        // only our coordinate exists
                        ListItemStats <double, double> ourComponent = this.components.GetValueAtIndex(ourIndex);
                        priority = ourComponent.Key;
                        weight   = ourComponent.Value;
                        ourIndex++;
                    }
                }
                else
                {
                    if (theirIndex < other.components.NumItems)
                    {
                        // only their coordinate exists
                        ListItemStats <double, double> theirComponent = other.components.GetValueAtIndex(theirIndex);
                        priority = theirComponent.Key;
                        weight   = theirComponent.Value;
                        theirIndex++;
                    }
                    else
                    {
                        // no more components are left
                        // debug-check: make sure there are no duplicate coordinates
                        int i;
                        for (i = 1; i < sum.components.NumItems; i++)
                        {
                            if (sum.components.GetValueAtIndex(i - 1).Key == sum.components.GetValueAtIndex(i).Key)
                            {
                                System.Diagnostics.Debug.WriteLine("error: layout score has a duplicate key");
                            }
                        }


                        return(sum);
                    }
                }
                // only add the coordinate if it is nonzero, because zeros are always provided by default
                if (weight != 0)
                {
                    sum.addComponent(priority, weight);
                }
            }
        }