Skip to content

Commit

Permalink
minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Law committed May 1, 2023
1 parent ac67e76 commit dd92b5e
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 123 deletions.
4 changes: 3 additions & 1 deletion FastLAS2/implementation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include_directories(/usr/local/include)


if(${APPLE})
set(BISON_EXECUTABLE "/opt/homebrew/opt/bison/bin/bison")
else()
set(THREADS_PREFER_PTHREAD_FLAG ON)
set(CMAKE_CXX_FLAGS -pthread)
Expand Down Expand Up @@ -71,4 +72,5 @@ add_library(
)

add_executable(FastLAS main.cpp)
target_link_libraries(FastLAS FastLAS_LIB ${Boost_LIBRARIES})
#target_link_libraries(FastLAS FastLAS_LIB ${Boost_LIBRARIES})
target_link_libraries(FastLAS FastLAS_LIB ${Boost_LIBRARIES} clingo)
7 changes: 6 additions & 1 deletion FastLAS2/implementation/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ void FastLAS::Clingo::operator()(const std::function<void()>& final_fn) const {
auto ret = system(string("clingo --outf=3 " + args + " " + inpipe + " > " + outpipe + " 2> /dev/null").c_str());
#endif

mtx.lock();

string buffer, incremental_buffer = "";
ifstream proc(outpipe);
stringstream full_string;
Expand All @@ -228,6 +230,9 @@ void FastLAS::Clingo::operator()(const std::function<void()>& final_fn) const {
incremental_buffer = "";
}
}

mtx.unlock();

//cout << full_string.str() << endl;

proc.close();
Expand Down Expand Up @@ -270,7 +275,7 @@ void FastLAS::add_example(const string& id, set<NAtom*>*& incs, set<NAtom*>*& ex
}


void FastLAS::write_cache(const std::string& path) {
void FastLAS::write_cache(const string& path) {
stringstream ss;

ss << "#cache {" << endl;
Expand Down
9 changes: 9 additions & 0 deletions FastLAS2/implementation/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ int main(int argc, char **argv) {
("version", "output version information.")
("categorical-contexts", "speeds up abduction if the \"bottom\" parts of all programs are known to have exactly one answer set.")
("debug", "verbose output.")
("delay-generalisation", "experimental optimisation approach.")
("nopl", "run the new phases of the FastNonOPL algorithm, needed for non-observational predicate learning.")
("opl", "do not run the new phases of the FastNonOPL algorithm, needed for non-observational predicate learning.")
("output-solve-program", "perform the main steps of the FastLAS algorithm, then write out the final ASP program used to search for an optimal solution.")
Expand Down Expand Up @@ -168,6 +169,9 @@ int main(int argc, char **argv) {
FastLAS::print_c_minus();
}

if (vm.count("write-cache")) {
FastLAS::write_cache(vm["write-cache"].as<string>());
}

if (vm.count("delay-generalisation")) {
if(debug) cout << "Computing opt-sufficient subset..." << endl << endl;
Expand All @@ -180,6 +184,11 @@ int main(int argc, char **argv) {
cout << "G^+(T):" << endl;
FastLAS::print_c_plus();
}

if (vm.count("write-cache")) {
FastLAS::write_cache(vm["write-cache"].as<string>());
}

if(debug) cout << "Computing opt-sufficient subset..." << endl << endl;

FastLAS::optimise();
Expand Down
25 changes: 12 additions & 13 deletions FastLAS2/implementation/meta_programs/OptimiseSym.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function onModel(m)
bound = 0
end
mp = new_model.." b"..tostring(bound).."| ;|"
print(mp)
end
function main(prg)
Expand All @@ -78,22 +79,20 @@ function main(prg)
violating = true
constraint = ""
prg:ground({{"base", {}}})
while violating do
violating = false
sat = false
prg:add("cons".."_"..index, {}, constraint)
prg:ground({{"cons".."_"..index, {}}})
prg:solve{on_model=onModel}
if sat then
print(mp)
end
end
prg:solve{on_model=onModel}
end
#end.
:~ penalty(P, ARGS), not invert(_).[P@1, ARGS]
:~ penalty(P, ARGS), invert(_).[-P@1, ARGS]
#show in/1.
#show len/1.
#show eg_uncov/1.
#show disj_satisfied/1.
pen(1..L) :- #sum { A, T : penalty(T, A) } = L, L < 10.
#heuristic len(A).[1@1, false]
#heuristic eg_uncov(A).[1@1, false]
#heuristic disj_satisfied(A).[1@1, true]
:- var(V), type(V, T1), type(V, T2), T1 < T2.
:- var(V), #count { N : type(V, N) } > 1.
Expand Down
22 changes: 20 additions & 2 deletions FastLAS2/implementation/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ void yyerror (std::string s) {
exit(1);
}

void check(std::string id) {
static std::set<std::string> ids;
if(ids.find(id) == ids.end()) {
ids.insert(id);
} else {
std::cerr << "Duplicated example ID " << id << "." << std::endl;
exit(2);
}
}


%}
%define parse.error verbose
Expand Down Expand Up @@ -149,8 +159,16 @@ task : task rule { background.push_back(*$2); delete $2;}
;


identifier : atom T_COMMA { $$ = new std::pair<std::string, int>($1->to_string(), -1); delete $1; }
| atom T_AT T_INT[penalty] T_COMMA { $$ = new std::pair<std::string, int>($1->to_string(), std::stoi(*$penalty)); delete $1; }
identifier : atom T_COMMA {
check($1->to_string());
$$ = new std::pair<std::string, int>($1->to_string(), -1);
delete $1;
}
| atom T_AT T_INT[penalty] T_COMMA {
check($1->to_string());
$$ = new std::pair<std::string, int>($1->to_string(), std::stoi(*$penalty));
delete $1;
}
| { $$ = new std::pair<std::string, int>("eg___" + std::to_string(eg_count++), -1); }
;

Expand Down
65 changes: 3 additions & 62 deletions FastLAS2/implementation/stages/Abduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ void FastLAS::abduce() {
int iterations = 0;

while(!partial_possibilities.empty()) {
static mutex p_mtx;
p_mtx.lock();
auto exceptions = anti_abduce(hyp_dependent_predictates, starting_point.first, partial_possibilities, eg);
for(auto p : exceptions) {
auto negated_exceptions = FastLAS::cnf_to_dnf(p.second);
Expand All @@ -375,6 +377,7 @@ void FastLAS::abduce() {
if(iterations > 10) {
cout << eg->id << ":" << iterations << endl;
}
p_mtx.unlock();
}


Expand All @@ -383,68 +386,6 @@ void FastLAS::abduce() {
}
}

//for(auto raw_ctx : raw_ctxs) {
// map<set<int>, set<set<int>>> poss_disjs;
// if(FastLAS::separate_abduction) {
// poss_disjs.insert(make_pair(raw_ctx, set<set<int>>()));
// }
// set<int> ctx, disj;
// bool null_sol = false;

// //sc_mtx.lock();
// cout << "----" << endl;
// cout << get_sat_suff_representation(eg, raw_ctx) << endl;
// //exit(2);

// Clingo(get_sat_suff_representation(eg, raw_ctx), "--heuristic=Domain --enum-mode=domRec --dom-mod=5,16 -n 0")
// ('i', [&](const string& atom) {
// ctx.insert(get_language_index(atom));
// })
// ('t', [&](const string& atom) {
// disj.insert(get_language_index(atom));
// }) ('n', [&](const string& atom) {
// disj.insert(-get_language_index(atom) - 1);
// }) ('g', [&](const string&) {
// null_sol = true;
// }) ([&]() {
// sc_mtx.lock();
// if(FastLAS::separate_abduction) {
// poss_disjs[raw_ctx].insert(disj);
// } else if(null_sol) {
// if(poss_disjs.find(ctx) == poss_disjs.end()) {
// poss_disjs.insert(make_pair(ctx, set<set<int>>()));
// }
// } else {
// poss_disjs[ctx].insert(disj);
// }
// null_sol = false;
// ctx.clear();
// disj.clear();
// sc_mtx.unlock();
// }
// );

// if(FastLAS::solver.compare("ILASP") == 0) {
// for(auto p : poss_disjs) {
// eg->add_possibility(p.first, p.second);
// }
// } else {
// for(auto p : poss_disjs) {
// auto dnf = FastLAS::cnf_to_dnf(p.second);
// for(auto conj : dnf) {
// set<int> incs, excs;
// for(auto c : conj) {
// if(c < 0) {
// incs.insert(-c - 1);
// } else {
// excs.insert(c);
// }
// }
// eg->add_possibility(incs, excs, p.first);
// }
// }
// }
//}
});


Expand Down
5 changes: 3 additions & 2 deletions FastLAS2/implementation/stages/Optimise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ void write_global_file(const string& global_pipe, int head=-1) {
}

if(FastLAS::force_safety) {
ss << ":- occurs_head(V), not occurs_pos(V)." << endl;
ss << ":- occurs_neg(V), not occurs_pos(V)." << endl;
ss << ":- var(V), occurs_head(V), not occurs_pos(V)." << endl;
ss << ":- var(V), occurs_neg(V), not occurs_pos(V)." << endl;
for(int i = 0; i < bias->maxv; i++) ss << "var(v_a_r" << i << ")." << endl;
}
//cout << ss.str() << endl;
//exit(2);
Expand Down
47 changes: 28 additions & 19 deletions FastLAS2/implementation/stages/OptimiseSym.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ void FastLAS::optimise_sym() {
for(auto head : heads) {
stringstream ss;

set<set<set<int>>> processed_cnfs, processed_n_cnfs;

ss << "in_head(" << FastLAS::language[head] << ")." << endl;
set<int> body_literals;
for(auto eg : examples) {
Expand Down Expand Up @@ -114,15 +116,18 @@ void FastLAS::optimise_sym() {
}
}
auto cnf = cnf_to_dnf(dnf);
for(auto c : cnf) {
ss << "disj_n_satisfied(" << disj_id << ") :- #true";
for(int bl : c) {
ss << ", in(" << bl << ")";
if(processed_cnfs.find(cnf) == processed_cnfs.end()) {
processed_cnfs.insert(cnf);
for(auto c : cnf) {
ss << "disj_n_satisfied(" << disj_id << ") :- #true";
for(int bl : c) {
ss << ", in(" << bl << ")";
}
ss << "." << endl;
}
ss << "." << endl;
ss << "disj_satisfied(" << disj_id << ") :- not disj_n_satisfied(" << disj_id << ")." << endl;
disj_id++;
}
ss << "disj_satisfied(" << disj_id << ") :- not disj_n_satisfied(" << disj_id << ")." << endl;
disj_id++;
}
set<set<int>> dnf;
for(auto schema : sub_eg->get_rule_violations()) {
Expand All @@ -137,18 +142,20 @@ void FastLAS::optimise_sym() {
}
}
auto cnf = cnf_to_dnf(dnf);
for(auto c : cnf) {
ss << "eg_cov(" << eg->id << ", " << disj_id << ") :- #true";
for(int bl : c) {
ss << ", in(" << bl << ")";
if(processed_n_cnfs.find(cnf) == processed_n_cnfs.end()) {
processed_n_cnfs.insert(cnf);
for(auto c : cnf) {
//ss << "sub_eg_cov(" << sub_eg->id << ", " << disj_id << ") :- ab_rep(" << sub_eg->id << ")";
ss << "sub_eg_cov(" << sub_eg->id << ", " << disj_id << ") :- #true";
for(int bl : c) {
ss << ", in(" << bl << ")";
}
ss << "." << endl;
}
ss << "." << endl;
}
if(eg->get_penalty() > 0) {
ss << "eg_uncov(" << eg->id << ")";
ss << "eg_uncov(" << sub_eg->id << ") ";
ss << ":- not sub_eg_cov(" << sub_eg->id << ", " << disj_id << ")." << endl;
disj_id++;
}
ss << ":- not eg_cov(" << eg->id << ", " << disj_id << ")." << endl;
disj_id++;
}
}
for(int i = 0; i < bias->maxv; i++) ss << "var(v_a_r" << i << ")." << endl;
Expand All @@ -159,11 +166,13 @@ void FastLAS::optimise_sym() {

ss << R"ESC(
:- not disj_satisfied(_).
:- occurs_pos(V), var(V2), not occurs_pos(V2), V > V2.
:- var(V), occurs_pos(V), var(V2), not occurs_pos(V2), V > V2.
)ESC" << endl;
ss << optimise_sym_meta_prg;
ss << bias->bias_constraints << endl << endl;

//static mutex mtx;
//mtx.lock();
//cerr << ss.str() << endl;
//exit(2);

Expand All @@ -172,7 +181,7 @@ void FastLAS::optimise_sym() {
set<string> intermediate_sf_facts;
map<string, string> types;

Clingo(ss.str(), "")
Clingo(ss.str(), "--enum=domrec --heuristic=domain -n0")
('i', [&](const string& atom) {
rule_body.insert(stoi(atom));
}) ('n', [&](const string& atom) {
Expand Down
Loading

0 comments on commit dd92b5e

Please sign in to comment.