-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenergy.js
380 lines (363 loc) · 11.9 KB
/
energy.js
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
var lastPat = 0;
var lastRule = 0;
$(document).ready(() => {
addRule();
addPattern();
});
function addPattern() {
addP(++lastPat, "A(l!1,r!2),B(l!2,r!3),C(l!3,r!1)", "1");
}
function addP(i, pattern, cost) {
$("#energy-terms").append(
`<div class="row form-group vcentre energy-term">
<div class="col-md-3" style="text-align: left">
<label for="pat${i}">energy pattern ${i}</label>
</div>
<div class="col-md-5">
<input class="form-control input-lg pattern" type="text"
name="pat${i}" value="${pattern}"/>
</div>
<div class="col-md-2">
<label for="cost${i}">with cost</label>
</div>
<div class="col-md-2">
<input class="form-control input-lg cost" type="text"
name="cost${i}" value="${cost}"/>
</div>
</div>`);
}
function addRule() {
addR(++lastRule, "A(r),B(l)", "A(r!1),B(l!1)");
}
function addR(i, lhs, rhs) {
$("#rules").append(
`<div class="row form-group vcentre rule">
<div class="col-md-1" style="text-align: left">
<label for="lhs${i}">rule ${i}</label>
</div>
<div class="col-md-5">
<input class="form-control input-lg lhs" type="text"
name="lhs${i}" value="${lhs}"/>
</div>
<div class="col-md-1">
<span class="glyphicon glyphicon-arrow-right"/>
</div>
<div class="col-md-5">
<input class="form-control input-lg rhs" type="text"
name="rhs${i}" value="${rhs}"/>
</div>
</div>`);
}
function vals(sel, f = x => x) {
return $(sel).map(function() { return f($(this).val()); }).get();
}
function computeRefinements() {
var costs = vals(".cost", c => _.toNumber(c)),
rules = _.zip(
vals(".lhs", lhs => buildGraph(parseGraph(
lhs, `error parsing <strong>${lhs}</strong>: `))),
vals(".rhs", rhs => buildGraph(parseGraph(
rhs, `error parsing <strong>${rhs}</strong>: `)))),
patterns = vals(".pattern", p => buildGraph(parseGraph(
p, `error parsing <strong>${p}</strong>: `))),
cg = buildContactGraph(_.flatten(rules).concat(patterns));
$("#results").empty().append(
`<div class="row">
<div class="col-md-11 col-md-offset-1">
<h5>Refinements</h5>
</div>
</div>`);
rules.forEach(([l, r], i) => {
var refs = refsOfRule(l, r, patterns, costs, cg);
// compute energy balances
var balance = refs.map(([l, r]) =>
patterns.map(p => embs(p, r).length - embs(p, l).length));
$("#results").append(
`<div class="row">
<div class="col-md-11 col-md-offset-1">
<div class="strike">
<span>rule ${i+1}</span>
</div>
</div>
</div>`);
showRefinements(refs, patterns, costs, balance, cg);
});
$("#results").append(
`<div class="row">
<div class="col-md-11 col-md-offset-1">
<button class="btn btn-default btn-lg">
Download KaSim code
</button>
</div>
</div>`).find("button").click(function() {
var sites = _.groupBy(cg.sites, x => x.split(".")[0]),
agents = _.values(_.mapValues(sites, (xs, a) =>
`%agent: ${a}(${xs.map(x => x.split(".")[1]).join()})`)),
vars = patterns.map((p, i) =>
`%var: 'e${i+1}' ${costs[i]} # ${toString(patterns[i])}`),
rules = refs.map(([l, r], i) => {
var name = `'r${i+1}'`,
indent = _.repeat(" ", name.length);
return name + ` ${toString(l)} -> \\\n` +
`${indent} ${toString(r)} \\\n` +
`${indent} @ ${rate(balance[i])}`;
}),
kasim = agents.concat(vars, rules, [""]).join("\n"),
blob = new Blob([kasim], {type: "text/plain;charset=utf-8"});
saveAs(blob, "model.ka");
});
}
function showRefinements(refs, patterns, costs, balance, cg) {
refs.forEach(([l, r], i) => $("#results").append(
`<div class="row vcentre" id="r${i+1}">
<div class="col-md-1">
${i+1}
</div>
<div class="col-md-5">
<div class="centre alert alert-info" role="alert">
${toString(l)}
</div>
</div>
<div class="col-md-1">
<span class="glyphicon glyphicon-arrow-right"/>
</div>
<div class="col-md-5">
<div class="centre alert alert-info" role="alert">
${toString(r)}
</div>
</div>
</div>`).find(`#r${i+1}`).click(function() {
$(this).after(
`<div class="row" style="display: none">
<div class="col-md-12">
${balance[i].map((e, j) =>
`<p>balance for ${toString(patterns[j])} is ${e}</p>`).join("")}
</div>
</div>`).next().slideDown();
$(this).off("click");
$(this).click(function() {
var n = $(this).next();
if (n.is(":hidden"))
n.slideDown();
else
n.slideUp();
});
}));
}
function rate(balance) {
var nzb = balance.filter(p => p != 0);
if (nzb.length == 0)
return "1";
var delta = nzb.map(
(p, i) => (p == 1) ? `'e${i+1}'` : `${p} * 'e${i+1}'`);
return `[exp] (-1/2 * (${delta.join(" + ")}))`;
}
function loadModel() {
var file = $("#file")[0].files[0],
reader = new FileReader(),
rules = $("#rules").empty(),
terms = $("#energy-terms").empty();
lastPat = 0;
lastRule = 0;
reader.onload = evt => {
var filecontents = evt.target.result;
filecontents.split("\n").forEach(line => {
var rule = line.match(/^'.*' (.*) -> (.*)$/) ||
line.match(/^(.*) -> (.*)$/);
if (rule) {
var lhs = rule[1],
rhs = rule[2];
addR(++lastRule, lhs, rhs);
} else {
var energy = line.match(/^%energy: ([0-9eE.-]*) (.*)$/);
if (energy) {
var cost = energy[1],
pattern = energy[2];
addP(++lastPat, pattern, cost);
}
}
});
};
reader.readAsText(file);
}
function saveModel() {
var rules = _.zip(vals(".lhs"), vals(".rhs")).map(
([l, r]) => `${l} -> ${r}`),
patterns = _.zip(vals(".pattern"), vals(".cost")).map(
([p, c]) => `%energy: ${c} ${p}`),
blob = new Blob([rules.concat(patterns, [""]).join("\n")],
{type: "text/plain;charset=utf-8"});
saveAs(blob, "model.eka"); // eka = energy kappa
}
function refsOfRule(l, r, patterns, costs, contactGraph) {
var mods = modsites(l, r);
// relevant minimal glueings
function relevantUnions(g) { // g can be l or r
return _.flatten(patterns.map(
p => unionsAndIntersections(g, p))).filter(
([u, i]) => isRelevant(i, mods)).map(_.first);
}
// site requests
function siteRequests(rmgs) {
return _.flatten(_.flatten(rmgs.map(m => m.ga_mg.map(
(ma, ga) => _.difference(m.sitesOf[ma], m.gs_mg).map(
ms => [ga, m.sitetype[ms]])))));
}
// add site requests iteratively
function iter(queue, refs) {
if (queue.length == 0)
return refs;
var [[l, r, reqs], ...rest] = queue,
lreqs = siteRequests(relevantUnions(l)),
rreqs = siteRequests(relevantUnions(r)),
nreqs = _.unionWith(reqs, lreqs, rreqs, _.isEqual);
if (nreqs.length == 0)
// found a refinement
return iter(rest, refs.concat([[l, r]]));
// console.log("requested sites", nreqs.map(
// r => r.join(".")).join(", "));
var [[a, x], ...reqtail] = nreqs;
// add requested site to l and r
// free first
var lf = addFree(_.cloneDeep(l), a, x),
rf = addFree(_.cloneDeep(r), a, x),
free = [lf, rf];
// console.log("lf", toString(lf), lf);
// bound next
function add(a, x, ys, f) {
return ys.map(y => {
var ly = f(_.cloneDeep(l), a, x, y),
ry = f(_.cloneDeep(r), a, x, y);
// console.log("ly", toString(ly), ly);
return [ly, ry];
});
}
var bound = add(a, x, contactGraph.edgemap[x], addBound);
// loops now
var loops = _.flatten(contactGraph.edgemap[x].map(y => {
// select all sites in l that are of type y
// and are not modified by the rule
// and that are free
var allys = _.compact(l.sitetype.map((z, i) => (y == z) && i)),
boundsites = _.keys(l.edgemap).map(_.toNumber),
yids = _.difference(allys, mods, boundsites);
// handle the case where a site of type y is not in l
// but could be added to an agent of l
// and the site is not requested by the growth policy
var bt = y.split(".")[0],
bs = _.compact(l.agenttype.map((at, i) => (at == bt) && i)),
bs2 = bs.filter(b => l.sitesOf[b].every(
z => l.sitetype[z] != y)),
bs3 = bs2.filter(b => reqtail.every(
([c, z]) => b != c && y != z));
return add(a, x, yids, addLoop).concat(
add(a, x, bs3.map(b => [b, y]), addBoundIn));
}));
var exts = [free].concat(bound, loops).map(
x => x.concat([reqtail]));
return iter(rest.concat(exts), refs);
}
return iter([[l, r, []]], []);
}
// add a site of type x in agent a
// bound to site of type y in agent b
function addBoundIn(g, a, x, b, y) {
var xid = addSite(g, a, x),
yid = addSite(g, b, y);
return addEdge(g, xid, yid);
}
// add a site of type x in agent a bound to site yid
function addLoop(g, a, x, yid) {
var xid = addSite(g, a, x);
return addEdge(g, xid, yid);
}
// add a free site of type x in agent a in graph g
function addFree(g, a, x) {
addSite(g, a, x);
return g;
}
// add a site of type x in agent a bound to site y
function addBound(g, a, x, y) {
var b = addAgent(g, y.split(".")[0]),
xid = addSite(g, a, x),
yid = addSite(g, b, y);
return addEdge(g, xid, yid);
}
// add agent of type at to graph g and return agent id
function addAgent(g, at) {
var a = g.agents.length;
g.agenttype.push(at);
g.agents.push(at);
g.sitesOf[a] = [];
return a;
}
// add site of type x to agent a and return site id
function addSite(g, a, x) {
var xid = g.sites.length;
g.sitetype.push(x);
g.sites.push(x);
g.sitesOf[a].push(xid);
g.sitemap[xid] = a;
return xid;
}
// add edge between sites xid and yid
function addEdge(g, xid, yid) {
g.edgemap[xid] = yid;
g.edgemap[yid] = xid;
g.edges.push([xid, yid]);
return g;
}
// compute the embeddings of g into h
// assumes g is connected
function embs(g, h) {
var root = 0;
return _.compact(h.agents.map(b => {
// try to match the root on b
function match(queue, matched) {
if (queue.length == 0)
return matched;
var [[a, b], ...rest] = queue;
if (g.agenttype[a] != h.agenttype[b])
return false;
var q = g.sitesOf[a].reduce((acc, x) => {
if (acc === false)
return false;
var ys = h.sitesOf[b].filter(
y => h.sitetype[y] == g.sitetype[x]);
if (ys.length == 0)
return false;
else if (ys.length > 1) {
console.log("w: agent", b,
"has more than one site of type", g.sitetype[x]);
return false;
} else if (x in g.edgemap) { // x is bound
var [y] = ys, nb = (x, g) => g.sitemap[g.edgemap[x]];
if (y in h.edgemap)
return _.unionWith(acc, [[nb(x, g), nb(y, h)]], _.isEqual);
else
return false;
// return (x in g.edgemap && y in h.edgemap) ||
// (!(x in g.edgemap) && !(y in h.edgemap));
} else { // x is free
if (ys[0] in h.edgemap)
return false;
else
return acc;
}
}, []);
if (q === false)
return false;
else {
var m = matched.concat([[a, b]]),
r = _.differenceWith(q, m, _.isEqual),
t = r.every(([a, b]) => m.every(
([c, d]) => (a != c) && (b != d)));
if (t)
return match(_.unionWith(rest, r, _.isEqual), m);
else
return false;
}
}
return match([[root, b]], []);
}));
}