File size: 15,176 Bytes
6886978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
modules/bayesian_optimizer.py
Bayesian Optimization for IntelliForm v1.3

Gaussian Process surrogate model + Expected Improvement acquisition.
Inspired by ProcessOptimizer (Novo Nordisk, JCIM 2025) and BoTorch (Meta).

No PyTorch dependency β€” uses scikit-learn GaussianProcessRegressor.
Suitable for Streamlit Cloud deployment.

What it does differently from PuLP LP:
  - Learns from each run β€” builds a surrogate model of the objective landscape
  - Handles noisy objectives β€” real formulations have measurement noise
  - Suggests which experiment to run next to maximize learning (active learning)
  - Works well with small datasets (10-100 evaluated formulations)
  - Multi-objective via scalarization or Pareto front approximation

References:
  - Shahriari et al., "Taking the Human Out of the Loop", IEEE 2016
  - ProcessOptimizer: github.com/novonordisk-research/ProcessOptimizer
  - scikit-learn GaussianProcessRegressor docs
"""
from __future__ import annotations

import numpy as np
import pandas as pd
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple
import warnings
warnings.filterwarnings("ignore")

try:
    from sklearn.gaussian_process import GaussianProcessRegressor
    from sklearn.gaussian_process.kernels import Matern, ConstantKernel, WhiteKernel
    from sklearn.preprocessing import StandardScaler
    from scipy.stats import norm
    from scipy.optimize import minimize
    BAYES_OK = True
except ImportError:
    BAYES_OK = False


# ── Result schema ─────────────────────────────────────────────────────────────

@dataclass
class BayesianResult:
    success: bool
    blend: Dict[str, float]
    cost_per_kg: float
    bio_pct: float
    perf_score: float
    expected_improvement: float
    uncertainty: float              # GP posterior std at this point
    n_observations: int             # how many runs fed into GP
    acquisition_function: str       # "EI" / "UCB" / "PI"
    next_suggestion: Optional[Dict[str, float]] = None  # next experiment suggestion
    error_msg: Optional[str] = None


@dataclass
class BayesianState:
    """Persistent state β€” grows with each formulation run."""
    X_observed: List[List[float]] = field(default_factory=list)  # feature vectors
    y_observed: List[float] = field(default_factory=list)         # objective values
    blend_history: List[Dict] = field(default_factory=list)
    n_iterations: int = 0
    gp_model: Optional[object] = None
    scaler_X: Optional[object] = None


# ── Objective function ────────────────────────────────────────────────────────

def _blend_to_features(blend: Dict[str, float], db: pd.DataFrame,
                        feature_cols: List[str]) -> np.ndarray:
    """Convert blend (ingredient: pct) to weighted-average feature vector."""
    idx = db.set_index("Ingredient") if "Ingredient" in db.columns else db
    total = sum(blend.values()) or 100.0
    feat = np.zeros(len(feature_cols))
    for ing, pct in blend.items():
        if ing in idx.index:
            w = pct / total
            for i, col in enumerate(feature_cols):
                try:
                    feat[i] += w * float(idx.loc[ing, col])
                except Exception:
                    pass
    return feat


def _composite_objective(blend: Dict[str, float], db: pd.DataFrame,
                          w_cost: float = 0.3, w_bio: float = 0.4,
                          w_perf: float = 0.3) -> float:
    """
    Compute weighted composite objective for a blend.
    Returns value in [0, 1] β€” higher is better.
    """
    idx = db.set_index("Ingredient") if "Ingredient" in db.columns else db
    total = sum(blend.values()) or 100.0

    cost, bio, perf = 0.0, 0.0, 0.0
    for ing, pct in blend.items():
        if ing in idx.index:
            w = pct / total / 100
            try:
                cost += w * float(idx.loc[ing, "Cost_USD_kg"])
                bio  += w * float(idx.loc[ing, "Bio_based_pct"])
                perf += w * float(idx.loc[ing, "Performance_Score"])
            except Exception:
                pass

    # Normalize: cost is minimized (invert), bio and perf are maximized
    cost_score = max(0, 1 - cost / 20.0)  # normalize by max expected $20/kg
    bio_score  = bio / 100.0
    perf_score = perf / 100.0

    return w_cost * cost_score + w_bio * bio_score + w_perf * perf_score


# ── GP model ──────────────────────────────────────────────────────────────────

def _fit_gp(X: np.ndarray, y: np.ndarray) -> Tuple[object, object]:
    """Fit Gaussian Process regressor. Returns (gp, scaler)."""
    scaler = StandardScaler()
    X_scaled = scaler.fit_transform(X)

    kernel = (ConstantKernel(1.0, (1e-3, 1e3)) *
              Matern(length_scale=1.0, length_scale_bounds=(1e-2, 1e2), nu=2.5) +
              WhiteKernel(noise_level=0.01, noise_level_bounds=(1e-5, 0.1)))

    gp = GaussianProcessRegressor(
        kernel=kernel,
        n_restarts_optimizer=5,
        normalize_y=True,
        random_state=42,
    )
    gp.fit(X_scaled, y)
    return gp, scaler


def _expected_improvement(X_candidate: np.ndarray, gp: object,
                           scaler: object, y_best: float,
                           xi: float = 0.01) -> np.ndarray:
    """
    Expected Improvement acquisition function.
    xi: exploration-exploitation tradeoff (higher = more exploration).
    """
    X_scaled = scaler.transform(X_candidate)
    mu, sigma = gp.predict(X_scaled, return_std=True)
    sigma = np.maximum(sigma, 1e-8)

    z = (mu - y_best - xi) / sigma
    ei = (mu - y_best - xi) * norm.cdf(z) + sigma * norm.pdf(z)
    ei[sigma < 1e-8] = 0.0
    return ei


def _upper_confidence_bound(X_candidate: np.ndarray, gp: object,
                              scaler: object, kappa: float = 2.0) -> np.ndarray:
    """UCB acquisition: mu + kappa * sigma. kappa controls exploration."""
    X_scaled = scaler.transform(X_candidate)
    mu, sigma = gp.predict(X_scaled, return_std=True)
    return mu + kappa * sigma


# ── Main Bayesian optimization run ────────────────────────────────────────────

def run_bayesian_optimization(
    db: pd.DataFrame,
    max_cost: float,
    min_bio: float,
    min_perf: float,
    state: Optional[BayesianState] = None,
    n_random_init: int = 10,
    n_candidates: int = 200,
    max_conc: float = 0.70,
    acquisition: str = "EI",
    vertical: str = "all",
) -> Tuple[BayesianResult, BayesianState]:
    """
    Run Bayesian optimization for formulation.

    Phase 1 (n_iterations < n_random_init): random exploration
    Phase 2 (n_iterations >= n_random_init): GP-guided exploitation

    Args:
        db: Ingredient database (vertical-filtered)
        state: Persistent BayesianState from previous runs (None = fresh start)
        n_random_init: Random exploration rounds before GP kicks in
        n_candidates: Random candidates to evaluate per iteration
        acquisition: "EI" (Expected Improvement) or "UCB" (Upper Confidence Bound)

    Returns:
        (BayesianResult, updated BayesianState)
    """
    if not BAYES_OK:
        return BayesianResult(
            success=False, blend={}, cost_per_kg=0, bio_pct=0, perf_score=0,
            expected_improvement=0, uncertainty=0, n_observations=0,
            acquisition_function=acquisition,
            error_msg="scikit-learn/scipy not available β€” install requirements."
        ), state or BayesianState()

    if state is None:
        state = BayesianState()

    idx = db.set_index("Ingredient") if "Ingredient" in db.columns else db
    names = db["Ingredient"].tolist()

    if len(names) < 3:
        return BayesianResult(
            success=False, blend={}, cost_per_kg=0, bio_pct=0, perf_score=0,
            expected_improvement=0, uncertainty=0, n_observations=0,
            acquisition_function=acquisition,
            error_msg="Not enough ingredients in filtered database."
        ), state

    feature_cols = ["Cost_USD_kg", "Bio_based_pct", "Performance_Score",
                    "Biodegradability", "Ecotoxicity_Score", "Renewability_Score"]
    feature_cols = [c for c in feature_cols if c in db.columns]

    # Generate random candidate blends
    candidates = []
    for _ in range(n_candidates):
        # Dirichlet sample β€” random blend that sums to 100
        n_ings = np.random.randint(2, min(8, len(names) + 1))
        selected = np.random.choice(names, n_ings, replace=False)
        weights = np.random.dirichlet(np.ones(n_ings))

        # Cap max concentration
        weights = np.minimum(weights, max_conc)
        weights /= weights.sum()

        blend = {ing: round(w * 100, 1) for ing, w in zip(selected, weights)}

        # Apply constraints
        cost = sum(float(idx.loc[ing, "Cost_USD_kg"]) * pct / 100
                   for ing, pct in blend.items() if ing in idx.index)
        bio  = sum(float(idx.loc[ing, "Bio_based_pct"]) * pct / 100
                   for ing, pct in blend.items() if ing in idx.index)
        perf = sum(float(idx.loc[ing, "Performance_Score"]) * pct / 100
                   for ing, pct in blend.items() if ing in idx.index)

        if cost <= max_cost and bio >= min_bio and perf >= min_perf:
            candidates.append((blend, cost, bio, perf))

    if not candidates:
        # Relax constraints progressively
        for relax in [0.9, 0.8, 0.7]:
            for _ in range(n_candidates):
                n_ings = np.random.randint(2, min(6, len(names) + 1))
                selected = np.random.choice(names, n_ings, replace=False)
                weights = np.random.dirichlet(np.ones(n_ings))
                weights = np.minimum(weights, max_conc)
                weights /= weights.sum()
                blend = {ing: round(w * 100, 1) for ing, w in zip(selected, weights)}
                cost = sum(float(idx.loc[ing, "Cost_USD_kg"]) * pct / 100
                           for ing, pct in blend.items() if ing in idx.index)
                bio  = sum(float(idx.loc[ing, "Bio_based_pct"]) * pct / 100
                           for ing, pct in blend.items() if ing in idx.index)
                perf = sum(float(idx.loc[ing, "Performance_Score"]) * pct / 100
                           for ing, pct in blend.items() if ing in idx.index)
                if cost <= max_cost * (2 - relax) and bio >= min_bio * relax and perf >= min_perf * relax:
                    candidates.append((blend, cost, bio, perf))
            if candidates:
                break

    if not candidates:
        return BayesianResult(
            success=False, blend={}, cost_per_kg=0, bio_pct=0, perf_score=0,
            expected_improvement=0, uncertainty=0, n_observations=state.n_iterations,
            acquisition_function=acquisition,
            error_msg="No feasible candidates found. Loosen constraints."
        ), state

    # Phase 1: Random exploration β€” pick best from candidates by objective
    if state.n_iterations < n_random_init or len(state.X_observed) < 3:
        # Score all candidates and pick best
        scored = [(blend, cost, bio, perf,
                   _composite_objective(blend, db))
                  for blend, cost, bio, perf in candidates]
        scored.sort(key=lambda x: -x[4])
        best = scored[0]
        blend, cost, bio, perf, obj_val = best

        # Update state
        feat = _blend_to_features(blend, db, feature_cols)
        state.X_observed.append(feat.tolist())
        state.y_observed.append(obj_val)
        state.blend_history.append(blend)
        state.n_iterations += 1

        return BayesianResult(
            success=True, blend=blend, cost_per_kg=round(cost, 2),
            bio_pct=round(bio, 1), perf_score=round(perf, 1),
            expected_improvement=obj_val,
            uncertainty=0.5,  # high uncertainty in exploration phase
            n_observations=state.n_iterations,
            acquisition_function=f"Random Exploration (iter {state.n_iterations}/{n_random_init})",
        ), state

    # Phase 2: GP-guided exploitation
    X = np.array(state.X_observed)
    y = np.array(state.y_observed)

    try:
        gp, scaler = _fit_gp(X, y)
        state.gp_model = gp
        state.scaler_X = scaler
    except Exception as e:
        # GP failed β€” fall back to random
        scored = [(blend, cost, bio, perf, _composite_objective(blend, db))
                  for blend, cost, bio, perf in candidates]
        scored.sort(key=lambda x: -x[4])
        best = scored[0]
        blend, cost, bio, perf, obj_val = best
        return BayesianResult(
            success=True, blend=blend, cost_per_kg=round(cost, 2),
            bio_pct=round(bio, 1), perf_score=round(perf, 1),
            expected_improvement=obj_val, uncertainty=0.3,
            n_observations=state.n_iterations,
            acquisition_function="Random (GP failed)",
        ), state

    # Compute features for all candidates
    cand_feats = np.array([
        _blend_to_features(blend, db, feature_cols)
        for blend, _, _, _ in candidates
    ])

    y_best = max(state.y_observed)

    # Acquisition function
    if acquisition == "UCB":
        acq_values = _upper_confidence_bound(cand_feats, gp, scaler, kappa=2.0)
    else:  # EI default
        acq_values = _expected_improvement(cand_feats, gp, scaler, y_best, xi=0.01)

    best_idx = int(np.argmax(acq_values))
    best_blend, best_cost, best_bio, best_perf = candidates[best_idx]
    best_ei = float(acq_values[best_idx])

    # GP uncertainty at this point
    feat_best = cand_feats[best_idx].reshape(1, -1)
    _, uncertainty = gp.predict(scaler.transform(feat_best), return_std=True)
    uncertainty = float(uncertainty[0])

    # Update state
    obj_val = _composite_objective(best_blend, db)
    feat = _blend_to_features(best_blend, db, feature_cols)
    state.X_observed.append(feat.tolist())
    state.y_observed.append(obj_val)
    state.blend_history.append(best_blend)
    state.n_iterations += 1

    # Suggest next experiment (highest uncertainty among candidates)
    uncertainties = []
    for cf in cand_feats:
        _, unc = gp.predict(scaler.transform(cf.reshape(1,-1)), return_std=True)
        uncertainties.append(float(unc[0]))
    max_unc_idx = int(np.argmax(uncertainties))
    next_blend = candidates[max_unc_idx][0] if max_unc_idx != best_idx else None

    return BayesianResult(
        success=True,
        blend=best_blend,
        cost_per_kg=round(best_cost, 2),
        bio_pct=round(best_bio, 1),
        perf_score=round(best_perf, 1),
        expected_improvement=round(best_ei, 4),
        uncertainty=round(uncertainty, 4),
        n_observations=state.n_iterations,
        acquisition_function=acquisition,
        next_suggestion=next_blend,
    ), state