Пример #1
0
        public void AddingAndRemovingConstraintsUpdatesValue()
        {
            var x = new ClVariable("x");

            _solver.AddConstraint(new ClLinearEquation(x, 100, ClStrength.Weak));

            var c10 = new ClLinearInequality(x, Cl.Operator.LessThanOrEqualTo, 10.0);
            var c20 = new ClLinearInequality(x, Cl.Operator.LessThanOrEqualTo, 20.0);

            _solver.AddConstraint(c10).AddConstraint(c20);
            Assert.IsTrue(Cl.Approx(x, 10.0));

            _solver.RemoveConstraint(c10);
            Assert.IsTrue(Cl.Approx(x, 20.0));

            _solver.RemoveConstraint(c20);
            Assert.IsTrue(Cl.Approx(x, 100.0));

            var c10Again = new ClLinearInequality(x, Cl.Operator.LessThanOrEqualTo, 10.0);

            _solver.AddConstraint(c10).AddConstraint(c10Again);
            Assert.IsTrue(Cl.Approx(x, 10.0));

            _solver.RemoveConstraint(c10);
            Assert.IsTrue(Cl.Approx(x, 10.0));

            _solver.RemoveConstraint(c10Again);
            Assert.IsTrue(Cl.Approx(x, 100.0));
        }
Пример #2
0
        public void AddingValueStayConstraintMakesValueNotChange()
        {
            var x = new ClVariable("x", 5);

            _solver.AddStay(x);

            Assert.IsTrue(Cl.Approx(x, 5));
        }
Пример #3
0
        public static bool JustStay1()
        {
            bool            okResult = true;
            ClVariable      x        = new ClVariable(5);
            ClVariable      y        = new ClVariable(10);
            ClSimplexSolver solver   = new ClSimplexSolver();

            solver.AddStay(x);
            solver.AddStay(y);
            okResult = okResult && Cl.Approx(x, 5);
            okResult = okResult && Cl.Approx(y, 10);

            Console.WriteLine("x == " + x.Value);
            Console.WriteLine("y == " + y.Value);

            return(okResult);
        }
Пример #4
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));
        }
Пример #5
0
        public void Casso1()
        {
            var x = new ClVariable("x");
            var y = new ClVariable("y");


            _solver
            .AddConstraint(new ClLinearInequality(x, Cl.Operator.LessThanOrEqualTo, y))
            .AddConstraint(new ClLinearEquation(y, Cl.Plus(x, 3.0)))
            .AddConstraint(new ClLinearEquation(x, 10.0, ClStrength.Weak))
            .AddConstraint(new ClLinearEquation(y, 10.0, ClStrength.Weak));

            Assert.IsTrue(
                (Cl.Approx(x, 10.0) && Cl.Approx(y, 13.0)) ||
                (Cl.Approx(x, 7.0) && Cl.Approx(y, 10.0))
                );
        }
Пример #6
0
        public static bool AddDelete1()
        {
            bool            okResult = true;
            ClVariable      x        = new ClVariable("x");
            ClSimplexSolver solver   = new ClSimplexSolver();

            solver.AddConstraint(new ClLinearEquation(x, 100, ClStrength.Weak));

            ClLinearInequality c10 = new ClLinearInequality(x, Cl.LEQ, 10.0);
            ClLinearInequality c20 = new ClLinearInequality(x, Cl.LEQ, 20.0);

            solver
            .AddConstraint(c10)
            .AddConstraint(c20);

            okResult = okResult && Cl.Approx(x, 10.0);
            Console.WriteLine("x == " + x.Value);

            solver.RemoveConstraint(c10);
            okResult = okResult && Cl.Approx(x, 20.0);
            Console.WriteLine("x == " + x.Value);

            solver.RemoveConstraint(c20);
            okResult = okResult && Cl.Approx(x, 100.0);
            Console.WriteLine("x == " + x.Value);

            ClLinearInequality c10again = new ClLinearInequality(x, Cl.LEQ, 10.0);

            solver
            .AddConstraint(c10)
            .AddConstraint(c10again);

            okResult = okResult && Cl.Approx(x, 10.0);
            Console.WriteLine("x == " + x.Value);

            solver.RemoveConstraint(c10);
            okResult = okResult && Cl.Approx(x, 10.0);
            Console.WriteLine("x == " + x.Value);

            solver.RemoveConstraint(c10again);
            okResult = okResult && Cl.Approx(x, 100.0);
            Console.WriteLine("x == " + x.Value);

            return(okResult);
        }
Пример #7
0
        public static bool Casso1()
        {
            bool            okResult = true;
            ClVariable      x        = new ClVariable("x");
            ClVariable      y        = new ClVariable("y");
            ClSimplexSolver solver   = new ClSimplexSolver();

            solver
            .AddConstraint(new ClLinearInequality(x, Cl.LEQ, y))
            .AddConstraint(new ClLinearEquation(y, Cl.Plus(x, 3.0)))
            .AddConstraint(new ClLinearEquation(x, 10.0, ClStrength.Weak))
            .AddConstraint(new ClLinearEquation(y, 10.0, ClStrength.Weak));

            okResult = okResult &&
                       (Cl.Approx(x, 10.0) && Cl.Approx(y, 13.0) ||
                        Cl.Approx(x, 7.0) && Cl.Approx(y, 10.0));

            Console.WriteLine("x == " + x.Value + ", y == " + y.Value);

            return(okResult);
        }
Пример #8
0
        public static bool AddDelete2()
        {
            bool            okResult = true;
            ClVariable      x        = new ClVariable("x");
            ClVariable      y        = new ClVariable("y");
            ClSimplexSolver solver   = new ClSimplexSolver();

            solver
            .AddConstraint(new ClLinearEquation(x, 100.0, ClStrength.Weak))
            .AddConstraint(new ClLinearEquation(y, 120.0, ClStrength.Strong));

            ClLinearInequality c10 = new ClLinearInequality(x, Cl.LEQ, 10.0);
            ClLinearInequality c20 = new ClLinearInequality(x, Cl.LEQ, 20.0);

            solver
            .AddConstraint(c10)
            .AddConstraint(c20);
            okResult = okResult && Cl.Approx(x, 10.0) && Cl.Approx(y, 120.0);
            Console.WriteLine("x == " + x.Value + ", y == " + y.Value);

            solver.RemoveConstraint(c10);
            okResult = okResult && Cl.Approx(x, 20.0) && Cl.Approx(y, 120.0);
            Console.WriteLine("x == " + x.Value + ", y == " + y.Value);

            ClLinearEquation cxy = new ClLinearEquation(Cl.Times(2.0, x), y);

            solver.AddConstraint(cxy);
            okResult = okResult && Cl.Approx(x, 20.0) && Cl.Approx(y, 40.0);
            Console.WriteLine("x == " + x.Value + ", y == " + y.Value);

            solver.RemoveConstraint(c20);
            okResult = okResult && Cl.Approx(x, 60.0) && Cl.Approx(y, 120.0);
            Console.WriteLine("x == " + x.Value + ", y == " + y.Value);

            solver.RemoveConstraint(cxy);
            okResult = okResult && Cl.Approx(x, 100.0) && Cl.Approx(y, 120.0);
            Console.WriteLine("x == " + x.Value + ", y == " + y.Value);

            return(okResult);
        }
Пример #9
0
        public void AddDelete2()
        {
            var x = new ClVariable("x");
            var y = new ClVariable("y");

            _solver
            .AddConstraint(new ClLinearEquation(x, 100.0, ClStrength.Weak))
            .AddConstraint(new ClLinearEquation(y, 120.0, ClStrength.Strong));

            var c10 = new ClLinearInequality(x, Cl.Operator.LessThanOrEqualTo, 10.0);
            var c20 = new ClLinearInequality(x, Cl.Operator.LessThanOrEqualTo, 20.0);

            _solver
            .AddConstraint(c10)
            .AddConstraint(c20);
            Assert.IsTrue(Cl.Approx(x, 10.0));
            Assert.IsTrue(Cl.Approx(y, 120.0));

            _solver.RemoveConstraint(c10);
            Assert.IsTrue(Cl.Approx(x, 20.0));
            Assert.IsTrue(Cl.Approx(y, 120.0));

            var cxy = new ClLinearEquation(Cl.Times(2.0, x), y);

            _solver.AddConstraint(cxy);
            Assert.IsTrue(Cl.Approx(x, 20.0));
            Assert.IsTrue(Cl.Approx(y, 40.0));

            _solver.RemoveConstraint(c20);
            Assert.IsTrue(Cl.Approx(x, 60.0));
            Assert.IsTrue(Cl.Approx(y, 120.0));

            _solver.RemoveConstraint(cxy);
            Assert.IsTrue(Cl.Approx(x, 100.0));
            Assert.IsTrue(Cl.Approx(y, 120.0));
        }
Пример #10
0
        protected IEnumerable <ClConstraint> GetConstraintsFromFluentLayout(IFluentLayout <T> fluentLayout)
        {
            var constraints = new List <ClConstraint>();
            ClLinearExpression firstExpression = null;

            firstExpression = GetExpressionFromViewAndAttribute(fluentLayout.View, fluentLayout.Attribute);

            ClLinearExpression secondExpression = null;

            if (fluentLayout.SecondItem != null)
            {
                secondExpression = GetExpressionFromViewAndAttribute(
                    fluentLayout.SecondItem.View,
                    fluentLayout.SecondItem.Attribute
                    );

                var multiplier = !Cl.Approx(fluentLayout.Multiplier, 0) ? fluentLayout.Multiplier : 1;
                //make sure to construct the least complicated tableau possible by avoiding needless operations
                if (!Cl.Approx(multiplier, 1))
                {
                    secondExpression = Cl.Plus(
                        Cl.Times(secondExpression, multiplier),
                        new ClLinearExpression(fluentLayout.Constant)
                        );
                }
                else if (!Cl.Approx(fluentLayout.Constant, 0))
                {
                    secondExpression = Cl.Plus(
                        secondExpression,
                        new ClLinearExpression(fluentLayout.Constant)
                        );
                }
            }
            else
            {
                secondExpression = new ClLinearExpression(fluentLayout.Constant);
            }

            ClConstraint cn       = null;
            var          strength = ClStrength.Strong;
            var          priority = fluentLayout.Priority / 1000;

            switch (fluentLayout.Relation)
            {
            case LayoutRelation.Equal:
                cn = new ClLinearEquation(
                    firstExpression,
                    secondExpression, strength, priority
                    );
                break;

            case LayoutRelation.GreaterThanOrEqual:
                cn = new ClLinearInequality(
                    firstExpression,
                    Cl.Operator.GreaterThanOrEqualTo,
                    secondExpression, strength, priority
                    );
                break;

            case LayoutRelation.LessThanOrEqual:
                cn = new ClLinearInequality(
                    firstExpression,
                    Cl.Operator.LessThanOrEqualTo,
                    secondExpression, strength, priority
                    );
                break;
            }

            constraints.Add(cn);

            return(constraints);
        }
Пример #11
0
        public static bool Multiedit()
        {
            try
            {
                bool okResult = true;

                ClVariable      x      = new ClVariable("x");
                ClVariable      y      = new ClVariable("y");
                ClVariable      w      = new ClVariable("w");
                ClVariable      h      = new ClVariable("h");
                ClSimplexSolver solver = new ClSimplexSolver();

                solver
                .AddStay(x)
                .AddStay(y)
                .AddStay(w)
                .AddStay(h);

                solver
                .AddEditVar(x)
                .AddEditVar(y)
                .BeginEdit();

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

                Console.WriteLine("x = " + x.Value + "; y = " + y.Value);
                Console.WriteLine("w = " + w.Value + "; h = " + h.Value);

                okResult = okResult &&
                           Cl.Approx(x, 10) && Cl.Approx(y, 20) &&
                           Cl.Approx(w, 0) && Cl.Approx(h, 0);

                solver
                .AddEditVar(w)
                .AddEditVar(h)
                .BeginEdit();

                solver
                .SuggestValue(w, 30)
                .SuggestValue(h, 40)
                .EndEdit();

                Console.WriteLine("x = " + x.Value + "; y = " + y.Value);
                Console.WriteLine("w = " + w.Value + "; h = " + h.Value);

                okResult = okResult &&
                           Cl.Approx(x, 10) && Cl.Approx(y, 20) &&
                           Cl.Approx(w, 30) && Cl.Approx(h, 40);

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

                Console.WriteLine("x = " + x.Value + "; y = " + y.Value);
                Console.WriteLine("w = " + w.Value + "; h = " + h.Value);

                okResult = okResult &&
                           Cl.Approx(x, 50) && Cl.Approx(y, 60) &&
                           Cl.Approx(w, 30) && Cl.Approx(h, 40);

                return(okResult);
            }
            catch (ExClRequiredFailure)
            {
                // we want this exception to get thrown
                Console.WriteLine("-- got the exception");
                return(true);
            }
        }