Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change population sampling to use random seed, not system random. #4

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.
### Fixed

- Fix import error for python<3.8 ([#3](https://github.com/AustinT/mol_ga/pull/3)) ([@austint])
- Fix unintended use of system random in sampling ([#4](https://github.com/AustinT/mol_ga/pull/4)) ([@austint])

## [0.1.0] - 2023-09-05

Expand Down
4 changes: 2 additions & 2 deletions mol_ga/general_ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def run_ga_maximization(
*,
scoring_func: Union[Callable[[list[str]], list[float]], CachedBatchFunction],
starting_population_smiles: set[str],
sampling_func: Callable[[list[tuple[float, str]], int], list[str]],
sampling_func: Callable[[list[tuple[float, str]], int, random.Random], list[str]],
offspring_gen_func: Callable[[list[str], int, random.Random, Optional[joblib.Parallel]], set[str]],
selection_func: Callable[[int, list[tuple[float, str]]], list[tuple[float, str]]],
max_generations: int,
Expand Down Expand Up @@ -108,7 +108,7 @@ def run_ga_maximization(
_, population_smiles = tuple(zip(*population)) # type: ignore[assignment]

# Sample SMILES from population to create offspring
samples_from_population = sampling_func(population, num_samples_per_generation)
samples_from_population = sampling_func(population, num_samples_per_generation, rng)

# Create the offspring
offspring = offspring_gen_func(
Expand Down
7 changes: 4 additions & 3 deletions mol_ga/sample_population.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from __future__ import annotations

import math
import random
from random import Random

import numpy as np


def uniform_qualitle_sampling(
population: list[tuple[float, str]],
n_sample: int,
rng: Random,
shuffle: bool = True,
) -> list[str]:
"""Sample SMILES by sampling uniformly from logarithmically spaced top-N."""
Expand All @@ -19,10 +20,10 @@ def uniform_qualitle_sampling(
for q in quantiles:
score_threshold = np.quantile([s for s, _ in population], q)
eligible_population = [smiles for score, smiles in population if score >= score_threshold]
samples.extend(random.choices(population=eligible_population, k=n_samples_per_quanitile))
samples.extend(rng.choices(population=eligible_population, k=n_samples_per_quanitile))

# Shuffle samples to decrease correlations between adjacent samples
if shuffle:
random.shuffle(samples)
rng.shuffle(samples)

return samples[:n_sample] # in case there are slightly too many samples