Spaces:
Sleeping
Sleeping
File size: 24,420 Bytes
72a3513 |
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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
"""Module containing a class Tree that used for tree search of retrosynthetic routes."""
import logging
import warnings
from collections import defaultdict, deque
from math import sqrt
from random import choice, uniform
from time import time
from typing import Dict, List, Set, Tuple
from CGRtools.reactor import Reactor
from CGRtools.containers import MoleculeContainer
from tqdm.auto import tqdm
from synplan.chem.precursor import Precursor
from synplan.chem.reaction import Reaction, apply_reaction_rule
from synplan.mcts.evaluation import ValueNetworkFunction
from synplan.mcts.expansion import PolicyNetworkFunction
from synplan.mcts.node import Node
from synplan.utils.config import TreeConfig
class Tree:
"""Tree class with attributes and methods for Monte-Carlo tree search."""
def __init__(
self,
target: MoleculeContainer,
config: TreeConfig,
reaction_rules: List[Reactor],
building_blocks: Set[str],
expansion_function: PolicyNetworkFunction,
evaluation_function: ValueNetworkFunction = None,
):
"""Initializes a tree object with optional parameters for tree search for target
molecule.
:param target: A target molecule for retrosynthetic routes search.
:param config: A tree configuration.
:param reaction_rules: A loaded reaction rules.
:param building_blocks: A loaded building blocks.
:param expansion_function: A loaded policy function.
:param evaluation_function: A loaded value function. If None, the rollout is
used as a default for node evaluation.
"""
# config parameters
self.config = config
assert isinstance(
target, MoleculeContainer
), "Target should be given as MoleculeContainer"
assert len(target) > 3, "Target molecule has less than 3 atoms"
target_molecule = Precursor(target)
target_molecule.prev_precursors.append(Precursor(target))
target_node = Node(
precursors_to_expand=(target_molecule,), new_precursors=(target_molecule,)
)
# tree structure init
self.nodes: Dict[int, Node] = {1: target_node}
self.parents: Dict[int, int] = {1: 0}
self.children: Dict[int, Set[int]] = {1: set()}
self.winning_nodes: List[int] = []
self.visited_nodes: Set[int] = set()
self.expanded_nodes: Set[int] = set()
self.nodes_visit: Dict[int, int] = {1: 0}
self.nodes_depth: Dict[int, int] = {1: 0}
self.nodes_prob: Dict[int, float] = {1: 0.0}
self.nodes_rules: Dict[int, float] = {}
self.nodes_init_value: Dict[int, float] = {1: 0.0}
self.nodes_total_value: Dict[int, float] = {1: 0.0}
# tree building limits
self.curr_iteration: int = 0
self.curr_tree_size: int = 2
self.start_time: float = 0
self.curr_time: float = 0
# building blocks and reaction reaction_rules
self.reaction_rules = reaction_rules
self.building_blocks = building_blocks
# policy and value functions
self.policy_network = expansion_function
if self.config.evaluation_type == "gcn":
if evaluation_function is None:
raise ValueError(
"Value function not specified while evaluation type is 'gcn'"
)
if (
evaluation_function is not None
and self.config.evaluation_type == "rollout"
):
raise ValueError(
"Value function is not None while evaluation type is 'rollout'. What should be evaluation type ?"
)
self.value_network = evaluation_function
# utils
self._tqdm = True # needed to disable tqdm with multiprocessing module
target_smiles = str(self.nodes[1].curr_precursor.molecule)
if target_smiles in self.building_blocks:
self.building_blocks.remove(target_smiles)
print(
"Target was found in building blocks and removed from building blocks."
)
def __len__(self) -> int:
"""Returns the current size (the number of nodes) in the tree."""
return self.curr_tree_size - 1
def __iter__(self) -> "Tree":
"""The function is defining an iterator for a Tree object.
Also needed for the bar progress display.
"""
self.start_time = time()
if self._tqdm:
self._tqdm = tqdm(
total=self.config.max_iterations, disable=self.config.silent
)
return self
def __repr__(self) -> str:
"""Returns a string representation of the tree (target SMILES, tree size, and
the number of found routes)."""
return self.report()
def __next__(self) -> [bool, List[int]]:
"""The __next__ method is used to do one iteration of the tree building.
:return: Returns True if the route was found and the node id of the last node in
the route. Otherwise, returns False and the id of the last visited node.
"""
if self.curr_iteration >= self.config.max_iterations:
raise StopIteration("Iterations limit exceeded.")
if self.curr_tree_size >= self.config.max_tree_size:
raise StopIteration("Max tree size exceeded or all possible routes found.")
if self.curr_time >= self.config.max_time:
raise StopIteration("Time limit exceeded.")
# start new iteration
self.curr_iteration += 1
self.curr_time = time() - self.start_time
if self._tqdm:
self._tqdm.update()
curr_depth, node_id = 0, 1 # start from the root node_id
explore_route = True
while explore_route:
self.visited_nodes.add(node_id)
if self.nodes_visit[node_id]: # already visited
if not self.children[node_id]: # dead node
self._update_visits(node_id)
explore_route = False
else:
node_id = self._select_node(node_id) # select the child node
curr_depth += 1
else:
if self.nodes[node_id].is_solved(): # found route
self._update_visits(
node_id
) # this prevents expanding of bb node_id
self.winning_nodes.append(node_id)
return True, [node_id]
if (
curr_depth < self.config.max_depth
): # expand node if depth limit is not reached
self._expand_node(node_id)
if not self.children[node_id]: # node was not expanded
value_to_backprop = -1.0
else:
self.expanded_nodes.add(node_id)
if self.config.search_strategy == "evaluation_first":
# recalculate node value based on children synthesisability and backpropagation
child_values = [
self.nodes_init_value[child_id]
for child_id in self.children[node_id]
]
if self.config.evaluation_agg == "max":
value_to_backprop = max(child_values)
elif self.config.evaluation_agg == "average":
value_to_backprop = sum(child_values) / len(
self.children[node_id]
)
elif self.config.search_strategy == "expansion_first":
value_to_backprop = self._get_node_value(node_id)
# backpropagation
self._backpropagate(node_id, value_to_backprop)
self._update_visits(node_id)
explore_route = False
if self.children[node_id]:
# found after expansion
found_after_expansion = set()
for child_id in iter(self.children[node_id]):
if self.nodes[child_id].is_solved():
found_after_expansion.add(child_id)
self.winning_nodes.append(child_id)
if found_after_expansion:
return True, list(found_after_expansion)
else:
self._backpropagate(node_id, self.nodes_total_value[node_id])
self._update_visits(node_id)
explore_route = False
return False, [node_id]
def _ucb(self, node_id: int) -> float:
"""Calculates the Upper Confidence Bound (UCB) statistics for a given node.
:param node_id: The id of the node.
:return: The calculated UCB.
"""
prob = self.nodes_prob[node_id] # predicted by policy network score
visit = self.nodes_visit[node_id]
if self.config.ucb_type == "puct":
u = (
self.config.c_ucb * prob * sqrt(self.nodes_visit[self.parents[node_id]])
) / (visit + 1)
ucb_value = self.nodes_total_value[node_id] + u
if self.config.ucb_type == "uct":
u = (
self.config.c_ucb
* sqrt(self.nodes_visit[self.parents[node_id]])
/ (visit + 1)
)
ucb_value = self.nodes_total_value[node_id] + u
if self.config.ucb_type == "value":
ucb_value = self.nodes_init_value[node_id] / (visit + 1)
return ucb_value
def _select_node(self, node_id: int) -> int:
"""Selects a node based on its UCB value and returns the id of the node with the
highest UCB.
:param node_id: The id of the node.
:return: The id of the node with the highest UCB.
"""
if self.config.epsilon > 0:
n = uniform(0, 1)
if n < self.config.epsilon:
return choice(list(self.children[node_id]))
best_score, best_children = None, []
for child_id in self.children[node_id]:
score = self._ucb(child_id)
if best_score is None or score > best_score:
best_score, best_children = score, [child_id]
elif score == best_score:
best_children.append(child_id)
# is needed for tree search reproducibility, when all child nodes has the same score
return best_children[0]
def _expand_node(self, node_id: int) -> None:
"""Expands the node by generating new precursor with policy (expansion) function.
:param node_id: The id the node to be expanded.
:return: None.
"""
curr_node = self.nodes[node_id]
prev_precursor = curr_node.curr_precursor.prev_precursors
tmp_precursor = set()
expanded = False
for prob, rule, rule_id in self.policy_network.predict_reaction_rules(
curr_node.curr_precursor, self.reaction_rules
):
for products in apply_reaction_rule(
curr_node.curr_precursor.molecule, rule
):
# check repeated products
if not products or not set(products) - tmp_precursor:
continue
tmp_precursor.update(products)
for molecule in products:
molecule.meta["reactor_id"] = rule_id
new_precursor = tuple(Precursor(mol) for mol in products)
scaled_prob = prob * len(
list(filter(lambda x: len(x) > self.config.min_mol_size, products))
)
if set(prev_precursor).isdisjoint(new_precursor):
precursors_to_expand = (
*curr_node.next_precursor,
*(
x
for x in new_precursor
if not x.is_building_block(
self.building_blocks, self.config.min_mol_size
)
),
)
child_node = Node(
precursors_to_expand=precursors_to_expand,
new_precursors=new_precursor,
)
for new_precursor in new_precursor:
new_precursor.prev_precursors = [new_precursor, *prev_precursor]
self._add_node(node_id, child_node, scaled_prob, rule_id)
expanded = True
if not expanded and node_id == 1:
raise StopIteration("\nThe target molecule was not expanded.")
def _add_node(
self,
node_id: int,
new_node: Node,
policy_prob: float = None,
rule_id: int = None,
) -> None:
"""Adds a new node to the tree with probability of reaction rules predicted by
policy function and applied to the parent node of the new node.
:param node_id: The id of the parent node.
:param new_node: The new node to be added.
:param policy_prob: The probability of reaction rules predicted by policy
function for thr parent node.
:return: None.
"""
new_node_id = self.curr_tree_size
self.nodes[new_node_id] = new_node
self.parents[new_node_id] = node_id
self.children[node_id].add(new_node_id)
self.children[new_node_id] = set()
self.nodes_visit[new_node_id] = 0
self.nodes_prob[new_node_id] = policy_prob
self.nodes_rules[new_node_id] = rule_id
self.nodes_depth[new_node_id] = self.nodes_depth[node_id] + 1
self.curr_tree_size += 1
if self.config.search_strategy == "evaluation_first":
node_value = self._get_node_value(new_node_id)
elif self.config.search_strategy == "expansion_first":
node_value = self.config.init_node_value
self.nodes_init_value[new_node_id] = node_value
self.nodes_total_value[new_node_id] = node_value
def _get_node_value(self, node_id: int) -> float:
"""Calculates the value for the given node (for example with rollout or value
network).
:param node_id: The id of the node to be evaluated.
:return: The estimated value of the node.
"""
node = self.nodes[node_id]
if self.config.evaluation_type == "random":
node_value = uniform(0, 1)
elif self.config.evaluation_type == "rollout":
node_value = min(
(
self._rollout_node(
precursor, current_depth=self.nodes_depth[node_id]
)
for precursor in node.precursors_to_expand
),
default=1.0,
)
elif self.config.evaluation_type == "gcn":
node_value = self.value_network.predict_value(node.new_precursors)
return node_value
def _update_visits(self, node_id: int) -> None:
"""Updates the number of visits from the current node to the root node.
:param node_id: The id of the current node.
:return: None.
"""
while node_id:
self.nodes_visit[node_id] += 1
node_id = self.parents[node_id]
def _backpropagate(self, node_id: int, value: float) -> None:
"""Backpropagates the value through the tree from the current.
:param node_id: The id of the node from which to backpropagate the value.
:param value: The value to backpropagate.
:return: None.
"""
while node_id:
if self.config.backprop_type == "muzero":
self.nodes_total_value[node_id] = (
self.nodes_total_value[node_id] * self.nodes_visit[node_id] + value
) / (self.nodes_visit[node_id] + 1)
elif self.config.backprop_type == "cumulative":
self.nodes_total_value[node_id] += value
node_id = self.parents[node_id]
def _rollout_node(self, precursor: Precursor, current_depth: int = None) -> float:
"""Performs a rollout simulation from a given node in the tree. Given the
current precursor, find the first successful reaction and return the new precursor.
If the precursor is a building_block, return 1.0, else check the
first successful reaction.
If the reaction is not successful, return -1.0.
If the reaction is successful, but the generated precursor are not
the building_blocks and the precursor cannot be generated without
exceeding current_depth threshold, return -0.5.
If the reaction is successful, but the precursor are not the
building_blocks and the precursor cannot be generated, return
-1.0.
:param precursor: The precursor to be evaluated.
:param current_depth: The current depth of the tree.
:return: The reward (value) assigned to the precursor.
"""
max_depth = self.config.max_depth - current_depth
# precursor checking
if precursor.is_building_block(self.building_blocks, self.config.min_mol_size):
return 1.0
if max_depth == 0:
print("max depth reached in the beginning")
# precursor simulating
occurred_precursor = set()
precursor_to_expand = deque([precursor])
history = defaultdict(dict)
rollout_depth = 0
while precursor_to_expand:
# Iterate through reactors and pick first successful reaction.
# Check products of the reaction if you can find them in in-building_blocks data
# If not, then add missed products to precursor_to_expand and try to decompose them
if len(history) >= max_depth:
reward = -0.5
return reward
current_precursor = precursor_to_expand.popleft()
history[rollout_depth]["target"] = current_precursor
occurred_precursor.add(current_precursor)
# Pick the first successful reaction while iterating through reactors
reaction_rule_applied = False
for prob, rule, rule_id in self.policy_network.predict_reaction_rules(
current_precursor, self.reaction_rules
):
for products in apply_reaction_rule(current_precursor.molecule, rule):
if products:
reaction_rule_applied = True
break
if reaction_rule_applied:
history[rollout_depth]["rule_index"] = rule_id
break
if not reaction_rule_applied:
reward = -1.0
return reward
products = tuple(Precursor(product) for product in products)
history[rollout_depth]["products"] = products
# check loops
if any(x in occurred_precursor for x in products) and products:
# sometimes manual can create a loop, when
# print('occurred_precursor')
reward = -1.0
return reward
if occurred_precursor.isdisjoint(products):
# added number of atoms check
precursor_to_expand.extend(
[
x
for x in products
if not x.is_building_block(
self.building_blocks, self.config.min_mol_size
)
]
)
rollout_depth += 1
reward = 1.0
return reward
def report(self) -> str:
"""Returns the string representation of the tree."""
return (
f"Tree for: {str(self.nodes[1].precursors_to_expand[0])}\n"
f"Time: {round(self.curr_time, 1)} seconds\n"
f"Number of nodes: {len(self)}\n"
f"Number of iterations: {self.curr_iteration}\n"
f"Number of visited nodes: {len(self.visited_nodes)}\n"
f"Number of found routes: {len(self.winning_nodes)}"
)
def route_score(self, node_id: int) -> float:
"""Calculates the score of a given route from the current node to the root node.
The score depends on cumulated node values nad the route length.
:param node_id: The id of the current given node.
:return: The route score.
"""
cumulated_nodes_value, route_length = 0, 0
while node_id:
route_length += 1
cumulated_nodes_value += self.nodes_total_value[node_id]
node_id = self.parents[node_id]
return cumulated_nodes_value / (route_length**2)
def route_to_node(self, node_id: int) -> List[Node,]:
"""Returns the route (list of id of nodes) to from the node current node to the
root node.
:param node_id: The id of the current node.
:return: The list of nodes.
"""
nodes = []
while node_id:
nodes.append(node_id)
node_id = self.parents[node_id]
return [self.nodes[node_id] for node_id in reversed(nodes)]
def synthesis_route(self, node_id: int) -> Tuple[Reaction,]:
"""Given a node_id, return a tuple of reactions that represent the
retrosynthetic route from the current node.
:param node_id: The id of the current node.
:return: The tuple of extracted reactions representing the synthesis route.
"""
nodes = self.route_to_node(node_id)
reaction_sequence = [
Reaction(
[x.molecule for x in after.new_precursors],
[before.curr_precursor.molecule],
)
for before, after in zip(nodes, nodes[1:])
]
for r in reaction_sequence:
r.clean2d()
return tuple(reversed(reaction_sequence))
def newickify(self, visits_threshold: int = 0, root_node_id: int = 1):
"""
Adopted from https://stackoverflow.com/questions/50003007/how-to-convert-python-dictionary-to-newick-form-format.
:param visits_threshold: The minimum number of visits for the given node.
:param root_node_id: The id of the root node.
:return: The newick string and meta dict.
"""
visited_nodes = set()
def newick_render_node(current_node_id: int) -> str:
"""Recursively generates a Newick string representation of the tree.
:param current_node_id: The id of the current node.
:return: A string representation of a node in a Newick format.
"""
assert (
current_node_id not in visited_nodes
), "Error: The tree may not be circular!"
node_visit = self.nodes_visit[current_node_id]
visited_nodes.add(current_node_id)
if self.children[current_node_id]:
# Nodes
children = [
child
for child in list(self.children[current_node_id])
if self.nodes_visit[child] >= visits_threshold
]
children_strings = [newick_render_node(child) for child in children]
children_strings = ",".join(children_strings)
if children_strings:
return f"({children_strings}){current_node_id}:{node_visit}"
# leafs within threshold
return f"{current_node_id}:{node_visit}"
return f"{current_node_id}:{node_visit}"
newick_string = newick_render_node(root_node_id) + ";"
meta = {}
for node_id in iter(visited_nodes):
node_value = round(self.nodes_total_value[node_id], 3)
node_synthesisability = round(self.nodes_init_value[node_id])
visit_in_node = self.nodes_visit[node_id]
meta[node_id] = (node_value, node_synthesisability, visit_in_node)
return newick_string, meta
|