Expand source code
Browse git
def estimateSpreads(searchExe, both=False):
TRY_LIMIT_F = searchExe.perfParams["tryLimitFrom"]
TRY_LIMIT_T = searchExe.perfParams["tryLimitTo"]
qnodes = searchExe.qnodes
relations = searchExe.relations
converse = searchExe.converse
qedges = searchExe.qedges
yarns = searchExe.yarns
spreadsC = {}
spreads = {}
for (e, (f, rela, t)) in enumerate(qedges):
tasks = [(f, rela, t, 1)]
if both:
tasks.append((t, converse[rela], f, -1))
for (tf, trela, tt, dir) in tasks:
s = relations[trela]["spin"]
yarnF = yarns[tf]
yarnT = yarns[tt]
dest = spreads if dir == 1 else spreadsC
if type(s) is float:
# fixed estimates
dest[e] = len(yarnT) * s
continue
yarnF = list(yarnF)
yarnT = yarns[tt]
yarnFl = len(yarnF)
if yarnFl < TRY_LIMIT_F:
triesn = yarnF
else:
triesn = {yarnF[randrange(yarnFl)] for n in range(TRY_LIMIT_F)}
if len(triesn) == 0:
dest[e] = 0
else:
r = relations[trela]["func"](qnodes[tf][0], qnodes[tt][0])
nparams = len(signature(r).parameters)
totalSpread = 0
if nparams == 1:
for n in triesn:
mFromN = {m for m in r(n) or () if m in yarnT}
totalSpread += len(mFromN)
else:
yarnTl = len(yarnT)
yarnTL = list(yarnT)
for n in triesn:
triesm = (
yarnT
if yarnTl < TRY_LIMIT_T
else set(
yarnTL[randrange(yarnTl)] for m in range(TRY_LIMIT_T)
)
)
if len(triesm) == 0:
thisSpread = 0
else:
thisSpread = 0
for m in triesm:
if r(n, m):
thisSpread += 1
thisSpread = thisSpread / len(triesm)
totalSpread += yarnTl * thisSpread
dest[e] = totalSpread / len(triesn)
searchExe.spreads = spreads
searchExe.spreadsC = spreadsC