Exemple #1
0
    def __init__(self, opts, args):
        """Mrs Setup (run on both master and slave)"""

        super(PI, self).__init__(opts, args)

        self.function = param.instantiate(opts, 'func')
        self.topology = param.instantiate(opts, 'top')

        self.function.setup()
        self.topology.setup(self.function)
Exemple #2
0
    def __init__(self, opts, args):
        """Mrs Setup (run on both master and slave)"""

        super(SpecExPSO, self).__init__(opts, args)

        self.specmethod = param.instantiate(opts, 'spec')
        pruner = param.instantiate(opts, 'pruner')

        pruner.setup(self)
        self.specmethod.setup(self, pruner)
        if self.opts.min_tokens * self.topology.num > self.opts.tokens:
            raise ValueError("There aren't enough tokens to satisfy the min "
                    "token requirement.")
Exemple #3
0
    def __init__(self, opts, args):
        """Mrs Setup (run on both master and slave)"""

        super(StandardPSO, self).__init__(opts, args)

        self.tmpfiles = None
        self.function = param.instantiate(opts, 'func')
        self.motion = param.instantiate(opts, 'motion')
        self.topology = param.instantiate(opts, 'top')

        self.function.setup(self.func_init_rand())
        self.motion.setup(self.function)
        self.topology.setup(self.function)
Exemple #4
0
    def __init__(self, opts, args):
        """Mrs Setup (run on both master and slave)"""

        super(SubswarmPSO, self).__init__(opts, args)

        self.link = param.instantiate(opts, 'link')
        self.link.setup(self.function)
Exemple #5
0
    def run(self, job):
        """Run Mrs PSO function

        This is run on the master.
        """
        if not self.cli_startup():
            return 1

        # Perform the simulation
        try:
            self.last_data = None
            self.output = param.instantiate(self.opts, 'out')
            self.output.start()

            job.default_partition = self.mod_partition
            if self.opts.numtasks:
                numtasks = self.opts.numtasks
            else:
                numtasks = self.link.num
            job.default_reduce_tasks = numtasks
            job.default_reduce_splits = numtasks

            # Ensure that we submit enough tasks at a time.
            if self.output.freq:
                self.iterative_qmax = 2 * self.output.freq
            else:
                self.iterative_qmax = 2 * numtasks

            mrs.GeneratorCallbackMR.run(self, job)
            self.output.finish()
            return 0
        except KeyboardInterrupt as e:
            print("# INTERRUPTED")
            return 1
Exemple #6
0
 def test_whole_algorithm(self):
     opts = self.opts
     opts.top = 'optprime.topology.DRing'
     opts.top__neighbors = 1
     opts.top__num = 8
     opts.top__noselflink = True
     opts.func__dims = 1
     opts.iters = 10
     opts.out = 'optprime.output.Pair'
     save_out = sys.stdout
     sys.stdout = StringIO.StringIO()
     mrs_impl = param.instantiate(opts, 'mrs')
     mrs_impl.program_class = SpecExPSO
     mrs_impl.main(opts, [])
     test_out = sys.stdout
     sys.stdout = save_out
     output = test_out.getvalue()
     output = [line for line in output.splitlines()
             if not (line.startswith('#') or len(line) == 0)]
     expected_output = ['2 0.00116340815465', '4 0.00116340815465',
             '6 0.00116340815465', '8 0.00116340815465',
             '10 0.00116340815465', '12 0.00116340815465',
             '14 0.00116340815465', '16 0.00116340815465',
             '18 0.00116340815465']
     self.assertEquals(expected_output, output)
Exemple #7
0
def test_hyphens_to_underscores(parser):
    argv = ['--obj', 'mrs.param._Rabbit', '--obj-a-b-c', 'hi']
    opts, args = parser.parse_args(argv)
    obj = param.instantiate(opts, 'obj')

    assert isinstance(obj, param._Rabbit)
    assert obj.a_b_c == 'hi'
Exemple #8
0
def test_basic(parser):
    argv = ['--obj', 'mrs.param._Rabbit', '--obj-weight', '17']
    opts, args = parser.parse_args(argv)
    obj = param.instantiate(opts, 'obj')

    assert isinstance(obj, param._Rabbit)
    assert obj.weight == 17
Exemple #9
0
    def bypass(self):
        """Run a "native" version of PSO without MapReduce."""

        if not self.cli_startup():
            return 1

        # Perform simulation
        try:
            self.output = param.instantiate(self.opts, 'out')
            self.output.start()
            self.bypass_run()
            self.output.finish()
        except KeyboardInterrupt as e:
            print("# INTERRUPTED")
        return 0
Exemple #10
0
 def test_whole_algorithm_2(self):
     opts = self.opts
     opts.top = 'optprime.topology.Rand'
     opts.top__neighbors = 2
     opts.top__num = 8
     opts.func__dims = 10
     opts.iters = 10
     opts.out = 'optprime.output.Pair'
     save_out = sys.stdout
     sys.stdout = StringIO.StringIO()
     mrs_impl = param.instantiate(opts, 'mrs')
     mrs_impl.program_class = SpecExPSO
     mrs_impl.main(opts, [])
     test_out = sys.stdout
     sys.stdout = save_out
     output = test_out.getvalue()
     output = [line for line in output.splitlines()
             if not (line.startswith('#') or len(line) == 0)]
     expected_output = ['2 5412.14094362', '4 4669.31920619',
             '6 4669.31920619', '8 4669.31920619', '10 4669.31920619',
             '12 4468.72038682', '14 2469.19827321', '16 2469.19827321',
             '18 2469.19827321']
     self.assertEquals(expected_output, output)
Exemple #11
0
 def test_whole_algorithm_3(self):
     opts = self.opts
     opts.top = 'optprime.topology.Ring'
     opts.top__neighbors = 1
     opts.top__num = 5
     opts.func__dims = 5
     opts.iters = 11
     opts.out = 'optprime.output.Pair'
     save_out = sys.stdout
     sys.stdout = StringIO.StringIO()
     mrs_impl = param.instantiate(opts, 'mrs')
     mrs_impl.program_class = SpecExPSO
     mrs_impl.main(opts, [])
     test_out = sys.stdout
     sys.stdout = save_out
     output = test_out.getvalue()
     output = [line for line in output.splitlines()
             if not (line.startswith('#') or len(line) == 0)]
     expected_output = ['2 1572.15761997', '4 1572.15761997',
             '6 1294.37665699', '8 200.329491766', '10 200.329491766',
             '12 200.329491766', '14 200.329491766', '16 200.329491766',
             '18 200.329491766', '20 65.0565230471']
     self.assertEquals(expected_output, output)
Exemple #12
0
    def bypass_run(self):
        """Performs PSO without MapReduce.

        Compare to the run_batch method, which uses MapReduce to do the same
        thing.
        """
        comp = self.function.comparator

        # Create the Population.
        subswarms = []
        for swarm_id in range(self.link.num):
            init_rand = self.initialization_rand(swarm_id)
            swarm = Swarm(swarm_id, self.topology.newparticles(init_rand))
            subswarms.append(swarm)

        # Perform PSO Iterations.  The iteration number represents the total
        # number of function evaluations that have been performed for each
        # particle by the end of the iteration.
        output = param.instantiate(self.opts, 'out')
        output.start()
        outer_iters = self.opts.iters // self.opts.subiters
        for i in range(1, 1 + outer_iters):
            iteration = i * self.opts.subiters
            for swarm in subswarms:
                subiters = self.subiters(swarm.id, i)
                for j in range(subiters):
                    self.bypass_iteration(swarm, swarm.id)

            # Communication phase.
            if self.opts.shuffle:
                newswarms = collections.defaultdict(list)
                for swarm in subswarms:
                    swarm_rand = self.swarm_rand(swarm)
                    neighbors = list(self.link.iterneighbors(swarm, swarm_rand))
                    for shift, p in enumerate(swarm.shuffled(swarm_rand)):
                        # Convert to a global particle id to ensure determinism.
                        p.id += swarm.id * self.link.num
                        # Pick a destination swarm.
                        dest_swarm = neighbors[shift % self.link.num]
                        newswarms[dest_swarm].append(p)
                subswarms = []
                for sid, particles in newswarms.items():
                    particles.sort(key=lambda p: p.id)
                    for pid, particle in enumerate(particles):
                        particle.id = pid
                    swarm = Swarm(sid, particles)
                    subswarms.append(swarm)
            else:
                for swarm in subswarms:
                    swarm_rand = self.swarm_rand(swarm)
                    if self.opts.send_best:
                        p = self.findbest(swarm)
                    else:
                        p = swarm[0]
                    for s_dep_id in self.link.iterneighbors(swarm, swarm_rand):
                        neighbor_swarm = subswarms[s_dep_id]
                        swarm_head = neighbor_swarm[0]
                        nbr_rand = self.neighborhood_rand(swarm_head, swarm.id)
                        for p_dep_id in self.topology.iterneighbors(swarm_head,
                                nbr_rand):
                            neighbor = neighbor_swarm[p_dep_id]
                            neighbor.nbest_cand(p.pbestpos, p.pbestval, comp)
                            if self.opts.transitive_best:
                                neighbor.nbest_cand(p.nbestpos, p.nbestval,
                                        comp)

            # Output phase.  (If freq is 5, output after iters 1, 6, 11, etc.)
            if output.freq and not ((i - 1) % output.freq):
                kwds = {}
                if 'iteration' in output.args:
                    kwds['iteration'] = iteration
                if 'particles' in output.args:
                    kwds['particles'] = particles
                if 'best' in output.args:
                    kwds['best'] = self.findbest(chain(*subswarms))
                output(**kwds)
                if self.stop_condition(chain(*subswarms)):
                    output.finish()
                    return