Example #1
0
        /// <summary>
        /// 创建非负整数限制
        /// </summary>
        /// <param name="threshold">阈值,所有变量的和必须等于阈值</param>
        /// <param name="limitValueRule">限制规则</param>
        /// <param name="keys">变量的key值</param>
        public NonNegativeIntLimitValue(long threshold, LimitValueRule limitValueRule, params string[] keys)
        {
            _LimitValueRule   = limitValueRule;
            Threshold         = threshold;
            _ReadOnlyIsNewest = true;
            _ValuesMap        = new Dictionary <string, long>();
            if (_LimitValueRule == LimitValueRule.EQUALS)
            {
                long rest = threshold % keys.Length;
                long avg  = (threshold - rest) / keys.Length;
                foreach (string key in keys)
                {
                    _ValuesMap.Add(key, avg);
                }
                _ValuesMap[keys[0]] += rest;
            }
            else
            {
                foreach (string key in keys)
                {
                    _ValuesMap.Add(key, 0);
                }
            }
            _ReadOnlyMap = new ReadOnlyDictionary <string, long>(_ValuesMap);
#if DEBUG
            if (_DebugOutput)
            {
                Console.WriteLine(OutputDebug());
            }
#endif
        }
Example #2
0
        /// <summary>
        /// 创建非负浮点数限制
        /// </summary>
        /// <param name="threshold">阈值,所有变量的和必须等于阈值</param>
        /// <param name="decimalCoumt">小数位数</param>
        /// <param name="limitValueRule">限制规则</param>
        /// <param name="keys">变量的key值</param>
        public NonNegativeDoubleLimitValue(double threshold, int decimalCount, LimitValueRule limitValueRule, params string[] keys)
        {
            NonNegativeIntLimitValue.CloseDebugOutput();
            _DecimalCount             = decimalCount;
            Threshold                 = threshold;
            _NonNegativeIntLimitValue = new NonNegativeIntLimitValue(Convert.ToInt64(threshold * Math.Pow(10, _DecimalCount)), limitValueRule, keys);
            _ReadOnlyMap              = _NonNegativeIntLimitValue.GetDictionary();
            Dictionary <string, double> retMap = new Dictionary <string, double>();

            foreach (var keyVal in _ReadOnlyMap)
            {
                retMap.Add(keyVal.Key, keyVal.Value / Math.Pow(10, _DecimalCount));
            }
            _ReadOnlyDoubleMap = new ReadOnlyDictionary <string, double>(retMap);
#if DEBUG
            Console.WriteLine(OutputDebug());
#endif
        }