Lawfare Group → Knowledge Institute → Lawfare Academy

Reproduce the worked examples

Copy the complete code below into a UTF-8 file named verify.py and run python verify.py with Python 3. It uses only the standard library and performs exact or rational synthetic calculations. It does not access a network or learner records. Assertions fail if the expected synthetic results change.

These checks verify the stated synthetic arithmetic, experimental-method and contingent-policy calculations, not independent academic acceptance or production migration safety.

"""Original synthetic Operations Strategy checks. No network, databases or learner records."""
from fractions import Fraction as F
from math import isclose
from itertools import product
import json
checks=[]
def eq(name,x,y):
 assert isclose(float(x),float(y),rel_tol=1e-10,abs_tol=1e-8),(name,x,y)
 checks.append(dict(case=name,value=float(x),expected=float(y)))
eq('responsive slack',8*120-500+F(1,2)*2*120,580)
eq('discount fully booked',8*120-500+2*40,540)
eq('urgent threshold',F(80,240),F(1,3))
eq('low urgency slack',460+F(1,5)*240,508)
eq('high urgency slack',460+F(4,5)*240,652)
eq('changed urgent margin threshold',F(80,160),.5)
eq('large capacity',F(1,2)*(1200-700)+F(1,2)*(600-700),200)
eq('small capacity',600-250,350)
eq('modular capacity',F(1,2)*(1200-250-400)+F(1,2)*(600-250),450)
eq('module loses two jobs',F(1,2)*(1000-250-400)+F(1,2)*(600-250),350)
eq('module reservation',450-100,350)
assert 600-250<400 and 650-250==400
eq('fixed finished variants',5*50-10*20,50)
eq('pooled before setup',10*50-10*20-10*5,250)
eq('pooled setup150',250-150,100);eq('pooled setup250',250-250,0)
eq('pooled tie setup',250-50,200)
eq('higher configuration tie setup',500-200-100-50,150)
eq('correlated finished demand',F(1,2)*10*50-200,50)
eq('correlated generic demand',F(1,2)*(10*50-10*5)-200-150,-125)
eq('generic configuration bottleneck',5*50-5*5-200-150,-125)
eq('independent joint failure',F(1,10)**2,.01)
eq('independent backup cost',F(1,100)*10000+500,600)
common=F(8,100);individual=F(2,100)
marginal=common+(1-common)*individual;joint=common+(1-common)*individual**2
eq('common marginal',marginal,.0984);eq('common joint',joint,.080368)
eq('common single loss',marginal*10000,984)
eq('common backup total',joint*10000+500,1303.68)
eq('common backup disadvantage',joint*10000+500-marginal*10000,319.68)
eq('common low-retainer saving',marginal*10000-(joint*10000+100),80.32)
# Independent event-tree enumeration checks the common-cause joint/marginal formulas.
tree=[]
for cf,af,bf in product([False,True],repeat=3):
 p=(common if cf else 1-common)*(individual if af else 1-individual)*(individual if bf else 1-individual)
 tree.append((p,cf or af,cf or bf))
assert sum(p for p,_,_ in tree)==1
assert sum(p for p,a,b in tree if a)==marginal and sum(p for p,a,b in tree if a and b)==joint
dist=[(2,F(1,4)),(4,F(1,2)),(6,F(1,4))]
def inv(q,cost=60):return sum(p*(100*min(q,d)+20*max(0,q-d)-cost*q) for d,p in dist)
assert [inv(q) for q in range(7)]==[0,40,80,100,120,100,80]
assert max(range(7),key=inv)==4
assert max(range(7),key=lambda q:inv(q,85))==2
eq('inventory original q4',inv(4),120);eq('inventory cost85 q2',inv(2,85),30);eq('inventory cost85 q4',inv(4,85),20)
eq('original critical CDF',F(100-60,100-20),.5)
eq('changed critical CDF',F(100-85,100-20),.1875)
assert max(range(4),key=inv)==3
for q in range(6):
 direct=inv(q+1)-inv(q)
 marginal_gain=(100-60)*sum(p for d,p in dist if d>q)-(60-20)*sum(p for d,p in dist if d<=q)
 assert direct==marginal_gain
eq('booking protect4',2*50+F(1,2)*4*120,340)
assert max(range(5),key=lambda k:(6-k)*50+F(1,2)*k*120)==4
assert max(range(5),key=lambda k:(6-k)*50+F(1,4)*k*120)==0
eq('impact old total',100*3,300);eq('impact new total',200*2,400)
eq('impact intensity change',F(2-3,3),F(-1,3));eq('impact total change',F(400-300,300),F(1,3))
eq('equivalent service transport90 reduction',300-(200+90),10)
eq('equivalent service transport120 increase',(200+120)-300,20)
eq('reusable monthly net',150-40-30,80)
eq('reusable payback',F(3000,80),37.5)
eq('reusable lifetime shortfall',36*80-3000,-120)
eq('reusable lower cleaning net',150-20-30,100)
eq('reusable lower cleaning lifetime',36*100-3000,600)
# Enumerate complete initial quantity and contingent expansion choices; check all joint states.
def policies(opening,reserve=100,extra_high=0):
 results=[]
 for initial in ['none','small','large']:
  setup={'none':0,'small':250,'large':700}[initial]
  for q in range(7):
   for expand_low,expand_high in product([False,True],repeat=2):
    if initial!='small' and (expand_low or expand_high):continue
    rows=[]
    for jd,ip in [(6,F(1,2)),(12,F(1,2))]:
     expand=expand_high if jd==12 else expand_low
     expansion=(400+(extra_high if jd==12 else 0)) if expand else 0
     before=opening-setup-60*q-expansion
     if before<reserve:break
     capacity={'none':0,'small':6,'large':12}[initial]+(6 if expand else 0)
     for d,p in dist:
      job=100*min(jd,capacity)
      accessory=100*min(q,d)+20*max(q-d,0)
      closing=before+job+accessory
      profit=job+accessory-setup-60*q-expansion
      assert closing-opening==profit
      rows.append(dict(jobDemand=jd,accessoryDemand=d,p=ip*p,profit=profit,beforeReceipt=before,closing=closing))
    if len(rows)==6:
     value=sum(s['p']*s['profit'] for s in rows)
     results.append(dict(initial=initial,q=q,expandLow=expand_low,expandHigh=expand_high,expected=value,minimumBeforeReceipt=min(s['beforeReceipt'] for s in rows),states=rows))
 return sorted(results,key=lambda r:r['expected'])
best=[]
for cash,value,qty,expand in [(750,470,4,False),(900,530,2,True),(1020,570,4,True)]:
 p=policies(cash)[-1];eq('complete cash-policy optimum '+str(cash),p['expected'],value)
 assert p['initial']=='small' and p['q']==qty and p['expandHigh']==expand and not p['expandLow']
 best.append(p)
eq('extra installation reverses funded policy',policies(900,extra_high=100)[-1]['expected'],470)
checks.append(dict(case='independent six-state complete-policy enumeration',passed=True,best=best))
# Retained native/companion calculations, independently reconstructed and preserved.
eq('retained custom contribution',20*300,6000)
eq('retained standard contribution',30*180,5400)
eq('retained support saving advantage',5400+1000-6000,400)
factor2=F(10,11)+F(100,121);factor3=factor2+F(1000,1331)
eq('retained immediate two-year',5000*factor2-10000,-1322.314049586777)
eq('retained high two-year',8000*factor2-10000,3884.297520661157)
eq('retained low two-year',2000*factor2-10000,-6528.925619834711)
assert F(8000,1)/F(11,10)-10000<0
eq('retained module1000',1000*(15-10)-8000,-3000)
eq('retained module3000',3000*(15-10)-8000,7000)
eq('retained module recovery',F(8000,5),1600)
eq('retained source A',1000*(40+2),42000)
eq('retained source B',1000*(37+3+4),44000)
eq('retained changed source B',1000*(37+3),40000)
eq('retained regular idle',5*(200-80),600)
eq('retained group',8*(150-80)+2*(200-80),800)
eq('retained regular full',10*(200-80),1200)
eq('retained reusable net',250-90-40,120)
eq('retained reusable payback',F(6000,120),50)
eq('retained reusable36',36*120,4320)
eq('retained cumulative immediate',3000*factor3-12000,-4539.444027047333)
assert 5000*factor2-12000<0
eq('retained cumulative changed immediate',5500*factor3-12000,1677.685950413223)
eq('retained cumulative changed wait',F(1,2)*(10000*factor2-12000)*F(10,11),2434.259954921112)
eq('retained native build now',F(1,2)*150000+F(1,2)*50000-100000,0)
eq('retained native wait',F(1,2)*50000,25000)
eq('retained native delayed loss',25000-30000,-5000)
eq('preserved operations clinic whole capacity',320//6,53)
eq('preserved operations gap',60-320//6,7)
eq('preserved operations lost contribution',7*150,1050)
eq('preserved operations hire recovery',F(3000,150),20)
eq('preserved operations subcontract',7*(330-240),630)
eq('preserved operations faster service capacity',320//5,64)
sales=60*330;variable=60*180;collections=6000+F(4,10)*sales
cash=4000+collections-variable-4000-1500-3000-5000
eq('preserved expansion profit',sales-variable-4000-1500-3000,500)
eq('preserved expansion collections',collections,13920)
eq('preserved expansion cash',cash,-6380)
eq('preserved expansion receivables',sales*(1-F(4,10)),11880)
eq('preserved expansion balance bridge',cash+11880+5000,10500)
eq('preserved expansion floor gap',5000-cash,11380)
eq('preserved expansion deferred equipment gap',5000-(cash+5000),6380)
eq('preserved expansion collection70 gap',5000-(cash+F(3,10)*sales),5440)
eq('preserved expansion both gap',5000-(cash+5000+F(3,10)*sales),440)
eq('preserved expansion revised marketing cash',cash+5000+F(3,10)*sales+500,5060)
eq('preserved equipment project NPV',-24000+10000*factor3+6000*F(1000,1331),5376.408715251691)
def clean(v):
 if isinstance(v,F):return float(v)
 raise TypeError(type(v).__name__)
print(json.dumps(dict(passed=True,checks=checks,limits='Original synthetic model/code checks; real demand, input validity, legal/operating authority, independent review and observed learner proficiency are not established.'),default=clean))

Related chapters