Skip to content

Commit

Permalink
Pyciemss optimize multiple constraints (#117)
Browse files Browse the repository at this point in the history
* Step 1

* correction to qoi + riskbound

* more info in QoI as isMinimized effects more

* rename to qoi_methods as its a bit better

* correct test request

* correction to optimzie test request
  • Loading branch information
Tom-Szendrey authored Sep 16, 2024
1 parent 8518c86 commit d3e133c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
45 changes: 33 additions & 12 deletions service/models/operations/optimize.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import ClassVar, List, Optional
from typing import ClassVar, List, Optional, Union
from enum import Enum
from utils.rabbitmq import OptimizeHook
from pika.exceptions import AMQPConnectionError
Expand Down Expand Up @@ -31,17 +31,33 @@ class QOIMethod(str, Enum):
class QOI(BaseModel):
method: QOIMethod = QOIMethod.day_average
contexts: List[str] = []
risk_bound: float = 0
is_minimized: bool = True

def gen_call(self):
contexts = self.contexts
qoi_map = {
QOIMethod.day_average: lambda samples: obs_nday_average_qoi(
samples, contexts, 1
),
QOIMethod.max: lambda samples: obs_max_qoi(samples, contexts),
}
if self.is_minimized is True:
qoi_map = {
QOIMethod.day_average: lambda samples: obs_nday_average_qoi(
samples, contexts, 1
),
QOIMethod.max: lambda samples: obs_max_qoi(samples, contexts),
}
else:
qoi_map = {
QOIMethod.day_average: lambda samples: -obs_nday_average_qoi(
samples, contexts, 1
),
QOIMethod.max: lambda samples: -obs_max_qoi(samples, contexts),
}
return qoi_map[self.method]

def gen_risk_bound(self):
if self.is_minimized is True:
return self.risk_bound
else:
return -self.risk_bound


def objfun(x, initial_guess, objective_function_option):
if objective_function_option == "lower_bound":
Expand Down Expand Up @@ -80,7 +96,7 @@ class OptimizeExtra(BaseModel):
)
maxiter: int = 5
maxfeval: int = 25
alpha: float = 0.95
alpha: Union[List[float], float] = 0.95
solver_method: str = "dopri5"
# https://github.com/ciemss/pyciemss/blob/main/pyciemss/integration_utils/interface_checks.py
solver_step_size: float = Field(
Expand All @@ -99,8 +115,7 @@ class Optimize(OperationRequest):
None
) # Theses are interventions provided that will not be optimized
logging_step_size: float = 1.0
qoi: QOI
risk_bound: float
qoi: list[QOI]
bounds_interventions: List[List[float]]
extra: OptimizeExtra = Field(
None,
Expand Down Expand Up @@ -170,6 +185,12 @@ def gen_pyciemss_args(self, job_id):
def progress_hook(current_results):
logging.info(f"Optimize current results: {current_results.tolist()}")

qoi_methods = []
risk_bounds = []
for qoi in self.qoi:
qoi_methods.append(qoi.gen_call())
risk_bounds.append(qoi.gen_risk_bound())

return {
"model_path_or_json": amr_path,
"logging_step_size": self.logging_step_size,
Expand All @@ -180,8 +201,8 @@ def progress_hook(current_results):
self.optimize_interventions.initial_guess[0],
self.optimize_interventions.objective_function_option[0],
),
"qoi": self.qoi.gen_call(),
"risk_bound": self.risk_bound,
"qoi": qoi_methods,
"risk_bound": risk_bounds,
"initial_guess_interventions": self.optimize_interventions.initial_guess,
"bounds_interventions": self.bounds_interventions,
"static_parameter_interventions": optimize_interventions,
Expand Down
12 changes: 6 additions & 6 deletions tests/examples/optimize/input/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
"start": 0,
"end": 90
},
"qoi": {
"qoi": [{
"contexts": ["Infected_state"],
"method": "day_average"
},
"risk_bound": 10.0,
"method": "day_average",
"risk_bound": 10.0,
"is_minimized": true
}],
"bounds_interventions": [[0.0], [3.0]],
"extra": {
"num_samples": 4,
"is_minimized": true
"num_samples": 4
}
}

0 comments on commit d3e133c

Please sign in to comment.