-
Notifications
You must be signed in to change notification settings - Fork 0
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
Added parallel search logic #10
Open
OleguerCanal
wants to merge
6
commits into
master
Choose a base branch
from
parallel_search
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf20c93
Added parallel search logic
OleguerCanal 81b53f1
Fixed bug
OleguerCanal 11f94d6
Added retrials when launching instances
OleguerCanal 1f8d5ba
Fixed bug
OleguerCanal f552b20
Fixed bug
OleguerCanal c762aa0
Removed print
OleguerCanal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
import sys | ||
|
||
sys.path.append(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
from src.gaussian_process_search import GaussianProcessSearch | ||
from src.parallel_searcher import ParallelSearcher | ||
from src.search_job_instance import SearchJobInstance |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
|
||
import time | ||
|
||
class ParallelSearcher: | ||
def __init__(self, optimizer, job_class): | ||
""" Instantiate parallel searching | ||
|
||
Args: | ||
optimizer (GaussianProcessSearch): Optimizer used to find next points to test | ||
job_class (SearchJobInstance): Implementation of SearchJobInstance to manage jobs | ||
""" | ||
self.optimizer = optimizer | ||
self.job_class = job_class | ||
|
||
def __launch(self, instance, candidate, retrials=5): | ||
launch_status = 1 | ||
attempt = 0 | ||
while launch_status != 0 and attempt < retrials: | ||
launch_status = instance.launch(**candidate) | ||
if launch_status != 0: | ||
print("There was some error launching the instance. Retrying (" + str(attempt) + "/" + retrials + ")") | ||
time.sleep(0.1) | ||
attempt += 1 | ||
|
||
def optimize(self, | ||
n_calls=10, | ||
n_random_starts=5, | ||
noise=0.01, | ||
n_parallel_jobs=1, | ||
refresh_rate=1, | ||
first_id=0, | ||
verbose=True, | ||
plot_results=False): | ||
|
||
# Instantiate all initial jobs | ||
instances = [self.job_class(i) for i in range(first_id, first_id + n_parallel_jobs)] | ||
|
||
# Get all initial candidates | ||
candidates = [] | ||
if len(self.optimizer.x_values) == 0: # If first points, sample random | ||
candidates = self.optimizer.get_random_candidate(n_parallel_jobs) | ||
else: | ||
candidates = self.optimizer.get_next_candidate(n_parallel_jobs) | ||
|
||
# Launch all instances | ||
for i in range(n_parallel_jobs): | ||
print(candidates[i]) | ||
self.__launch(instance=instances[i], candidate=candidates[i]) | ||
n_calls -= 1 | ||
|
||
while n_calls > 0: | ||
time.sleep(refresh_rate) # refresh rate in seconds | ||
for i in range(n_parallel_jobs): | ||
instance = instances[i] | ||
if instance.done(): | ||
n_calls -= 1 | ||
instance_params = instance.passed_args | ||
instance_result = instance.get_result() | ||
|
||
# Display information | ||
print("*****") | ||
print("Finished job:", instance.id) | ||
print("Instance_params:", instance_params) | ||
print("Instance_result:", instance_result) | ||
print("*****") | ||
|
||
# Add point-evaluation info to the optimizer | ||
self.optimizer.add_point_value(instance_params, instance_result) | ||
self.optimizer.save_values() | ||
|
||
# Instantiate new job instance | ||
candidate = self.optimizer.get_next_candidate(1, n_random_starts)[0] | ||
instances[i] = self.job_class(instance.id + n_parallel_jobs) | ||
self.__launch(instance=instances[i], candidate=candidate) | ||
|
||
# Display information | ||
print("*****") | ||
print("Starting job:", instances[i]) | ||
print("Instance_params:", candidate) | ||
print("*****") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
class SearchJobInstance(ABC): | ||
"""Abstract class linked to a single job. | ||
It is used to manage | ||
""" | ||
def __init__(self, id): | ||
self.id = id | ||
|
||
@abstractmethod | ||
def launch(self, **kwargs) -> int: | ||
"""Execute command given the objective arguments | ||
IMPORTANT: Must be non-blocking! | ||
|
||
Returns: | ||
status(Int): 0 everything is ok, 1 there was some error | ||
""" | ||
self.passed_args = kwargs | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def get_result(self) -> float: | ||
"""Return final result of the optimization | ||
""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def done(self) -> bool: | ||
"""True if job has finished, false otherwise | ||
IMPORTANT: Must be non-blocking! | ||
""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def kill(self) -> None: | ||
"""Finish job | ||
""" | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def end(self) -> None: | ||
"""Run any task necessary when done | ||
""" | ||
raise NotImplementedError | ||
Empty file.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these two functions called by anyone? If not, I would not include them in the abstract SearchJobInstance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are not called now. I thought they might be useful but you are right, they are not needed.
I'll remove them for now :)