public void OrthonormalBasisTest()
        {
            var p = new CSP();

            p.MaxSteps = 10000000;
            var v1 = new Vector3Variable("v1", p, box);
            var v2 = new Vector3Variable("v2", p, box);
            var v3 = new Vector3Variable("v3", p, box);

            v1.Magnitude.MustEqual(1);
            v2.Magnitude.MustEqual(1);
            v3.Magnitude.MustEqual(1);
            v1.MustBePerpendicular(v2);
            v2.MustBePerpendicular(v3);
            v3.MustBePerpendicular(v1);

            for (int count = 0; count < 10; count++)
            {
                p.NewSolution();
                double magnitude = Math.Sqrt(v1.X * v1.X + v1.Y * v1.Y + v1.Z * v1.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
                double dotProduct = v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: " + dotProduct);

                magnitude = Math.Sqrt(v2.X * v2.X + v2.Y * v2.Y + v2.Z * v2.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
                dotProduct = v2.X * v3.X + v2.Y * v3.Y + v2.Z * v3.Z;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: " + dotProduct);

                magnitude = Math.Sqrt(v3.X * v3.X + v3.Y * v3.Y + v3.Z * v3.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
                dotProduct = v3.X * v1.X + v3.Y * v1.Y + v3.Z * v1.Z;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: " + dotProduct);
            }
        }
Beispiel #2
0
        public void UnconstrainedSumTest()
        {
            var p   = new CSP();
            var a   = new FloatVariable("a", p, 0, 1);
            var b   = new FloatVariable("b", p, 0, 1);
            var sum = a + b;

            for (int i = 0; i < 1000; i++)
            {
                p.NewSolution();
                Assert.IsTrue(MathUtil.NearlyEqual(sum.UniqueValue, (a.UniqueValue + b.UniqueValue)));
            }
        }
        public void UnitVectorTest()
        {
            var p = new CSP();
            var v = new Vector3Variable("v", p, box);

            v.Magnitude.MustEqual(1);

            for (int count = 0; count < 1000; count++)
            {
                p.NewSolution();
                double magnitude = Math.Sqrt(v.X * v.X + v.Y * v.Y + v.Z * v.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
            }
        }
        public void DotProductOneVectorFixedTest()
        {
            var p = new CSP();
            var eX = new Vector3Variable("eX", p, 1, 0, 0);
            var unknown = new Vector3Variable("unknown", p, box);
            var dot = Vector3Variable.Dot(eX, unknown);
            dot.MustEqual(0);

            for (int count = 0; count < 1000; count++)
            {
                p.NewSolution();
                Assert.IsTrue(MathUtil.NearlyEqual(unknown.X.UniqueValue, 0));
            }
        }
        public void DotProductOneVectorFixedTest()
        {
            var p       = new CSP();
            var eX      = new Vector3Variable("eX", p, 1, 0, 0);
            var unknown = new Vector3Variable("unknown", p, box);
            var dot     = Vector3Variable.Dot(eX, unknown);

            dot.MustEqual(0);

            for (int count = 0; count < 1000; count++)
            {
                p.NewSolution();
                Assert.IsTrue(MathUtil.NearlyEqual(unknown.X.UniqueValue, 0));
            }
        }
        public void DotProductTest()
        {
            var p = new CSP();
            var v1 = new Vector3Variable("v1", p, box);
            var v2 = new Vector3Variable("v2", p, box);
            var dot = Vector3Variable.Dot(v1, v2);
            dot.MustEqual(0);

            for (int count = 0; count < 1000; count++)
            {
                p.NewSolution();
                double dotProduct = v1.X.UniqueValue * v2.X.UniqueValue + v1.Y.UniqueValue * v2.Y.UniqueValue + v1.Z.UniqueValue * v2.Z.UniqueValue;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: "+dotProduct);
            }
        }
        public void DotProductTest()
        {
            var p   = new CSP();
            var v1  = new Vector3Variable("v1", p, box);
            var v2  = new Vector3Variable("v2", p, box);
            var dot = Vector3Variable.Dot(v1, v2);

            dot.MustEqual(0);

            for (int count = 0; count < 1000; count++)
            {
                p.NewSolution();
                double dotProduct = v1.X.UniqueValue * v2.X.UniqueValue + v1.Y.UniqueValue * v2.Y.UniqueValue + v1.Z.UniqueValue * v2.Z.UniqueValue;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: " + dotProduct);
            }
        }
Beispiel #8
0
        public void QuadraticTest()
        {
            var  p    = new CSP();
            var  a    = new FloatVariable("a", p, -100, 100);
            var  b    = new FloatVariable("b", p, -100, 100);
            var  quad = (a ^ 2) + b;
            bool fail = false;

            quad.NarrowTo(new Interval(10, 20), ref fail);

            for (int i = 0; i < 1000; i++)
            {
                p.NewSolution();
                Assert.IsTrue(MathUtil.NearlyEqual(quad.UniqueValue, (a.UniqueValue * a.UniqueValue + b.UniqueValue)));
            }
        }
        public void DotProductTest()
        {
            var p = new CSP();
            var v1 = new Vector3Variable("v1", p, box);
            var v2 = new Vector3Variable("v2", p, box);
            var dot = Vector3Variable.Dot(v1, v2);
            dot.MustEqual(0);

            for (int count = 0; count < 1000; count++)
            {
                try {
                    p.NewSolution ();
                }
                catch (Exception e) {
                    Console.Write ("Finding a new solution failed due to: ");
                    Console.WriteLine (e);
                    Assert.Fail ();
                }

                double dotProduct = v1.X.UniqueValue * v2.X.UniqueValue + v1.Y.UniqueValue * v2.Y.UniqueValue + v1.Z.UniqueValue * v2.Z.UniqueValue;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: "+dotProduct);
            }
        }
Beispiel #10
0
        public void EqualityConstraintTest()
        {
            {
                var p = new CSP();
                var a = new FloatVariable("a", p, 0, 1);
                var b = new FloatVariable("b", p, 0, 1);
                var c = new FloatVariable("c", p);
                c.MustEqual(b);
                var sum = a + b;
                sum.MustEqual(1);

                for (int i = 0; i < 10; i++)
                {
                    p.NewSolution();
                    Assert.IsTrue(MathUtil.NearlyEqual(sum.UniqueValue, (a.UniqueValue + b.UniqueValue)));
                    Assert.AreEqual(c.Value, b.Value);
                }
            }

            {
                var p = new CSP();
                var a = new FloatVariable("a", p, 0, 1);
                var b = new FloatVariable("b", p, 0, 1);
                var c = new FloatVariable("c", p);
                b.MustEqual(c);
                var sum = a + b;
                sum.MustEqual(1);

                for (int i = 0; i < 10; i++)
                {
                    p.NewSolution();
                    Assert.IsTrue(MathUtil.NearlyEqual(sum.UniqueValue, (a.UniqueValue + b.UniqueValue)));
                    Assert.AreEqual(c.Value, b.Value);
                }
            }
        }
Beispiel #11
0
    /// <summary>
    /// Finds a set of values for the variables.
    /// Call this to manually randomize if SolveOnLevelLoad is set to false.
    /// </summary>
    public void Solve()
    {
        if (CSP == null)
        {
            this.MakeCSP();
        }

        int  retries = 0;
        bool done    = false;

        timer.Reset();
        timer.Start();

        while (!done)
        {
            try
            {
                // ReSharper disable once PossibleNullReferenceException
                CSP.NewSolution();
                done = true;
            }
            catch (TimeoutException)
            {
                if (retries++ == MaxRestarts)
                {
                    // Give up
                    throw;
                }
            }
        }

        timer.Stop();
        LastSolveTime = (1000000f * timer.ElapsedTicks) / Stopwatch.Frequency;
        LastSolution  = Variables.Select(v => v.ToString()).ToArray();
        WriteValuesToLinkedComponents();
    }
        public void EqualityConstraintTest()
        {
            {
                var p = new CSP();
                var a = new FloatVariable("a", p, 0, 1);
                var b = new FloatVariable("b", p, 0, 1);
                var c = new FloatVariable("c", p);
                c.MustEqual(b);
                var sum = a + b;
                sum.MustEqual(1);

                for (int i = 0; i < 10; i++)
                {
                    p.NewSolution();
                    Assert.IsTrue(MathUtil.NearlyEqual(sum.UniqueValue, (a.UniqueValue + b.UniqueValue)));
                    Assert.AreEqual(c.Value, b.Value);
                }
            }

            {
                var p = new CSP();
                var a = new FloatVariable("a", p, 0, 1);
                var b = new FloatVariable("b", p, 0, 1);
                var c = new FloatVariable("c", p);
                b.MustEqual(c);
                var sum = a + b;
                sum.MustEqual(1);

                for (int i = 0; i < 10; i++)
                {
                    p.NewSolution();
                    Assert.IsTrue(MathUtil.NearlyEqual(sum.UniqueValue, (a.UniqueValue + b.UniqueValue)));
                    Assert.AreEqual(c.Value, b.Value);
                }
            }
        }
        public void OrthonormalBasisTest()
        {
            var p = new CSP();
            p.MaxSteps = 10000000;
            var v1 = new Vector3Variable("v1", p, box);
            var v2 = new Vector3Variable("v2", p, box);
            var v3 = new Vector3Variable("v3", p, box);
            v1.Magnitude.MustEqual(1);
            v2.Magnitude.MustEqual(1);
            v3.Magnitude.MustEqual(1);
            v1.MustBePerpendicular(v2);
            v2.MustBePerpendicular(v3);
            v3.MustBePerpendicular(v1);

            for (int count = 0; count < 10; count++)
            {
                try {
                    p.NewSolution ();
                }
                catch (Exception e) {
                    Console.Write ("Finding a new solution failed due to: ");
                    Console.WriteLine (e);
                    Assert.Fail ();
                }

                double magnitude = Math.Sqrt(v1.X * v1.X + v1.Y * v1.Y + v1.Z * v1.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
                double dotProduct = v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: " + dotProduct);

                magnitude = Math.Sqrt(v2.X * v2.X + v2.Y * v2.Y + v2.Z * v2.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
                dotProduct = v2.X * v3.X + v2.Y * v3.Y + v2.Z * v3.Z;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: " + dotProduct);

                magnitude = Math.Sqrt(v3.X * v3.X + v3.Y * v3.Y + v3.Z * v3.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
                dotProduct = v3.X * v1.X + v3.Y * v1.Y + v3.Z * v1.Z;
                Assert.IsTrue(MathUtil.NearlyEqual(dotProduct, 0), "Dot product not zero; was: " + dotProduct);
            }
        }
        public void UnitVectorTest()
        {
            var p = new CSP();
            var v = new Vector3Variable("v", p, box);
            v.Magnitude.MustEqual(1);

            for (int count = 0; count < 1000; count++)
            {
                p.NewSolution();
                double magnitude = Math.Sqrt(v.X*v.X+v.Y*v.Y+v.Z*v.Z);
                Assert.IsTrue(MathUtil.NearlyEqual(magnitude, 1), "Magnitude not unity; was: " + magnitude);
            }
        }
        public void UnconstrainedSumTest()
        {
            var p = new CSP();
            var a = new FloatVariable("a", p, 0, 1);
            var b = new FloatVariable("b", p, 0, 1);
            var sum = a + b;

            for (int i = 0; i < 1000; i++)
            {
                p.NewSolution();
                Assert.IsTrue(MathUtil.NearlyEqual(sum.UniqueValue, (a.UniqueValue + b.UniqueValue)));
            }
        }
        public void QuadraticTest()
        {
            var p = new CSP();
            var a = new FloatVariable("a", p, -100, 100);
            var b = new FloatVariable("b", p, -100, 100);
            var quad = (a^2) + b;
            bool fail = false;
            quad.NarrowTo(new Interval(10, 20), ref fail);

            for (int i = 0; i < 1000; i++)
            {
                p.NewSolution();
                Assert.IsTrue(MathUtil.NearlyEqual(quad.UniqueValue, (a.UniqueValue * a.UniqueValue + b.UniqueValue)));
            }
        }