1
1
import numbers
2
2
import re
3
3
from itertools import permutations
4
- from typing import Any , Set , Tuple
4
+ from typing import Any , Set , Tuple , Optional
5
5
6
6
import clingo
7
7
import clingo .script
@@ -19,6 +19,7 @@ def arg_to_symbol(arg):
19
19
return Number (arg )
20
20
if isinstance (arg , str ):
21
21
return Function (arg )
22
+ raise TypeError (f"Cannot translate { arg } to clingo Symbol" )
22
23
23
24
24
25
def atom_to_symbol (pred , args ) -> Function :
@@ -38,6 +39,9 @@ def atom_to_symbol(pred, args) -> Function:
38
39
39
40
class Generator (AbstractGenerator ):
40
41
settings : Settings
42
+ model : Optional [clingo .Model ]
43
+ solver : Optional [clingo .Control ]
44
+ handler : Optional [clingo .SolveHandle ]
41
45
42
46
def __init__ (self , settings : Settings , bkcons = []):
43
47
self .savings = 0
@@ -46,26 +50,41 @@ def __init__(self, settings: Settings, bkcons=[]):
46
50
self .handle = None
47
51
self .pruned_sizes = set ()
48
52
53
+ encoding = self .build_encoding (bkcons , settings )
54
+
55
+ with open ('/tmp/ENCODING-GEN.pro' , 'w' ) as f :
56
+ f .write (encoding )
57
+
58
+ solver = self .init_solver (encoding )
59
+ self .solver = solver
60
+
61
+ self .model = None
62
+
63
+ def init_solver (self , encoding ):
64
+ if self .settings .single_solve :
65
+ solver = clingo .Control (['--heuristic=Domain' , '-Wnone' ])
66
+ solver .configuration .solve .models = 0
67
+ solver .add ('base' , [], encoding )
68
+ solver .ground ([('base' , [])])
69
+ return solver
70
+
71
+ def build_encoding (self , bkcons , settings ):
49
72
encoding = []
50
73
alan = resource_string (__name__ , "lp/alan.pl" ).decode ()
51
74
encoding .append (alan )
52
-
53
75
with open (settings .bias_file ) as f :
54
76
bias_text = f .read ()
55
77
bias_text = re .sub (r'max_vars\(\d*\).' , '' , bias_text )
56
78
bias_text = re .sub (r'max_body\(\d*\).' , '' , bias_text )
57
79
bias_text = re .sub (r'max_clauses\(\d*\).' , '' , bias_text )
58
-
59
80
# AC: NEED TO COMPLETELY REFACTOR THIS CODE
60
- for p ,a in settings .pointless :
81
+ for p , a in settings .pointless :
61
82
bias_text = re .sub (rf'body_pred\({ p } ,\s*{ a } \)\.' , '' , bias_text )
62
83
bias_text = re .sub (rf'constant\({ p } ,.*?\).*' , '' , bias_text , flags = re .MULTILINE )
63
-
64
84
encoding .append (bias_text )
65
85
encoding .append (f'max_clauses({ settings .max_rules } ).' )
66
86
encoding .append (f'max_body({ settings .max_body } ).' )
67
87
encoding .append (f'max_vars({ settings .max_vars } ).' )
68
-
69
88
# ADD VARS, DIRECTIONS, AND TYPES
70
89
head_arity = len (settings .head_literal .arguments )
71
90
encoding .append (f'head_vars({ head_arity } , { tuple (range (head_arity ))} ).' )
@@ -76,7 +95,6 @@ def __init__(self, settings: Settings, bkcons=[]):
76
95
encoding .append (f'vars({ arity } , { tuple (xs )} ).' )
77
96
for i , x in enumerate (xs ):
78
97
encoding .append (f'var_pos({ x } , { tuple (xs )} , { i } ).' )
79
-
80
98
type_encoding = set ()
81
99
if self .settings .head_types :
82
100
types = tuple (self .settings .head_types )
@@ -90,38 +108,22 @@ def __init__(self, settings: Settings, bkcons=[]):
90
108
for i , x in enumerate (types ):
91
109
type_encoding .add (f'type_pos({ str_types } , { i } , { x } ).' )
92
110
encoding .extend (type_encoding )
93
-
94
111
for pred , xs in self .settings .directions .items ():
95
112
for i , v in xs .items ():
96
113
if v == '+' :
97
114
encoding .append (f'direction_({ pred } , { i } , in).' )
98
115
if v == '-' :
99
116
encoding .append (f'direction_({ pred } , { i } , out).' )
100
-
101
117
max_size = (1 + settings .max_body ) * settings .max_rules
102
118
if settings .max_literals < max_size :
103
119
encoding .append (f'custom_max_size({ settings .max_literals } ).' )
104
-
105
120
if settings .noisy :
106
121
encoding .append (NOISY_ENCODING )
107
-
108
122
encoding .extend (bkcons )
109
-
110
123
if settings .single_solve :
111
124
encoding .append (DEFAULT_HEURISTIC )
112
-
113
125
encoding = '\n ' .join (encoding )
114
-
115
- with open ('/tmp/ENCODING-GEN.pro' , 'w' ) as f :
116
- f .write (encoding )
117
-
118
- if self .settings .single_solve :
119
- solver = clingo .Control (['--heuristic=Domain' , '-Wnone' ])
120
-
121
- solver .configuration .solve .models = 0
122
- solver .add ('base' , [], encoding )
123
- solver .ground ([('base' , [])])
124
- self .solver = solver
126
+ return encoding
125
127
126
128
def update_solver (self , size ):
127
129
# not used when learning programs without pi or recursion
0 commit comments