-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdcop_instance.py
More file actions
executable file
·308 lines (269 loc) · 10.2 KB
/
dcop_instance.py
File metadata and controls
executable file
·308 lines (269 loc) · 10.2 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import xml.dom.minidom as md
import xml.etree.ElementTree as ET
import json
import commons as cm
def create_xml_instance(name, agts, vars, doms, cons, fileout=''):
"""
Creates an XML instance
:param name: The name of the instance
:param agts: Dict of agents:
key: agt_name, val = null
:param vars: Dict of variables:
key: var_name,
vals: 'dom' = dom_name; 'agt' = agt_name
:param doms: Dict of domains:
key: dom_name,
val: array of values (integers)
:param cons: Dict of constraints:
key: con_name,
vals: 'arity' = int; 'def_cost' = int, values = list of dics {v: values, c: cost}
"""
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
#tree = ET.parse(pathToFile, OrderedXMLTreeBuilder())
rough_string = ET.tostring(elem.getroot(), encoding='utf-8', method='xml')
reparsed = md.parseString(rough_string)
return reparsed.toprettyxml(indent="\t")
def dump_rel(c_values):
s = ''
for i, t in enumerate(c_values):
if t['cost'] is None:
continue
s += str(t['cost']) + ':'
s += ' '.join(str(x) for x in t['tuple'])
if i < len(c_values) -1 :
s += ' |'
return s
root = ET.Element('instance')
ET.SubElement(root, 'presentation',
name=name,
maxConstraintArity= str(max([cons[cid]['arity'] for cid in cons])),
maximize="true",
format="XCSP 2.1_FRODO")
xml_agts = ET.SubElement(root, 'agents', nbAgents=str(len(agts)))
for aname in agts:
ET.SubElement(xml_agts, 'agent', name='a_'+aname)
xml_vars = ET.SubElement(root, 'variables', nbVariables=str(len(vars)))
for vname in vars:
ET.SubElement(xml_vars, 'variable',
name='v_'+vname,
domain='d', #+vars[vname]['dom'],
agent='a_'+vars[vname]['agt'])
xml_doms = ET.SubElement(root, 'domains', nbDomains=str(len(doms)))
for dname in doms:
ET.SubElement(xml_doms, 'domain', name='d',#+dname,
nbValues=str(len(doms[dname]))).text \
= str(doms[dname][0]) + '..' + str(doms[dname][-1])
# = ' '.join(str(x) for x in doms[dname])
xml_rels = ET.SubElement(root, 'relations', nbRelations=str(len(cons)))
xml_cons = ET.SubElement(root, 'constraints', nbConstraints=str(len(cons)))
for cname in cons:
X = [x for x in cons[cname]['values'] if x['cost'] is not None]
#r_3_1
r_name = 'r';
for e in cons[cname]['scope']:
r_name += '_' + str(e)
ET.SubElement(xml_rels, 'relation', name=r_name, arity=str(cons[cname]['arity']),
nbTuples=str(len(X)),
semantics='soft',
defaultCost="0" #str(cons[cname]['def_cost'])
).text = dump_rel(cons[cname]['values'])
ET.SubElement(xml_cons, 'constraint', name='c_'+cname, arity=str(cons[cname]['arity']),
scope=' '.join('v_'+str(e) for e in cons[cname]['scope']),
reference=r_name)
tree = ET.ElementTree(root)
if fileout:
with open(fileout, "w") as f:
f.write(prettify(tree))
else:
print(prettify(tree))
def create_wcsp_instance(name, agts, vars, doms, cons, fileout=''):
"""
Line 1:
<Problem name> <N> <K> <C> <UB>
where
<N> is the number of variables (integer)
<K> is the maximum domain size (integer)
<C> is the total number of constraints (integer)
<UB> is the global upper bound of the problem (long integer)
Variables:
<domain size of variable with index 0> ...
<domain size of variable with index N-1>
Constraints:
<Arity of the constraint>
<Index of the first variable in the scope of the constraint>
...
<Index of the last variable in the scope of the constraint>
<Default cost value>
<Number of tuples with a cost different than the default cost>
and for every tuple (again in one line):
:param name: The name of the instance
:param agts: Dict of agents:
key: agt_name, val = null
:param vars: Dict of variables:
key: var_name,
vals: 'dom' = dom_name; 'agt' = agt_name
:param doms: Dict of domains:
key: dom_name,
val: array of values (integers)
:param cons: Dict of constraints:
key: con_name,
vals: 'arity' = int; 'def_cost' = int, values = list of dics {v: values, c: cost}
"""
max_d = max( [len(doms[d]) for d in doms])
s = name + ' ' + str(len(vars)) + ' ' + str(max_d) + ' ' + str(len(cons)) + ' 99999' + '\n'
s += ' '.join( str(len(doms[vars[vname]['dom']])) for vname in vars) + '\n'
for cname in cons:
c = cons[cname]
s += str(c['arity']) + ' ' + \
' '.join(x for x in c['scope']) + ' ' + \
str(c['def_cost']) + ' ' + \
str(len(c['values'])) + '\n'
for v in c['values']:
for vid in [x for x in v['tuple']]:
s+= str(vid) + ' '
if v['cost'] is not None:
s += str(v['cost']) + '\n'
else:
s += 'infinity' + '\n'
#s += ' '.join(str(vid) for vid in v['tuple']) + ' ' + str(v['cost']) + '\n'
if fileout:
with open(fileout, "w") as f:
f.write(s)
else:
print(s)
def create_json_instance(name, agts, vars, doms, cons, fileout=''):
""""
It assumes constraint tables are complete
"""
jagts = {}
jvars = {}
jcons = {}
for vid in vars:
v = vars[vid]
d = doms[v['dom']]
aid = v['agt']
jvars['v'+vid] = {
'value': None,
'domain': d,
'agent': 'a'+str(aid),
'type': 1,
'id': int(vid),
'cons': []
}
for aid in agts:
jagts['a'+aid] = {'vars': ['v'+vid for vid in vars if vars[vid]['agt'] == aid]}
jagts['id'] = int(aid)
for cid in cons:
c = cons[cid]
jcons['c'+cid] = {
'scope': ['v'+vid for vid in c['scope']],
'vals': [x['cost'] if x['cost'] is not None else 'infinity' for x in c['values']]
}
for vid in c['scope']:
jvars['v'+str(vid)]['cons'].append('c'+cid)
instance = {'variables': jvars, 'agents': jagts, 'constraints': jcons}
if fileout:
#cm.save_json_file(fileout, instance)
with open(fileout, 'w') as outfile:
json.dump(instance, outfile, indent=2)
else:
print(json.dumps(instance, indent=2))
def create_maxsum_instance(name, agts, vars, doms, cons, fileout=''):
"""
:param name: The name of the instance
:param agts: Dict of agents:
key: agt_name, val = null
:param vars: Dict of variables:
key: var_name,
vals: 'dom' = dom_name; 'agt' = agt_name
:param doms: Dict of domains:
key: dom_name,
val: array of values (integers)
:param cons: Dict of constraints:
key: con_name,
vals: 'arity' = int; 'def_cost' = int, values = list of dics {v: tuples, c: cost}
"""
s = 'AGENT 1\n'
map_vidx = {}
for i, vname in enumerate(vars):
d = doms[vars[vname]['dom']]
map_vidx[vname] = i
s += 'VARIABLE ' + str(i) + ' 1 ' + str(len(d)) + '\n'
for i, cname in enumerate(cons):
c = cons[cname]
s += 'CONSTRAINT ' + str(i) + ' 1 '
for x in c['scope']:
s += str(map_vidx[x]) + ' '
#' '.join(str(map_vidx[x]) for x in c['scope']) + '\n'
s += '\n'
for v in c['values']:
cost = v['cost'] if v['cost'] is not None else -9999999
s += 'F ' + ' '.join(str(t) for t in v['tuple']) + ' ' + str(cost) + '\n'
if fileout:
with open(fileout, "w") as f:
f.write(s)
else:
print(s)
def create_dalo_instance(name, agts, vars, doms, cons, fileout=''):
"""
:param name: The name of the instance
:param agts: Dict of agents:
key: agt_name, val = null
:param vars: Dict of variables:
key: var_name,
vals: 'dom' = dom_name; 'agt' = agt_name
:param doms: Dict of domains:
key: dom_name,
val: array of values (integers)
:param cons: Dict of constraints:
key: con_name,
vals: 'arity' = int; 'def_cost' = int, values = list of dics {v: tuples, c: cost}
"""
s = 'AGENT ' + str(len(agts)) + '\n'
map_vidx = {}
for i, vname in enumerate(vars):
d = doms[vars[vname]['dom']]
map_vidx[vname] = i
map_vidx[vname] = i
s += 'VARIABLE ' + str(i) + ' ' + str(i) + ' ' + str(len(d)) + '\n'
for i, cname in enumerate(cons):
c = cons[cname]
s += 'CONSTRAINT '
for x in c['scope']:
s += str(map_vidx[x]) + ' '
s += '\n'
for v in c['values']:
if v['cost'] is not None:
cost = v['cost'] #if v['cost'] is not None else -9999999
s += 'F ' + ' '.join(str(t) for t in v['tuple']) + ' ' + str(cost) + '\n'
if fileout:
with open(fileout, "w") as f:
f.write(s)
else:
print(s)
def sanity_check(vars, cons):
""" Check all variables participate in some constraint """
v_con = []
for c in cons:
for x in cons[c]['scope']:
if x not in v_con:
v_con.append(x)
for v in vars:
if v not in v_con:
return False
return True
if __name__ == '__main__':
agts = {'1': None}
vars = {'1': {'dom': '1', 'agt': '1'},
'2': {'dom': '1', 'agt': '1'}}
doms = {'1': [0, 1]}
cons = {'1': {'arity': 2, 'def_cost': 0, 'scope': ['1', '2'],
'values': [{'tuple': [0, 0], 'cost': 1}, {'tuple': [0, 1], 'cost': 2},
{'tuple': [1, 0], 'cost': 5}, {'tuple': [1, 1], 'cost': 3}]}}
# create_xml_instance("test", agts, vars, doms, cons)
# create_wcsp_instance("test", agts, vars, doms, cons)
# create_json_instance("test", agts, vars, doms, cons)
create_maxsum_instance("test", agts, vars, doms, cons)
create_dalo_instance("test", agts, vars, doms, cons)