Exemplo n.º 1
0
        public void RemoveLayoutConstraint(int ndx)
        {
            Constraint c = (Constraint)Constraints[ndx];

            solver.RemoveConstraint(c.constraint);
            //TODO: Determine if target controls need to be in Controls, ControlVariables, VarContraints
            Constraints.RemoveAt(ndx);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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));
        }