flock_log.py 1.55 KB
Newer Older
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
# -*- coding: utf-8 -*-
import math

params = {
  "scheme": {
    "threshold": {
      "descr":  "Threshold c",
      "values": range(1, 201),
      "value":  10
    }
  }
}

def generateProtocol(params):
  c = params["scheme"]["threshold"]["value"]
  i = int(math.log(c, 2))
  states = [0] + [2**j for j in range(0, i + 1)]

  cur_state = 2**i

  for j in range(i-1, 0, -1):
    if c & 2**j:
      cur_state += 2**j
      states.append(cur_state)

  if c not in states:
    states.append(c)

  transitions = []

  for x in range(len(states)):
    for y in range(x, len(states)):
      q1 = states[x]
      q2 = states[y]
      sum = q1 + q2
      pre = (q1, q2)
      post = None

      if sum < c and q1 != 0 and q2 != 0 and sum in states:
        post = (sum, 0)
      elif sum >= c:
        post = (c, c)

      if (post is not None) and (not Utils.silent(pre, post)):
        transitions.append(Utils.transition(pre, post))

  style = {q: {} for q in states}

  for q in states:
    if q < c:
      style[q]["size"] = 0.5 + 0.5 * (q + 1) / (c + 1)
      style[q]["color"] = "rgb({}, {}, {})".format(105 + 128 * (c - q) / c, 30, 99)

  style[0]["color"] = "rgb(175, 175, 175)"

  return {
    "title": 				 "Logarithmic flock-of-birds protocol",
    "states":        states,
    "transitions":   transitions,
    "initialStates": [1],
    "trueStates":    [c],
    "predicate":     "C[1] >= {}".format(c),
    "description":   """This protocol computes whether the population size
                        is at least c. The protocol has O(log c) states.""",
    "statesStyle": style
  }