def test_consistency_random_kb(pdkb, kb): assert isinstance(pdkb, PDKB) NUM_RUNS = 10000 fluents = map(Literal, 'pqrst') agents = list(range(1, 4)) added = [] for i in range(0, NUM_RUNS): print >> sys.stderr, str(i) + " ", kb.reset() pdkb.reset() rmls = [ random_rml(4, agents, fluents), random_rml(3, agents, fluents), random_rml(2, agents, fluents), ] # Only add the RMLs if they are consistent check_consistency = PDKB(4, agents, fluents) check_consistency.update(set(rmls)) if check_consistency.is_consistent(): assert pdkb.is_consistent() kb_copy = kb.copy() pdkb_copy = pdkb.copy() pdkb.update(set(rmls)) kb.update(set(rmls)) rml = compare(pdkb, kb) if isinstance(rml, RML): kb_copy.restrict(rml) pdkb_copy.restrict(rml) kb.restrict(rml) pdkb.restrict(rml) print "before = " + str(kb_copy) print "before = " + str(pdkb_copy) print "\t add " + str(rmls) print "\t after = " + str(kb) print "\t after = " + str(pdkb) sys.exit()
def generate_kbs(): numRMLs = num_agents #num_agents * depth * 2 closed_kb = PDKB(depth, agents, fluents) indexed_kb = IndexedPDKB(depth, agents, fluents) count = 0 while count < numRMLs: next_rml = random_rml(depth, agents, fluents) if not closed_kb.query(neg(next_rml)): closed_kb.add_rml(next_rml) indexed_kb.expand(set([next_rml])) count += 1 inf_kb = [] #INF.PDKB2INF(closed_kb) return (inf_kb, closed_kb, indexed_kb)
def get_size_and_time(num_agents, depth, fluents): agents = range(1, num_agents + 1) def generate_kbs(): numRMLs = num_agents #num_agents * depth * 2 closed_kb = PDKB(depth, agents, fluents) indexed_kb = IndexedPDKB(depth, agents, fluents) count = 0 while count < numRMLs: next_rml = random_rml(depth, agents, fluents) if not closed_kb.query(neg(next_rml)): closed_kb.add_rml(next_rml) indexed_kb.expand(set([next_rml])) count += 1 inf_kb = [] #INF.PDKB2INF(closed_kb) return (inf_kb, closed_kb, indexed_kb) ''' print print "Generating %d PDKBs..." % NUM_PDKBS kbs = [] infs = [] indexed_kbs = [] progress = 10 trial = 1 for i in range(NUM_PDKBS): if trial > progress: print "%d%%" % progress progress += 10 trial += 1 (inf_kb, closed_kb, indexed_kb) = generate_kbs() kbs.append(closed_kb) indexed_kbs.append(indexed_kb) infs.append(inf_kb) print print "Closing PDKBs..." closed_kbs = [kb.copy() for kb in kbs] closure_time = [] progress = 10 trial = 1 for kb in closed_kbs: if trial > progress: print "%d%%" % progress progress += 10 trial += 1 start = now() kb.logically_close() assert kb.is_consistent() closure_time.append(now() - start) print print "Computing INFs..." for kb in kbs: start = now() infs.append(INF.PDKB2INF(kb)) inf_time.append(now() - start) ''' def run_queries(index, rml, infs_kb, closed_kb, indexed_kb): start = now() #ans1 = infs_kb.query(rml) inf_query = 0.0 #now() - start start = now() #ans2 = rml in closed_kbs[index].rmls ans2 = closed_kb.query(rml) closed_query = now() - start start = now() ans3 = indexed_kb.query(rml) indexed_query = now() - start ans1 = ans2 assert ans1 == ans2 assert ans2 == ans3 # Copy the KBs to run update commands without changing the original KBs copy_kb = closed_kb.copy() copy_indexed_kb = indexed_kb.copy() #start = now() # INF update is not yet implemented... inf_update = 0.0 #now() - start start = now() copy_kb.update(set([rml])) closed_update = now() - start start = now() copy_indexed_kb.update(set([rml])) indexed_update = now() - start return (ans1, ans2, ans3, inf_query, closed_query, indexed_query, inf_update, closed_update, indexed_update) #print "Performing random misc queries..." for i in range(NUM_PDKBS): #for i in range(0): (infs_kb, closed_kb, indexed_kb) = generate_kbs() for j in range(QUERIES_PER_PDKB): rml = random_rml(closed_kb.depth, closed_kb.agents, closed_kb.props) (ans1, ans2, ans3, inf_query, closed_query, indexed_query, inf_update, closed_update, indexed_update) = run_queries(i, rml, infs_kb, closed_kb, indexed_kb) #(inf_update, closed_update, indexed_update) = #csv.append("%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%s" % # (len(kbs[i].agents), kbs[i].depth, len(kbs[i].props), len(kbs[i].rmls), len(closed_kbs[i].rmls), infs[i].size(), closed_kbs[i].size(), kbs[i].size(), # inf_time[i], closure_time[i], inf_query, closed_query, indexed_query, str(ans1))) print "Performing random successful queries..." times = [0.0,0.0,0.0,0.0,0.0,0.0] for i in range(NUM_PDKBS): (infs_kb, closed_kb, indexed_kb) = generate_kbs() for j in range(QUERIES_PER_PDKB): # Get a random RML from the PDKB rml = random.choice(list(closed_kb.rmls)) # Get the closed set #entailed = list(kd_closure(rml)) # Pick a random element #rml = random.choice(entailed) #(infs_kb, closed_kb, indexed_kb) = generate_kbs() (ans1, ans2, ans3, inf_query, closed_query, indexed_query, inf_update, closed_update, indexed_update) = run_queries(i, rml, infs_kb, closed_kb, indexed_kb) assert ans1 == ans2 assert ans2 == ans3 times[0] += inf_query times[1] += closed_query times[2] += indexed_query times[3] += inf_update times[4] += closed_update times[5] += indexed_update #csv_yes.append("%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%s" % # (len(kbs[i].agents), kbs[i].depth, len(kbs[i].props), len(kbs[i].rmls), len(closed_kbs[i].rmls), infs[i].size(), closed_kbs[i].size(), kbs[i].size(), # inf_time[i], closure_time[i], inf_query, closed_query, indexed_query, str(ans1))) sizes = [0.0, 0.0, 0.0] print "Performing random unsuccessful queries..." for i in range(NUM_PDKBS): (infs_kb, closed_kb, indexed_kb) = generate_kbs() sizes[0] += 0.0 #infs_kb.size() sizes[1] += closed_kb.size() sizes[2] += indexed_kb.size() for j in range(QUERIES_PER_PDKB): going = True while going: rml = random_rml(closed_kb.depth, closed_kb.agents, closed_kb.props) if rml not in closed_kb.rmls: going = False (ans1, ans2, ans3, inf_query, closed_query, indexed_query, inf_update, closed_update, indexed_update) = run_queries(i, rml, infs_kb, closed_kb, indexed_kb) assert ans1 == ans2 assert ans2 == ans3 times[0] += inf_query times[1] += closed_query times[2] += indexed_query times[3] += inf_update times[4] += closed_update times[5] += indexed_update #csv_no.append("%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%f,%f,%f,%s" % # (len(kbs[i].agents), kbs[i].depth, len(kbs[i].props), len(kbs[i].rmls), len(closed_kbs[i].rmls), infs[i].size(), closed_kbs[i].size(), kbs[i].size(), # inf_time[i], closure_time[i], inf_query, closed_query, indexed_query, str(ans1))) times[0] /= float(NUM_PDKBS * QUERIES_PER_PDKB * 2) times[1] /= float(NUM_PDKBS * QUERIES_PER_PDKB * 2) times[2] /= float(NUM_PDKBS * QUERIES_PER_PDKB * 2) times[3] /= float(NUM_PDKBS * QUERIES_PER_PDKB * 2) times[4] /= float(NUM_PDKBS * QUERIES_PER_PDKB * 2) times[5] /= float(NUM_PDKBS * QUERIES_PER_PDKB * 2) #sizes.append(float(sum([inf.size() for inf in infs])) / float(NUM_PDKBS)) #sizes.append(float(sum([kb.size() for kb in kbs])) / float(NUM_PDKBS)) #sizes.append(float(sum([kb.size() for kb in indexed_kbs])) / float(NUM_PDKBS)) sizes[0] /= float(NUM_PDKBS) sizes[1] /= float(NUM_PDKBS) sizes[2] /= float(NUM_PDKBS) print "\nDone!\n" return (times, sizes)