File size: 1,175 Bytes
1263695 | 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 | #!/usr/bin/env python3
"""Two small exact examples. Does not write to the repository."""
import sys
sys.dont_write_bytecode=True
from fractions import Fraction as F
from preemptive_ordering import optimal_one_split_order
from scalar_ordering import build_scalar_reduction,endpoint
from sentinel_ordering import balance_scalar_centers
result=optimal_one_split_order([-1,1],[F(1,2)]*2,target=0)
assert result['endpoint']==0 and result['objective']==0
assert len(result['fragments'])<=3
print('One interruption:')
print(' centers:',[-1,1])
print(' fragments (zero-based label, retention):',[(i,str(a)) for i,a in result['fragments']])
print(' endpoint:',result['endpoint'],'squared error:',result['objective'])
base=build_scalar_reduction([1,1])
balanced=balance_scalar_centers(base['centers'],base['rho'])
order=[0,1,3,2]+list(balanced['sentinel_suffix'])
assert sum(balanced['centers'])==0
assert endpoint(balanced['centers'],order,balanced['rho'])==0
print('Balanced scalar construction:')
print(' requests:',len(balanced['centers']),'mean:',sum(balanced['centers'])/len(balanced['centers']))
print(' zero-endpoint witness:',order)
print('Both exact examples passed.')
|