How do I encode this in answer set programming? - answer-set-programming

I am a total newbie to answer set programming, and I am struggling with a rather simple question. The program needs to be written in clingo.
So here is the question:
An abstract argumentation framework consists of a set A of arguments
and an attack relation R ⊆ A X A between them. For any two arguments
a1 and a2, if (a1, a2) ∈ R then we say that a1 attacks a2: if one
admits argument a1 then it casts doubts on argument a2. Formally, a
subset of arguments E ⊆ A is stable if the following two conditions
hold:
no arguments in E attack any other argument of E.
any argument outside of E is attacked by an argument from E.
Write an ASP program that identifies stable subsets of arguments in a
given instance through answer sets. The instance will be provided via
two predicates argument/1 and attack/2 corresponding to A and R
respectively.
Here is an example:
argument (a).
argument (b).
argument (c).
argument (d).
attack (a,b).
attack (b,c).
attack (d,c).
Valid output:
choose (a) choose (d)
This what I tried, which is obviously wrong:
choose(X) :- argument(X), attack(X,Y).
I don't know how to approach this at all.
Please help.

A simple 3 Step solving approach is the following:
describe the facts (check)
generate what you want as a result, but leave the program a choice
give rules which solutions do not apply
So start with 2:
generate possible outcomes. Think of it in simple words: For every argument I choose it or not.
The may or may not part can be solved with a subsum {}.
{choose(X)} :- argument(X).
or even simpler: I choose a subsum from the arguments
{choose(X):argument(X)}.
Lets check the solutions with Potassco and #show choose/1., resoning mode enumerate all:
Answer: 1
Answer: 2
choose(b)
Answer: 3
choose(c).
..
Answer: 15
choose(a) choose(b) choose(c)
Answer: 16
choose(a) choose(b) choose(c) choose(d)
SATISFIABLE
All combinations are found. Time to remove the wrong stuff. Again: think of it in simple words: It is not possible that I choose two arguments where one attacks the other. (If the head is left open, this is read a False.)
:- choose(X), attack(X,Y), choose(Y).
Now check it again:
Answer: 1
Answer: 2
choose(a)
Answer: 3
choose(d)
Answer: 4
choose(a) choose(d)
Answer: 5
choose(c)
Answer: 6
choose(a) choose(c)
Answer: 7
choose(b)
Answer: 8
choose(b) choose(d)
SATISFIABLE
Now we need to make sure every not choosen argument is attacked by a at least one choosen element:
1 {choose(Y):attack(Y,X)} :- argument(X), not choose(X).
Reads: For every argument X, which is not choosen, the number of choosen arguments which attack it, is at least one.
Lets check it:
Answer: 1
choose(a) choose(d)
SATISFIABLE
Nice.
Since the contraints are normally formulated with an empty head, lets reformulate the last rule:
:- argument(X), not choose(X), {choose(Y):attack(Y,X)} 0.
Reads: There is no argument X, which is not choosen and has maximum 0 choosen arguments, which attack X. It gives the same output.
Complete code:
argument (a;b;c;d).
attack (a,b).
attack (b,c).
attack (d,c).
{choose(X):argument(X)}.
:- choose(X), attack(X,Y), choose(Y).
:- argument(X), not choose(X), {choose(Y):attack(Y,X)} 0.
#show choose/1.

Related

Converge (fixed point) syntax difference in q and k

We should use square brackets when flattering all levels in list:
q)b:(1 2;(3 4;5 6);7;8)
q)raze/[b] / flatten all levels
1 2 3 4 5 6 7 8
q)raze/b
'/
[0] raze/b
But why one forced to use raze/[b] for Converge syntax instead of simple raze/b?
Upd
Why this syntax works in k, for example {x+y}/1 2 3 but doesn't work in q?
My assumption that it's been made to prevent qbies errors when using / adverb instead of %. I think there may be a discussion about it in some dev channel, but I've found only Shakti discussion group for now at https://groups.google.com/forum/#!forum/shaktidb, and kx.com also shutted down community wiki, so I don't know where to find an additional info - asking here
Upd2
The / is quite overloaded in k too: see (not official ref though) https://github.com/JohnEarnest/ok/blob/gh-pages/docs/Manual.md#over - over, fixedpoint, for and while. Pretty the same as in q, right? But why the interpreter 'ban' the k syntax in q context, - is there a technical reason why q can't recognise a user intention as it k does?
The reason that, say, cos/1 works in k but not q is that q has no ambivalence. That is to say that all q operators are not overloaded on valence, as noted in section 6.1.2 in q4m.
With any of q's adverbs (each:', over:/ scan:\, prior::, eachright:/:, eachleft:\:) the resulting derivative function is interpreted by q to be dyadic, unless you uses []'s to specifically pass one argument.
For the example above q interprets cos/ as do rather than converge and so requires the left argument specifying the number of iterations (Note the results of 0 cos/ 1, 1 cos/ 1, 2 cos/ 1, etc.). The preferred way to resolve is to use []'s: cos/[1].
(cos/) 1 works because user defined functions can never use infix notation, so the expression is automatically interpreted as applying monadically. This is also why 2 (cos/) 1 fails. To resolve this you would again have to use []'s: (cos/)[2;1].
You don't necessarily need square brackets here. You could use
(raze/)b
if you do not want to use square brackets around b. The way you are using over ( or /) without the brackets around b requires the parenthesis around raze/b if you do not specify the initial value of the accumulator. This is because the q interpreter needs to know that you are applying raze/ to the list b rather than applying / to the list first (which is why a '/ error is thrown) then raze after (reading the code from right to left).
More info on using / can be found here: https://code.kx.com/q4m3/6_Functions/#676-over-for-accumulation

regress with a constraint/condition

I want to execute a regression but I need a condition/constraint in the command but mine doesn't work. My dependent variable is COMP_STD and my independent is BGroup. BGroup has the following values: 1 2 3 4 5.
I want to run 5 different regressions with the variable BGroup, so that this variable equals 1, 2, and so on. This is the syntax that I tried:
regress COMP_STD if inrange (BGroup, 1)
but Stata says inrange not found so I guess it has to be another syntax which I can't find.
inrange() shouldn't be presented as inrange (whatever), i.e. with a following space. There is a hint that Stata can't understand you; otherwise why is it asking about inrange? The reason for that puzzlement is that --when separated from its arguments -- a bare inrange looks like the name of a variable (or scalar), and you have no such variable (or scalar).
You didn't get that far, but the inrange() call was illegal for another reason. It needs three arguments, e.g. if inrange(BGroup, 1, 1).
But if BGroup == 1 is much simpler.
Note that regress is irrelevant here as the problem is entirely about selecting observations. That said, statsby is a command possibly useful here for getting several regressions at once.

Tags in vowpal wabbit

I am doing binary classification using vowpal-wabbit. A particular record(set of features) has 10 zeroes and 5 ones. So, I am creating two lines in vowpal-format
-1 10 `50 |f f1
1 5 `50 |f f1
Since the prediction(probability) for both these records would be same, I want to keep the same tag, so that I can dedupe the predictions({tag,prediction}) later and join with my original raw-data.
Is it possible to keep the same tag for more than one record in vowpal-wabbit?
First, the syntax above isn't correct
To be identified as such, tags should either:
Touch the | separator (no space between them) OR
The leading quote, needs to be a simple quote, not a backquote, by convention.
(or both).
Otherwise you get:
warning: `50 is not a good float, replacing with 0
warning: `50 is not a good float, replacing with 0
Which hints that vw interprets these "tags" as prediction-base.
For details, see Input format in the official documentation
Once the example is fixed to the correct syntax:
-1 10 '50|f f1
1 5 '50|f f1
Which runs fine, we can answer the question:
Is it possible to keep the same tag for more than one record in vowpal-wabbit?
Yes, you can. The tag is merely a simple way to connect input and output (when predictions are involved), there's no check for uniqueness anywhere. If you duplicate tags on input, you'll simply get the same duplicate tags on prediction output as well.
More notes:
Even if two examples are identical, you may get different predictions, if the model has changed somewhat in between them. Remember vw is an online learner, so the model can continuously change with each example unless you add the -t (test-only, don't learn) option.
Features whose value is zero are ignored, so you can drop them. The standard way in vw to say this is 'positive' and this is 'negative' is to use the values {+1, -1}. This is true for both labels and input features.

UIMA Ruta. Retrieve phrases separated by WS (spaces, breaks, etc.)

I'm going to retrieve phrases separated by spaces, breaks and other punctuation symbols.
I've spent a lot of time trying to find out the best way to do that.
Option 1. The easiest way.
DECLARE T1, T2;
"cool rules" -> T1;
"cool rule" -> T2;
Input: "123cool rules".
Result: T1 and T2 are triggered;
Option 2. Using WORDLIST and WORDTABLE.
Let wordlist 1.txt contains 2 rows:
cool rules
cool
code for extraction is the following
WORDLIST WList = '1.txt';
DECLARE W1;
Document{-> MARKFAST(W1, WList, true, 2)};
Input: "cool rules".
Result: only first row is extracted. I guess that in this case intersected rules are not triggered.
Option 3. Mark combination of two tokens
DECLARE T1;
("cool" "rule") {-> T1};
Input: "cool rules cool rule 1cool rule"
Result: 2 annotations: cool rule + 1cool rule. Loss of extraction speed in 10 times.
Option 4. REGEXP matching
Maybe it is possible to match such pattern "cool\\srule", but I have no idea how to define the type expression. SW*{REGEXP("cool\\srule")->T1} does not provide results.
As you see, I'm trying to solve a very simple task, but did not succeed yet. The option 3 is a really good way to do that, but extraction process becomes slower in 10 times.
If you want to identify specific phrases, you should use a dictionary lookup, not directly rules.
Therefore, I'd recommend the MARKFAST option 2. However, there are two problems: (a) only longest matches are supported and (b) you either need to change the segmentation (tokenization) or do some postprocessing.
(a) This cannot be solved. If this is really required, a different dictionary annotator should be used. See e.g., the UIMA mailing lists.
(b) The MARKFAST works on RutaBasic annotations which are automatically created for each smallest part. Because of the default seeder, the token "1cool" consists of two RutaBasics, one for the NUM, one for the SW. If you do not want to change the preprocessing, you can simply apply a rule that fixed that like
RETAINTYPE(WS);
ANY{-PARTOF(WS)} t:#T1{-> UNMARK(t)};
btw, option 4 won't work because the REGEXP condition checks on the covered text of the matched annotation SW which only represents one token. If you do something like (SW+){REGEXP("cool\\srule")->T1}, then the rule wont match if there is another SW afterwards.
DISCLAIMER: I am a developer Of UIMA Ruta

How to avoid a meta argument warning in SICStus SPIDER?

This is probably related to a comp.lang.prolog-discussion.
I'm getting several warnings like this using Eclipse with the SICStus SPIDER:
The plain meta argument (Y) is passed as a closure argument
(with 0 suppressed arguments) to the callee.
Here is a code sample:
% Prologs set_of is baroque %% RS-140614 130sec runtime vs. 28sec runtime
:- meta_predicate set_of(+,:,+) .
set_of(X,Y,Z):- %%
setof(X,Y^Y,Z),!; %% Trick to avoid alternatives
Z=[]. %% What is wrong with empty sets ?
How can I get rid of the SPIDER warnings?
I'm not really interested in simply suppressing the warnings.
I'm using the latest version of SPIDER IDE (0.0.51), and SICStus Prolog 4.2.3.
There are several issues in the code you show.
Bad meta argument
First, the built-in predicate setof/3 has the following properties:
?- predicate_property(setof(A,B,C),P).
P = (meta_predicate setof(?,0,?))
; P = built_in
; P = jittable.
which closely corresponds to the ISO declarations in ISO/IEC 13211-1:
8.10.3.2 Template and modes
setof(?term, +callable_term, ?list)
The second argument is a goal to be executed by call/1. No extra arguments are needed. This is what the 0 tells us.
On the other hand, your code you show contains a different meta predicate declaration:
:- meta_predicate(set_of(+,:,+)) .
Here, the second argument is a :. In SICStus, YAP, and SWI, the : means: This argument will be automatically qualified with the current module, such that the module information can be passed further on. Think of asserta(:). Here, the argument is not a goal but a clause.
So what you need to fix this, is to replace : by 0. And you might indicate this fact in the variable name used. That is, Goal_0 for call(Goal_0), Goal_1 for call(Goal_1, Arg1), Goal_2for call(Goal_2, Arg1, Arg2) etc.
Bad modes
The + in the first and third argument is inappropriate. The 3rd argument is commonly an uninstantiated variable to be unified with the resulting list.
Prolog's setof/3 baroque?
% Prologs set_of is baroque
The comment probably wants to say that setof/3 contains superfluous ornaments. In fact, setof/3 is much more versatile than mentioned set_of/3. Take this recent question or that. Often you first think about a very specific situation. Say, you want the list of actors of a particular movie. Then, later on you want to ask what movies there are. It is this generalization which works very smoothly with setof/3 whereas it is extremely complex if you do not have it.
Another very useful way to use setof/3 is when you want to eliminate redundant answers:
?- (X=2;X=1;X=2).
X = 2
; X = 1
; X = 2.
?- setof(t, (X=2;X=1;X=2), _).
X = 1
; X = 2.
Try to emulate that efficiently.
Runtime overheads
They are next to negligible. If you really believe that there are overheads, simply use setof/3 with a single goal. In this manner preprocessing is next to naught.