def parsecsp(ConMatrix, buffer, TypeId, Vars, otpt_dir, operator, otpt): if otpt: # output each input QCN to a single file filename = otpt_dir + "InputQCN-" + operator + TypeId if not os.path.exists(otpt_dir): os.makedirs(otpt_dir) f = open(filename, "w") if globs["qcnNo"] <= 9: no_str = "00" + str(globs["qcnNo"]) elif globs["qcnNo"] <= 99: no_str = "0" + str(globs["qcnNo"]) else: no_str = str(globs["qcnNo"]) new_typeid = "" + TypeId new_typeid = new_typeid.replace("P10-RandomConstraints", "R0.00-D11.00-IA-") new_typeid = str(Vars - 2) + " " + new_typeid + no_str print(new_typeid, file=f) for line in buffer: l = line.strip().replace('(', '').replace(')', '').split() if otpt: print(line.strip(), file=f) # condition to end parsing if l == ['.']: break s = reduce(lambda x, y: x | y, [translateR(i) for i in l[2:]]) a = int(l[0]) b = int(l[1]) #print("l[0] is %d, l[1] is %d" % (a, b)) ConMatrix[int(l[0])][int(l[1])] = s ConMatrix[int(l[1])][int(l[0])] = inv(s) #print(ConMatrix[9]) if otpt: f.close()
def randomScenario(neighbors): # initialize constraints N = globs["size"] Id = 0 from helpfuncs import B_dict with open("allen.identity") as f: Id = B_dict[f.readline().strip()] if N <= 8: ConMatrix = tuple([ array('B', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) ConMatrixS = tuple([ array('B', [0 if i != j else Id for i in range(neighbors)]) for j in range(neighbors) ]) elif N <= 16: ConMatrix = tuple([ array('H', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) ConMatrixS = tuple([ array('H', [0 if i != j else Id for i in range(len(neighbors))]) for j in range(len(neighbors)) ]) else: ConMatrix = tuple([ array('I', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) ConMatrixS = tuple([ array('I', [0 if i != j else Id for i in range(len(neighbors))]) for j in range(len(neighbors)) ]) unsele_edges = [] for i in range(len(neighbors)): for j in neighbors[i]: # traverse each edge baselist = bitdecoding(ConMatrix[i][j]) if j > i and len(baselist) > 1: unsele_edges.append((i, j)) while unsele_edges: # there are edges need to be handled ran_e = random.randint(0, len(unsele_edges) - 1) # randomly select an edge u, v = unsele_edges[ran_e] baselist = bitdecoding(ConMatrix[u][v]) ran_b = random.randint(0, len(baselist) - 1) # randomly select a base relation b = baselist[ran_b] ConMatrix[u][v] = b # substitute the constraint ConMatrix[v][u] = inv(b) unsele_edges.remove( unsele_edges[ran_e]) # after handling an edge, remove it # G-consistent if not ppc(ConMatrix, neighbors): ConMatrix = randomScenario(neighbors) print("inconsistency occurs: ppc in random scenario") return ConMatrix return ConMatrix
def pPC(ConMatrix, neighbors): startT = time.time() pq = deque([]) # list of entries arranged in a heap entry_finder = set([]) # mapping of tasks to entries Vars = len(ConMatrix) #conmatrix行数 def add_task(task): 'Add a new task or update the priority of an existing task' # print("enter task") if task not in entry_finder: entry_finder.add(task) pq.append(task) def pop_task(): 'Remove and return the lowest priority task. Raise KeyError if empty.' task = pq.popleft() entry_finder.discard(task) return task # initialize the queue for i in range(Vars): for j in range(i + 1, Vars): if ConMatrix[i][j] != DALL: task = (i, j) add_task(task) # as long as the queue is not empty, process it while pq: (i, j) = pop_task() # grab the appropriate relation # create all triplets to be checked for path consistency for k in neighbors[i] & neighbors[ j]: # for each common neighbors of i and j temp = complex_relations( ConMatrix[k][i], ConMatrix[i][j]) # composition of ki and ij if temp != DALL: # constraint arc (k,i,j) temp = temp & ConMatrix[k][j] # (ki comp ij) & kj if temp != ConMatrix[k][ j]: # there is a relation in kj which is not in the composition of ki and ij if not temp: # kj and the composition of ki ij totally different passT = time.time() - startT globs["ppc_total"] += passT globs["ppc_num"] += 1 return False # inconsistency ConMatrix[k][j] = temp # remove inconsistent relation ConMatrix[j][k] = inv(temp) if k < j: add_task( (k, j) ) # after updating the constraint of kj, update the constraint between their neighbors and them else: add_task((j, k)) temp2 = complex_relations( ConMatrix[i][j], ConMatrix[j][k]) # composition of ij and jk if temp2 != DALL: # constraint arc (i,j,k) temp2 = temp2 & ConMatrix[i][k] if temp2 != ConMatrix[i][k]: if not temp2: passT = time.time() - startT globs["ppc_total"] += passT globs["ppc_num"] += 1 return False # inconsistency ConMatrix[i][k] = temp2 ConMatrix[k][i] = inv(temp2) if i < k: add_task((i, k)) else: add_task((k, i)) # the network is concistent and can't be refined further passT = time.time() - startT globs["ppc_total"] = globs["ppc_total"] + passT globs["ppc_num"] += 1 return True
def crossVarsB(N, neighbors, S1, S2): startT = time.time() V1 = [i for i in range(len(N))] V2 = [] while len(V1) > len(V2): ran_v = random.randint(0, len(V1) - 1) V2.append(V1[ran_v]) V1.remove(V1[ran_v]) n = globs["size"] Id = 0 from helpfuncs import B_dict with open("allen.identity") as f: Id = B_dict[f.readline().strip()] if n <= 8: N1 = tuple([ array('B', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) N2 = tuple([ array('B', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) elif n <= 16: N1 = tuple([ array('H', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) N2 = tuple([ array('H', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) else: N1 = tuple([ array('I', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) N2 = tuple([ array('I', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) for i in range(len(N1)): for j in range(i + 1, len(N1)): if i in V1 and j in V1: N1[i][j] = S1[i][j] N1[j][i] = S1[j][i] N2[i][j] = S2[i][j] N2[j][i] = S2[j][i] elif i in V2 and j in V2: N1[i][j] = S2[i][j] N1[j][i] = S2[j][i] N2[i][j] = S1[i][j] N2[j][i] = S1[j][i] a1 = alpha(N, N1) a2 = alpha(N, N2) if a1 <= a2: S = N1 else: S = N2 unsele_edges = [] for i in range(len(neighbors)): for j in neighbors[i]: baselist = bitdecoding(S[i][j]) if j > i and len(baselist) > 1: unsele_edges.append((i, j)) while unsele_edges: # if there exist some edges need to be handled ran_e = random.randint(0, len(unsele_edges) - 1) u, v = unsele_edges[ran_e] if S[u][v] & N[u][v] != 0: rel = S[u][v] & N[u][v] else: rel = S[u][v] rellist = bitdecoding(rel) ran_b = random.randint(0, len(rellist) - 1) S[u][v] = rellist[ran_b] S[v][u] = inv(rellist[ran_b]) unsele_edges.remove(unsele_edges[ran_e]) # G-consistent ppc(S, neighbors) #path consistency on the initial graph (partial) return S
def crossVarsA(N, neighbors, S1, S2): startT = time.time() V1 = [i for i in range(len(N))] V2 = [] while len(V1) > len(V2): ran_v = random.randint(0, len(V1) - 1) # randomly choose a variable V2.append(V1[ran_v]) V1.remove(V1[ran_v]) n = globs["size"] Id = 0 from helpfuncs import B_dict with open("allen.identity") as f: Id = B_dict[f.readline().strip()] if n <= 8: S = tuple([ array('B', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) elif n <= 16: S = tuple([ array('H', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) else: S = tuple([ array('I', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) for i in range(len(S)): for j in range(i + 1, len(S)): if i in V1 and j in V1: S[i][j] = S1[i][j] S[j][i] = S1[j][i] elif i in V2 and j in V2: S[i][j] = S2[i][j] S[j][i] = S2[j][i] unsele_edges = [] for i in range(len(neighbors)): for j in neighbors[i]: # traverse each edge baselist = bitdecoding(S[i][j]) if j > i and len(baselist) > 1: unsele_edges.append((i, j)) while unsele_edges: # if there exist some edges need to be handled ran_e = random.randint(0, len(unsele_edges) - 1) # randomly choose an edge u, v = unsele_edges[ran_e] if S[u][v] & N[u][v] != 0: rel = S[u][v] & N[u][v] else: rel = S[u][v] rellist = bitdecoding(rel) ran_b = random.randint(0, len(rellist) - 1) S[u][v] = rellist[ran_b] S[v][u] = inv(rellist[ran_b]) unsele_edges.remove(unsele_edges[ran_e]) # G-consistent if not ppc( S, neighbors): #path consistency on the initial graph (partial) print("Inconsistency occurs in crossConsB") return None passT = time.time() - startT globs["cross_total"] = globs["cross_total"] + passT globs["cross_num"] += 1 return S
def crossConsD(N, neighbors, S1, S2): startT = time.time() n = globs["size"] Id = 0 from helpfuncs import B_dict with open("allen.identity") as f: Id = B_dict[f.readline().strip()] if n <= 8: S = tuple([ array('B', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) elif n <= 16: S = tuple([ array('H', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) else: S = tuple([ array('I', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) # constraint of each edge is set as a random base relation unsele = [] unsele_not0 = [] unsele_0 = [] for i in range(len(neighbors)): for j in neighbors[i]: baselist = bitdecoding(S[i][j]) if j > i and len(baselist) > 1: unsele.append((i, j)) for (i, j) in unsele: if N[i][j] & S[i][j] == 0: unsele_0.append((i, j)) else: unsele_not0.append((i, j)) while unsele: # while there exist unhandled edges if unsele_not0: # if there exist some edges need to be handled which has common relation with the target QCN print("unsele_not0") ran_e = random.randint(0, len(unsele_not0) - 1) u, v = unsele_not0[ran_e] if S[u][v] & N[u][v] != 0: rel = S[u][v] & N[u][v] else: rel = S[u][v] if S1[u][v] & rel != 0: rel = S1[u][v] & rel elif S2[u][v] & rel != 0: rel = S2[u][v] & rel baselist = bitdecoding(rel) if len(baselist) > 1: ran_b = random.randint(0, len(baselist) - 1) elif len(baselist) > 0: ran_b = 0 else: print("crossover baselist 0 elements") return None b = baselist[ran_b] S[u][v] = b S[v][u] = inv(b) unsele.remove(unsele[ran_e]) unsele_not0.remove(unsele_not0[ran_e]) # G-consistent if not ppc(S, neighbors): print("Inconsistency occurs in crossConsD") return None for (i, j) in unsele_not0: if N[i][j] & S[i][j] == 0: unsele_0.append((i, j)) unsele_not0.remove((i, j)) elif unsele_0: print("unsele_0") ran_e = random.randint(0, len(unsele_0) - 1) u, v = unsele_0[ran_e] if S[u][v] & N[u][v] != 0: rel = S[u][v] & N[u][v] else: rel = S[u][v] if S1[u][v] & rel != 0: rel = S1[u][v] & rel elif S2[u][v] & rel != 0: rel = S2[u][v] & rel baselist = bitdecoding(rel) if len(baselist) > 1: ran_b = random.randint(0, len(baselist) - 1) elif len(baselist) > 0: ran_b = 0 else: print("crossover baselist 0 elements") return None b = baselist[ran_b] S[u][v] = b S[v][u] = inv(b) unsele.remove(unsele[ran_e]) unsele_0.remove(unsele_0[ran_e]) # G-consistent if not ppc(S, neighbors): print("Inconsistency occurs in crossConsD") return None print("----------------------after while----------------------") passT = time.time() - startT globs["cross_total"] = globs["cross_total"] + passT globs["cross_num"] += 1 return S
def crossConsA(N, neighbors, S1, S2): startT = time.time() n = globs["size"] Id = 0 from helpfuncs import B_dict with open("allen.identity") as f: Id = B_dict[f.readline().strip()] if n <= 8: S = tuple([ array('B', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) elif n <= 16: S = tuple([ array('H', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) else: S = tuple([ array('I', [ B_dict['DALL'] if i != j else Id for i in range(len(neighbors)) ]) for j in range(len(neighbors)) ]) # constraint of each edge is set as a random base relation unsele_edges = [] for i in range(len(neighbors)): for j in neighbors[i]: # each edge baselist = bitdecoding(S[i][j]) if j > i and len(baselist) > 1: unsele_edges.append((i, j)) while unsele_edges: # if there exist some edges need to be handled ran_e = random.randint(0, len(unsele_edges) - 1) u, v = unsele_edges[ran_e] ran_s = random.randint(0, 1) if ran_s == 0: S_1 = S1 S_2 = S2 else: S_1 = S2 S_2 = S1 if S[u][v] & N[u][v] != 0: rel = S[u][v] & N[u][v] else: rel = S[u][v] if S_1[u][v] & rel != 0: rel = S_1[u][v] & rel elif S_2[u][v] & rel != 0: rel = S_2[u][v] & rel baselist = bitdecoding(rel) ran_b = random.randint(0, len(baselist) - 1) b = baselist[ran_b] S[u][v] = b S[v][u] = inv(b) unsele_edges.remove(unsele_edges[ran_e]) # G-consistent if not ppc(S, neighbors): print("Inconsistency occurs in crossConsB") return None passT = time.time() - startT globs["cross_total"] = globs["cross_total"] + passT globs["cross_num"] += 1 return S
def exploreNeighborhoodAux(ini_N, ini_neighbors, ini_N_, ini_bestNg, besta): N = copy.deepcopy(ini_N) neighbors = copy.deepcopy(ini_neighbors) N_ = copy.deepcopy(ini_N_) bestNg = copy.deepcopy(ini_bestNg) # G-consistent from ppc import pPC as ppc if ppc(N_, neighbors): #path consistency on the initial graph (partial) a = alpha(N, N_) if a >= besta: return bestNg, besta unsele_edges = [] for i in range(len(neighbors)): for j in neighbors[i]: # traverse each edge baselist = bitdecoding(N_[i][j]) if j > i and len(baselist) > 1: unsele_edges.append((i, j)) if len(unsele_edges) > 0: # if there exist unhandled edges ran_e = random.randint(0, len(unsele_edges) - 1) # randomly choose one u, v = unsele_edges[ran_e] baselist = bitdecoding(N_[u][v]) ran_b = random.randint(0, len(baselist) - 1) # random base relation removeblist = copy.deepcopy(baselist) r1 = baselist[ran_b] # substitute to b removeblist.remove(removeblist[ran_b]) # remove b r2 = 0 for k in removeblist: r2 = r2 | k N1 = copy.deepcopy(N_) N2 = copy.deepcopy(N_) N1[u][v] = r1 N1[v][u] = inv(r1) N2[u][v] = r2 N2[v][u] = inv(r2) bestNgtmp, bestatmp = exploreNeighborhoodAux( N, neighbors, N1, bestNg, besta) # recursion if bestNgtmp is not None and bestatmp is not None: bestNg = bestNgtmp besta = bestatmp if besta <= 0.000001 and besta >= -0.000001: return bestNg, besta bestNgtmp, bestatmp = exploreNeighborhoodAux( N, neighbors, N2, bestNg, besta) # recursion if bestNgtmp is not None and bestatmp is not None: bestNg = bestNgtmp besta = bestatmp else: # if there is no unhandled edge bestNg = N_ besta = a return bestNg, besta else: return None, None