Example #1
0
 public EdgeConstraints(EdgeInsets insets)
 {
     Top    = insets.Top;
     Right  = insets.Right;
     Bottom = insets.Bottom;
     Left   = insets.Left;
 }
Example #2
0
 void Factor(out ClLinearExpression e)
 {
     e = null; ClDouble d; ClVariable v; bool negate = false;
     if (la.kind == 7)
     {
         Get();
         negate = true;
     }
     if (la.kind == 13)
     {
         Number(out d);
         e = new ClLinearExpression(d.Value);
     }
     else if (la.kind == 12)
     {
         Variable(out v);
         e = new ClLinearExpression(v);
     }
     else if (la.kind == 10)
     {
         Get();
         Expression(out e);
         Expect(11);
     }
     else
     {
         SynErr(16);
     }
     if (negate)
     {
         e = Cl.Minus(0, e);
     }
 }
Example #3
0
 public EdgeConstraints(ClLinearExpression top = null, ClLinearExpression right = null, ClLinearExpression bottom = null, ClLinearExpression left = null)
 {
     Top    = top;
     Right  = right;
     Bottom = bottom;
     Left   = left;
 }
        public ClLinearInequality(ClLinearExpression cle1, Cl.Operator op, ClLinearExpression cle2, ClStrength strength, double weight = 1.0)
            : base(cle2.Clone(), strength, weight)
            /* throws ExClInternalError */
        {
            const double EPSILON = 1e-8;

            switch (op)
            {
            case Cl.Operator.GreaterThanOrEqualTo:
                SetupGreaterThanOrEqualTo(cle1);
                break;

            case Cl.Operator.GreaterThan:
                SetupGreaterThanOrEqualTo(cle1);
                ExpressionField.IncrementConstant(-EPSILON);
                break;

            case Cl.Operator.LessThanOrEqualTo:
                SetupLessThanOrEqualTo(cle1);
                break;

            case Cl.Operator.LessThan:
                SetupLessThanOrEqualTo(cle1);
                ExpressionField.IncrementConstant(-EPSILON);
                break;

            default:
                // invalid operator
                throw new CassowaryInternalException("Invalid operator in ClLinearInequality constructor");
            }
        }
Example #5
0
 public EdgeConstraints(double margin)
 {
     Top    = margin;
     Right  = margin;
     Bottom = margin;
     Left   = margin;
 }
 protected ClEditOrStayConstraint(ClVariable var,
                                  ClStrength strength,
                                  double weight = 1.0)
     : base(strength, weight)
 {
     _variable   = var;
     _expression = new ClLinearExpression(_variable, -1.0, _variable.Value);
 }
        protected ClLinearExpression GetExpressionFromViewAndAttribute(T view, LayoutAttribute attribute /*, out ClLinearEquation egality*/)
        {
            if (HasVariableForViewAndAttribute(view, attribute))
            {
                return(new ClLinearExpression(GetVariableFromViewAndAttribute(view, attribute)));
            }

            var compositeVariable = GetCompositeVariableFromViewAndAttribute(view, attribute);

            if (compositeVariable != null)
            {
                return(new ClLinearExpression(compositeVariable));
            }

            var variable   = GetVariableFromViewAndAttribute(view, attribute);
            var expression = new ClLinearExpression(variable);

            var greaterZero = new LayoutAttribute[] {
                LayoutAttribute.Width,
                LayoutAttribute.Height,
                LayoutAttribute.Top,
                LayoutAttribute.Bottom,
                LayoutAttribute.Left,
                LayoutAttribute.Right
            };

            if (greaterZero.Contains(attribute))
            {
                solver.AddConstraint(new ClLinearInequality(
                                         expression,
                                         Cl.Operator.GreaterThanOrEqualTo,
                                         new ClLinearExpression(0)
                                         ));
            }


            if (attribute == LayoutAttribute.Left || attribute == LayoutAttribute.Right)
            {
                solver.AddConstraint(new ClLinearInequality(
                                         GetVariableFromViewAndAttribute(view, LayoutAttribute.Right),
                                         Cl.Operator.GreaterThanOrEqualTo,
                                         GetVariableFromViewAndAttribute(view, LayoutAttribute.Left)
                                         ));
            }
            if (attribute == LayoutAttribute.Top || attribute == LayoutAttribute.Bottom)
            {
                solver.AddConstraint(new ClLinearInequality(
                                         GetVariableFromViewAndAttribute(view, LayoutAttribute.Bottom),
                                         Cl.Operator.GreaterThanOrEqualTo,
                                         GetVariableFromViewAndAttribute(view, LayoutAttribute.Top)
                                         ));
            }

            return(expression);
        }
Example #8
0
        internal ClLinearConstraint Materialize(Dictionary <string, object> objectsMap)
        {
            ClLinearExpression expression = new ClLinearExpression();

            foreach (var term in Constraint.Terms)
            {
                if (term.OwnerId != null)
                {
                    var anchor = GetAnchor(term.OwnerId, term.AnchorName, objectsMap);
                    expression.AddVariable(anchor, term.Multiplier);
                }
                else
                {
                    expression.Constant += term.Multiplier;
                }
            }

            return(new ClLinearConstraint(expression, Constraint.Operator, Strength));
        }
Example #9
0
 void Expression(out ClLinearExpression e)
 {
     e = null; ClLinearExpression e1;
     Term(out e);
     while (la.kind == 6 || la.kind == 7)
     {
         if (la.kind == 6)
         {
             Get();
             Term(out e1);
             e = Cl.Plus(e, e1);
         }
         else
         {
             Get();
             Term(out e1);
             e = Cl.Minus(e, e1);
         }
     }
 }
Example #10
0
 void Term(out ClLinearExpression e)
 {
     e = null; ClLinearExpression e1;
     Factor(out e);
     while (la.kind == 8 || la.kind == 9)
     {
         if (la.kind == 8)
         {
             Get();
             Factor(out e1);
             e = Cl.Times(e, e1);
         }
         else
         {
             Get();
             Factor(out e1);
             e = Cl.Divide(e, e1);
         }
     }
 }
 public ClLinearConstraint(ClLinearExpression cle, ClStrength strength, double weight)
     : base(strength, weight)
 {
     ExpressionField = cle;
 }
 public ClLinearEquation(ClLinearExpression cle1, ClLinearExpression cle2)
     : this(cle1, cle2, ClStrength.Required)
 {
 }
 public ClLinearEquation(ClLinearExpression cle1, ClLinearExpression cle2, ClStrength strength, double weight = 1.0)
     : base(cle1.Clone(), strength, weight)
 {
     ExpressionField.AddExpression(cle2, -1.0);
 }
 public ClLinearEquation(ClLinearExpression cle, ClAbstractVariable clv)
     : this(cle, clv, ClStrength.Required)
 {
 }
 public ClLinearInequality(ClLinearExpression cle, Cl.Operator op, ClAbstractVariable clv, ClStrength strength, double weight = 1.0)
     : this(cle, op, new ClLinearExpression(clv), strength, weight)
     /* throws ExClInternalError */
 {
 }
 protected void SetExpression(ClLinearExpression expr)
 {
     ExpressionField = expr;
 }
 private void SetupLessThanOrEqualTo(ClLinearExpression expr)
 {
     ExpressionField.AddExpression(expr, -1.0);
 }
 public ClLinearInequality(ClLinearExpression cle, ClStrength strength, double weight)
     : base(cle, strength, weight)
 {
 }
 public ClLinearInequality(ClLinearExpression cle, ClStrength strength)
     : base(cle, strength)
 {
 }
        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);
        }
Example #21
0
        public static bool AddDel(int nCns, int nVars, int nResolves)
        {
            Timer  timer    = new Timer();
            double ineqProb = 0.12;
            int    maxVars  = 3;

            Console.WriteLine("starting timing test. nCns = " + nCns +
                              ", nVars = " + nVars + ", nResolves = " + nResolves);

            timer.Start();
            ClSimplexSolver solver = new ClSimplexSolver();

            ClVariable[] rgpclv = new ClVariable[nVars];
            for (int i = 0; i < nVars; i++)
            {
                rgpclv[i] = new ClVariable(i, "x");
                solver.AddStay(rgpclv[i]);
            }

            ClConstraint[] rgpcns = new ClConstraint[nCns];
            int            nvs    = 0;
            int            k;
            int            j;
            double         coeff;

            for (j = 0; j < nCns; j++)
            {
                // number of variables in this constraint
                nvs = RandomInRange(1, maxVars);
                ClLinearExpression expr = new ClLinearExpression(UniformRandomDiscretized() * 20.0 - 10.0);
                for (k = 0; k < nvs; k++)
                {
                    coeff = UniformRandomDiscretized() * 10 - 5;
                    int iclv = (int)(UniformRandomDiscretized() * nVars);
                    expr.AddExpression(Cl.Times(rgpclv[iclv], coeff));
                }
                if (UniformRandomDiscretized() < ineqProb)
                {
                    rgpcns[j] = new ClLinearInequality(expr);
                }
                else
                {
                    rgpcns[j] = new ClLinearEquation(expr);
                }
                if (Trace)
                {
                    TracePrint("Constraint " + j + " is " + rgpcns[j]);
                }
            }

            Console.WriteLine("done building data structures");
            Console.WriteLine("time = " + timer.ElapsedTime);
            timer.Start();
            int cExceptions = 0;

            for (j = 0; j < nCns; j++)
            {
                // add the constraint -- if it's incompatible, just ignore it
                try
                {
                    solver.AddConstraint(rgpcns[j]);
                }
                catch (ExClRequiredFailure)
                {
                    cExceptions++;
                    if (Trace)
                    {
                        TracePrint("got exception adding " + rgpcns[j]);
                    }

                    rgpcns[j] = null;
                }
            }
            Console.WriteLine("done adding constraints [" + cExceptions + " exceptions]");
            Console.WriteLine("time = " + timer.ElapsedTime + "\n");
            timer.Start();

            int e1Index = (int)(UniformRandomDiscretized() * nVars);
            int e2Index = (int)(UniformRandomDiscretized() * nVars);

            Console.WriteLine("indices " + e1Index + ", " + e2Index);

            ClEditConstraint edit1 = new ClEditConstraint(rgpclv[e1Index], ClStrength.Strong);
            ClEditConstraint edit2 = new ClEditConstraint(rgpclv[e2Index], ClStrength.Strong);

            solver
            .AddConstraint(edit1)
            .AddConstraint(edit2);

            Console.WriteLine("done creating edit constraints -- about to start resolves");
            Console.WriteLine("time = " + timer.ElapsedTime + "\n");
            timer.Start();

            for (int m = 0; m < nResolves; m++)
            {
                solver.Resolve(rgpclv[e1Index].Value * 1.001,
                               rgpclv[e2Index].Value * 1.001);
            }

            Console.WriteLine("done resolves -- now removing constraints");
            Console.WriteLine("time = " + timer.ElapsedTime + "\n");

            solver.RemoveConstraint(edit1);
            solver.RemoveConstraint(edit2);

            timer.Start();

            for (j = 0; j < nCns; j++)
            {
                if (rgpcns[j] != null)
                {
                    solver.RemoveConstraint(rgpcns[j]);
                }
            }

            Console.WriteLine("done removing constraints and AddDel timing test");
            Console.WriteLine("time = " + timer.ElapsedTime + "\n");

            timer.Start();

            return(true);
        }
 public ClLinearInequality(ClLinearExpression cle)
     : base(cle)
 {
 }
 public ClLinearInequality(ClLinearExpression cle, Cl.Operator op, ClAbstractVariable clv)
     : this(cle, op, clv, ClStrength.Required)
     /* throws ExClInternalError */
 {
 }
 public ClLinearConstraint(ClLinearExpression cle, ClStrength strength)
     : base(strength, 1.0)
 {
     ExpressionField = cle;
 }
 public ClLinearConstraint(ClLinearExpression cle)
     : base(ClStrength.Required, 1.0)
 {
     ExpressionField = cle;
 }
 public ClLinearEquation(ClLinearExpression cle, ClStrength strength)
     : base(cle, strength)
 {
 }
 public ClLinearEquation(ClLinearExpression cle)
     : base(cle)
 {
 }
 public ClLinearEquation(ClLinearExpression cle, ClAbstractVariable clv, ClStrength strength, double weight = 1.0)
     : base(cle.Clone(), strength, weight)
 {
     ExpressionField.AddVariable(clv, -1.0);
 }
 public ClLinearEquation(ClLinearExpression cle, ClStrength strength, double weight)
     : base(cle, strength, weight)
 {
 }
 public ClLinearInequality(ClLinearExpression cle1, Cl.Operator op, ClLinearExpression cle2)
     : this(cle1, op, cle2, ClStrength.Required)
     /* throws ExClInternalError */
 {
 }