bitten: add a couple tests

This commit is contained in:
Connor Olding 2022-06-07 06:58:15 +02:00
parent 4c113eed15
commit 5b75e03b35
2 changed files with 60 additions and 0 deletions

46
bitten/test_b2.py Normal file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env python3
from bitten import *
import numpy as np
constraints, tol = (
lambda *p: 2.0 * p[0] + 2.0 * p[1] + p[9] + p[10] - 10.0,
lambda *p: 2.0 * p[0] + 2.0 * p[2] + p[9] + p[11] - 10.0,
lambda *p: 2.0 * p[1] + 2.0 * p[2] + p[10] + p[11] - 10.0,
lambda *p: -8.0 * p[0] + p[9],
lambda *p: -8.0 * p[1] + p[10],
lambda *p: -8.0 * p[2] + p[11],
lambda *p: -2.0 * p[3] - p[4] + p[9],
lambda *p: -2.0 * p[5] - p[6] + p[10],
lambda *p: -2.0 * p[7] - p[8] + p[11],
), 1e-15
@bite(Minimize(), Constrain(*constraints, tol=tol), _debug=True)
def fun(
p0=Bound(0.0, 1.0),
p1=Bound(0.0, 1.0),
p2=Bound(0.0, 1.0),
p3=Bound(0.0, 1.0),
p4=Bound(0.0, 1.0),
p5=Bound(0.0, 1.0),
p6=Bound(0.0, 1.0),
p7=Bound(0.0, 1.0),
p8=Bound(0.0, 1.0),
p9=Bound(0.0, 100.0),
pA=Bound(0.0, 100.0),
pB=Bound(0.0, 100.0),
pC=Bound(0.0, 1.0),
):
p = (p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pA, pB, pC)
s1, s2, s3 = 0.0, 0.0, 0.0
for i in range(4):
s1 += p[i]
s2 += np.square(p[i])
for i in range(4, 13):
s3 += p[i]
return 5.0 * s1 - 5.0 * s2 - s3
print("solution:", fun())
for k, v in fun.__optimized__.items():
print(f"{k:>4}: {v:.8f}")

14
bitten/test_e.py Normal file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env python3
from bitten import *
@bite(Minimize(0), budget=1_000, _debug=True)
def nothing(x):
return 420.69
print("OPTIMIZED:", nothing.__optimized__, "RESULT:", nothing.__result__, sep="\n")
@bite(Minimize(1, 2), budget=1_000, _debug=True)
def nothing(a, b):
return a + b
print("OPTIMIZED:", nothing.__optimized__, "RESULT:", nothing.__result__, sep="\n")