Upload code/polyedit/multiobjective.py with huggingface_hub
Browse files
code/polyedit/multiobjective.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Preference-conditioned metrics and utilities for two-property PolyEdit."""
|
| 2 |
+
from __future__ import annotations
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def preference_grid(n=11):
|
| 8 |
+
return tuple((float(a), float(1.0 - a)) for a in np.linspace(0.0, 1.0, n))
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def reward_vector(poly, directions, values, means, scales, clip=3.0):
|
| 12 |
+
raw = [d * (values[poly][p] - means[p]) / (scales[p] + 1e-9)
|
| 13 |
+
for p, d in zip(("Egc", "Egb"), directions)]
|
| 14 |
+
return (np.clip(raw, -clip, clip) + clip) / (2.0 * clip)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def hypervolume_2d(points, reference=(0.0, 0.0)):
|
| 18 |
+
"""Area dominated by two-dimensional maximization points above a fixed reference."""
|
| 19 |
+
rx, ry = reference
|
| 20 |
+
pts = sorted(((max(float(x), rx), max(float(y), ry)) for x, y in points), reverse=True)
|
| 21 |
+
area, top = 0.0, ry
|
| 22 |
+
for x, y in pts:
|
| 23 |
+
if y > top:
|
| 24 |
+
area += (x - rx) * (y - top)
|
| 25 |
+
top = y
|
| 26 |
+
return area
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def mip(weights, rewards):
|
| 30 |
+
return float(np.mean([np.dot(w, r) for w, r in zip(weights, rewards)]))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def induced_graph(graph, nodes):
|
| 34 |
+
nodes = set(nodes)
|
| 35 |
+
return {p: [q for q in graph.get(p, ()) if q in nodes]
|
| 36 |
+
for p in sorted(nodes) if any(q in nodes for q in graph.get(p, ())) }
|