Ejemplo n.º 1
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.º 2
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));
        }
Ejemplo n.º 3
0
        public void LinearSpacedCountOneReturnsEnd()
        {
            var ls = CrMath.LinearSpaced(1, 1f, 2f);

            Assert.That(ls[0], Is.EqualTo(2.0f));
            Assert.That(ls.Length == 1);
        }
        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.º 5
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.º 6
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.º 7
0
 public void LinearSpacedCountZeroOrNegativeThrows()
 {
     Assert.Throws <ArgumentOutOfRangeException>(() => CrMath.LinearSpaced(0, 1f, 2f));
     Assert.Throws <ArgumentOutOfRangeException>(() => CrMath.LinearSpaced(-1, 1f, 2f));
 }