File size: 1,525 Bytes
987b273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Exact phase-average capture for a periodically repeated schedule, ideal fixed slots."""
import argparse
import json

def compare(period, shifts, width):
    if type(period) is not int or period<=0 or not shifts:
        raise ValueError("a positive integer period and a nonempty schedule are required")
    if not 0<width<period or any(type(x) is not int or not 0<=x<period for x in shifts):
        raise ValueError("width must lie in (0,T), shifts in [0,T)")
    a=lambda x:min(width,x)
    b=lambda x:max(0,x-(period-width))
    static=sum(a(x)-b(x) for x in shifts)
    switching=sum(max(0,b(shifts[i-1])-a(x)) for i,x in enumerate(shifts))
    lost=sum(max(0,a(x)-b(shifts[i-1])) for i,x in enumerate(shifts))
    assert lost==static+switching
    scale=len(shifts)*period
    return dict(lost_start_measure=lost,total_start_measure=scale,
                loss_probability=lost/scale,constant_shift_component=static,
                switching_component=switching,mean_persistent_gain=sum(shifts)/len(shifts))

def clustered(period, high, enabled, length, width):
    if not 0<=enabled<=length or length<1:
        raise ValueError("invalid duty")
    shifts=[high]*enabled+[0]*(length-enabled)
    return shifts,compare(period,shifts,width)

if __name__=="__main__":
    p=argparse.ArgumentParser(description=__doc__);p.add_argument("period",type=int)
    p.add_argument("width",type=int);p.add_argument("shifts",nargs="+",type=int)
    a=p.parse_args();print(json.dumps(compare(a.period,a.shifts,a.width),indent=2))