Exemple #1
0
        public void AddingConstraintToMakeTwoVariablesEqualMakesVariablesEqual()
        {
            var x = new ClVariable("x", 167);
            var y = new ClVariable("y", 2);

            var eq = new ClLinearEquation(x, new ClLinearExpression(y));

            _solver.AddConstraint(eq);

            Assert.AreEqual(x.Value, y.Value);
        }
Exemple #2
0
        protected void SetValue(ClVariable v, double x, ClStrength s)
        {
            // TODO: Find a better way then manually adding/removing constriants.
            if (VarConstraints.ContainsKey(v.Name))
            {
                ClLinearEquation eq = (ClLinearEquation)VarConstraints[v.Name];
                solver.RemoveConstraint(eq);
                VarConstraints.Remove(v.Name);
            }
            ClLinearEquation eq2 = new ClLinearEquation(v, new ClLinearExpression(x), s);

            solver.AddConstraint(eq2);
            VarConstraints.Add(v.Name, eq2);
        }
Exemple #3
0
        public static bool Simple1()
        {
            bool            okResult = true;
            ClVariable      x        = new ClVariable(167);
            ClVariable      y        = new ClVariable(2);
            ClSimplexSolver solver   = new ClSimplexSolver();

            ClLinearEquation eq = new ClLinearEquation(x, new ClLinearExpression(y));

            solver.AddConstraint(eq);
            okResult = (x.Value == y.Value);

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

            return(okResult);
        }
Exemple #4
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);
        }
Exemple #5
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));
        }
        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);
        }
Exemple #7
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);
        }
Exemple #8
0
        public void AddDel()
        {
            const int nCns      = 450;
            const int nVars     = 450;
            const int nResolves = 5000;

            var          timer    = new Stopwatch();
            const double ineqProb = 0.12;
            const int    maxVars  = 3;

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

            timer.Start();


            var rgpclv = new ClVariable[nVars];

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

            var rgpcns = new ClConstraint[nCns];
            int j;

            for (j = 0; j < nCns; j++)
            {
                // number of variables in this constraint
                var nvs  = RandomInRange(1, maxVars);
                var expr = new ClLinearExpression(UniformRandomDiscretized() * 20.0 - 10.0);
                int k;
                for (k = 0; k < nvs; k++)
                {
                    var coeff = UniformRandomDiscretized() * 10 - 5;
                    var 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);
                }
            }

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

            for (j = 0; j < nCns; j++)
            {
                // add the constraint -- if it's incompatible, just ignore it
                try
                {
                    _solver.AddConstraint(rgpcns[j]);
                }
                catch (CassowaryRequiredFailureException)
                {
                    cExceptions++;
                    rgpcns[j] = null;
                }
            }
            Console.WriteLine("done adding constraints [" + cExceptions + " exceptions]");
            Console.WriteLine("time = " + timer.Elapsed + "\n");
            timer.Start();

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

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

            var edit1 = new ClEditConstraint(rgpclv[e1Index], ClStrength.Strong);
            var 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.Elapsed + "\n");
            timer.Start();

            //for (var 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.Elapsed + "\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.Elapsed + "\n");

            timer.Start();
        }