-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminion.py
More file actions
282 lines (224 loc) · 8.68 KB
/
Copy pathminion.py
File metadata and controls
282 lines (224 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Minion interface based on Peter Jipsen 2011-03-26 alpha version
import os
import codecs
import subprocess as sp
from select import poll, POLLIN
from isomorphisms import Automorphism, Isomorphism, Homomorphism
import config
import files
from itertools import product
from collections import defaultdict
from misc import *
def identity_table(size):
result = "I 1 %s\n" % size
result += " ".join(str(i) for i in range(size))
return result + "\n"
class MinionSol(object):
count = 0
def __init__(self, input_data, allsols=True, fun=lambda x: x):
"""
Toma el input para minion, si espera todas las soluciones y una funcion para aplicar
a las listas que van a ir siendo soluciones.
"""
self.EOF = False
self.id = MinionSol.count
MinionSol.count += 1
self.fun = fun
self.allsols = allsols
self.input_filename = config.minion_path + "input_minion%s_%s" % (self.id,os.getpid())
files.create_pipe(self.input_filename) # TODO SACAR PIPE
minionargs = ["-printsolsonly", "-randomseed", "0"]
if allsols:
minionargs += ["-findallsols"]
minionargs += [self.input_filename]
self.minionapp = sp.Popen([config.minion_path + "minion"] + minionargs,
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
files.write(self.input_filename, input_data)
self.solutions = []
def __parse_solution(self):
"""
Bloquea hasta conseguir una solucion, o el EOF
La parsea y devuelve una lista
"""
str_sol = self.minionapp.stdout.readline().decode('utf-8').strip()
if str_sol:
try:
result = list(map(int, str_sol.strip().split(" ")))
for i, v in enumerate(result):
if v == -1:
result[i] = None
result = {i:v for i,v in enumerate(result)}
# ACA IRIAN LAS TRADUCCIONES DE NOMBRES EN EL FUTURO
except ValueError:
str_sol += "\n"
# leo toda la respuesta de minion para saber que paso
str_sol += self.minionapp.stdout.read().decode('utf-8')
raise ValueError("Minion Error:\n%s" % str_sol)
if not self.allsols:
self.EOF = True
self.__terminate()
return result
else:
str_err = self.minionapp.stderr.read().decode('utf-8')
if str_err:
raise ValueError("Minion Error:\n%s" % str_err)
self.EOF = True
self.__terminate()
def __iter__(self):
for solution in self.solutions:
yield self.fun(solution)
while not self.EOF:
solution = self.__parse_solution()
if solution:
self.solutions.append(solution)
yield self.fun(solution)
def __getitem__(self, index):
try:
return self.fun(self.solutions[index])
except IndexError:
for i, solution in enumerate(self):
if i == index:
# no hace falta aplicar self.fun porque esta llamando a
# __iter__
return solution
raise IndexError("There aren't so many solutions.")
def __bool__(self):
if self.solutions or self.EOF:
return bool(self.solutions)
else:
solution = self.__parse_solution()
if solution:
self.solutions.append(solution)
return True
else:
return False
def __len__(self):
if not self.EOF:
for i in self:
pass
return len(self.solutions)
def __terminate(self):
"""
Mata a Minion
"""
if hasattr(self, 'minionapp'):
self.minionapp.stdout.close()
self.minionapp.stdin.close()
self.minionapp.stderr.close()
self.minionapp.kill()
del self.minionapp
files.remove(self.input_filename)
def __del__(self):
"""
Si no lo habia matado, mata a Minion.
"""
if not self.EOF:
self.__terminate()
def automorphisms(model,subtype):
result = "MINION 3\n**VARIABLES**\nDISCRETE f[%s]{0..%s}\n" % (len(model),len(model)-1)
result += "**TUPLELIST**\n"
result += model.minion_tables(subtype)
result += identity_table(len(model))
result += "**CONSTRAINTS**\n"
result += model.minion_constraints(subtype)
result += "alldiff([f["
result += "],f[".join(str(i) for i in range(len(model)))
result += "]])\n"
result += "negativetable([f[" # evito identidades
result += "],f[".join(str(i) for i in range(len(model)))
result += "]],I)\n"
result += "**EOF**"
return MinionSol(result,allsols=True,fun=lambda aut:(Automorphism({model.universe[k]:model.universe[aut[k]] for k in aut},model,subtype)))
def isomorphisms(source,target,subtype,allsols=True):
if len(source)!=len(target):
return [] # generador vacio
if source.rels_sizes(subtype) != target.rels_sizes(subtype):
return [] # generador vacio
result = "MINION 3\n**VARIABLES**\nDISCRETE f[%s]{0..%s}\n" % (len(source),len(target)-1)
result += "**TUPLELIST**\n"
result += target.minion_tables(subtype)
result += "**CONSTRAINTS**\n"
result += source.minion_constraints(subtype)
result += "alldiff([f["
result += "],f[".join(str(i) for i in range(len(source)))
result += "]])\n"
result += "**EOF**"
return MinionSol(result,allsols,fun=lambda iso:(Isomorphism({source.universe[k]:target.universe[iso[k]] for k in iso},source,target,subtype)))
def bihomomorphisms(source,target,subtype,allsols=True):
if len(source)!=len(target):
return [] # generador vacio
if source.rels_sizes(subtype) > target.rels_sizes(subtype):
return [] # generador vacio
result = "MINION 3\n**VARIABLES**\nDISCRETE f[%s]{0..%s}\n" % (len(source),len(target)-1)
result += "**TUPLELIST**\n"
result += target.minion_tables(subtype)
result += "**CONSTRAINTS**\n"
result += source.minion_constraints(subtype)
result += "alldiff([f["
result += "],f[".join(str(i) for i in range(len(source)))
result += "]])\n"
result += "**EOF**"
return MinionSol(result,allsols,fun=lambda iso:(Homomorphism({source.universe[k]:target.universe[iso[k]] for k in iso},source,target,subtype)))
def homomorphisms_surj(source,target,subtype,allsols=True):
if len(source)<len(target):
return [] # generador vacio
if source.rels_sizes(subtype) > target.rels_sizes(subtype):
return [] # generador vacio
result = "MINION 3\n**VARIABLES**\nDISCRETE f[%s]{0..%s}\n" % (len(source),len(target)-1)
result += "**TUPLELIST**\n"
result += target.minion_tables(subtype)
result += "**CONSTRAINTS**\n"
result += source.minion_constraints(subtype)
result += "**EOF**"
return MinionSol(result,allsols,fun=lambda iso:(Homomorphism({source.universe[k]:target.universe[iso[k]] for k in iso},source,target,subtype)))
def is_bihomomorphic(source,target,subtype):
bh = bihomomorphisms(source,target,subtype,allsols=False)
if bh:
return bh[0]
else:
return False
def bihomomorphisms_to_any(source, targets, subtype):
"""
Devuelve un iso si source es bihomomorfica a algun target
sino, false. Usa multiples preguntas a minion en paralelo.
"""
if not targets:
return
for target in targets:
for bh in bihomomorphisms(source,target,subtype):
yield bh
return
def bihomomorphisms_from_any(sources, target, subtype):
"""
Devuelve un iso si algun source es bihomomorfica al target
sino, false. Usa multiples preguntas a minion en paralelo.
"""
if not sources:
return
for source in sources:
for bh in bihomomorphisms(source,target,subtype):
yield bh
return
def is_isomorphic(source, target, subtype):
i = isomorphisms(source,target,subtype,allsols=False)
if i:
return i[0]
else:
return False
def is_isomorphic_to_any(source, targets, subtype):
"""
Devuelve un iso si source es isomorfa a algun target
sino, false. Usa multiples preguntas a minion en paralelo.
"""
if not targets:
return False
for target in targets.iterate(len(source)):
iso = is_isomorphic(source,target,subtype)
if iso:
return iso
return False
if __name__ == "__main__":
import doctest
doctest.testmod()