Exemple #1
0
        private void SetupQueenProblem()
        {
            X      = new Bdd[N, N];
            queens = new Bdd(true);
            /* Build variable array */
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    X[i, j] = new Bdd(i * N + j);
                }
            }

            /* Place a queen in each colum */
            for (int i = 0; i < N; i++)
            {
                Bdd q = new Bdd(false);
                for (int j = 0; j < N; j++)
                {
                    q = Kernel.Or(q, X[i, j]);
                }
                queens = Kernel.And(queens, q);
            }

            /* Build requirements for each variable(field) */
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    BuildQueenProblem(i, j);
                }
            }
        }
Exemple #2
0
        public Bdd Transitions(Bdd[] t, Bdd[] tp, Bdd[] h, Bdd[] hp, Bdd[] c, Bdd[] cp)
        {
            Bdd P, E2, E;
            Bdd T = new Bdd(false);

            for (int i = 0; i < N; i++)
            {
                //P = ((c[i] > cp[i]) & (tp[i] > t[i]) & hp[i] & A(c, cp, i)
                // & A(t, tp, i) & A(h, hp, i))
                // | ((h[i] > hp[i]) & cp[(i + 1) % N] & A(c, cp, (i + 1) % N) & A(h, hp, i)
                // & A(t, tp, N));

                P = Kernel.Or(Kernel.And(Kernel.And(Kernel.And(Kernel.And
                                                                   (Kernel.And(Kernel.Greater(c[i], cp[i]), Kernel.Greater(tp[i], t[i])),
                                                                   hp[i]), Assign(c, cp, i)), Assign(t, tp, i)), Assign(h, hp, i)),
                              Kernel.And(Kernel.And(Kernel.And(Kernel.And(Kernel.Greater(h[i], hp[i]),
                                                                          cp[(i + 1) % N]), Assign(c, cp, (i + 1) % N)), Assign(h, hp, i)), Assign(t, tp, N)));

                //P2 = Kernel.Or(Kernel.And(Kernel.And(Kernel.And(Kernel.And(
                //    Kernel.And(Kernel.And(Kernel.And(c[i], Kernel.Not(t[i])), tp[i]), Kernel.Not(cp[i])), hp[i]),
                //    Assign(c, cp, i)), Assign(t, tp, i)), Assign(h, hp, i)),
                //    Kernel.And(Kernel.And(Kernel.And(Kernel.And(Kernel.And(h[i], Kernel.Not(hp[i])), cp[(i % N) + 1]),
                //    Assign(c, cp, (i % N) + 1)), Assign(h, hp, i)), Assign(t, tp, N)));

                //Denne E er implementeret efter forskrifterne i noterne
                //E = Kernel.And(Kernel.And(t[i], Kernel.Not(tp[i])), Assign(t, tp, i));

                //Denne E er implementeret ligesom i Milners.cxx i BuDDy
                E2 = Kernel.And(Kernel.And(Kernel.And(Kernel.And(t[i], Kernel.Not(tp[i])),
                                                      Assign(t, tp, i)), Assign(c, cp, N)), Assign(h, hp, N));

                T = Kernel.Or(T, Kernel.Or(P, E2));
            }
            return(T);
        }
Exemple #3
0
        public Bdd InitialState(Bdd[] t, Bdd[] h, Bdd[] c)
        {
            Bdd I = Kernel.And(Kernel.Not(t[0]), Kernel.And(c[0], Kernel.Not(h[0])));

            for (int i = 1; i < N; i++)
            {
                I = Kernel.And(I, Kernel.And(Kernel.And(Kernel.Not(c[i]), Kernel.Not(h[i])), Kernel.Not(t[i])));
            }
            return(I);
        }
Exemple #4
0
        public void CompactAnySatTest()
        {
            Bdd    u1       = new Bdd(1);
            Bdd    u2       = new Bdd(2);
            Bdd    result   = Kernel.And(u1, u2);
            string expected = "11";
            string actual;

            actual = BddSharp.Kernel.Kernel.CompactAnySat(result);

            Assert.AreEqual(expected, actual, "BddSharp.Kernel.Kernel.CompactAnySat did not return the expected value.");
        }
Exemple #5
0
        public Bdd Assign(Bdd[] x, Bdd[] xp, int n)
        {
            Bdd Result = new Bdd(true);

            for (int i = 0; i < N; i++)
            {
                if (i != n)
                {
                    Result = Kernel.And(Result, Kernel.Equal(x[i], xp[i]));
                }
            }
            return(Result);
        }
        public void BuildTransitions()
        {
            T = new Bdd(false);
            Bdd temp;

            for (int i = 0; i < N; i++)
            {
                int j = (i + 1) % N;
                temp = Kernel.And(FddKernel.Equal(F, i), FddKernel.Equal(Fp, j));

                T |= temp;
                transList.Add(temp, String.Format("from {0} to {1}", i, j));
            }
        }
Exemple #7
0
        public static Bdd ReachableStates(Bdd I, Bdd T, List <int> PreState, List <int> PostState)
        {
            Bdd Rp, tmp, t;
            Bdd R = new Bdd(false);

            do
            {
                Rp  = R;
                tmp = Kernel.And(T, R);
                t   = Kernel.Exists(PreState, tmp);

                R = Kernel.Or(I, Kernel.Compose(t, PreState, PostState));
            }while (!R.Equals(Rp));
            return(R);
        }
Exemple #8
0
        private void solve_Click(object sender, EventArgs e)
        {
            Kernel.Setup();
            Fdd[] nodeArray = new Fdd[81];
            for (int i = 0; i < 81; i++)
            {
                nodeArray[i] = new Fdd(9);
            }

            //vertical xors
            Bdd b = new Bdd(true);

            //horisontal xors
            Bdd a = new Bdd(true);

            for (int i = 0; i < 9; i++)
            {
                for (int j = 1; j <= 8; j++)
                {
                    for (int k = j; k < 9; k++)
                    {
                        a = Kernel.And(a, FddKernel.Xor(nodeArray[i * 9 + j], nodeArray[i * 9 + k]));
                        b = Kernel.And(b, FddKernel.Xor(nodeArray[i + j * 9], nodeArray[i + k * 9]));
                    }
                }
            }

            //own grid restriction all nodes just check 2 nodes on line below.
            Bdd c = new Bdd(true);

            for (int i = 0; i < 81; i++)
            {
                c = Kernel.And(c, FddKernel.Xor(nodeArray[i], nodeArray[getLeft(i)]));
                c = Kernel.And(c, FddKernel.Xor(nodeArray[i], nodeArray[getRight(i)]));
            }

            Bdd result = Kernel.And(Kernel.And(a, b), c);

            int[] foo = FddKernel.AnySat(result, 9);

            //BddSerializer.Serialize(result, "Sudoku");
        }
Exemple #9
0
        public void ComposeTest1()
        {
            Bdd t = Kernel.And(new Bdd(1), new Bdd(2));

            int tp = 3;

            int x = 1;

            Bdd expected = new Bdd(true);
            Bdd actual;

            actual = BddSharp.Kernel.Kernel.Compose(t, tp, x);

            int result1 = actual.High.Low.U;
            int result2 = actual.Low.U;
            int result3 = actual.High.High.U;
            int result4 = actual.High.Var;

            Assert.AreEqual(0, result1, "BddSharp.Kernel.Kernel.Compose did not return the expected value.");
            Assert.AreEqual(0, result2);
            Assert.AreEqual(1, result3);
            Assert.AreEqual(3, result4);
        }
Exemple #10
0
        private void BuildQueenProblem(int i, int j)
        {
            //BDD demonstration of the N-Queen chess problem.
            //-----------------------------------------------
            //The BDD variables correspond to a NxN chess board like:

            //   0    N      2N     ..  N*N-N
            //   1    N+1    2N+1   ..  N*N-N+1
            //   2    N+2    2N+2   ..  N*N-N+2
            //   ..   ..     ..     ..  ..
            //   N-1  2N-1   3N-1   ..  N*N-1

            // So for example a 4x4 is:

            //   0 4  8 12
            //   1 5  9 13
            //   2 6 10 14
            //   3 7 11 15

            // One solution is then that 2,4,11,13 should be true, meaning a queen
            // should be placed there:

            //   . X . .
            //   . . . X
            //   X . . .
            //   . . X .

            /* Build the requirements for all other fields than (i,j) assuming
             * that (i,j) has a queen */


            Bdd a = new Bdd(true);
            Bdd b = new Bdd(true);
            Bdd c = new Bdd(true);
            Bdd d = new Bdd(true);

            int k, l;

            /* No one in the same column */
            for (l = 0; l < N; l++)
            {
                if (l != j)
                {
                    a = Kernel.And(a, Kernel.Imp(X[i, j], Kernel.Not(X[i, l])));
                }
            }

            /* No one in the same row */
            for (k = 0; k < N; k++)
            {
                if (k != i)
                {
                    b = Kernel.And(b, Kernel.Imp(X[i, j], Kernel.Not(X[k, j])));
                }
            }

            /* No one in the same up-right diagonal */
            for (k = 0; k < N; k++)
            {
                int ll = k - i + j;
                if (ll >= 0 && ll < N)
                {
                    if (k != i)
                    {
                        c = Kernel.And(c, Kernel.Imp(X[i, j], Kernel.Not(X[k, ll])));
                    }
                }
            }

            /* No one in the same down-right diagonal */
            for (k = 0; k < N; k++)
            {
                int ll = i + j - k;
                if (ll >= 0 && ll < N)
                {
                    if (k != i)
                    {
                        d = Kernel.And(d, Kernel.Imp(X[i, j], Kernel.Not(X[k, ll])));
                    }
                }
            }

            queens = Kernel.And(queens, Kernel.And(a, Kernel.And(b, Kernel.And(c, d))));
        }
Exemple #11
0
        static void test()
        {
            FddKernel.Setup();
            Fdd[] X = new Fdd[3];
            for (int i = 0; i < 3; i++)
            {
                X[i] = new Fdd(8);
            }

            Bdd result = new Bdd(true);

            DateTime startSetup = DateTime.Now;

            //for(int i=0;i<8;i++)
            //    for(int j=i+1;j<9;j++)
            //        result = Kernel.And(result, FddKernel.Xor(X[i], X[j]));

            result = Kernel.And(result, FddKernel.Xor(X[0], X[1]));
            result = Kernel.And(result, FddKernel.Xor(X[0], X[2]));
            //result = Kernel.And(result, FddKernel.Xor(X[0], X[3]));
            //result = Kernel.And(result, FddKernel.Xor(X[0], X[4]));
            //result = Kernel.And(result, FddKernel.Xor(X[0], X[5]));
            //result = Kernel.And(result, FddKernel.Xor(X[0], X[6]));
            //result = Kernel.And(result, FddKernel.Xor(X[0], X[7]));
            //result = Kernel.And(result, FddKernel.Xor(X[0], X[8]));

            result = Kernel.And(result, FddKernel.Xor(X[1], X[2]));
            //result = Kernel.And(result, FddKernel.Xor(X[1], X[3]));
            //result = Kernel.And(result, FddKernel.Xor(X[1], X[4]));
            //result = Kernel.And(result, FddKernel.Xor(X[1], X[5]));
            //result = Kernel.And(result, FddKernel.Xor(X[1], X[6]));
            //result = Kernel.And(result, FddKernel.Xor(X[1], X[7]));
            //result = Kernel.And(result, FddKernel.Xor(X[1], X[8]));

            //result = Kernel.And(result, FddKernel.Xor(X[2], X[3]));
            //result = Kernel.And(result, FddKernel.Xor(X[2], X[4]));
            //result = Kernel.And(result, FddKernel.Xor(X[2], X[5]));
            //result = Kernel.And(result, FddKernel.Xor(X[2], X[6]));
            //result = Kernel.And(result, FddKernel.Xor(X[2], X[7]));
            //result = Kernel.And(result, FddKernel.Xor(X[2], X[8]));

            //result = Kernel.And(result, FddKernel.Xor(X[3], X[4]));
            //result = Kernel.And(result, FddKernel.Xor(X[3], X[5]));
            //result = Kernel.And(result, FddKernel.Xor(X[3], X[6]));
            //result = Kernel.And(result, FddKernel.Xor(X[3], X[7]));
            //result = Kernel.And(result, FddKernel.Xor(X[3], X[8]));

            //result = Kernel.And(result, FddKernel.Xor(X[4], X[5]));
            //result = Kernel.And(result, FddKernel.Xor(X[4], X[6]));
            //result = Kernel.And(result, FddKernel.Xor(X[4], X[7]));
            //result = Kernel.And(result, FddKernel.Xor(X[4], X[8]));

            //result = Kernel.And(result, FddKernel.Xor(X[5], X[6]));
            //result = Kernel.And(result, FddKernel.Xor(X[5], X[7]));
            //result = Kernel.And(result, FddKernel.Xor(X[5], X[8]));

            //result = Kernel.And(result, FddKernel.Xor(X[6], X[7]));
            //result = Kernel.And(result, FddKernel.Xor(X[6], X[8]));

            //result = Kernel.And(result, FddKernel.Xor(X[7], X[8]));

            DateTime finishSetup = DateTime.Now;
            TimeSpan timeSetup   = finishSetup - startSetup;

            //result = FddKernel.Restrict(result, X[1], 0);
            result = Kernel.And(result, FddKernel.Equal(X[1], 6));

            int[] foo = FddKernel.AnySat(result, 8);        //returns 0,1,2,3

            //BddSerializer.Serialize(result, "foo");
        }
Exemple #12
0
        //0  |18 |             row1
        // 8 |   |             row2
        //  2|   |             row3
        //--- --- ---
        public static void EnforceTest2()
        {
            FddKernel.Setup();
            Fdd[] X = new Fdd[27];
            for (int i = 0; i < 27; i++)
            {
                X[i] = new Fdd(8);
            }

            Bdd result = new Bdd(true);

            //row1 restricts
            result = Kernel.And(result, FddKernel.Equal(X[0], 0));
            result = Kernel.And(result, FddKernel.Equal(X[3], 1));
            result = Kernel.And(result, FddKernel.Equal(X[4], 8));

            //grid 1 restricts (except row1)
            result = Kernel.And(result, FddKernel.Equal(X[10], 8));
            result = Kernel.And(result, FddKernel.Equal(X[20], 2));

            //row1 maxvalues restricts
            result = Kernel.Or(result, FddKernel.EnforceMaxValue(X[1], 8));
            result = Kernel.Or(result, FddKernel.EnforceMaxValue(X[2], 8));


            //row1
            for (int i = 0; i < 8; i++)
            {
                for (int j = i + 1; j < 9; j++)
                {
                    result = Kernel.And(result, FddKernel.Xor(X[i], X[j]));
                }
            }


            //small grid 1
            result = Kernel.And(result, FddKernel.Xor(X[0], X[9]));
            result = Kernel.And(result, FddKernel.Xor(X[0], X[10]));
            result = Kernel.And(result, FddKernel.Xor(X[0], X[11]));
            result = Kernel.And(result, FddKernel.Xor(X[0], X[18]));
            result = Kernel.And(result, FddKernel.Xor(X[0], X[19]));
            result = Kernel.And(result, FddKernel.Xor(X[0], X[20]));
            result = Kernel.And(result, FddKernel.Xor(X[1], X[9]));
            result = Kernel.And(result, FddKernel.Xor(X[1], X[10]));
            result = Kernel.And(result, FddKernel.Xor(X[1], X[11]));
            result = Kernel.And(result, FddKernel.Xor(X[1], X[18]));
            result = Kernel.And(result, FddKernel.Xor(X[1], X[19]));
            result = Kernel.And(result, FddKernel.Xor(X[1], X[20]));
            result = Kernel.And(result, FddKernel.Xor(X[2], X[9]));
            result = Kernel.And(result, FddKernel.Xor(X[2], X[10]));
            result = Kernel.And(result, FddKernel.Xor(X[2], X[11]));
            result = Kernel.And(result, FddKernel.Xor(X[2], X[18]));
            result = Kernel.And(result, FddKernel.Xor(X[2], X[19]));
            result = Kernel.And(result, FddKernel.Xor(X[2], X[20]));

            //row2
            //for (int i = 9; i < 17; i++)
            //    for (int j = i; j < 18; j++)
            //        result = Kernel.And(result, FddKernel.Xor(X[i], X[j]));

            ////row3
            //for (int i = 18; i < 26; i++)
            //    for (int j = i; j < 27; j++)
            //        result = Kernel.And(result, FddKernel.Xor(X[i], X[j]));
        }
Exemple #13
0
        public string Run(bool b1, bool b2, bool b3)
        {
            Fdd[] nodeArray = new Fdd[16];
            for (int i = 0; i < 16; i++)
            {
                nodeArray[i] = new Fdd(4);
            }
            Bdd result = new Bdd(true);

            result = Kernel.And(result, FddKernel.Xor(nodeArray[0], nodeArray[1]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[0], nodeArray[2]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[0], nodeArray[3]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[0], nodeArray[4]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[0], nodeArray[5]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[0], nodeArray[8]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[0], nodeArray[12]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[1], nodeArray[2]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[1], nodeArray[3]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[1], nodeArray[4]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[1], nodeArray[5]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[1], nodeArray[9]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[1], nodeArray[13]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[2], nodeArray[3]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[2], nodeArray[6]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[2], nodeArray[7]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[2], nodeArray[10]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[2], nodeArray[14]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[3], nodeArray[6]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[3], nodeArray[7]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[3], nodeArray[11]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[3], nodeArray[15]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[4], nodeArray[5]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[4], nodeArray[6]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[4], nodeArray[7]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[4], nodeArray[8]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[4], nodeArray[12]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[5], nodeArray[9]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[5], nodeArray[13]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[6], nodeArray[7]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[6], nodeArray[10]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[6], nodeArray[14]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[7], nodeArray[11]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[7], nodeArray[15]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[8], nodeArray[9]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[8], nodeArray[10]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[8], nodeArray[11]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[8], nodeArray[12]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[8], nodeArray[13]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[9], nodeArray[10]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[9], nodeArray[11]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[9], nodeArray[13]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[10], nodeArray[11]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[10], nodeArray[14]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[10], nodeArray[15]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[11], nodeArray[14]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[11], nodeArray[15]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[12], nodeArray[13]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[12], nodeArray[14]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[12], nodeArray[15]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[13], nodeArray[14]));
            result = Kernel.And(result, FddKernel.Xor(nodeArray[13], nodeArray[15]));

            result = Kernel.And(result, FddKernel.Xor(nodeArray[14], nodeArray[15]));

            long l = Kernel.SatCount(result);

            string s = PrintNice(FddKernel.AnySat(result, 3));

            return(String.Format("Number of results: {0}{1}{2}", l, Environment.NewLine, s));
        }