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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Physics-Informed Bayesian Optimization
|
| 3 |
+
emoji: ⚗️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "5.23.0"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
license: mit
|
| 11 |
+
tags:
|
| 12 |
+
- bayesian-optimization
|
| 13 |
+
- physics-informed
|
| 14 |
+
- experiment-design
|
| 15 |
+
- materials-science
|
| 16 |
+
- gaussian-process
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# Physics-Informed Bayesian Optimization Platform (PIBO)
|
| 20 |
+
|
| 21 |
+
A platform for designing experiments using physics-informed surrogate models with Bayesian optimization. The core idea: **use physical models as structured priors for Gaussian Processes**, so the GP learns residuals between physics predictions and real observations, dramatically improving sample efficiency.
|
| 22 |
+
|
| 23 |
+
## Architecture
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
physics_informed_bo/
|
| 27 |
+
├── config.py # Configuration classes
|
| 28 |
+
├── models/ # Surrogate models
|
| 29 |
+
│ ├── base.py # Abstract base class
|
| 30 |
+
│ ├── physics_model.py # Physics model wrapper + GPyTorch mean function
|
| 31 |
+
│ ├── gp_model.py # Standard GP & Physics-Informed GP
|
| 32 |
+
│ ├── hybrid_model.py # Hybrid surrogate (physics + GP)
|
| 33 |
+
│ └── multi_fidelity.py # Multi-fidelity model (physics=low, data=high)
|
| 34 |
+
├── priors/ # Prior management
|
| 35 |
+
│ ├── data_prior.py # Initial data management
|
| 36 |
+
│ ├── physics_prior.py # Physics model + constraints
|
| 37 |
+
│ └── prior_manager.py # Orchestrates prior combination
|
| 38 |
+
├── optimizers/ # Optimizer backends
|
| 39 |
+
│ ├── base_optimizer.py # Abstract optimizer
|
| 40 |
+
│ ├── botorch_optimizer.py # BoTorch backend (primary)
|
| 41 |
+
│ ├── ax_optimizer.py # AX Platform backend
|
| 42 |
+
│ ├── bofire_optimizer.py # BoFire backend
|
| 43 |
+
│ └── factory.py # Optimizer factory
|
| 44 |
+
├── experiment/ # Experiment design
|
| 45 |
+
│ ├── parameter_space.py # Parameter space definitions
|
| 46 |
+
│ ├── designer.py # Main experiment designer API
|
| 47 |
+
│ └── campaign.py # Full campaign management
|
| 48 |
+
├── utils/ # Utilities
|
| 49 |
+
│ ├── visualization.py # Plotting functions
|
| 50 |
+
│ └── diagnostics.py # Model diagnostics
|
| 51 |
+
└── examples/ # Usage examples
|
| 52 |
+
├── minimal_example.py # Quick start (~30 lines)
|
| 53 |
+
├── polymer_optimization.py # Full polymer design example
|
| 54 |
+
└── multi_fidelity_example.py
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
## Core Concept
|
| 58 |
+
|
| 59 |
+
Traditional BO uses a GP with a constant or zero mean function. This platform replaces that with a **physics model as the GP mean function**:
|
| 60 |
+
|
| 61 |
+
```
|
| 62 |
+
f(x) = physics_model(x) + GP_residual(x)
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
Where `GP_residual ~ GP(0, k(x,x'))` only needs to learn the discrepancy between physics and reality. Benefits:
|
| 66 |
+
|
| 67 |
+
1. **Sample efficiency**: Physics captures the trend, GP only needs to learn deviations
|
| 68 |
+
2. **Extrapolation**: Physics model provides reasonable predictions outside observed data
|
| 69 |
+
3. **Constraint awareness**: Physical constraints are naturally incorporated
|
| 70 |
+
4. **Graceful degradation**: System works with physics-only (no data), hybrid, or GP-only modes
|
| 71 |
+
|
| 72 |
+
## Quick Start
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
import torch
|
| 76 |
+
from physics_informed_bo import ExperimentDesigner, ParameterSpace
|
| 77 |
+
|
| 78 |
+
# Define your physics model
|
| 79 |
+
def my_physics_model(X):
|
| 80 |
+
temp, pressure = X[:, 0], X[:, 1]
|
| 81 |
+
return torch.exp(-5000 / temp) * pressure**0.5
|
| 82 |
+
|
| 83 |
+
# Define parameter space
|
| 84 |
+
space = ParameterSpace()
|
| 85 |
+
space.add_continuous("temperature", 300, 600, units="K")
|
| 86 |
+
space.add_continuous("pressure", 1, 50, units="bar")
|
| 87 |
+
|
| 88 |
+
# Create designer with physics + initial data
|
| 89 |
+
designer = ExperimentDesigner(
|
| 90 |
+
parameter_space=space,
|
| 91 |
+
physics_fn=my_physics_model,
|
| 92 |
+
initial_data=(X_init, y_init), # Your initial experiments
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Get next experiment suggestions
|
| 96 |
+
next_experiments = designer.suggest(n=3)
|
| 97 |
+
|
| 98 |
+
# After running experiments, update
|
| 99 |
+
designer.update(X_new, y_new)
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
## Full Campaign Example
|
| 103 |
+
|
| 104 |
+
```python
|
| 105 |
+
from physics_informed_bo import OptimizationCampaign, ParameterSpace
|
| 106 |
+
from physics_informed_bo.config import OptimizationConfig, AcquisitionType
|
| 107 |
+
|
| 108 |
+
config = OptimizationConfig(
|
| 109 |
+
acquisition_type=AcquisitionType.PHYSICS_INFORMED_EI,
|
| 110 |
+
max_iterations=30,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
campaign = OptimizationCampaign(
|
| 114 |
+
name="my_experiment",
|
| 115 |
+
parameter_space=space,
|
| 116 |
+
physics_fn=my_physics_model,
|
| 117 |
+
initial_data=(X_init, y_init),
|
| 118 |
+
config=config,
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Automated loop
|
| 122 |
+
results = campaign.run_automated(objective_fn=run_experiment)
|
| 123 |
+
|
| 124 |
+
# Or human-in-the-loop
|
| 125 |
+
suggestion = campaign.suggest_next()
|
| 126 |
+
# ... run experiment manually ...
|
| 127 |
+
campaign.report_result(suggestion[0], measured_value)
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## Surrogate Model Modes
|
| 131 |
+
|
| 132 |
+
The platform automatically selects the best mode based on available information:
|
| 133 |
+
|
| 134 |
+
| Data Available | Physics Model | Mode Selected | Description |
|
| 135 |
+
|---|---|---|---|
|
| 136 |
+
| None | Yes | `physics_only` | Pure physics predictions |
|
| 137 |
+
| < 20 points | Yes | `physics_as_mean` | Physics as GP mean, GP learns residual |
|
| 138 |
+
| 20-50 points | Yes | `weighted_ensemble` | Adaptive weighting of physics + GP |
|
| 139 |
+
| Any | No | `gp_only` | Standard GP (data-driven only) |
|
| 140 |
+
|
| 141 |
+
## Optimizer Backends
|
| 142 |
+
|
| 143 |
+
### BoTorch (Default)
|
| 144 |
+
- Full BoTorch acquisition function suite (EI, UCB, KG, NEI)
|
| 145 |
+
- Custom `PhysicsInformedEI` that penalizes physically implausible regions
|
| 146 |
+
- Batch optimization support
|
| 147 |
+
|
| 148 |
+
### AX Platform
|
| 149 |
+
- Structured experiment management
|
| 150 |
+
- Human-in-the-loop support
|
| 151 |
+
- Trial tracking and analysis
|
| 152 |
+
|
| 153 |
+
### BoFire
|
| 154 |
+
- Chemistry/materials-focused features
|
| 155 |
+
- Mixture constraints (sum-to-one)
|
| 156 |
+
- Multi-objective optimization
|
| 157 |
+
- Categorical and molecular parameters
|
| 158 |
+
|
| 159 |
+
## Physics Constraints
|
| 160 |
+
|
| 161 |
+
```python
|
| 162 |
+
from physics_informed_bo.priors import PhysicsPrior
|
| 163 |
+
|
| 164 |
+
physics = PhysicsPrior(physics_fn=my_model)
|
| 165 |
+
|
| 166 |
+
# Add thermodynamic constraint
|
| 167 |
+
physics.add_constraint(
|
| 168 |
+
name="gibbs_feasibility",
|
| 169 |
+
constraint_fn=lambda X: compute_gibbs(X), # <=0 is feasible
|
| 170 |
+
constraint_type="inequality",
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Add mass balance constraint
|
| 174 |
+
physics.add_constraint(
|
| 175 |
+
name="mass_balance",
|
| 176 |
+
constraint_fn=lambda X: X.sum(dim=-1) - 1.0, # ==0
|
| 177 |
+
constraint_type="equality",
|
| 178 |
+
)
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
## Installation
|
| 182 |
+
|
| 183 |
+
```bash
|
| 184 |
+
pip install torch gpytorch botorch numpy pandas matplotlib
|
| 185 |
+
|
| 186 |
+
# Optional backends
|
| 187 |
+
pip install ax-platform # For AX
|
| 188 |
+
pip install bofire # For BoFire
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
## Key Dependencies
|
| 192 |
+
|
| 193 |
+
- **PyTorch**: Tensor computation and autograd
|
| 194 |
+
- **GPyTorch**: Gaussian Process models
|
| 195 |
+
- **BoTorch**: Bayesian optimization acquisition functions
|
| 196 |
+
- **AX Platform** (optional): Experiment management
|
| 197 |
+
- **BoFire** (optional): Chemistry-focused BO
|