Skip to content

Commit

Permalink
Adds more access to initial solutions in ehrlich-holo (#279)
Browse files Browse the repository at this point in the history
* Adds more access to initial solutions of ehrlich holo

* Adds dist to gitignore

* Updates config to specifiy python version
  • Loading branch information
miguelgondu authored Oct 20, 2024
1 parent bdb3789 commit e9d0cc6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ src/poli.egg-info/
temp
build
.tox
dist/

# Ignore the registered objectives
src/poli/registered_objectives/*.sh
Expand Down
5 changes: 3 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[metadata]
name = poli
version = "1.0.0.dev11"
author = Miguel González-Duque
author_email = [email protected]
description = Protein Objectives Library
description = A library of discrete objective functions
long_description = file: README.md
long_description_content_type = text/markdown
classifiers =
Expand All @@ -14,7 +15,7 @@ classifiers =
package_dir =
=src
packages = find:
python_requires = >=3.8
python_requires = >=3.9
include_package_data = True

[options.packages.find]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ def __init__(
def __call__(self, x: np.ndarray, context: None) -> np.ndarray:
# First, we transform the strings into integers using the alphabet
batch_size = x.shape[0]
if len(x.shape) > 1:
# Flattening each element
x = np.array(["".join(x_i) for x_i in x])
x_ = np.array([[self.alphabet.index(c) for c in s] for s in x.flatten()])

values = self.inner_ehrlich(torch.from_numpy(x_)).numpy(force=True)
values[values == -np.inf] = self.return_value_on_unfeasible

return values.reshape(batch_size, 1)

@property
def initial_solution(self):
return self.inner_ehrlich.initial_solution()
def initial_solution(self, n_samples: int = 1):
return self.inner_ehrlich.initial_solution(n=n_samples)

@property
def optimal_solution(self):
Expand Down
18 changes: 15 additions & 3 deletions src/poli/objective_repository/ehrlich_holo/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,24 @@ def __init__(
evaluation_budget=evaluation_budget,
)

def initial_solution(self) -> np.ndarray:
def initial_solution(self, n_samples: int = 1) -> np.ndarray:
# This is a sequence of ints.
initial_solution_as_ints = self.inner_function.initial_solution
initial_solution_as_ints = self.inner_function.initial_solution(
n_samples=n_samples
)

# We convert it to a sequence of strings.
return np.array(["".join([self.alphabet[i] for i in initial_solution_as_ints])])
if n_samples == 1:
return np.array(
["".join([self.alphabet[i] for i in initial_solution_as_ints])]
)
else:
return np.array(
[
"".join([self.alphabet[i] for i in z_i])
for z_i in initial_solution_as_ints
]
)

def random_solution(self) -> np.ndarray:
random_solution_as_ints = self.inner_function.random_solution
Expand Down

0 comments on commit e9d0cc6

Please sign in to comment.