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; }
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; } }
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; }
//public PCPSolver(PCPInstance instance){ // int ret; // int succ; // //clock_t time_start,time_end; // int time_tips; // int count = 0, nosol_count = 0, sol_count = 0, unsolved_count=0; // PCPInstance PCP, ReversePCP; // //time_start = clock(); // ret = SolvePCPInstance(instance, 0); // //time_end = clock(); //} public static int TryFind(Config arr, Config arr2, Config arr3) { throw new NotImplementedException(); }
// 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; }
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; }