python3 - <<'PY' import math ln2=math.log(2) def f(x): return 0.0 if x<=0 else -x*math.log(x)/ln2 def H(x): return f(x)+f(1-x) def obj(r,b,c): pA=20-b-c-3/r;pB=3/r+b;a1=6-3/r;a=5-b-c if pA<=0 or pB<=0 or a1<0 or a<0 or c<3 or c<4-b*r: return 1e9 HX=(pA/20)*(f(9/pA)+f(a1/pA)+f(a/pA))+(pB/20)*(f((3/r)/pB)+f(b/pB)) HY=(pB/20)*H(r)+(c/20)*(f(3/c)+f((1-b*r)/c)+f((c-4+b*r)/c)) return 2.1822287-HX-HY def golden(fun,a,b,tol=1e-13): g=(math.sqrt(5)-1)/2;c=b-g*(b-a);d=a+g*(b-a);fc=fun(c);fd=fun(d) while b-a>tol: if fc1e-10: xn=[xw[i]+t*dx[i] for i in range(8)] if norm(res(xn))