def run_island(i): class Problem: def __init__(self, evaluator, lb, ub): from .utils import expect, check_vector # type: (Evaluator, array, array) -> None ''' Inits the problem with an objective evaluator (implementing the method evaluate), the parameter vector lower bound (a numpy array) and upper bound. Both bounds must have the same dimension. ''' check_vector(lb) check_vector(ub) expect(len(lb) == len(ub), 'Bounds mismatch') self.evaluator = evaluator self.lb = lb self.ub = ub def fitness(self, x): return evaluator.evaluate(x) def get_bounds(self): return (lb,ub) def get_name(self): return 'Sabaody udp' def get_extra_info(self): return 'Sabaody extra info' import pygmo as pg from multiprocessing import cpu_count from b2problem import B2Problem from params import getDefaultParamValues, getLowerBound, getUpperBound from pymemcache.client.base import Client mc_client = Client((i.mc_host,i.mc_port)) #udp = i.problem_factory() algorithm = pg.algorithm(pg.de()) problem = pg.problem(Problem(B2Problem(i.problem_factory), getLowerBound(), getUpperBound())) # TODO: configure pop size #a = pg.archipelago(n=cpu_count,algo=algorithm, prob=problem, pop_size=100) mc_client.set(i.domain_qualifier('island', str(i.id), 'status'), 'Running', 10000) mc_client.set(i.domain_qualifier('island', str(i.id), 'n_cores'), str(cpu_count()), 10000) print('Starting island {} with {} cpus'.format(str(i.id), str(cpu_count()))) #a.evolve(100) return 0
def benchmark_differential_evolution(generations): island = pg_island( algo=de(gen=generations), prob=B2_UDP(getLowerBound(),getUpperBound(),'../../../../../sbml/b2.xml'), size=10) N = 50 import arrow time_start = arrow.utcnow() print('Differential Evolution (pop. size {})'.format(island.get_population().get_f().size)) for k in range(N): island.evolve() island.wait() delta_t = arrow.utcnow() - time_start print('DE {:2}/{}: best fitness {:9.2f}, fevals {}, duration {}'.format( k,N,float(island.get_population().champion_f[0]), island.get_population().problem.get_fevals(), delta_t))
def benchmark_simulated_annealing(): island = pg_island( algo=simulated_annealing(Ts=1.,Tf=.01), prob=problem(B2_UDP(getLowerBound(),getUpperBound(),'../../../../../sbml/b2.xml')), size=10) N = 10 import arrow time_start = arrow.utcnow() print('Simulated Annealing (pop. size {})'.format(island.get_population().get_f().size)) for k in range(N): island.evolve() island.wait() delta_t = arrow.utcnow() - time_start print('SA {:2}/{}: best fitness {:9.2f}, fevals {}, duration {}'.format( k,N,float(island.get_population().champion_f[0]), island.get_population().problem.get_fevals(), delta_t))
def make_problem(): class B2_UDP: def __init__(self, lb, ub): # type: (Evaluator, array, array) -> None ''' Inits the problem with an objective evaluator (implementing the method evaluate), the parameter vector lower bound (a numpy array) and upper bound. Both bounds must have the same dimension. ''' from sabaody.utils import check_vector, expect check_vector(lb) check_vector(ub) expect(len(lb) == len(ub), 'Bounds mismatch') self.lb = lb self.ub = ub from b2problem import B2Problem self.evaluator = B2Problem('b2.xml') def fitness(self, x): return (self.evaluator.evaluate(x),) def get_bounds(self): return (self.lb,self.ub) def get_name(self): return 'Sabaody udp' def get_extra_info(self): return 'Sabaody extra info' def __getstate__(self): return { 'lb': self.lb, 'ub': self.ub} def __setstate__(self, state): self.lb = state['lb'] self.ub = state['ub'] from b2problem import B2Problem self.evaluator = B2Problem('b2.xml') import pygmo as pg return pg.problem(B2_UDP(getLowerBound(),getUpperBound()))
def run_island(id): class B2_UDP: def __init__(self, lb, ub): # type: (Evaluator, array, array) -> None ''' Inits the problem with an objective evaluator (implementing the method evaluate), the parameter vector lower bound (a numpy array) and upper bound. Both bounds must have the same dimension. ''' from sabaody.utils import check_vector, expect check_vector(lb) check_vector(ub) expect(len(lb) == len(ub), 'Bounds mismatch') self.lb = lb self.ub = ub from b2problem import B2Problem self.evaluator = B2Problem('b2.xml') def fitness(self, x): return (self.evaluator.evaluate(x), ) def get_bounds(self): return (self.lb, self.ub) def get_name(self): return 'Sabaody udp' def get_extra_info(self): return 'Sabaody extra info' def __getstate__(self): return {'lb': self.lb, 'ub': self.ub} def __setstate__(self, state): self.lb = state['lb'] self.ub = state['ub'] from b2problem import B2Problem self.evaluator = B2Problem('b2.xml') import pygmo as pg from pymemcache.client.base import Client mc_client = Client(('luna', 11211)) algorithm = pg.algorithm(pg.de(gen=1000)) from params import getLowerBound, getUpperBound problem = pg.problem(B2_UDP(getLowerBound(), getUpperBound())) i = pg.island(algo=algorithm, prob=problem, size=20) #mc_client.set(id.domain_qualifier('island', str(id), 'status'), 'Running', 10000) i.evolve() i.wait() import socket hostname = socket.gethostname() ip = [ l for l in ([ ip for ip in socket.gethostbyname_ex(socket.gethostname())[2] if not ip.startswith("127.") ][:1], [[(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)] ][0][1]]) if l ][0][0] return (ip, hostname)
def get_udp(validation_mode, n): if not validation_mode: return B4_UDP(getLowerBound(), getUpperBound()) else: raise RuntimeError('No validation')
from b2problem_validator import B2Validator_UDP import arrow from scipy.optimize import differential_evolution from pprint import pprint # make sure the database and table are ready before we do anything table = 'scipy_de_solo_runs' create_solo_benchmark_table( host='luna', user='******', database='sabaody', password='******', table=table) problem = B2Validator_UDP(getLowerBound(),getUpperBound(),'../../../../../sbml/b2.xml') print('initial score: {}'.format(problem.fitness(getDefaultParamValues())[0])) time_start = arrow.utcnow() N = 1000 r = differential_evolution( func=lambda x: float(problem.fitness(x)[0]), bounds=[(lb,ub) for lb,ub in zip(getLowerBound(),getUpperBound())], maxiter=N) time_end = arrow.utcnow() print('final score: {}'.format(problem.fitness(r.x)[0])) print('max iterations: {}'.format(N))
def get_udp(validation_mode, n): if not validation_mode: return B2_UDP(getLowerBound(), getUpperBound()) else: return B2Validator_UDP(getLowerBound(), getUpperBound(), n=n)