bitten: update bitten.py

This commit is contained in:
Connor Olding 2022-06-19 02:43:55 -07:00
parent c66e25da24
commit 191bdec97d

View File

@ -133,84 +133,32 @@ def _flatten(it):
def _penalize_v2021_23(penalties, tol=1e-5, scale=1e10):
n_con = len(penalties)
unmet = sum(p > 0.0 for p in penalties)
unmet = sum(p > tol for p in penalties)
growth = 4.0 ** (1.0 / n_con) # "ps"
penalty = 0.0
for p in penalties:
p = max(p, 0.0)
p = p if p > tol else 0.0
squared = p * p
penalty = growth * penalty + p + squared + p * squared
return scale * (unmet + penalty)
def _penalize_v2022_11(penalties, tol=1e-5, scale=1e10):
n_con = len(penalties)
unmet = sum(p > 0.0 for p in penalties)
growth = 3.0 ** (1.0 / n_con) # "ps"
penalty = 0.0
for p in penalties:
p = max(p, 0.0)
penalty = growth * penalty + p + p * p * p
return scale * (unmet + penalty)
def _penalize_v2022_19(penalties, tol=1e-5, scale=1e10):
n_con = len(penalties)
growth = 3.0 ** (1.0 / n_con) # "ps"
increment = n_con**-0.5 # "pnsi"
penalty, nominal = 0.0, 0.0 # "pns", "pnsm"
for p in penalties:
p = max(p, 0.0)
penalty = growth * penalty + increment + p + p * p * p
nominal = growth * nominal + increment
return scale * (penalty - nominal + 1.0)
def _penalize_v2022_22(penalties, tol=1e-5, scale=1e10):
n_con = len(penalties)
growth = 3.0 ** (1.0 / n_con) # "ps"
increment = n_con**-0.5 # "pnsi"
coeff = increment**3.0 # "pnm"
penalty, nominal = 0.0, 0.0 # "pns", "pnsm"
for p in penalties:
p = coeff * max(p, 0.0)
squared = p * p
penalty = growth * penalty + increment + p + squared + p * squared
nominal = growth * nominal + increment
return scale * (penalty - nominal + 1.0)
def _penalize_v2022_25(penalties, tol=1e-5, scale=1e10):
n_con = len(penalties)
growth = 3.0 ** (1.0 / n_con) # "ps"
increment = n_con**-0.5 # "pnsi"
penalty, nominal = 0.0, 0.0 # "pns", "pnsm"
for p in penalties:
p = max(p, 0.0)
p = p - tol if p > tol else 0.0
squared = p * p
penalty = growth * penalty + increment + p + squared + p * squared
nominal = growth * nominal + increment
return scale * (penalty - nominal + 1.0)
def _penalize_v2022_25_1(penalties, tol=1e-5, scale=1e10):
n_con = len(penalties)
growth = 3.0 ** (1.0 / n_con) # "ps"
increment = n_con**-0.5 # "pnsi"
penalty = 0.0 # "pns"
for p in penalties:
p = max(p, 0.0)
penalty = growth * penalty + increment + p + p * p
return scale * (1.0 + penalty + penalty * penalty)
penalizers = {
1: _penalize_v2021_23,
2: _penalize_v2022_11,
3: _penalize_v2022_19,
4: _penalize_v2022_22,
5: _penalize_v2022_25,
6: _penalize_v2022_25_1,
+1: _penalize_v2021_23,
+5: _penalize_v2022_25,
}
@ -340,7 +288,7 @@ class Constrain(Objective):
def penalize(self, *x):
penalties = _flatten(cons(*x) for cons in self.constraints)
if not any(p > 0.0 for p in penalties):
if not any(p > self.tol for p in penalties):
return 0.0
return self._penalize(penalties, tol=self.tol)