def test_fixed_sample2(): "Check that FixedStrategy does retries and None proposals right" def run(strategy): c = ScriptedController() c.strategy = strategy r1 = c.accept_eval(args=(1, )) r2 = c.accept_eval(args=(2, )) c.reject_eval(args=(3, )) r1.kill() r2.running() c.accept_eval(args=(3, )).complete(3) c.accept_eval(args=(1, )).complete(1) c.accept_eval(args=(4, )).complete(4) c.no_proposal() r2.complete(2) c.reject_terminate() c.accept_terminate() c.terminate() def g(): for k in range(1, 5): yield k run(FixedSampleStrategy([1, 2, 3, 4])) run(FixedSampleStrategy(g()))
def main(): "Testing routine." samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] controller = SerialController(lambda x: (x - 0.123) * (x - 0.123)) controller.strategy = FixedSampleStrategy(samples) result = controller.run() print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
def main(): logging.basicConfig(format="%(name)-18s: %(levelname)-8s %(message)s", level=logging.INFO) # Launch controller strategy = FixedSampleStrategy([1, 2, 3, 4, 5]) server = ThreadedTCPServer(strategy=strategy) cthread = threading.Thread(target=server.run) cthread.start() # Get controller port name = server.sockname logging.info("Launch controller at {0}".format(name)) # Launch workers wthreads = [] for k in range(2): wthread = threading.Thread(target=worker_main, args=(name, )) wthread.start() wthreads.append(wthread) # Wait on controller and workers cthread.join() for t in wthreads: t.join() result = server.controller.best_point() print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
def test_multistart1(): "Test MultistartStrategy" c = ScriptedController() strategy1 = PromiseStrategy(block=False) strategy2 = FixedSampleStrategy([1, 2, 3]) c.strategy = MultiStartStrategy(c, [strategy1, strategy2]) # Allow strategy2 to get one in, then strategy1 pre-empts r1 = c.accept_eval(args=(1, )) p = strategy1.promise_eval(100) time.sleep(0.1) r2 = c.accept_eval(args=(100, )) r1.complete(1) assert not p.ready() r2.complete(100) assert p.value == 100 strategy1.terminate() # Now strategy2 finishes c.accept_eval(args=(2, )).complete(2) c.accept_eval(args=(3, )).complete(3) # Now we should see termination c.reject_terminate() c.reject_terminate() c.accept_terminate() c.terminate()
def testSimpleThreadWorkerPreemption(threadWorker): """Testing routine.""" print( '<<<<<<<<<<<<<<<<<<<< Testing Preemption on {0} >>>>>>>>>>>>>>>>>>>'. format(threadWorker.__name__)) samples = [0.0, 0.1] controller = PreemptibleThreadController() strategy = FixedSampleStrategy(samples) strategy = CheckWorkerStrategy(controller, strategy) controller.strategy = strategy add_monitor(controller, 1) for threadID in range(4): """ Worker 0 will be pre-empted as soon as it is launched. Worker 1 will be pre-empted as soon as an eval is launched. Workers 2 and 3 will execute the requests. """ controller.launch_worker(threadWorker(threadID, controller, objective)) result = controller.run() if result.value == 1: print("Test Passed") else: print("Test Failed:") print("Final: {0:.3e} @ {1}".format(result.value, result.params))
def main(): "Testing routine." controller = SerialController(lambda x: (x-0.123)*(x-0.123)) strategies = [MaxEvalStrategy(controller, 100), FixedSampleStrategy(random_generator())] controller.strategy = SimpleMergedStrategy(controller, strategies) result = controller.run() print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
def test_maxeval1(): "Test MaxEvalStrategy" c = ScriptedController() strategy1 = MaxEvalStrategy(c, 2) strategy2 = FixedSampleStrategy([1, 2, 3, 4, 5]) c.strategy = SimpleMergedStrategy(c, [strategy1, strategy2]) c.accept_eval(args=(1, )).complete(1) c.accept_eval(args=(2, )).complete(2) c.accept_terminate() c.terminate()
def test_fixed_sample1(): "Check FixedStrategy in case of no kills or failures" c = ScriptedController() c.strategy = FixedSampleStrategy([1, 2, 3, 4]) c.accept_eval(args=(1, )).complete(1) c.accept_eval(args=(2, )).complete(2) c.accept_eval(args=(3, )).complete(3) c.accept_eval(args=(4, )).complete(4) c.accept_terminate() c.terminate()
def testSimpleSocketWorkerEvaluation(socketWorker): """Testing routine.""" print('<<<<<<<<<<<<<<<<<<<< Testing Evaluation on {0} >>>>>>>>>>>>>>>>>>>'.format(socketWorker.__name__)) # Launch controller samples = [0.0] strategy = FixedSampleStrategy(samples) hostip = socket.gethostbyname(socket.gethostname()) port = 50000 portopen = False while not portopen and port < 60000: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((hostip, port)) s.close() portopen = True logger.debug("Port open") except socket.error as error: if not error.errno == errno.EADDRINUSE: raise else: logger.debug("Port closed") port += 1 name = (hostip, port) server = PreemptibleThreadedTCPServer(sockname=name, strategy=strategy) cthread = threading.Thread(target=server.run, name='Server for {0}'.format(socketWorker.__name__)) cthread.start() # Get controller port name = server.sockname logging.debug("Launch controller at {0}".format(name)) # Launch workers def worker_main(name, threadID): logging.debug("Launching worker on port {0}".format(name[1])) socketWorker(objective, sockname=name, retries=1).run() wthreads = [] for threadID in range(1): wthread = threading.Thread(target=worker_main, args=(name, threadID), name='SocketWorker') print(wthread.name) wthread.start() wthreads.append(wthread) # Wait on controller and workers cthread.join() for t in wthreads: t.join() result = server.controller.best_point() if result.value == 1: print("Test Passed") else: print("Test Failed:") print("Final: {0:.3e} @ {1}".format(result.value, result.params))
def test_check_worker0(): "Check: Without CheckWorkerStrategy, FixedStrategy ignores availability" c = ScriptedController() c.strategy = FixedSampleStrategy([1, 2]) c.set_worker(False) r1 = c.accept_eval(args=(1, )) r1.complete(1) r2 = c.accept_eval(args=(2, )) r2.complete(2) c.accept_terminate() c.terminate()
def main(): "Testing routine." logging.basicConfig(format="%(name)-18s: %(levelname)-8s %(message)s", level=logging.INFO) samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] controller = SimTeamController(objective, delay, 5) strategy = FixedSampleStrategy(samples) strategy = CheckWorkerStrategy(controller, strategy) controller.strategy = strategy add_monitor(controller, 1) result = controller.run() print("Final: {0:.3e} @ {1}".format(result.value, result.params))
def test_check_worker1(): "Test CheckWorkerStrategy" c = ScriptedController() strategy = FixedSampleStrategy([1, 2]) c.strategy = CheckWorkerStrategy(c, strategy) r1 = c.accept_eval(args=(1, )) c.set_worker(False) r2 = c.no_proposal() r1.complete(1) c.set_worker(True) r2 = c.accept_eval(args=(2, )) r2.complete(2) c.accept_terminate() c.terminate()
def main(): "Controller process main." logging.basicConfig(format="%(name)-18s: %(levelname)-8s %(message)s", filename='test_mpi_serve.log-{0}'.format(rank), level=logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.INFO) formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s") ch.setFormatter(formatter) logging.getLogger('').addHandler(ch) strategy = FixedSampleStrategy([1, 2, 3, 4, 5]) c = MPIController(strategy) result = c.run() print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
def main(): "Testing routine." logging.basicConfig(format="%(name)-18s: %(levelname)-8s %(message)s", level=logging.INFO) samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] controller = ThreadController() strategy = FixedSampleStrategy(samples) strategy = CheckWorkerStrategy(controller, strategy) controller.strategy = strategy add_monitor(controller, 1) for _ in range(5): controller.launch_worker(BasicWorkerThread(controller, objective)) result = controller.run() print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
def testSimpleThreadWorkerEvaluation(threadWorker): """Testing routine.""" print('<<<<<<<<<<<<<<<<<<<< Testing Evaluation on {0} >>>>>>>>>>>>>>>>>>>'.format(threadWorker.__name__)) samples = [0.0] controller = ThreadController() strategy = FixedSampleStrategy(samples) controller.strategy = strategy for threadID in range(1): controller.launch_worker(SimpleEventThreadWorker(controller, objective)) result = controller.run() if result.value == 1: print("Test Passed") else: print("Test Failed:") print("Final: {0:.3e} @ {1}".format(result.value, result.params))
def main(): "Testing routine." logging.basicConfig(format="%(name)-18s: %(levelname)-8s %(message)s", level=logging.INFO) samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] controller = SimTeamController(objective, delay, 5) strategy = FixedSampleStrategy(samples) strategy = CheckWorkerStrategy(controller, strategy) strategy = InputStrategy(controller, strategy) controller.strategy = strategy add_monitor(controller, 1) def shutdown(): logging.info("Initiating external shutdown") strategy.terminate() controller.add_timer(7.0, shutdown) result = controller.run() print(("Final: {0:.3e} @ {1}".format(result.value, result.params)))
def main(): "Testing routine." # Log at DEBUG level to file, higher level to console logging.basicConfig(format="%(name)-18s: %(levelname)-8s %(message)s", filename='test_mpi_pw.log-{0}'.format(rank), level=logging.DEBUG) ch = logging.StreamHandler() ch.setLevel(logging.INFO) formatter = logging.Formatter("%(name)-12s: %(levelname)-8s %(message)s") ch.setFormatter(formatter) logging.getLogger('').addHandler(ch) samples = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] controller = MPIController() strategy = FixedSampleStrategy(samples) strategy = CheckWorkerStrategy(controller, strategy) strategy = AddArgStrategy(strategy, extra_args='./dummy_sim') strategy = ChaosMonkeyStrategy(controller, strategy, mtbf=3) controller.strategy = strategy result = controller.run() print("Final: {0:.3e} @ {1}".format(result.value, result.params))
def test_simple_merge1(): "Test SimpleMergeStrategy" c = ScriptedController() strategy1 = PromiseStrategy(block=False) strategy2 = FixedSampleStrategy([1, 2, 3]) c.strategy = SimpleMergedStrategy(c, [strategy1, strategy2]) # Allow strategy2 to get one in, then strategy1 pre-empts c.set_worker(False) c.no_proposal() c.set_worker(True) r1 = c.accept_eval(args=(1, )) p = strategy1.promise_eval(100) r2 = c.accept_eval(args=(100, )) r1.complete(1) assert not p.ready() r2.complete(100) assert p.value == 100 strategy1.terminate() c.reject_terminate() c.reject_terminate() c.accept_terminate() c.terminate()
def test_input1(): "Test InputStrategy" c = ScriptedController() strategy = FixedSampleStrategy([1, 2, 3]) strategy = InputStrategy(c, strategy) c.strategy = strategy r = c.accept_eval(args=(1, )) strategy.kill(r) c.accept_kill(r) r.kill() r = c.accept_eval(args=(1, )) r.complete(1) strategy.eval(100) c.reject_eval(args=(100, )) c.accept_eval(args=(100, )).complete(100) c.accept_eval(args=(2, )).complete(2) strategy.eval(101) r = c.accept_eval(args=(101, )) strategy.kill(r) c.reject_kill(r) r.complete(101) strategy.terminate() c.reject_terminate() c.accept_terminate() c.terminate()