Ejemplo n.º 1
0
        public void EndEdit_Throws()
        {
            var value = new ClVariable("value", 0);

            var solver = new ClSimplexSolver();

            solver.AddStay(value, ClStrength.Strong);

            solver.BeginEdit(value)
                  .SuggestValue(value, 25)
                  .EndEdit(); // <- Exception raised here
        }
Ejemplo n.º 2
0
        public void Multiedit()
        {
            var x = new ClVariable("x");
            var y = new ClVariable("y");
            var w = new ClVariable("w");
            var h = new ClVariable("h");


            var e1 = _solver
                     .AddStay(x)
                     .AddStay(y)
                     .AddStay(w)
                     .AddStay(h)

                     .BeginEdit(x, y)

                     .SuggestValue(x, 10)
                     .SuggestValue(y, 20)
                     .Resolve();

            Assert.IsTrue(Cl.Approx(x, 10));
            Assert.IsTrue(Cl.Approx(y, 20));
            Assert.IsTrue(Cl.Approx(w, 0));
            Assert.IsTrue(Cl.Approx(h, 0));

            _solver
            .BeginEdit(w, h)
            .SuggestValue(w, 30)
            .SuggestValue(h, 40)
            .EndEdit();

            Assert.IsTrue(Cl.Approx(x, 10));
            Assert.IsTrue(Cl.Approx(y, 20));
            Assert.IsTrue(Cl.Approx(w, 30));
            Assert.IsTrue(Cl.Approx(h, 40));

            e1
            .SuggestValue(x, 50)
            .SuggestValue(y, 60)
            .EndEdit();

            Assert.IsTrue(Cl.Approx(x, 50));
            Assert.IsTrue(Cl.Approx(y, 60));
            Assert.IsTrue(Cl.Approx(w, 30));
            Assert.IsTrue(Cl.Approx(h, 40));
        }
Ejemplo n.º 3
0
        public void EndEdit_NonRequiredVariable_SolvesCorrectly()
        {
            //Create a solver
            var solver = new ClSimplexSolver();
            var variable = new ClVariable(0f);

            //Add a stay, indicating this var should stay at it's current value if possible
            solver.AddStay(variable, ClStrength.Strong, 1);

            //Bug Fixing Note: The stay is strong, which overpowers the SuggestValue. If the stay is Medium or Weak then the SuggestValue takes hold and this test does *not* fail!
            //tl;dr: This bug only happens if the suggest value does nothing?

            const double EXPECTED_VALUE = 10.0;

            //Suggest a value for the variable in an edit context
            var editContext = solver.BeginEdit(variable);
            editContext.SuggestValue(variable, EXPECTED_VALUE);
            editContext.EndEdit();
        }
Ejemplo n.º 4
0
 void BeginEdit()
 {
     solver.BeginEdit();
 }
Ejemplo n.º 5
0
        public void Playground()
        {
            //Implementation of this example: https://cassowary.readthedocs.org/en/latest/topics/examples.html#quadrilaterals

            var _0 = new ClPoint(10, 10);
            var _1 = new ClPoint(10, 200);
            var _2 = new ClPoint(200, 200);
            var _3 = new ClPoint(200, 10);

            var m0 = new ClPoint(0, 0);
            var m1 = new ClPoint(0, 0);
            var m2 = new ClPoint(0, 0);
            var m3 = new ClPoint(0, 0);

            //We don't want the points to move unless necessary
            _solver.AddPointStays(new[] { _0, _1, _2, _3 });

            //Define the midpoints
// ReSharper disable CompareOfFloatsByEqualityOperator
            _solver.AddConstraint(m0.X, _0.X, _1.X, (m, a, b) => m == a * 0.5 + b * 0.5);
            _solver.AddConstraint(m0.Y, _0.Y, _1.Y, (m, a, b) => m == a * 0.5 + b * 0.5);

            _solver.AddConstraint(m1.X, _1.X, _2.X, (m, a, b) => m == a * 0.5 + b * 0.5);
            _solver.AddConstraint(m1.Y, _1.Y, _2.Y, (m, a, b) => m == a * 0.5 + b * 0.5);

            _solver.AddConstraint(m2.X, _2.X, _3.X, (m, a, b) => m == a * 0.5 + b * 0.5);
            _solver.AddConstraint(m2.Y, _2.Y, _3.Y, (m, a, b) => m == a * 0.5 + b * 0.5);

            _solver.AddConstraint(m3.X, _3.X, _0.X, (m, a, b) => m == a * 0.5 + b * 0.5);
            _solver.AddConstraint(m3.Y, _3.Y, _0.Y, (m, a, b) => m == a * 0.5 + b * 0.5);
// ReSharper restore CompareOfFloatsByEqualityOperator

            //Make sure left stays left and top stays top
            _solver.AddConstraint(_0.X, _2.X, (a, b) => a + 20 <= b);
            _solver.AddConstraint(_0.X, _3.X, (a, b) => a + 20 <= b);

            _solver.AddConstraint(_1.X, _2.X, (a, b) => a + 20 <= b);
            _solver.AddConstraint(_1.X, _3.X, (a, b) => a + 20 <= b);

            _solver.AddConstraint(_0.Y, _1.Y, (a, b) => a + 20 <= b);
            _solver.AddConstraint(_0.Y, _2.Y, (a, b) => a + 20 <= b);

            _solver.AddConstraint(_3.Y, _1.Y, (a, b) => a + 20 <= b);
            _solver.AddConstraint(_3.Y, _2.Y, (a, b) => a + 20 <= b);

            //Make sure all points stay in 500x500 convas
            _solver.AddConstraint(_0.X, a => a >= 0);
            _solver.AddConstraint(_0.Y, a => a >= 0);
            _solver.AddConstraint(_0.X, a => a <= 500);
            _solver.AddConstraint(_0.Y, a => a <= 500);

            Console.WriteLine(m0.X.Value + " " + m0.Y.Value);
            Console.WriteLine(m1.X.Value + " " + m1.Y.Value);
            Console.WriteLine(m2.X.Value + " " + m2.Y.Value);
            Console.WriteLine(m3.X.Value + " " + m3.Y.Value);

            _solver.BeginEdit(_2.X, _2.Y)
            .SuggestValue(_2.X, 300)
            .SuggestValue(_2.Y, 400)
            .EndEdit();

            Console.WriteLine(m0.X.Value + " " + m0.Y.Value);
            Console.WriteLine(m1.X.Value + " " + m1.Y.Value);
            Console.WriteLine(m2.X.Value + " " + m2.Y.Value);
            Console.WriteLine(m3.X.Value + " " + m3.Y.Value);
        }