Ejemplo n.º 1
0
        public static float CalculateChebyshevNorm(IEnumerable <Utility> list)
        {
            var vlist = new List <float>();
            var wsum  = 0.0f;

            foreach (var u in list)
            {
                wsum += u.Weight;
            }

            if (CrMath.AeqZero(wsum))
            {
                return(0.0f);
            }

            foreach (var el in list)
            {
                vlist.Add(el.Weight / wsum * el.Value);
            }

            float res = -10.0f;

            foreach (var v in vlist)
            {
                if (v >= res)
                {
                    res = v;
                }
            }

            return(res);
        }
Ejemplo n.º 2
0
        public void LinearSpacedCountOneReturnsEnd()
        {
            var ls = CrMath.LinearSpaced(1, 1f, 2f);

            Assert.That(ls[0], Is.EqualTo(2.0f));
            Assert.That(ls.Length == 1);
        }
Ejemplo n.º 3
0
        public static float CalculateWeightedMetricsNorm(ICollection <Utility> list, float p = 2.0f)
        {
            var vlist = new List <float>();
            var wsum  = 0.0f;

            foreach (var u in list)
            {
                wsum += u.Weight;
            }

            if (CrMath.AeqZero(wsum))
            {
                return(0.0f);
            }

            foreach (var u in list)
            {
                var v = u.Weight / wsum * (float)Math.Pow(u.Value, p);
                vlist.Add(v);
            }

            var sum = 0.0f;

            foreach (var v in vlist)
            {
                sum += v;
            }

            return((float)Math.Pow(sum, 1 / p));
        }
Ejemplo n.º 4
0
        public void WeightedMetricsInCollectionTest()
        {
            var o = OptionConstructor.WeightedMetrics("optionwc", _oc);

            Assert.That(o.Measure is WeightedMetrics);
            Assert.That(CrMath.AeqB(((WeightedMetrics)o.Measure).PNorm, 2.0f));
        }
Ejemplo n.º 5
0
        public void EvaluateTest(float xA, float yA, float xB, float yB)
        {
            var        pt1 = new Pointf(xA, yA);
            var        pt2 = new Pointf(xB, yB);
            IEvaluator ev  = new LinearEvaluator(pt1, pt2);

            var yAn     = yA.Clamp01();
            var yBn     = yB.Clamp01();
            var xdf     = xB - xA;
            var ydf     = yBn - yAn;
            var alpha   = ydf / xdf;
            var beta    = yBn - alpha * xB;
            var xqArray = CrMath.LinearSpaced(_evN, xA - 10.0f, xB + 10.0f);

            for (int i = 0; i < _evN - 1; i++)
            {
                var cVal = LinearClamped(xqArray[i], alpha, beta);
                var aVal = ev.Evaluate(xqArray[i]);
                Assert.That(aVal, Is.EqualTo(cVal).Within(Tolerance));
                Assert.That(aVal, Is.EqualTo(cVal).Within(Tolerance));
                Assert.That(aVal <= 1.0f);
                Assert.That(aVal >= 0.0f);
            }
            // Check the end points
            var utilA = ev.Evaluate(xA);
            var utilB = ev.Evaluate(xB);

            Assert.That(utilA, Is.EqualTo(yA.Clamp01()).Within(Tolerance));
            Assert.That(utilB, Is.EqualTo(yB.Clamp01()).Within(Tolerance));
        }
Ejemplo n.º 6
0
        public void EvaluateTest(float xA, float yA, float xB, float yB, float power)
        {
            var ptA = new Pointf(xA, yA);
            var ptB = new Pointf(xB, yB);
            var ev  = new PowerEvaluator(ptA, ptB, power);

            var yAn = yA.Clamp01();
            var yBn = yB.Clamp01();

            var xqArray = CrMath.LinearSpaced(_evN, xA - 0.001f * xA, xB + 0.001f * xB);

            for (int i = 0; i < _evN - 1; i++)
            {
                var dy   = yBn - yAn;
                var dx   = xB - xA;
                var cVal = dy * (float)Math.Pow((xqArray[i].Clamp <float>(xA, xB) - xA) / dx, power) + yAn;

                Utility cResult = cVal;
                var     aResult = ev.Evaluate(xqArray[i]);
                Assert.That(aResult, Is.EqualTo(cResult.Value).Within(Tolerance));
                Assert.That(aResult <= 1.0f);
                Assert.That(aResult >= 0.0f);
            }
            // Check the end points
            var utilA = ev.Evaluate(xA);
            var utilB = ev.Evaluate(xB);

            Assert.That(utilA, Is.EqualTo(yAn).Within(Tolerance));
            Assert.That(utilB, Is.EqualTo(yBn).Within(Tolerance));
        }
        public void EvaluateTest(
            float xA1, float yA1,
            float xB1, float yB1,
            float xA2, float yA2,
            float xB2, float yB2,
            float xA3, float yA3,
            float xB3, float yB3,
            float xA4, float yA4,
            float xB4, float yB4)
        {
            var ptA1 = new Pointf(xA1, yA1);
            var ptB1 = new Pointf(xB1, yB1);
            var ev1  = new LinearEvaluator(ptA1, ptB1);

            var ptA2 = new Pointf(xA2, yA2);
            var ptB2 = new Pointf(xB2, yB2);
            var ev2  = new PowerEvaluator(ptA2, ptB2, 3.0f);

            var ptA3 = new Pointf(xA3, yA3);
            var ptB3 = new Pointf(xB3, yB3);
            var ev3  = new SigmoidEvaluator(ptA3, ptB3, -0.4f);

            var ptA4 = new Pointf(xA4, yA4);
            var ptB4 = new Pointf(xB4, yB4);
            var ev4  = new SigmoidEvaluator(ptA4, ptB4, 0.6f);

            var minX = Math.Min(Math.Min(xA1, xA2), Math.Min(xA3, xA4));
            var maxX = Math.Max(Math.Max(xB1, xB2), Math.Max(xB3, xB4));

            var compositeXInterval = new Interval <float>(minX, maxX);

            var cev = new CompositeEvaluator();

            // AddConsideration them out of order to ensure the ordering works.
            cev.Add(ev2);
            cev.Add(ev4);
            cev.Add(ev3);
            cev.Add(ev1);
            Assert.That(cev.Evaluators.Count == 4);

            var xqArray = CrMath.LinearSpaced(_evN, minX * 0.999f, 1.001f * maxX);

            for (int i = 0; i < _evN - 1; i++)
            {
                var     cVal    = CombinedEvaluatorEvaluate(xqArray[i], ev1, ev2, ev3, ev4);
                Utility cResult = cVal;

                var aResult = cev.Evaluate(xqArray[i]);
                Assert.That(aResult, Is.EqualTo(cResult.Value).Within(Tolerance));
                Assert.That(aResult <= 1.0f);
                Assert.That(aResult >= 0.0f);
            }
            // Check the end points
            var utilA = cev.Evaluate(compositeXInterval.LowerBound);
            var utilB = cev.Evaluate(compositeXInterval.UpperBound);

            Assert.That(utilA, Is.EqualTo(ev1.PtA.Y).Within(Tolerance));
            Assert.That(utilB, Is.EqualTo(ev4.PtB.Y).Within(Tolerance));
        }
Ejemplo n.º 8
0
        public void LinearSpacedTest(int count, float start, float end)
        {
            var ls = CrMath.LinearSpaced(count, start, end);

            Assert.That(ls.Length == count);
            Assert.That(ls[0], Is.EqualTo(start));
            Assert.That(ls[ls.Length - 1], Is.EqualTo(end));
        }
Ejemplo n.º 9
0
        public void UpdateEpsTest()
        {
            var eps = CrMath.Eps;

            CrMath.UpdateEps(1000.0f);
            var eps1000 = CrMath.Eps;

            Assert.That(eps1000, Is.GreaterThan(eps));
        }
Ejemplo n.º 10
0
        public void UpdateDepsTest()
        {
            var deps = CrMath.Deps;

            CrMath.UpdateDeps(1000.0);
            var deps1000 = CrMath.Deps;

            Assert.That(deps1000, Is.GreaterThan(deps));
        }
Ejemplo n.º 11
0
        /// <summary>
        ///   Returns true if the interval defined by @this is to the right of the interval defined
        ///   by other and is adjacent, i.e. if other = [b, c] and @this = [c, d].
        /// </summary>
        /// <returns><c>true</c>, if adjacent to the right, <c>false</c> otherwise.</returns>
        /// <param name="this">Value.</param>
        /// <param name="other">Other.</param>
        public static bool RightAdjacent(this Interval <float> @this, Interval <float> other)
        {
            if (CrMath.AeqB(@this.LowerBound, other.UpperBound) &&
                @this.LowerBoundType == Closed &&
                other.UpperBoundType == Closed)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 12
0
        IOption GetCorrectOption(IOption optionA, float aVal, IOption optionB, float bVal)
        {
            IOption cOption;

            cOption = aVal > bVal ? optionA : optionB;
            if (CrMath.AeqB(aVal, bVal))
            {
                cOption = optionA;
            }
            return(cOption);
        }
Ejemplo n.º 13
0
        public void IsOneTest(
            [Range(-2.0f, 2.0f, 0.5f)] float value,
            [Range(-2.0f, 2.0f, 0.5f)] float weight)
        {
            var cValue  = value.Clamp01();
            var cWeight = weight.Clamp01();
            var cResult = CrMath.AeqB(cValue * cWeight, 1.0f);
            var util    = new Utility(value, weight);
            var aResult = util.IsOne;

            Assert.AreEqual(cResult, aResult);
        }
Ejemplo n.º 14
0
        public void NotEqualOperatorTest(
            [Range(0.0f, 1.0f, 0.5f)] float valueA,
            [Range(0.0f, 1.0f, 0.5f)] float weightA,
            [Range(0.0f, 1.0f, 0.5f)] float valueB,
            [Range(0.0f, 1.0f, 0.5f)] float weightB)
        {
            var cValueA  = valueA.Clamp01();
            var cWeightA = weightA.Clamp01();
            var cValueB  = valueB.Clamp01();
            var cWeightB = weightB.Clamp01();
            var cResult  = !(CrMath.AeqB(cValueA, cValueB) && CrMath.AeqB(cWeightA, cWeightB));
            var utilA    = new Utility(valueA, weightA);
            var utilB    = new Utility(valueB, weightB);
            var aResult  = utilA != utilB;

            Assert.AreEqual(cResult, aResult);
        }
Ejemplo n.º 15
0
        public void InvertedEvaluateTest(float xA, float yA, float xB, float yB, float k)
        {
            var        ptA = new Pointf(xA, yA);
            var        ptB = new Pointf(xB, yB);
            IEvaluator ev  = new SigmoidEvaluator(ptA, ptB, k);
            var        kN  = k.Clamp <float>(_minK, _maxK);
            var        yAn = yA.Clamp01();
            var        yBn = yB.Clamp01();

            ev.IsInverted = true;

            var xqArray = CrMath.LinearSpaced(_evN, xA - 0.001f * xA, xB + 0.001f * xB);

            for (int i = 0; i < _evN - 1; i++)
            {
                var dy           = yBn - yAn;
                var dx           = xB - xA;
                var dyOver2      = dy / 2.0f;
                var twoOverAbsDx = 2.0f / Math.Abs(dx);
                var meanX        = (xA + xB) / 2.0f;
                var oneMinusK    = 1.0f - kN;
                var meanY        = (yAn + yBn) / 2.0f;
                var xqMinusMeanX = xqArray[i].Clamp <float>(xA, xB) - meanX;
                var num          = twoOverAbsDx * xqMinusMeanX * oneMinusK;
                var den          = kN * (1 - 2 * Math.Abs(twoOverAbsDx * xqMinusMeanX)) + 1;
                var cVal         = 1f - (dyOver2 * (num / den) + meanY);

                var aVal = ev.Evaluate(xqArray[i]);
                Assert.That(aVal, Is.EqualTo(cVal).Within(Tolerance));
                Assert.That(aVal <= 1.0f);
                Assert.That(aVal >= 0.0f);
            }
            // Check the end points
            var utilA = ev.Evaluate(xA);
            var utilB = ev.Evaluate(xB);

            Assert.That(utilA, Is.EqualTo(1f - yAn).Within(Tolerance));
            Assert.That(utilB, Is.EqualTo(1f - yBn).Within(Tolerance));
        }
Ejemplo n.º 16
0
 public void AneqBFloatTests(float a, float b, bool expected)
 {
     Assert.That(CrMath.AneqB(a, b), Is.EqualTo(expected));
 }
Ejemplo n.º 17
0
 public void AneqBDoubleTests(double a, double b, bool expected)
 {
     Assert.That(CrMath.AneqB(a, b), Is.EqualTo(expected));
 }
Ejemplo n.º 18
0
 public void AneqZeroFloatTests(float a, bool expected)
 {
     Assert.That(CrMath.AneqZero(a), Is.EqualTo(expected));
 }
Ejemplo n.º 19
0
 public void AneqZeroDoubleTests(double a, bool expected)
 {
     Assert.That(CrMath.AneqZero(a), Is.EqualTo(expected));
 }
Ejemplo n.º 20
0
 public void LinearSpacedCountZeroOrNegativeThrows()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => CrMath.LinearSpaced(0, 1f, 2f));
     Assert.Throws <ArgumentOutOfRangeException>(() => CrMath.LinearSpaced(-1, 1f, 2f));
 }