public LoadPrediction(byte levelCount, Dictionary <byte, int[]> values)
        {
            var controlers = new List <FeedbackController>();

            foreach (var value in values)
            {
                var dict       = new SortedDictionary <FeedbackLevel, FeedbackLevelData>();
                var thresholds = value.Value;
                for (var i = 0; i < thresholds.Length / 3; ++i)
                {
                    var upDownValues = new FeedbackLevelData
                    {
                        UpperBound = thresholds[3 * i + 1], LowerBound = thresholds[3 * i + 2]
                    };
                    if (i == (int)FeedbackLevel.Highest)
                    {
                        upDownValues.UpperBound = int.MaxValue;
                    }
                    dict.Add((FeedbackLevel)thresholds[3 * i], upDownValues);
                }

                var controller = new FeedbackController((FeedbackName)value.Key, dict, 0, FeedbackLevel.Lowest);
                controlers.Add(controller);
            }
            this.controllerCollection = new FeedbackControllerCollection(controlers.ToArray());
        }
        private bool UpdateLevelValues(FeedbackLevel level, int input, float factor, out FeedbackLevelData upDownValues, out FeedbackLevelData higherLevelValues, Dictionary <FeedbackLevel, FeedbackLevelData> updatedLevels)
        {
            higherLevelValues = new FeedbackLevelData(-1, -1);
            if (!this.thresholdValues.TryGetValue(level, out upDownValues))
            {
                log.WarnFormat("Can not find values for level {0}", level);
                return(false);
            }

            if (input < 0)
            {
                input = 0;
            }

            if (level == FeedbackLevel.Highest)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("We do not update upper bound for 'Highest' level");
                }
                //upDownValues.UpperBound = (int)(factor * (input - upDownValues.UpperBound)) + upDownValues.UpperBound;
                //this.thresholdValues[level] = upDownValues;
                //updatedLevels[level] = upDownValues;
                return(true);
            }

            var nextLevel = this.GetNextExistingHigherLevel(level);

            if (!this.thresholdValues.TryGetValue(nextLevel, out higherLevelValues))
            {
                log.WarnFormat("Can not find values for level {0}", nextLevel);
                return(false);
            }

            var upDownDiff = upDownValues.UpperBound - higherLevelValues.LowerBound;

            upDownValues.UpperBound      = (int)(factor * (input - upDownValues.UpperBound)) + upDownValues.UpperBound;
            higherLevelValues.LowerBound = upDownValues.UpperBound - upDownDiff;
            if (higherLevelValues.LowerBound > upDownValues.UpperBound)
            {
                higherLevelValues.LowerBound = upDownValues.UpperBound;
            }

            this.thresholdValues[level]     = upDownValues;
            this.thresholdValues[nextLevel] = higherLevelValues;
            updatedLevels[level]            = upDownValues;
            updatedLevels[nextLevel]        = higherLevelValues;

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Level updated. Input:{0}, level:{1}, next:{2}, {1}'s data:{3}, {2}'s data:{4}",
                                input, level, nextLevel, upDownValues, higherLevelValues);
            }

            return(true);
        }
        private void Initialize(string workLoadConfigFile)
        {
            // try to load feedback controllers from file:

            string message;
            LoadPredictionSystemSection section;
            string filename = Path.Combine(this.rootPath, workLoadConfigFile);

            if (!ConfigurationLoader.TryLoadFromFile(filename, out section, out message))
            {
                log.WarnFormat(
                    "Could not initialize Load Prediction System from configuration: Invalid configuration file {0}. Using default settings... ({1})",
                    filename,
                    message);
            }

            if (section != null)
            {
                // load controllers from config file.);
                foreach (FeedbackControllerElement controllerElement in section.FeedbackControllers)
                {
                    if (controllerElement.Name != FeedbackName.PeerCount)
                    {
                        continue;
                    }

                    var dict = new SortedDictionary <FeedbackLevel, FeedbackLevelData>();
                    foreach (FeedbackLevelElement level in controllerElement.Levels)
                    {
                        var values = new FeedbackLevelData
                        {
                            UpperBound = level.Value,
                            LowerBound = level.ValueDown == -1 ? level.Value : level.ValueDown,
                        };
                        if (level.Level == FeedbackLevel.Highest)
                        {
                            values.UpperBound = int.MaxValue;
                        }

                        dict.Add(level.Level, values);
                    }
                    this.thresholdValues = dict;
                }


                log.InfoFormat("Initialized Load Prediction with {0} controllers from config file.", section.FeedbackControllers.Count);
            }
            else
            {
                // default settings, in case no config file was found.
                this.thresholdValues = DefaultConfiguration.GetDefaultControllers();
            }
        }
        public void UpdatePredictionLevels(Dictionary <byte, int[]> predictionData)
        {
            foreach (var value in predictionData)
            {
                var dict       = new SortedDictionary <FeedbackLevel, FeedbackLevelData>();
                var thresholds = value.Value;
                for (var i = 0; i < thresholds.Length / 3; ++i)
                {
                    var upDownValues = new FeedbackLevelData
                    {
                        UpperBound = thresholds[3 * i + 1],
                        LowerBound = thresholds[3 * i + 2]
                    };
                    if (i == (int)FeedbackLevel.Highest)
                    {
                        upDownValues.UpperBound = int.MaxValue;
                    }
                    dict.Add((FeedbackLevel)thresholds[3 * i], upDownValues);
                }

                this.controllerCollection.UpdateFeedbackController((FeedbackName)value.Key, dict);
            }
        }