Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
filter: Predicate to use for filtering candidates
reraise: Flag indicating whether exceptions in the
objective function evaluations should be re-raised,
terminating the optimization.
Returns:
Record minimizing merit() and satisfying filter();
or None if nothing satisfies the filter
"""
try:
return self._run(merit=merit, filter=filter, reraise=reraise)
finally:
self.call_term_callbacks()
class ThreadController(Controller):
"""Thread-based optimization controller.
The optimizer dispatches work to a queue of workers.
Each worker has methods of the form
worker.eval(record)
worker.kill(record)
These methods are asynchronous: they start a function evaluation
or termination, but do not necessarily complete it. The worker
must respond to eval requests, but may ignore kill requests. On
eval requests, the worker should either attempt the evaluation or
mark the record as killed. The worker sends status updates back
to the controller in terms of lambdas (executed at the controller)
that update the relevant record. When the worker becomes
available again, it should use add_worker to add itself back to
Args:
merit: Function to minimize (default is r.value)
filter: Predicate to use for filtering candidates
Returns:
Record minimizing merit() and satisfying filter();
or None if nothing satisfies the filter
"""
try:
return self._run(merit=merit, filter=filter)
finally:
self.call_term_callbacks()
class ScriptedController(Controller):
"""Run a test script of actions from the controller.
The ScriptedController is meant to test that a strategy adheres
to an expected sequence of proposed actions in a given scenario.
Attributes:
strategy: Strategy for choosing optimization actions.
fevals: Database of function evaluations
"""
def __init__(self):
Controller.__init__(self)
self._can_work = True
def add_timer(self, timeout, callback):
"Add timer."
import logging
from threading import Thread
from mpi4py import MPI
from poap.controller import Controller
# Get module-level logger
logger = logging.getLogger(__name__)
# Get MPI communicator and rank
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nproc = comm.Get_size()
class MPIController(Controller):
"""MPI controller.
The MPI controller *must* run at rank 0.
The server sends messages of the form
('eval', record_id, args, extra_args)
('eval', record_id, args)
('kill', record_id)
('terminate')
The default messages received are
('update_dict', record_id, dict)
('running', record_id)
('kill', record_id)
('cancel', record_id)
('complete', record_id, value)
"""
if self.process is not None and self.process.poll() is None:
logger.debug("ProcessWorker is killing subprocess")
self.process.terminate()
def kill(self, record):
"Send kill message."
self._kill_process()
super(ProcessWorkerThread, self).kill(record)
def terminate(self):
"Send termination message."
self._kill_process()
super(ProcessWorkerThread, self).terminate()
class SimTeamController(Controller):
"""Simulated parallel optimization controller.
Run events in simulated time. If two events are scheduled at the
same time, we prioritize by when the event was added to the queue.
Attributes:
strategy: Strategy for choosing optimization actions.
objective: Objective function
delay: Time delay function
workers: Number of workers available
fevals: Database of function evaluations
time: Current simulated time
time_events: Time-stamped event heap
"""
def __init__(self, objective, delay, workers):
import logging
from threading import Thread
from mpi4py import MPI
from poap.controller import Controller
# Get module-level logger
logger = logging.getLogger(__name__)
# Get MPI communicator and rank
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nproc = comm.Get_size()
class MPIController(Controller):
"""MPI controller.
The MPI controller *must* run at rank 0.
The server sends messages of the form
('eval', record_id, args, extra_args)
('eval', record_id, args)
('kill', record_id)
('terminate')
The default messages received are
('update_dict', record_id, dict)
('running', record_id)
('kill', record_id)
('cancel', record_id)
('complete', record_id, value)
"""
self.feval_callbacks.append(callback)
def remove_term_callback(self, callback):
"Remove a callback from the term callback list."
self.term_callbacks = [
c for c in self.term_callbacks if c != callback
]
def remove_feval_callback(self, callback):
"Remove a callback from the feval callback list."
self.feval_callbacks = [
c for c in self.feval_callbacks if c != callback
]
class SerialController(Controller):
"""Serial optimization controller.
Attributes:
strategy: Strategy for choosing optimization actions.
objective: Objective function
fevals: Database of function evaluations
skip: if True, skip over "None" proposals
"""
def __init__(self, objective, skip=False):
"""Initialize the controller.
Args:
objective: Objective function
skip: if True, skip over "None" proposals
"""