|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import math |
| 4 | + |
| 5 | +from app.core.schema import Candidate, Session |
| 6 | +from app.samplers.base import clamp_vector, make_rng |
| 7 | + |
| 8 | + |
| 9 | +class QualityDiversityMixSampler: |
| 10 | + """Sampler inspired by quality-diversity search with several complementary emitters.""" |
| 11 | + |
| 12 | + name = "quality_diversity_mix" |
| 13 | + |
| 14 | + def propose(self, session: Session, seed: int) -> list[Candidate]: |
| 15 | + rng = make_rng(seed + 991) |
| 16 | + dimensions = max(1, len(session.current_z)) |
| 17 | + base_direction = self._base_direction(session.current_z, dimensions) |
| 18 | + lateral_direction = self._orthogonal_direction(base_direction) |
| 19 | + cover_pool = [self._unit_vector([rng.uniform(-1.0, 1.0) for _ in range(dimensions)]) for _ in range(28)] |
| 20 | + far_directions = self._greedy_cover(cover_pool, max(2, session.config.candidate_count // 2)) |
| 21 | + |
| 22 | + medium = min(max(session.config.trust_radius * 0.42, 0.16), session.config.trust_radius) |
| 23 | + far = min(max(session.config.trust_radius * 0.82, 0.28), session.config.trust_radius) |
| 24 | + counter = min(max(session.config.trust_radius * 0.3, 0.12), session.config.trust_radius) |
| 25 | + |
| 26 | + patterns: list[tuple[str, list[float], float]] = [ |
| 27 | + ("qd_refine", base_direction, medium * 0.62), |
| 28 | + ("qd_forward", base_direction, medium), |
| 29 | + ("qd_lateral_plus", lateral_direction, medium), |
| 30 | + ("qd_far_cover_1", far_directions[0], far), |
| 31 | + ("qd_lateral_minus", [-value for value in lateral_direction], medium), |
| 32 | + ("qd_counter", [-value for value in base_direction], counter), |
| 33 | + ] |
| 34 | + for index, direction in enumerate(far_directions[1:], start=2): |
| 35 | + patterns.append((f"qd_far_cover_{index + 1}", direction, far)) |
| 36 | + |
| 37 | + candidates: list[Candidate] = [] |
| 38 | + for index in range(session.config.candidate_count): |
| 39 | + role, direction, radius = patterns[index % len(patterns)] |
| 40 | + jitter_scale = 0.014 if "refine" in role else 0.024 if "far_cover" not in role else 0.03 |
| 41 | + jitter = [rng.uniform(-jitter_scale, jitter_scale) for _ in range(dimensions)] |
| 42 | + z = clamp_vector( |
| 43 | + [ |
| 44 | + current + (axis * radius) + noise |
| 45 | + for current, axis, noise in zip(session.current_z, direction, jitter, strict=False) |
| 46 | + ], |
| 47 | + session.config.trust_radius, |
| 48 | + ) |
| 49 | + candidates.append( |
| 50 | + Candidate( |
| 51 | + round_id="", |
| 52 | + candidate_index=index, |
| 53 | + z=z, |
| 54 | + sampler_role=role, |
| 55 | + predicted_score=sum(z) + (0.01 if "far_cover" in role else 0.0), |
| 56 | + predicted_uncertainty=0.16 + (0.02 * index), |
| 57 | + seed=seed, |
| 58 | + generation_params={ |
| 59 | + "image_size": session.config.image_size, |
| 60 | + "qd_radius": round(radius, 4), |
| 61 | + "qd_direction": [round(value, 4) for value in direction], |
| 62 | + "qd_emitter_role": role, |
| 63 | + }, |
| 64 | + ) |
| 65 | + ) |
| 66 | + return candidates |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def _base_direction(current_z: list[float], dimensions: int) -> list[float]: |
| 70 | + length = math.sqrt(sum(value * value for value in current_z)) |
| 71 | + if length > 1e-8: |
| 72 | + return [value / length for value in current_z] |
| 73 | + direction = [0.0 for _ in range(dimensions)] |
| 74 | + direction[0] = 1.0 |
| 75 | + if dimensions > 1: |
| 76 | + direction[1] = 0.35 |
| 77 | + norm = math.sqrt(sum(value * value for value in direction)) |
| 78 | + return [value / norm for value in direction] |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def _orthogonal_direction(base_direction: list[float]) -> list[float]: |
| 82 | + dimensions = len(base_direction) |
| 83 | + if dimensions == 1: |
| 84 | + return [1.0] |
| 85 | + lateral = [0.0 for _ in range(dimensions)] |
| 86 | + lateral[0] = -base_direction[1] |
| 87 | + lateral[1] = base_direction[0] |
| 88 | + for index in range(2, dimensions): |
| 89 | + lateral[index] = base_direction[index] * (-0.45 if index % 2 == 0 else 0.45) |
| 90 | + length = math.sqrt(sum(value * value for value in lateral)) |
| 91 | + if length == 0.0: |
| 92 | + lateral[1] = 1.0 |
| 93 | + return lateral |
| 94 | + return [value / length for value in lateral] |
| 95 | + |
| 96 | + @classmethod |
| 97 | + def _greedy_cover(cls, pool: list[list[float]], count: int) -> list[list[float]]: |
| 98 | + if not pool: |
| 99 | + return [] |
| 100 | + selected = [pool[0]] |
| 101 | + remaining = pool[1:] |
| 102 | + while remaining and len(selected) < count: |
| 103 | + best_direction = max( |
| 104 | + remaining, |
| 105 | + key=lambda candidate: min(cls._angular_distance(candidate, prior) for prior in selected), |
| 106 | + ) |
| 107 | + selected.append(best_direction) |
| 108 | + remaining = [candidate for candidate in remaining if candidate is not best_direction] |
| 109 | + return selected[:count] |
| 110 | + |
| 111 | + @staticmethod |
| 112 | + def _angular_distance(left: list[float], right: list[float]) -> float: |
| 113 | + cosine = sum(a * b for a, b in zip(left, right, strict=False)) |
| 114 | + cosine = max(-1.0, min(1.0, cosine)) |
| 115 | + return math.acos(cosine) |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def _unit_vector(values: list[float]) -> list[float]: |
| 119 | + norm = math.sqrt(sum(value * value for value in values)) |
| 120 | + if norm == 0.0: |
| 121 | + fallback = [0.0 for _ in values] |
| 122 | + fallback[0] = 1.0 |
| 123 | + return fallback |
| 124 | + return [value / norm for value in values] |
0 commit comments