def generate_archipelago(self, topology_name, metric, monitor): from os.path import isfile from re import compile db_regex = compile( r'sql:(\w+)@([\w:]+),pw=([^,]+),db=([\w]+)\(name=(\w+),n_islands=(\d+),island_size=(\d+),migrant_pool_size=(\d+),generations=(\d+)\):(.*)' ) if isfile(topology_name): import pickle with open(topology_name) as f: return pickle.load(f)['archipelago'] elif db_regex.match(topology_name) is not None: m = db_regex.match(topology_name) from sabaody import TopologyGenerator, BiopredynTopologyGenerator name = m.group(5) if name == 'pagmo': generator_class = TopologyGenerator elif name == 'biopredyn': generator_class = BiopredynTopologyGenerator generator = generator_class(n_islands=int(m.group(6)), island_size=int(m.group(7)), migrant_pool_size=int(m.group(8)), generations=int(m.group(9))) topology, id = generator.find_in_database(desc=m.group(10), user=m.group(1), host=m.group(2), pw=m.group(3), db=m.group(4)) self.topology_set_id = id self.topology_id = topology['id'] self.generations = topology['generations'] return Archipelago( TopologyFactory.prefixIds(topology['archipelago'].topology, prefix=self.run_id + '-')) else: # generate the topology from available presets via command line arguments topology_factory = TopologyFactory( problem=self.make_problem(), island_size=self.island_size, migrant_pool_size=self.migrant_pool_size, domain_qualifier=monitor.getNameQualifier(), mc_host=monitor.mc_host, mc_port=monitor.mc_port) if topology_name == 'ring' or topology_name == 'bidir-ring': return Archipelago( topology_factory.createBidirRing(self.make_algorithm(), self.n_islands), metric) elif topology_name == 'one-way-ring': return Archipelago( topology_factory.createOneWayRing(self.make_algorithm(), self.n_islands), metric) else: raise RuntimeError('Unrecognized topology')
def create_variants(self, n, desc, category, constructor): def assign_2nd_alg(archipelago, algo): if category == 'rings': for island in archipelago.topology.every_other_island(): island.algorithm = algo elif hasattr(archipelago.topology, 'endpoints'): for island in archipelago.topology.endpoints: island.algorithm = algo elif isinstance(archipelago.topology, FullyConnectedTopology): for island in islice(archipelago.topology.islands, None, None, 2): island.algorithm = algo return archipelago def assign_algs(archipelago, algos): ''' Evenly partitions and assigns algorithms to islands. ''' for island,algo in zip(archipelago.topology.islands, cycle(algos)): island.algorithm = algo g = self.generations self.new_topology( desc='{}, de'.format(desc), category=category, algorithms=['de'], archipelago=Archipelago(constructor(de(gen=g),n))) self.new_topology( desc='{}, de1220'.format(desc), category=category, algorithms=['de1220'], archipelago=Archipelago(constructor(de1220(gen=g),n))) self.new_topology( desc='{}, sade'.format(desc), category=category, algorithms=['sade'], archipelago=Archipelago(constructor(sade(gen=g),n))) self.new_topology( desc='{}, bee_colony'.format(desc), category=category, algorithms=['bee_colony'], archipelago=Archipelago(constructor(bee_colony(gen=g),n))) # de + nelder mead combo self.new_topology( desc='{}, de+nelder mead'.format(desc), category=category, algorithms=['de','neldermead'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_nelder_mead())) # de + praxis combo self.new_topology( desc='{}, de+praxis'.format(desc), category=category, algorithms=['de','praxis'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_praxis())) # de + sade combo self.new_topology( desc='{}, de+sade'.format(desc), category=category, algorithms=['de','sade'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), sade(gen=g)))
import arrow from tempfile import gettempdir from os.path import join import json start = arrow.utcnow() champions = [] stage_deltas = [] N = 2 for i in range(N): with RBRun('luna', 11211) as run: print('Started run {} of {}'.format(i+1,N)) from rbsetup import make_problem a = Archipelago(4, make_problem, run.initial_score, None, run.getNameQualifier(), run.mc_host, run.mc_port) result = a.run(sc) import pprint pp = pprint.PrettyPrinter(indent=2) pp.pprint(result) with open(join(gettempdir(),'deltas.json'),'w') as f: json.dump(result,f) with open(join(gettempdir(),'deltas.json')) as f: ip,host,island_id,raw_deltas = json.load(f)[0][:4] champions.append(array([c for c,d,ids in raw_deltas])) stage_deltas.append(array([float(nan_to_num(mean(d))) for c,d,ids in raw_deltas])) end = arrow.utcnow() print('Total time: {}'.format((end-start)))
# dumbed down archipelago from __future__ import print_function, division, absolute_import from pyspark import SparkContext, SparkConf conf = SparkConf().setAppName("dumbeddown") sc = SparkContext(conf=conf) from uuid import uuid4 from sabaody import Island, Archipelago, run_island, problem_constructor, getQualifiedName from toolz import partial run_id = str(uuid4()) num_islands = 4 a = Archipelago(4, problem_constructor, 0., None, partial(getQualifiedName, 'B2', str(run_id)), 'luna', 11211) a.run(sc, 0.)
def create_variants(self, n, desc, category, constructor): def assign_2nd_alg(archipelago, algo): if category == 'rings': for island in archipelago.topology.every_other_island(): island.algorithm = algo elif hasattr(archipelago.topology, 'endpoints'): for island in archipelago.topology.endpoints: island.algorithm = algo elif isinstance(archipelago.topology, FullyConnectedTopology): for island in islice(archipelago.topology.islands, None, None, 2): island.algorithm = algo return archipelago def assign_algs(archipelago, algos): ''' Evenly partitions and assigns algorithms to islands. ''' for island,algo in zip(archipelago.topology.islands, cycle(algos)): island.algorithm = algo g = self.generations self.new_topology( desc='{}, de'.format(desc), category=category, algorithms=['de'], archipelago=Archipelago(constructor(de(gen=g),n))) self.new_topology( desc='{}, de1220'.format(desc), category=category, algorithms=['de1220'], archipelago=Archipelago(constructor(de1220(gen=g),n))) self.new_topology( desc='{}, sade'.format(desc), category=category, algorithms=['sade'], archipelago=Archipelago(constructor(sade(gen=g),n))) self.new_topology( desc='{}, ihs'.format(desc), category=category, algorithms=['ihs'], archipelago=Archipelago(constructor(ihs(gen=g),n))) self.new_topology( desc='{}, pso'.format(desc), category=category, algorithms=['pso'], archipelago=Archipelago(constructor(pso(gen=g),n))) self.new_topology( desc='{}, pso_gen'.format(desc), category=category, algorithms=['pso_gen'], archipelago=Archipelago(constructor(pso_gen(gen=g),n))) # self.new_topology( # desc='{}, simulated_annealing'.format(desc), # category=category, # algorithms=['simulated_annealing'], # archipelago=Archipelago(constructor(simulated_annealing(),n))) self.new_topology( desc='{}, bee_colony'.format(desc), category=category, algorithms=['bee_colony'], archipelago=Archipelago(constructor(bee_colony(gen=g),n))) self.new_topology( desc='{}, cmaes'.format(desc), category=category, algorithms=['cmaes'], archipelago=Archipelago(constructor(cmaes(gen=g),n))) self.new_topology( desc='{}, nsga2'.format(desc), category=category, algorithms=['nsga2'], archipelago=Archipelago(constructor(nsga2(gen=g),n))) self.new_topology( desc='{}, xnes'.format(desc), category=category, algorithms=['xnes'], archipelago=Archipelago(constructor(xnes(gen=g),n))) # de + nelder mead combo self.new_topology( desc='{}, de+nelder mead'.format(desc), category=category, algorithms=['de','neldermead'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_nelder_mead())) # de + praxis combo self.new_topology( desc='{}, de+praxis'.format(desc), category=category, algorithms=['de','praxis'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), self.make_praxis())) # de + nsga2 combo self.new_topology( desc='{}, de+nsga2'.format(desc), category=category, algorithms=['de','nsga2'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), nsga2(gen=g))) # de + de1220 combo self.new_topology( desc='{}, de+de1220'.format(desc), category=category, algorithms=['de','de1220'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), de1220(gen=g))) # de + sade combo self.new_topology( desc='{}, de+sade'.format(desc), category=category, algorithms=['de','sade'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), sade(gen=g))) # de + pso combo self.new_topology( desc='{}, de+pso'.format(desc), category=category, algorithms=['de','pso'], archipelago=assign_2nd_alg(Archipelago(constructor(de(gen=g),n)), pso(gen=g))) # extra configurations for fully connected topology if constructor is self.factory.createFullyConnected: self.new_topology( desc='{}, de+pso+praxis'.format(desc), category=category, algorithms=['de','pso','praxis'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis()))) self.new_topology( desc='{}, de+pso+praxis+nsga2'.format(desc), category=category, algorithms=['de','pso','praxis','nsga2'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), nsga2(gen=g)))) self.new_topology( desc='{}, de+pso+praxis+cmaes'.format(desc), category=category, algorithms=['de','pso','praxis','cmaes'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), cmaes(gen=g)))) self.new_topology( desc='{}, de+pso+praxis+xnes'.format(desc), category=category, algorithms=['de','pso','praxis','xnes'], archipelago=assign_algs(Archipelago(constructor(de(gen=g),n)), (de(gen=g), pso(gen=g), self.make_praxis(), xnes(gen=g))))
run = 1 else: run += 1 client.set('com.how2cell.sabaody.B2.run', run, 604800) run_id = str(uuid4()) client.set('com.how2cell.sabaody.B2.runId', run_id, 604800) client.set('com.how2cell.sabaody.B2.run.startTime', str(time()), 604800) client.set('com.how2cell.sabaody.B2.run.status', 'active', 604800) print('Starting run {} of B2 problem with id {}...'.format(run, run_id)) #with open('../../../sbml/b2.xml') as f: #sbml = f.read() # show initial score #p = B2Problem(sbml) #initial_score = p.evaluate(getDefaultParamValues()) initial_score = 0. #print('Initial score: {}'.format(initial_score)) from toolz import partial from b2problem import make_problem a = Archipelago(4, make_problem, initial_score, None, partial(getQualifiedName, 'B2', str(run_id)), mc_host, mc_port) a.run(sc) client.set('com.how2cell.sabaody.B2.run.status', 'finished', 604800) client.set('com.how2cell.sabaody.B2.run.endTime', str(time()), 604800)