Skip to content

Commit 33abd29

Browse files
Merge pull request #29 from logic-and-learning-lab/pipable
Pipable
2 parents 6c2741e + d18c607 commit 33abd29

File tree

10 files changed

+303
-254
lines changed

10 files changed

+303
-254
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include popper/lp/*.pl

README.md

Lines changed: 62 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
Popper is an [inductive logic programming](https://arxiv.org/pdf/2008.07912.pdf) (ILP) system.
44
Popper is still a **major** work-in-progress, so please notify us of bugs or usability issues.
55

6-
If you use Popper for research, please cite the paper: Andrew Cropper and Rolf Morel. [Learning programs by learning from failures](https://arxiv.org/abs/2005.02259). Mach. Learn. 110(4): 801-856 (2021)
6+
If you use Popper, please cite the paper: Andrew Cropper and Rolf Morel. [Learning programs by learning from failures](https://arxiv.org/abs/2005.02259). Mach. Learn. 110(4): 801-856 (2021)
77

8+
## Installation
9+
```pip install popper-ilp```
810

911
## Requirements
1012

@@ -15,9 +17,9 @@ If you use Popper for research, please cite the paper: Andrew Cropper and Rolf M
1517
[pyswip](https://pypi.org/project/pyswip/)
1618

1719

18-
# Usage
20+
# Command line usage
1921

20-
You can run Popper with the command `python popper.py <input dir>`
22+
You can run Popper with the command `python popper.py <input dir>`.
2123
For instance, running the command `python popper.py examples/dropk` produces the output:
2224

2325
```prolog
@@ -35,17 +37,26 @@ f(A):-long(B),roof_closed(B),has_car(A,B),three_wheels(C),has_car(A,C).
3537

3638
Take a look at the examples folder for examples.
3739

40+
# Library usage
3841

39-
# Example problem
42+
You can import Popper and use it in your Python code like so:
43+
44+
```python
45+
from popper.util import Settings
46+
from popper.loop import learn_solution
47+
prog, stats_ = learn_solution(Settings('bias.pl', 'exs.pl', 'bk.pl'))
48+
print(prog)
49+
```
4050

51+
# Example problem
4152

4253
Popper requires three files:
4354

4455
- an examples file
4556
- a background knowledge (BK) file
4657
- a bias file
4758

48-
An examples file simply contains positive and negative examples of the relation you wish to learn:
59+
An examples file contains positive and negative examples of the relation you want to learn:
4960

5061
```prolog
5162
pos(grandparent(ann,amelia)).
@@ -56,7 +67,7 @@ pos(grandparent(linda,amelia)).
5667
neg(grandparent(amy,amelia)).
5768
```
5869

59-
Likewise, a BK file contains helpful information about the relation you are trying to learn:
70+
A BK file contains other information about the problem:
6071

6172
```prolog
6273
mother(ann,amy).
@@ -69,26 +80,25 @@ father(gavin,amelia).
6980
father(andy,spongebob).
7081
```
7182

72-
The bias file contains all the information necessary to restrict the search space of Popper.
73-
74-
There two main things to add to this file are predicate declarations. These simply inform Popper whether it can use a predicate symbol in the head or body of a rule in a solution, such as:
83+
A bias file contains information necessary to restrict the search space of Popper.
84+
The first key thing thing to add to this file are *predicate declarations*.
85+
These tell Popper which predicate symbols it can use in the head or body of a rule, such as:
7586

7687
```prolog
7788
head_pred(grandparent,2).
7889
body_pred(mother,2).
7990
body_pred(father,2).
8091
```
8192

82-
In other words, the above says each role in a program must have the symbol grandparent with arity two in the head and mother and/or father in the body, also with arity two.
83-
84-
Popper needs three parameters to restrict the search space:
93+
These declarations say that each rule in a program must have the symbol *grandparent* with arity two in the head and *mother* and/or *father* in the body, also with arity two.
8594

86-
- `max_vars(N).` sets the maximum number of variables allowed in a rule to be `N`
87-
- `max_body(N).` sets the maximum number of body literals in a rule to be `N`
88-
- `max_clauses(N).` sets the maximum number of rules/clause to be `N`
95+
Popper also needs three parameters to restrict the search space:
8996

90-
These parameters are important as they greatly influence the search space. If they are way too high then Popper will likely take a long time to learn a solution. If the settings are way too low then the search space might be too small to contain a good solution. We are currently working on techniques to automatically deduce these settings. But in the meantime finding the correct values can often be process of trial and error.
97+
- `max_vars(N).` sets the maximum number of variables in a rule to `N`
98+
- `max_body(N).` sets the maximum number of body literals in a rule to `N`
99+
- `max_clauses(N).` sets the maximum number of rules/clauses to `N`
91100

101+
These parameters are very important as they greatly influence the search space. If the values are too high then Popper will might struggle to learn a solution. If the settings are too low then the search space might be too small to contain a good solution. We are currently working on method to automatically set these settings, but in the meantime finding the correct values can often be a process of trial and error.
92102

93103
In our running example, we will add these three lines to our bias file:
94104
```prolog
@@ -97,7 +107,7 @@ max_vars(4).
97107
max_body(3).
98108
```
99109

100-
If we call Popper with these three files, then it will produce the output:
110+
If we call Popper with these three files it will produce the output:
101111

102112
```prolog
103113
grandparent(A,B):-mother(A,C),father(C,B).
@@ -107,29 +117,9 @@ grandparent(A,B):-mother(A,C),mother(C,B).
107117
% Precision:1.00, Recall:1.00, TP:5, FN:0, TN:1, FP:0
108118
```
109119

110-
# Predicate invention
120+
# Anytime
111121

112-
Popper supports [automatic predicate invention](https://arxiv.org/pdf/2104.14426.pdf) (PI). To enable PI, add the setting `enable_pi.` to the bias file.
113-
With PI enabled, Popper (`python popper.py examples/kinship-pi`) learns the following program:
114-
115-
```prolog
116-
grandparent(A,B):-inv1(C,B),inv1(A,C).
117-
inv1(A,B):-mother(A,B).
118-
inv1(A,B):-father(A,B).
119-
% Precision:1.00, Recall:1.00, TP:5, FN:0, TN:1, FP:0
120-
```
121-
122-
<!-- Popper can invent multiple levels of predicates. For instance, running `python popper.py examples/robots-pi` produces the output:
123-
124-
```prolog
125-
126-
``` -->
127-
128-
Predicate invention is currently very expensive so it is best to avoid it if possible.
129-
130-
# Anytime
131-
132-
Popper is an anytime algorithm. To see the intermediate solutions use the `--info` flag. For instance, running the command `python popper.py examples/trains2 --info` produces the output:
122+
Popper is an anytime algorithm. To see intermediate solutions use the `--info` flag (or `settings.info = True`). For instance, running the command `python popper.py examples/trains2 --info` produces the output:
133123

134124
```prolog
135125
% NEW BEST PROG 1:
@@ -163,7 +153,7 @@ f(A):-rectangle(B),has_load(E,B),has_car(A,E),has_car(A,D),has_load(D,C),triangl
163153

164154
# Recursion
165155
To enable recursion add `enable_recursion.` to the bias file.
166-
This allows Popper to learn programs where a predicate symbol appears both in the head and body of a rule, such as to find a duplicate element (`python popper.py examples/find-dupl`) in a list:
156+
This flag allows Popper to learn programs where a predicate symbol appears in both the head and body of a rule, such as to find a duplicate element (`python popper.py examples/find-dupl`) in a list:
167157

168158
```prolog
169159
f(A,B):-head(A,B),tail(A,C),element(C,B).
@@ -194,7 +184,7 @@ type(prepend,(element,list,list)).
194184
```
195185

196186
# Directions
197-
Prolog often require arguments to be ground.
187+
Prolog often requires arguments to be ground.
198188
For instance, when asking Prolog to answer the query:
199189
```prolog
200190
X is 3+K.
@@ -220,6 +210,32 @@ direction(prepend,(in,int,out)).
220210
direction(geq,(in,in)).
221211
```
222212

213+
# Predicate invention
214+
215+
Popper supports [automatic predicate invention](https://arxiv.org/pdf/2104.14426.pdf) (PI). To enable PI, add the setting `enable_pi.` to the bias file.
216+
With PI enabled, Popper (`python popper.py examples/kinship-pi`) learns the following program:
217+
218+
```prolog
219+
grandparent(A,B):-inv1(C,B),inv1(A,C).
220+
inv1(A,B):-mother(A,B).
221+
inv1(A,B):-father(A,B).
222+
% Precision:1.00, Recall:1.00, TP:5, FN:0, TN:1, FP:0
223+
```
224+
225+
<!-- Popper can invent multiple levels of predicates. For instance, running `python popper.py examples/robots-pi` produces the output:
226+
227+
```prolog
228+
229+
``` -->
230+
231+
Predicate invention is currently very expensive so it is best to avoid it if possible.
232+
233+
234+
# Non-observational predicate learning
235+
236+
Popper supports non-observational predicate learning, where it must learn definitions for relations not given as examples.
237+
See the example 'non-OPL'.
238+
223239
# Parallelisation
224240
[Coming soon](https://arxiv.org/pdf/2109.07132.pdf)
225241

@@ -231,17 +247,17 @@ direction(geq,(in,in)).
231247

232248
# Popper settings
233249

234-
To run with statistics use the flag `--stats`.
250+
To run with statistics use the flag `--stats` (`settings.stats = True`).
235251

236-
To run in debug mode use the flag `--debug`.
252+
To run in debug mode use the flag `--debug` (`settings.debug = True`).
237253

238-
To run in information mode use the flag `--info`.
254+
To run in information mode use the flag `--info` (`settings.info = True`).
239255

240256
To show the full hypothesis space (bounded by `N`) use the flag `--hspace N`.
241257

242-
To run with a maximum learning time use the flag `--timeout`.
258+
To run with a maximum learning time use the flag `--timeout` (`settings.timeout = N`).
243259

244-
To run with a maximum example testing time use the flag `--eval-timeout`.
260+
To run with a maximum example testing time use the flag `--eval-timeout` (`settings.eval_timeout = True`).
245261

246262
To allow non-Datalog clauses, where a variable in the head need not appear in the body, add ``non_datalog.` to your bias file.
247263

0 commit comments

Comments
 (0)