Optimization constraints and custom scores
Declare supported chemistry requirements before custom scoring with Navigator.
Declare supported chemistry in the optimization call: hard requirements in constraints= /
properties=, preferences in objectives=. Navigator guides synthon search and sampling before
verifying final products. Docking and proprietary models belong in an optional callback.
import deepmedchem as dmc
capabilities = dmc.catalog()["optimization"]
assert capabilities.get("constraints", {}).get("enabled"), "Constraint support is not enabled"
# docking_score is your batch scorer: list[str] -> one score per molecule.
result = dmc.optimize(
docking_score,
direction="minimize",
database="enamine",
budget=2000,
batch_size=100,
properties={"MolWt": (None, 400)},
constraints={"structures": {
"all": [{"pattern_id": "alpha-amino-acid/v1"}],
"none": [{"query": {"format": "smarts", "value": "[N+](=O)[O-]"}}],
}},
)The versioned pattern has a precise chemical definition. Use a typed SMARTS or SMILES query for
a different motif. Structure groups support all, none, and any with minimum and
conditions. Matching uses chirality. Counts mean unique atom matches, including overlapping
matches; count bounds are limited to 1000.
properties= is a convenient descriptor-window mapping. constraints.properties accepts typed
registry conditions such as
{"property_id": "rdkit.mol_wt", "operator": "lte", "value": 400, "units": "Da"}.
Both forms intersect with the druglike profile if supplied; contradictions are rejected.
Guided acquisition is the default. Structure anchors restrict synthon tuple branches, including
motifs crossing reaction junctions. Released descriptor caches, reaction offsets and calibrated
margins guide property-feasible sampling. Exact product checks then remove false positives,
including count and stereochemistry mismatches. Unsupported anchor cases retain broader routes;
missing property assets use a reported fallback. Inspect optimization.engine.constraint_acquisition
for the actual execution path. Search remains heuristic rather than exhaustive.
A built-in-only optimization needs no callback or local RDKit installation:
result = dmc.optimize(
database="enamine", budget=500, batch_size=50, name="internal-qed",
properties={"MolWt": (None, 400), "MolLogP": (0, 3)},
objectives=[{"type": "property", "property_id": "rdkit.qed",
"lower": 0, "upper": 1}],
)Check catalog()["optimization"]["objectives"]["enabled"]. Supported objective types are
property, similarity, predicted_property and an optional external term. Each term declares
finite normalization bounds lower < upper, direction (default maximize), and a positive
weight (default 1). The service clips the normalized value to [0,1], reverses it for minimized
terms and maximizes their weighted mean. Objective normalization bounds do not enforce
eligibility; hard bounds belong in constraints.
result = dmc.optimize(
docking_score, direction="maximize", database="enamine", budget=500, batch_size=50,
properties={"MolWt": (None, 400)},
objectives=[
{"type": "external", "direction": "minimize", "lower": -12, "upper": -4, "weight": 2},
{"type": "property", "property_id": "rdkit.qed", "lower": 0, "upper": 1},
],
)Here the callback returns raw docking scores only. Built-in components are computed internally;
the learned external scorer is trained separately. Results contain the aggregate score and
raw components in metrics.objective_0, metrics.objective_1, etc.
For similarity within a required substructure domain, combine the structure constraint with
{"type": "similarity", "metric_id": "rdkit.ecfp4_tanimoto", "reference_smiles": "CCO", "lower": 0, "upper": 1}. Morgan-OR ranks tuples inside the constrained branches; exact product
fingerprints determine the final score.
ADMET availability is specific to the database and its installed release. With a client,
inspect client.optimizations.capabilities("enamine"); the returned
predicted_properties maps endpoint IDs to units. The equivalent catalog field is
optimization.database_capabilities["enamine-real-v5a"].predicted_properties.
The legacy optimization.constraints.predicted_properties contains only endpoints
shared by every advertised database, so it can be empty while Enamine supports ADMET.
Also check the catalog's constraints.enabled and objectives.enabled flags.
with dmc.Client() as client:
capabilities = client.optimizations.capabilities("enamine")
endpoint = "openadmet-herg-pchembl"
units = capabilities["predicted_properties"][endpoint]
result = dmc.optimize(
client=client,
database="enamine",
direction="maximize", # aggregate desirability
budget=100,
batch_size=20,
constraints={"predicted_properties": [
{"endpoint_id": endpoint, "operator": "lte", "value": 6, "units": units},
]},
objectives=[
{"type": "predicted_property", "endpoint_id": endpoint, "units": units,
"direction": "minimize", "lower": 3, "upper": 8},
],
)The bounds above illustrate the API; choose thresholds for your experiment. CP16 ranks synthon tuples before assembly. The matching full-product model supplies the final eligibility check and objective value. These are model predictions, not experimental measurements. A database with no advertised endpoint rejects that term before a campaign is created. Adding an external docking term keeps docking in the callback while the same internal ADMET guidance and checks remain active.
Do not hide supported motifs, descriptor limits or ADMET thresholds in callback penalties. Rejected candidates use no molecule scoring budget. Built-in-only hosted runs reserve their complete requested credit budget at creation; external and mixed runs retain per-round credit reservations. Imported seed evidence retains its existing audit policy.
For the CLI, save the constraint object as constraints.json:
dmc optimize run my-docking -d enamine --minimize --property MolWt=:400 \
--constraints constraints.json --score-cmd './dock.sh {input} {output}'Constraints are part of the run identity. Reuse the same specification to resume; choose a new
run name to change requirements. The manual synchronous and asynchronous create/ask/tell APIs
accept the same constraints object. Resource constraint_execution entries explain where
checks execute. RDKit is optional on the client; chemistry queries compile on the worker before
the first batch is proposed.
For built-in-only CLI runs, supply --maximize --objectives objectives.json and omit
--score-cmd. Mixed runs use both options and an explicit external term. The synchronous and
asynchronous create APIs accept the same objective list. Changing terms, normalization or
constraints requires a new run name.