Skip to content

Commit

Permalink
Merge branch 'main' of github.com:kwrx/qasp
Browse files Browse the repository at this point in the history
  • Loading branch information
kwrx committed May 4, 2021
2 parents 8fc8582 + 11c91f9 commit 0661b9d
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 57 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/cmake-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: s-weigand/[email protected]

- name: Install Dependencies
shell: bash
working-directory: ${{github.workspace}}
run: sudo apt update && sudo apt install libboost-dev ninja-build
run: sudo apt update && sudo apt install libboost-dev ninja-build && conda install -c potassco clingo

- name: Create Build Environment (Debug)
run: cmake -E make_directory ${{github.workspace}}/debug
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/cmake-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: s-weigand/[email protected]

- name: Install Dependencies
working-directory: ${{github.workspace}}
shell: bash
run: sudo apt update && sudo apt install dpkg rpm libboost-dev ninja-build
run: sudo apt update && sudo apt install dpkg rpm libboost-dev ninja-build && conda install -c potassco clingo

- name: Create Build Environment
run: cmake -E make_directory ${{github.workspace}}/build
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ set(HAVE_PRETTY_LOGGER 1)

include(cmake/version.cmake)
include(cmake/package.cmake)
include(cmake/deps.cmake)
include(cmake/build.cmake)
include(cmake/wasp.cmake)
include(CPack)
include(GNUInstallDirs)

Expand Down
2 changes: 1 addition & 1 deletion cmake/build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES GNU OR CMAKE_CXX_COMPILER_ID MATCHES Clang)
endif()

if(HAVE_THREADS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_THREAD_LIBS_INIT}")
endif()
endif()

Expand Down
25 changes: 25 additions & 0 deletions cmake/deps.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
if(${HAVE_THREADS})
find_package(Threads REQUIRED)
endif()

if(${HAVE_WASP})

find_package(Boost REQUIRED)

if(Boost_FOUND)
add_subdirectory(lib/wasp)
endif()

endif()

if(${HAVE_CLASP})
message(FATAL_ERROR "clasp not yet supported")
endif()

if(${HAVE_GRINGO})
find_program(GRINGO gringo REQUIRED)
endif()

if(${HAVE_IDLV})
find_program(IDLV idlv REQUIRED)
endif()
10 changes: 0 additions & 10 deletions cmake/wasp.cmake

This file was deleted.

37 changes: 19 additions & 18 deletions src/qasp/QaspSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void QaspSolver::init() {
if(unlikely(program().subprograms().empty()))
throw std::invalid_argument("empty fragments collection");

if(unlikely(program().subprograms()[0].type() != TYPE_EXISTS))
throw std::invalid_argument("fragment #0 must be @exists");
// if(unlikely(program().subprograms()[0].type() != TYPE_EXISTS))
// throw std::invalid_argument("fragment #0 must be @exists");


const auto& found = std::find_if(program().subprograms().begin(), program().subprograms().end(), [] (const auto& i) {
Expand Down Expand Up @@ -82,26 +82,28 @@ bool QaspSolver::check(const AnswerSet& answer) const {



bool QaspSolver::get_coherent_answer(const Program& program, const std::vector<AnswerSet>& solution, std::vector<AnswerSet>& coherencies) const {

size_t incoherencies = 0;
size_t max = 0;
size_t QaspSolver::get_max_incoherencies(const Program& program, const std::vector<AnswerSet>& solution) const {

switch(program.type()) {

case TYPE_EXISTS:
max = solution.size();
break;
return solution.size();

case TYPE_FORALL:
max = 1;
break;
return 1;

default:
assert(0 && "BUG! Invalid Program Type");
return false;
throw std::runtime_error("invalid Program Type");

}

}


bool QaspSolver::get_coherent_answer(const Program& program, const std::vector<AnswerSet>& solution, const size_t& max, std::vector<AnswerSet>& coherencies) const {

size_t incoherencies = 0;

for(const auto& s : solution) {

if(check(s))
Expand Down Expand Up @@ -144,10 +146,10 @@ bool QaspSolver::execute(std::vector<Program>::iterator chain, Assumptions assum
return false;


// TODO: DUBBIO: COERENZA ALLA FINE O NEL MENTRE?
std::vector<AnswerSet> coherencies;
std::size_t max = get_max_incoherencies(program, solution);

if(!get_coherent_answer(program, solution, coherencies)) {
if(!get_coherent_answer(program, solution, max, coherencies)) {

LOG(__FILE__, ERROR) << "No sufficient coherent solution found for program #"
<< program.id() << std::endl;
Expand All @@ -157,16 +159,15 @@ bool QaspSolver::execute(std::vector<Program>::iterator chain, Assumptions assum



size_t success = 0;
std::size_t fail = 0;

for(const auto& i : coherencies)
assumptions.insert(assumptions.end(), i.begin(), i.end());

for(const auto& i : coherencies)
success += execute(chain + 1, assumptions, i) ? 1 : 0;

fail += execute(chain + 1, assumptions, i) ? 0 : 1;

return success > 0;
return fail < max;

}

Expand Down
3 changes: 2 additions & 1 deletion src/qasp/QaspSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ namespace qasp {
bool check(const AnswerSet& answer) const;
bool execute(std::vector<Program>::iterator chain, Assumptions assumptions = {}, AnswerSet answer = {});

bool get_coherent_answer(const Program& program, const std::vector<AnswerSet>& solution, std::vector<AnswerSet>& coherencies) const;
size_t get_max_incoherencies(const Program& program, const std::vector<AnswerSet>& solution) const;
bool get_coherent_answer(const Program& program, const std::vector<AnswerSet>& solution, const size_t& max, std::vector<AnswerSet>& coherencies) const;

inline void model(const ProgramModel value) {
this->__model = value;
Expand Down
8 changes: 4 additions & 4 deletions src/qasp/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ static std::vector<Program> parseSources(const std::vector<std::string>& sources


#ifdef DEBUG
LOG(__FILE__, TRACE) << "<PARSER> Found annotation with identifier: (" << programs.size() << ") @" << identifier.str()
LOG(__FILE__, TRACE) << "<PARSER> Found annotation with identifier: #" << programs.size() + 1 << " @" << identifier.str()
//<< " and source: \n" << source.str() << "\n"
<< " in " << (*begin).tk_source
<< " at " << (*begin).tk_line << ":" << (*begin).tk_column << std::endl;
#endif


if(identifier.str() == ANNOTATION_EXISTS)
programs.emplace_back(programs.size(), ProgramType::TYPE_EXISTS, source.str());
programs.emplace_back(programs.size() + 1, ProgramType::TYPE_EXISTS, source.str());
else if(identifier.str() == ANNOTATION_FORALL)
programs.emplace_back(programs.size(), ProgramType::TYPE_FORALL, source.str());
programs.emplace_back(programs.size() + 1, ProgramType::TYPE_FORALL, source.str());
else if(identifier.str() == ANNOTATION_CONSTRAINTS)
programs.emplace_back(programs.size(), ProgramType::TYPE_CONSTRAINTS, source.str());
programs.emplace_back(programs.size() + 1, ProgramType::TYPE_CONSTRAINTS, source.str());
else {

#ifdef DEBUG
Expand Down
12 changes: 2 additions & 10 deletions test/Test01.asp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@ a | b. % P1 = {a} {b}
c | d :- a. % P2 U {a} = {a,c} {a,d} -> {a,c} U C -> INCOHERENT | Non tutti gli AS(P2) U {a} sono coerenti con un AS(P1)
d :- b. % -> {a,d} U C -> COHERENT |
% P2 U {b} = {b,d} -> {b,d} U C -> COHERENT | Un AS(P1) = {b} è coerente con tutti gli AS(P2) U {b}
%@exists
e | f | g :- b, d. % P3 U {b,d} = {b,d,f} {b,d,f} -> {b,d,e} U C -> INCOHERENT |
% -> {b,d,f} U C -> COHERENT | Tutti gli AS(P2) sono coerenti con almeno un AS(P3) U {b,d}
% -> {b,d,g} U C -> COHERENT | Tutti gli AS(P2) sono coerenti con almeno un AS(P3) U {b,d}
%@forall
:- f. % P4 U {b,d,f} = INCOHERENT -> {b,d,f} U C -> INCOHERENT |
% P4 U {b,d,g} = {b,d,g} -> {b,d,g} U C -> COHERENT | Almeno un AS(P3) è coerente con tutti gli AS(P4) U {b,d,g}
%@constraints
{c;d;e}.
{c;d}.
:- c, not d. % C
:- e.

% Result: { b, d, g } is COHERENT.
% Result: { b, d } is COHERENT.
2 changes: 1 addition & 1 deletion test/Test01.expected
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{b,d,g}
{b,d}
23 changes: 15 additions & 8 deletions test/Test02.asp
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
%@exists
a | na.
b | nb.
a | b. % P1 = {a} {b}
%@forall
c | nc.
d | nd.
e | ne.
c | d :- a. % P2 U {a} = {a,c} {a,d} -> {a,c} U C -> INCOHERENT | Non tutti gli AS(P2) U {a} sono coerenti con un AS(P1)
d :- b. % -> {a,d} U C -> COHERENT |
% P2 U {b} = {b,d} -> {b,d} U C -> COHERENT | Un AS(P1) = {b} è coerente con tutti gli AS(P2) U {b}
%@exists
e | f | g :- b, d. % P3 U {b,d} = {b,d,f} {b,d,f} -> {b,d,e} U C -> INCOHERENT |
% -> {b,d,f} U C -> COHERENT | Tutti gli AS(P2) sono coerenti con almeno un AS(P3) U {b,d}
% -> {b,d,g} U C -> COHERENT | Tutti gli AS(P2) sono coerenti con almeno un AS(P3) U {b,d}
%@forall
:- f. % P4 U {b,d,f} = INCOHERENT -> {b,d,f} U C -> INCOHERENT |
% P4 U {b,d,g} = {b,d,g} -> {b,d,g} U C -> COHERENT | Almeno un AS(P3) è coerente con tutti gli AS(P4) U {b,d,g}
%@constraints
{c;d;e}.
:- c, not e.
:- d, not e.
:- not c, not d, e.
:- c, not d. % C
:- e.

% Result: { b, d, g } is COHERENT.
2 changes: 1 addition & 1 deletion test/Test02.expected
Original file line number Diff line number Diff line change
@@ -1 +1 @@
INCOHERENT
{b,d,g}
12 changes: 12 additions & 0 deletions test/Test03.asp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%@exists
a | na.
b | nb.
%@forall
c | nc.
d | nd.
e | ne.
%@constraints
{c;d;e}.
:- c, not e.
:- d, not e.
:- not c, not d, e.
1 change: 1 addition & 0 deletions test/Test03.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INCOHERENT
16 changes: 16 additions & 0 deletions test/Test04.asp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
% https://en.wikipedia.org/wiki/Boolean_satisfiability_problem
% ∀x ∀y ∃z (x ∨ y ∨ z) ∧ (¬x ∨ ¬y ∨ ¬z)

%@forall
x | nx.
%@forall
y | ny.
%@exists
z | nz.
%@constraints
{x;y;z;nx;ny;nz}.
:- nx, ny, nz.
:- x, y, z.


%% Result: COHERENT
6 changes: 6 additions & 0 deletions test/Test04.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{x,y,nz}
{x,ny,z}
{x,ny,nz}
{nx,y,z}
{nx,y,nz}
{nx,ny,z}
16 changes: 16 additions & 0 deletions test/Test05.asp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
% https://en.wikipedia.org/wiki/Boolean_satisfiability_problem
% ∀x ∀y ∀z (x ∨ y ∨ z) ∧ (¬x ∨ ¬y ∨ ¬z)

%@forall
x | nx.
%@forall
y | ny.
%@forall
z | nz.
%@constraints
{x;y;z;nx;ny;nz}.
:- nx, ny, nz.
:- x, y, z.


%% Result: INCOHERENT
1 change: 1 addition & 0 deletions test/Test05.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INCOHERENT

0 comments on commit 0661b9d

Please sign in to comment.