Exemple #1
0
        /// <summary>
        /// Check whether there exists one pair whose one string is the prefix of the other.
        /// </summary>
        /// <param name="?">Instance being checked.</param>
        /// <returns></returns>
        public static int Pass_Prefix_Filter(PCPInstance instance)
        {
            int  i;
            int  len = 0;
            Pair p;

            for (i = 0; i < instance.size; i++)
            {
                p = instance.arrPair[i];

                if (p.downlen > p.uplen)
                {
                    len = p.uplen;
                }
                else if (p.downlen < p.uplen)
                {
                    len = p.downlen;
                }

                if (p.down.Substring(0, len) == p.up.Substring(0, len))
                {
                    return(1);
                }
            }
            return(0);
        }
Exemple #2
0
        // Check whether there exists one pair with upstring longer
        // than downstring and one pair with downstring longer than upstring
        public static int Pass_Length_Balance_Filter(PCPInstance instance)
        {
            int  i;
            int  upflag   = 0;
            int  downflag = 0;
            Pair p;

            for (i = 0; i < instance.size; i++)
            {
                p = instance.arrPair[i];
                if (p.downlen > p.uplen)
                {
                    downflag = 1;
                }
                else if (p.downlen < p.uplen)
                {
                    upflag = 1;
                }
            }

            if ((downflag == 1) && (upflag == 1))
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }
Exemple #3
0
        public void CreateReversePCP(out PCPInstance reversedPCP)
        {
            reversedPCP = new PCPInstance();
            reversedPCP.Init();


            // copy member attributions
            reversedPCP.size        = size;
            reversedPCP.width       = width;
            reversedPCP.up_offset   = up_offset;
            reversedPCP.down_offset = down_offset;
            reversedPCP.offset      = offset;
            reversedPCP.gcd         = gcd;

            int i = 0;

            foreach (Pair p in arrPair)
            {
                reversedPCP.arrPair[i] = p;

                Pair newPair = reversedPCP.arrPair[i];

                newPair.down = Reverse(newPair.down);
                newPair.up   = Reverse(newPair.up);

                i++;
            }
            reversedPCP.CountOffset();
        }
Exemple #4
0
        /// <summary>
        /// Check whether there exists one pair whose one string is the postfix of the other
        /// </summary>
        /// <param name="?">Instance being checked.</param>
        /// <returns></returns>
        public static int Pass_Postfix_Filter(PCPInstance instance)
        {
            int    i;
            Pair   p;
            string str;

            for (i = 0; i < instance.size; i++)
            {
                p = instance.arrPair[i];

                if (p.downlen > p.uplen)
                {
                    str = p.down + (p.downlen - p.uplen);
                    if (!(str.Substring(0, p.uplen) == p.up.Substring(0, p.uplen)))
                    {
                        return(1);
                    }
                }
                else if (p.downlen < p.uplen)
                {
                    str = p.up + (p.uplen - p.downlen);
                    if (!(str.Substring(0, p.downlen) == p.down.Substring(0, p.downlen)))
                    {
                        return(1);
                    }
                }
            }
            return(0);
        }
Exemple #5
0
        static public int FindMask(PCPInstance instance, PCPInstance reverseInstance)
        {
            FindSideMask(instance, reverseInstance, 1);
            FindSideMask(instance, reverseInstance, 0);
            FindSideMask(reverseInstance, instance, 1);
            FindSideMask(reverseInstance, instance, 0);

            FindSideMask(instance, reverseInstance, 1);
            FindSideMask(instance, reverseInstance, 0);
            FindSideMask(reverseInstance, instance, 1);
            FindSideMask(reverseInstance, instance, 0);

            if (Convert.ToBoolean(instance.upmask))
            {
                reverseInstance.downmask = 1;
            }
            if (Convert.ToBoolean(instance.downmask))
            {
                reverseInstance.upmask = 1;
            }
            if (Convert.ToBoolean(reverseInstance.upmask))
            {
                instance.downmask = 1;
            }
            if (Convert.ToBoolean(reverseInstance.downmask))
            {
                instance.upmask = 1;
            }

            return(1);
        }
Exemple #6
0
        //DominoScene dominoScene;

        public Game1()
        {
            graphics              = new GraphicsDeviceManager(this);
            this.IsMouseVisible   = true;
            Content.RootDirectory = "Content";
            //dominoScene = new DominoScene();

            Console.WriteLine("PCPSolver:\n Team Members:\n Chris Dillard\n John Futch");


            // SIMPLE SOLVABLE INSTANCE
            //
            //3 2
            //0 01 110
            //100 00 11
            //
            //

            PCPInstance instance = new PCPInstance();

            /*
             * Pair p = new Pair();
             * p.up= "0";
             * p.down = "100";
             *
             * Pair p2 = new Pair();
             * p2.up= "01";
             * p2.down = "00";
             *
             * Pair p3 = new Pair();
             * p3.up = "110";
             * p3.down = "11";
             *
             * instance.arrPair = new Pair[3] { p, p2, p3 };
             */


            Pair p = new Pair();

            p.up   = "100";
            p.down = "1";

            Pair p2 = new Pair();

            p2.up   = "0";
            p2.down = "100";

            Pair p3 = new Pair();

            p3.up   = "1";
            p3.down = "00";

            instance.arrPair = new Pair[3] {
                p, p2, p3
            };

            instance.Print();

            PCPSolver.SolvePCPInstance(instance, 1);
        }
Exemple #7
0
        public static int BeginSolveConfig(PCPInstance instance,Config config, int maxLen, int maxDepth)
        {
            iterative_depth_threshold = maxDepth;
            int ret = SolveConfig(instance, config, maxLen);
            ClearHashTable();

            return ret;
        }
Exemple #8
0
        public static int BeginSolveConfig(PCPInstance instance, Config config, int maxLen, int maxDepth)
        {
            iterative_depth_threshold = maxDepth;
            int ret = SolveConfig(instance, config, maxLen);

            ClearHashTable();

            return(ret);
        }
Exemple #9
0
        // Check whether there exists one pair whose upstring containing
        // more 1's than downstring, and another pair whose downstring
        // containing more 1's than upstring. The same situation for element 0
        public static int Pass_Element_Balance_Filter(PCPInstance instance)
        {
            int i ,j;
            int zero_upflag = 0;
            int zero_downflag = 0;
            int one_upflag = 0;
            int one_downflag = 0;
            int one_upcount;
            int zero_upcount;
            int one_downcount;
            int zero_downcount;
            Pair p;

            for (i=0;i<instance.size;i++)
            {
                one_upcount = 0;
                zero_upcount = 0;
                one_downcount = 0;
                zero_downcount = 0;
                p = instance.arrPair[i];

                for (j=0; j<p.uplen;j++)
                {
                    if (p.up[j] == 0)
                        zero_upcount++;
                    else one_upcount++;
                }

                for (j=0; j<p.downlen;j++)
                {
                    if (p.down[j] == 0)
                        zero_downcount++;
                    else one_downcount++;
                }

                if (zero_upcount>zero_downcount)
                    zero_upflag = 1;
                else if (zero_upcount<zero_downcount)
                    zero_downflag = 1;

                if (one_upcount>one_downcount)
                    one_upflag = 1;
                else if (one_upcount<one_downcount)
                    one_downflag = 1;
            }

            if (zero_upflag == 1 && zero_downflag==0 ||
                zero_upflag == 0 && zero_downflag==1 )
                return 0;

            if (one_upflag == 1 && one_downflag==0 ||
                one_upflag == 0 && one_downflag==1  )
                return 0;

            return 1;
        }
Exemple #10
0
        //DominoScene dominoScene;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            this.IsMouseVisible = true;
            Content.RootDirectory = "Content";
            //dominoScene = new DominoScene();

            Console.WriteLine("PCPSolver:\n Team Members:\n Chris Dillard\n John Futch");

            // SIMPLE SOLVABLE INSTANCE
            //
            //3 2
            //0 01 110
            //100 00 11
            //
            //

            PCPInstance instance = new PCPInstance();
            /*
            Pair p = new Pair();
            p.up= "0";
            p.down = "100";

            Pair p2 = new Pair();
            p2.up= "01";
            p2.down = "00";

            Pair p3 = new Pair();
            p3.up = "110";
            p3.down = "11";

            instance.arrPair = new Pair[3] { p, p2, p3 };
            */

            Pair p = new Pair();
            p.up = "100";
            p.down = "1";

            Pair p2 = new Pair();
            p2.up = "0";
            p2.down = "100";

            Pair p3 = new Pair();
            p3.up = "1";
            p3.down = "00";

            instance.arrPair = new Pair[3] { p, p2, p3 };

            instance.Print();

            PCPSolver.SolvePCPInstance(instance, 1);
        }
Exemple #11
0
        /// <summary>
        /// Identify the trivial case where some string has its downstring and upstring
        /// identical.
        /// </summary>
        /// <param name="?">Instance being checked.</param>
        /// <returns></returns>
        public static int Pass_Trivialcase_Filter(PCPInstance instance)
        {
            int  i;
            Pair p;

            for (i = 0; i < instance.size; i++)
            {
                p = instance.arrPair[i];
                if (p.downlen == p.uplen)
                {
                    if (p.down == p.up)
                    {
                        return(0);
                    }
                }
            }
            return(1);
        }
Exemple #12
0
        static public int Solution_Found(PCPInstance instance, Config config)
        {
            int k;



            // for test, may not be solution, so need not record length and count
            //if (globalStatus == FIND_MASK || globalStatus == EXCLUSION_METHOD)
            //    return pConfig->depth;

            // just to find solution
            if (true)
            {
                // solution_length = pConfig->depth;
                solution_count = 1;
                return(config.depth);
            }
        }
Exemple #13
0
        // reture value:
        // -1: trivial case
        // -2: element balance violated
        // 0: no solution
        // 1: pass all filters
        public static int PassAllFilters(PCPInstance instance)
        {
            // has solution, but it is the trivial case
            if (!(Pass_Trivialcase_Filter(instance) == 0))
            {
                Console.WriteLine("Trivial case: one pair contains the same strings\n");
                return(-1);
            }

            if (!(Pass_Prefix_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the prefix filter!\n");
                return(0);
            }

            if (!(Pass_Postfix_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the postfix filter!\n");
                return(0);
            }

            if (!(Pass_Length_Balance_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the length balance filter!\n");
                return(0);
            }

            if (!(Pass_Element_Balance_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the element balance filter!\n");
                Console.WriteLine("Some pair can not be used!\n");

                if (instance.size <= 2)
                {
                    return(0);
                }
                else
                {
                    return(-2); //It may have solution. Need improvement
                }
            }

            return(1);
        }
Exemple #14
0
        public static int FindMask(PCPInstance instance, PCPInstance reverseInstance)
        {
            FindSideMask(instance, reverseInstance, 1);
            FindSideMask(instance, reverseInstance, 0);
            FindSideMask(reverseInstance, instance, 1);
            FindSideMask(reverseInstance, instance, 0);

            FindSideMask(instance, reverseInstance, 1);
            FindSideMask(instance, reverseInstance, 0);
            FindSideMask(reverseInstance, instance, 1);
            FindSideMask(reverseInstance, instance, 0);

            if (Convert.ToBoolean(instance.upmask)) reverseInstance.downmask = 1;
            if (Convert.ToBoolean(instance.downmask)) reverseInstance.upmask = 1;
            if (Convert.ToBoolean(reverseInstance.upmask)) instance.downmask = 1;
            if (Convert.ToBoolean(reverseInstance.downmask)) instance.upmask = 1;

            return 1;
        }
Exemple #15
0
        // reture value:
        // -1: trivial case
        // -2: element balance violated
        // 0: no solution
        // 1: pass all filters
        public static int PassAllFilters(PCPInstance instance)
        {
            // has solution, but it is the trivial case
            if (!(Pass_Trivialcase_Filter(instance) == 0))
            {
                Console.WriteLine("Trivial case: one pair contains the same strings\n");
                return -1;
            }

            if (!(Pass_Prefix_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the prefix filter!\n");
                return 0;
            }

            if (!(Pass_Postfix_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the postfix filter!\n");
                return 0;
            }

            if (!(Pass_Length_Balance_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the length balance filter!\n");
                return 0;
            }

            if (!(Pass_Element_Balance_Filter(instance) == 0))
            {
                Console.WriteLine("Cannot pass the element balance filter!\n");
                Console.WriteLine("Some pair can not be used!\n");

                if (instance.size<=2)
                    return 0;
                else return -2; //It may have solution. Need improvement
            }

            return 1;
        }
Exemple #16
0
        // return value:
        //  1  -- solved, find solution
        //  -1 -- solved, no solution
        //  0  -- unsolved
        public static int SolvePCPInstance(PCPInstance instance, int iterative)
        {
            instance.CountOffset();

            int ret = 0;

            //PCPInstance ReversePCP;
            //// create the inverse instance
            //instance.CreateReversePCP(out ReversePCP);

            //Console.WriteLine("After reversal PCP Instance Is:");
            //instance.Print();

            //Mask.FindMask(instance, ReversePCP);

            //if (Convert.ToBoolean(instance.upmask) && Convert.ToBoolean(instance.downmask) ||
            //        Convert.ToBoolean(ReversePCP.upmask) && Convert.ToBoolean(ReversePCP.downmask))
            //    return -1;

            int solved_flag = 0;
            // try original direction
            int original_visit_nodenum;
            node_num = 0;
            ret = SearchSolution(instance);
            if (solution_count >= 1) solved_flag = 1;
            else if (ret == -1) solved_flag = -1;

            original_visit_nodenum = node_num;

            ////TODO IMPLEMENT HASH TABLE
            ////ClearHashTable();

            //// try reverse direction
            //int reverse_visit_nodenum;
            //node_num = 0;

            //ret = SearchSolution(ReversePCP);
            //if (solution_count >= 1) solved_flag = 1;
            //else if (ret == -1) solved_flag = -1;

            //reverse_visit_nodenum = node_num;

            ////TODO: CONVERT TO C# HASHMAP
            ////ClearHashTable();

            if (solved_flag == 1)
            {
                Console.WriteLine("SOLUTION FOUND OR FALSE POSITIVE:");
                Console.WriteLine(PCPSolver.arrSelection);
            }

            return 0;
        }
Exemple #17
0
        // Check whether there exists one pair whose upstring containing
        // more 1's than downstring, and another pair whose downstring
        // containing more 1's than upstring. The same situation for element 0
        public static int Pass_Element_Balance_Filter(PCPInstance instance)
        {
            int  i, j;
            int  zero_upflag   = 0;
            int  zero_downflag = 0;
            int  one_upflag    = 0;
            int  one_downflag  = 0;
            int  one_upcount;
            int  zero_upcount;
            int  one_downcount;
            int  zero_downcount;
            Pair p;

            for (i = 0; i < instance.size; i++)
            {
                one_upcount    = 0;
                zero_upcount   = 0;
                one_downcount  = 0;
                zero_downcount = 0;
                p = instance.arrPair[i];

                for (j = 0; j < p.uplen; j++)
                {
                    if (p.up[j] == 0)
                    {
                        zero_upcount++;
                    }
                    else
                    {
                        one_upcount++;
                    }
                }

                for (j = 0; j < p.downlen; j++)
                {
                    if (p.down[j] == 0)
                    {
                        zero_downcount++;
                    }
                    else
                    {
                        one_downcount++;
                    }
                }

                if (zero_upcount > zero_downcount)
                {
                    zero_upflag = 1;
                }
                else if (zero_upcount < zero_downcount)
                {
                    zero_downflag = 1;
                }

                if (one_upcount > one_downcount)
                {
                    one_upflag = 1;
                }
                else if (one_upcount < one_downcount)
                {
                    one_downflag = 1;
                }
            }

            if (zero_upflag == 1 && zero_downflag == 0 ||
                zero_upflag == 0 && zero_downflag == 1)
            {
                return(0);
            }

            if (one_upflag == 1 && one_downflag == 0 ||
                one_upflag == 0 && one_downflag == 1)
            {
                return(0);
            }

            return(1);
        }
Exemple #18
0
        // Solve the instance with the given config
        // maxlen is the size of the maximum space allocated in pConfig
        // return value:
        //    -1: unsolvable
        //     0: unsolved till the depth threshold
        //   n>0: solved at depth of n
        public static int SolveConfig(PCPInstance instance,Config config, int maxLen)
        {
            int i, j, k;

            // store the matched pairs and the lengths of resulting configs
            int[] arrRetValue = new int[17];
            int[] arrMatchedPair = new int[17];
            int matchedPairNum = 0;

            // ret value
            int matchret=0;
            int solveret;
            int ret = -1;
            int succ;
            int newlen;

            // store the location of the config in the cache
            Config pCacheConfig;
            Config pNewConfig;

            for (i = 0; i < instance.size; i++)
            {
                matchret = config.TestMatchingPair(instance.arrPair[i]);

                if (matchret == 0) // find one solution
                {
                    config.depth++;
                    PCPSolver.arrSelection[config.depth] = instance.arrPair[i].ID;
                    succ = Solution_Found(instance, config);
                    if (succ > 0) { return config.depth; }
                    else ret = 0;
                    config.depth--;
                }
                else if (matchret > 0) // find one match
                {
                    //// having exceeded the threshold, prune it
                    //if (config.depth == iterative_depth_threshold - 1)
                    //    ret = 0;
                    //else  // record the pair
                    //{
                        arrRetValue[matchedPairNum] = matchret;
                        arrMatchedPair[matchedPairNum++] = i;
                    //}
                }
            }

            //// sort the matched pair array according to its matchret value
            for (i = 0; i < matchedPairNum - 1; i++)
                for (j = i + 1; j < matchedPairNum; j++)
                {
                    if (arrRetValue[i] > arrRetValue[j])
                    {
                        k = arrRetValue[i];
                        arrRetValue[i] = arrRetValue[j];
                        arrRetValue[j] = k;

                        k = arrMatchedPair[i];
                        arrMatchedPair[i] = arrMatchedPair[j];
                        arrMatchedPair[j] = k;
                    }
                }

             // try all matched pairs
            for (i = 0; i < matchedPairNum; i++)
            {
                // the last matched pair and the config can be reused? Jump back!
                if (i + 1 == matchedPairNum && arrRetValue[i] <= maxLen)
                {

                    config.MatchPair(instance.arrPair[arrMatchedPair[i]]);

                }
            }

            return matchret;
        }
Exemple #19
0
        public static int FindSideMask(PCPInstance instance, PCPInstance reverseInstance, int maskflag)
        {
            int i, j, k, m, n, t;
            Pair pair;
            int ret;
            int maxlen;
            String longstr, shortstr;
            int pIntMask;
            int up = maskflag%2;

            switch (maskflag)
            {
                case 0: pIntMask = instance.downmask; break;
                case 1: pIntMask = instance.upmask; break;
                case 2: pIntMask = instance.turnover_downmask; break;
                case 3: pIntMask = instance.turnover_upmask; break;
                //default: assert(0);
                default:
                    break;
            }

            for (i=0;i<instance.size;i++)
            {
                pair = instance.arrPair[i];

                if (up==1)  // up
                {
                    n = pair.downlen;
                    t = pair.uplen;
                    longstr = pair.down;
                    shortstr = pair.up;
                }
                else // down
                {
                    t = pair.downlen;
                    n = pair.uplen;
                    shortstr = pair.down;
                    longstr = pair.up;
                }

                if (n > t)
                {
                    for (k=0; k<n-t;k++)
                    {
                        j = n-1-k;
                        m = j-t+1;

                        // they must be the multiple of gcd
                        if (m%instance.gcd!=0 || (n-1-j)%instance.gcd!=0)
                            continue;

                        if (shortstr == longstr.Substring(m,longstr.Length-m)) continue;
                        // the tail string in the bottom should possibly lead to the solution
                        if (j<n-1)
                        {
                            maxlen = n-j-1+10*instance.offset;
                            Config config = new Config(maxlen);
                            config.ConfigAssign(longstr.Substring(j+1), n-j-1, Convert.ToInt32(!Convert.ToBoolean(up)));
                            config.depth = 1;

                            ret = PCPSolver.BeginSolveConfig(instance, config, maxlen, 100);

                            if (ret == -1) continue;
                        }
                        else if (maskflag>=2) // find turnover
                            continue;

                        // the head string should be generated, so we use the reverse PCP to parse it
                        maxlen = m + 10*instance.offset;
                        Config config2 = new Config(maxlen);
                        int index;

                        char[] str = new char[m];

                        for (index=0;index<m;index++)
                            str[index] = longstr[m-index-1];

                        String st = new String(str);

                        config2.ConfigAssign(st, m, Convert.ToInt32(!Convert.ToBoolean(up)));

                        config2.depth = 1;

                        ret = PCPSolver.BeginSolveConfig(reverseInstance,config2, maxlen, 100);

                        if (ret == -1) continue;
                        else // no mask!
                        {
                            pIntMask = 0;
                            return 0;
                        }
                    }
                }
            }

            pIntMask = 1;

            return 1;
        }
Exemple #20
0
        static public int FindSideMask(PCPInstance instance, PCPInstance reverseInstance, int maskflag)
        {
            int    i, j, k, m, n, t;
            Pair   pair;
            int    ret;
            int    maxlen;
            String longstr, shortstr;
            int    pIntMask;
            int    up = maskflag % 2;

            switch (maskflag)
            {
            case 0: pIntMask = instance.downmask; break;

            case 1: pIntMask = instance.upmask; break;

            case 2: pIntMask = instance.turnover_downmask; break;

            case 3: pIntMask = instance.turnover_upmask; break;

            //default: assert(0);
            default:
                break;
            }

            for (i = 0; i < instance.size; i++)
            {
                pair = instance.arrPair[i];

                if (up == 1)        // up
                {
                    n        = pair.downlen;
                    t        = pair.uplen;
                    longstr  = pair.down;
                    shortstr = pair.up;
                }
                else         // down
                {
                    t        = pair.downlen;
                    n        = pair.uplen;
                    shortstr = pair.down;
                    longstr  = pair.up;
                }

                if (n > t)
                {
                    for (k = 0; k < n - t; k++)
                    {
                        j = n - 1 - k;
                        m = j - t + 1;

                        // they must be the multiple of gcd
                        if (m % instance.gcd != 0 || (n - 1 - j) % instance.gcd != 0)
                        {
                            continue;
                        }



                        if (shortstr == longstr.Substring(m, longstr.Length - m))
                        {
                            continue;
                        }
                        // the tail string in the bottom should possibly lead to the solution
                        if (j < n - 1)
                        {
                            maxlen = n - j - 1 + 10 * instance.offset;
                            Config config = new Config(maxlen);
                            config.ConfigAssign(longstr.Substring(j + 1), n - j - 1, Convert.ToInt32(!Convert.ToBoolean(up)));
                            config.depth = 1;


                            ret = PCPSolver.BeginSolveConfig(instance, config, maxlen, 100);

                            if (ret == -1)
                            {
                                continue;
                            }
                        }
                        else if (maskflag >= 2) // find turnover
                        {
                            continue;
                        }

                        // the head string should be generated, so we use the reverse PCP to parse it
                        maxlen = m + 10 * instance.offset;
                        Config config2 = new Config(maxlen);
                        int    index;


                        char[] str = new char[m];

                        for (index = 0; index < m; index++)
                        {
                            str[index] = longstr[m - index - 1];
                        }

                        String st = new String(str);

                        config2.ConfigAssign(st, m, Convert.ToInt32(!Convert.ToBoolean(up)));

                        config2.depth = 1;

                        ret = PCPSolver.BeginSolveConfig(reverseInstance, config2, maxlen, 100);

                        if (ret == -1)
                        {
                            continue;
                        }
                        else                 // no mask!
                        {
                            pIntMask = 0;
                            return(0);
                        }
                    }
                }
            }

            pIntMask = 1;

            return(1);
        }
Exemple #21
0
        public void CreateReversePCP(out PCPInstance reversedPCP)
        {
            reversedPCP = new PCPInstance();
            reversedPCP.Init();

            // copy member attributions
            reversedPCP.size = size;
            reversedPCP.width = width;
            reversedPCP.up_offset = up_offset;
            reversedPCP.down_offset = down_offset;
            reversedPCP.offset = offset;
            reversedPCP.gcd = gcd;

            int i = 0;
            foreach (Pair p in arrPair)
            {

                reversedPCP.arrPair[i] = p;

                Pair newPair = reversedPCP.arrPair[i];

                newPair.down = Reverse(newPair.down);
                newPair.up = Reverse(newPair.up);

                i++;
            }
            reversedPCP.CountOffset();
        }
Exemple #22
0
        // Check whether there exists one pair with upstring longer
        // than downstring and one pair with downstring longer than upstring
        public static int Pass_Length_Balance_Filter(PCPInstance instance)
        {
            int i;
            int upflag = 0;
            int downflag = 0;
            Pair p;

            for (i=0;i<instance.size;i++)
            {
                p = instance.arrPair[i];
                if (p.downlen>p.uplen)
                    downflag = 1;
                else if (p.downlen<p.uplen)
                    upflag = 1;
            }

            if ((downflag == 1) && (upflag == 1))
                return 1;
            else return 0;
        }
Exemple #23
0
        static public int SearchSolution(PCPInstance instance)
        {
            //start at beginning.
            int startingpoint_flag = 0;



            int    i, len, succ;
            int    ret = -1, solveret = -1;
            Pair   pPair;
            int    maxlen = instance.offset * (100 + 1);
            Config pCacheConfig;

            //CConfig pNewconfig(maxlen);
            Config pNewConfig;


            pNewConfig = new Config(maxlen);


            for (i = 0; i < instance.size; i++)
            {
                // initialize config
                pNewConfig.depth = 0;
                pNewConfig.len   = 0;

                // if use exclusion method, startingpoints are meaningful
                if (startingpoint_flag == 1)
                {
                    if (!Convert.ToBoolean(instance.arrStartingPoint[i]))
                    {
                        continue;
                    }
                }

                // simply search from the start
                if (startingpoint_flag == 0)
                {
                    pPair = instance.arrPair[i];
                    len   = (pPair.uplen < pPair.downlen) ?
                            pPair.uplen : pPair.downlen;

                    if (pPair.up.Substring(len) == pPair.down.Substring(len))
                    {
                        continue;
                    }
                }


                // find one starting point
                pNewConfig.MatchPair(instance.arrPair[i]);

                // check masks
                if (Convert.ToBoolean(pNewConfig.up))
                {
                    if (Convert.ToBoolean(instance.upmask))
                    {
                        continue;
                    }
                }
                if (!Convert.ToBoolean(pNewConfig.up))
                {
                    if (Convert.ToBoolean(instance.downmask))
                    {
                        continue;
                    }
                }

                // add to the cache
                //succ = TryFind(hashTable, pNewConfig, &pCacheConfig);

                // try to solve
                solveret = SolveConfig(instance, pNewConfig, maxlen);

                if (solveret > 0)       // solve it
                {
                    return(solveret);
                }
                if (solveret == 0)       // don't solve it to the iterative_depth_threshold
                {
                    ret = 0;
                }
            }

            return(ret);
        }
Exemple #24
0
        /// <summary>
        /// Check whether there exists one pair whose one string is the postfix of the other
        /// </summary>
        /// <param name="?">Instance being checked.</param>
        /// <returns></returns>
        public static int Pass_Postfix_Filter(PCPInstance instance)
        {
            int i;
            Pair p;
            string str;
            for (i=0;i<instance.size;i++)
            {
                p =  instance.arrPair[i];

                if (p.downlen>p.uplen)
                {
                    str = p.down+(p.downlen - p.uplen);
                    if (!(str.Substring(0, p.uplen) == p.up.Substring(0, p.uplen)))
                        return 1;
                }
                else if (p.downlen<p.uplen)
                {
                    str = p.up+(p.uplen-p.downlen);
                    if (!(str.Substring(0, p.downlen) == p.down.Substring(0, p.downlen)))
                        return 1;
                }
            }
            return 0;
        }
Exemple #25
0
        // return value:
        //  1  -- solved, find solution
        //  -1 -- solved, no solution
        //  0  -- unsolved
        static public int SolvePCPInstance(PCPInstance instance, int iterative)
        {
            instance.CountOffset();

            int ret = 0;

            //PCPInstance ReversePCP;
            //// create the inverse instance
            //instance.CreateReversePCP(out ReversePCP);

            //Console.WriteLine("After reversal PCP Instance Is:");
            //instance.Print();



            //Mask.FindMask(instance, ReversePCP);

            //if (Convert.ToBoolean(instance.upmask) && Convert.ToBoolean(instance.downmask) ||
            //        Convert.ToBoolean(ReversePCP.upmask) && Convert.ToBoolean(ReversePCP.downmask))
            //    return -1;


            int solved_flag = 0;
            // try original direction
            int original_visit_nodenum;

            node_num = 0;
            ret      = SearchSolution(instance);
            if (solution_count >= 1)
            {
                solved_flag = 1;
            }
            else if (ret == -1)
            {
                solved_flag = -1;
            }

            original_visit_nodenum = node_num;


            ////TODO IMPLEMENT HASH TABLE
            ////ClearHashTable();

            //// try reverse direction
            //int reverse_visit_nodenum;
            //node_num = 0;

            //ret = SearchSolution(ReversePCP);
            //if (solution_count >= 1) solved_flag = 1;
            //else if (ret == -1) solved_flag = -1;

            //reverse_visit_nodenum = node_num;


            ////TODO: CONVERT TO C# HASHMAP
            ////ClearHashTable();


            if (solved_flag == 1)
            {
                Console.WriteLine("SOLUTION FOUND OR FALSE POSITIVE:");
                Console.WriteLine(PCPSolver.arrSelection);
            }


            return(0);
        }
Exemple #26
0
        public static int SearchSolution(PCPInstance instance)
        {
            //start at beginning.
            int startingpoint_flag = 0;

            int i, len, succ;
            int ret = -1, solveret = -1;
            Pair pPair;
            int maxlen = instance.offset*(100+1);
            Config pCacheConfig;

            //CConfig pNewconfig(maxlen);
            Config pNewConfig;

            pNewConfig = new Config(maxlen);

            for (i=0;i<instance.size;i++)
            {
             		        // initialize config
                pNewConfig.depth = 0;
                pNewConfig.len = 0;

                // if use exclusion method, startingpoints are meaningful
                if (startingpoint_flag == 1)
                {

                    if (!Convert.ToBoolean(instance.arrStartingPoint[i])) continue;

                }

               	            // simply search from the start
                if (startingpoint_flag == 0)
                {
                    pPair = instance.arrPair[i];
                    len = (pPair.uplen < pPair.downlen) ?
                            pPair.uplen : pPair.downlen;

                    if (pPair.up.Substring(len) == pPair.down.Substring(len)) continue;
                }

                // find one starting point
                pNewConfig.MatchPair(instance.arrPair[i]);

                // check masks
                if (Convert.ToBoolean(pNewConfig.up))
                    if (Convert.ToBoolean(instance.upmask)) continue;
                if (!Convert.ToBoolean(pNewConfig.up))
                    if (Convert.ToBoolean(instance.downmask)) continue;

                // add to the cache
                //succ = TryFind(hashTable, pNewConfig, &pCacheConfig);

                // try to solve
                solveret=SolveConfig(instance, pNewConfig, maxlen);

                if (solveret>0) // solve it
                    return solveret;
                if (solveret==0) // don't solve it to the iterative_depth_threshold
                    ret = 0;
            }

            return ret;
        }
Exemple #27
0
        /// <summary>
        /// Check whether there exists one pair whose one string is the prefix of the other.
        /// </summary>
        /// <param name="?">Instance being checked.</param>
        /// <returns></returns>
        public static int Pass_Prefix_Filter(PCPInstance instance)
        {
            int i;
            int len = 0;
            Pair p;

            for (i=0;i< instance.size; i++)
            {
                p = instance.arrPair[i];

                if (p.downlen > p.uplen)
                    len = p.uplen;
                else if (p.downlen < p.uplen)
                    len = p.downlen;

                if (p.down.Substring(0, len) == p.up.Substring(0, len))
                    return 1;
            }
            return 0;
        }
Exemple #28
0
 /// <summary>
 /// Identify the trivial case where some string has its downstring and upstring
 /// identical.
 /// </summary>
 /// <param name="?">Instance being checked.</param>
 /// <returns></returns>
 public static int Pass_Trivialcase_Filter(PCPInstance instance)
 {
     int i;
     Pair p;
     for (i=0; i < instance.size; i++)
     {
         p = instance.arrPair[i];
         if (p.downlen == p.uplen)
             if (p.down == p.up)
                 return 0;
     }
     return 1;
 }
Exemple #29
0
        public static int Solution_Found(PCPInstance instance,Config config)
        {
            int k;

            // for test, may not be solution, so need not record length and count
            //if (globalStatus == FIND_MASK || globalStatus == EXCLUSION_METHOD)
            //    return pConfig->depth;

            // just to find solution
            if (true)
            {
               // solution_length = pConfig->depth;
                solution_count = 1;
                return config.depth;
            }
        }
Exemple #30
0
        // Solve the instance with the given config
        // maxlen is the size of the maximum space allocated in pConfig
        // return value:
        //    -1: unsolvable
        //     0: unsolved till the depth threshold
        //   n>0: solved at depth of n
        static public int SolveConfig(PCPInstance instance, Config config, int maxLen)
        {
            int i, j, k;

            // store the matched pairs and the lengths of resulting configs
            int[] arrRetValue    = new int[17];
            int[] arrMatchedPair = new int[17];
            int   matchedPairNum = 0;

            // ret value
            int matchret = 0;
            int solveret;
            int ret = -1;
            int succ;
            int newlen;

            // store the location of the config in the cache
            Config pCacheConfig;
            Config pNewConfig;

            for (i = 0; i < instance.size; i++)
            {
                matchret = config.TestMatchingPair(instance.arrPair[i]);


                if (matchret == 0) // find one solution
                {
                    config.depth++;
                    PCPSolver.arrSelection[config.depth] = instance.arrPair[i].ID;
                    succ = Solution_Found(instance, config);
                    if (succ > 0)
                    {
                        return(config.depth);
                    }
                    else
                    {
                        ret = 0;
                    }
                    config.depth--;
                }
                else if (matchret > 0) // find one match
                {
                    //// having exceeded the threshold, prune it
                    //if (config.depth == iterative_depth_threshold - 1)
                    //    ret = 0;
                    //else  // record the pair
                    //{
                    arrRetValue[matchedPairNum]      = matchret;
                    arrMatchedPair[matchedPairNum++] = i;
                    //}
                }
            }

            //// sort the matched pair array according to its matchret value
            for (i = 0; i < matchedPairNum - 1; i++)
            {
                for (j = i + 1; j < matchedPairNum; j++)
                {
                    if (arrRetValue[i] > arrRetValue[j])
                    {
                        k = arrRetValue[i];
                        arrRetValue[i] = arrRetValue[j];
                        arrRetValue[j] = k;

                        k = arrMatchedPair[i];
                        arrMatchedPair[i] = arrMatchedPair[j];
                        arrMatchedPair[j] = k;
                    }
                }
            }

            // try all matched pairs
            for (i = 0; i < matchedPairNum; i++)
            {
                // the last matched pair and the config can be reused? Jump back!
                if (i + 1 == matchedPairNum && arrRetValue[i] <= maxLen)
                {
                    config.MatchPair(instance.arrPair[arrMatchedPair[i]]);
                }
            }

            return(matchret);
        }