Пример #1
0
 def test_sepso_reduce(self):
     opts = self.opts
     opts.top = 'optprime.topology.DRing'
     opts.top__neighbors = 1
     opts.top__num = 10
     opts.top__noselflink = True
     opts.func__dims = 1
     specex = SpecExPSO(self.opts, [])
     key = '0'
     value_iter = ['mp:7;0;1;15.883955477773782;-72.011378817328151;252.30004161989973;15.883955477773782;252.30004161989973;15.883955477773782;',
     'mp:8;0;1;30.481880635459589;56.257684094780927;929.14504707440631;30.481880635459589;929.14504707440631;30.481880635459589;',
     'mp:9;0;1;-42.752068209100081;73.768055162775312;1827.7393361555457;-42.752068209100081;1827.7393361555457;-42.752068209100081;',
     'p:0;0;1;47.764627438385048;-25.777880710367839;2281.4596343277258;47.764627438385048;2281.4596343277258;47.764627438385048;',
     'semp:8;0;2;61.816473973169757;31.334593337710167;3821.2764544755737;61.816473973169757;3821.2764544755737;15.883955477773782;;False;7',
     'semp:8;0;2;61.816473973169757;31.334593337710167;3821.2764544755737;61.816473973169757;3821.2764544755737;15.883955477773782;;True;7',
     'semp:8;0;2;71.541201906522957;41.059321271063361;5118.1435702298841;71.541201906522957;5118.1435702298841;30.481880635459589;;False;-1',
     'semp:8;0;2;71.541201906522957;41.059321271063361;5118.1435702298841;71.541201906522957;5118.1435702298841;30.481880635459589;;True;-1',
     'semp:9;0;2;11.087088613761502;53.839156822861582;122.92353392939994;11.087088613761502;122.92353392939994;-42.752068209100081;;False;-1',
     'semp:9;0;2;11.087088613761502;53.839156822861582;122.92353392939994;11.087088613761502;122.92353392939994;-42.752068209100081;;True;-1',
     'semp:9;0;2;46.38737206373191;89.13944027283199;2151.7882869790956;46.38737206373191;2151.7882869790956;30.481880635459589;;False;8',
     'semp:9;0;2;46.38737206373191;89.13944027283199;2151.7882869790956;46.38737206373191;2151.7882869790956;30.481880635459589;;True;8',
     'sep:0;0;2;10.139376734253396;-37.625250704131652;102.80696055911906;10.139376734253396;102.80696055911906;-42.752068209100081;;False;9',
     'sep:0;0;2;10.139376734253396;-37.625250704131652;102.80696055911906;10.139376734253396;102.80696055911906;-42.752068209100081;;True;9',
     'sep:0;0;2;28.950801330809266;-18.813826107575782;838.14889769598756;28.950801330809266;838.14889769598756;47.764627438385048;;False;-1',
     'sep:0;0;2;28.950801330809266;-18.813826107575782;838.14889769598756;28.950801330809266;838.14889769598756;47.764627438385048;;True;-1']
     expected_output = ['0^p:0;0;3;-93.567063360236261;-103.70644009448966;102.80696055911906;10.139376734253396;102.80696055911906;-42.752068209100081;1827.7393361555457',
     '10^sep:0;0;4;-27.507893111121206;66.059170249115056;102.80696055911906;10.139376734253396;102.80696055911906;-42.752068209100081;1827.7393361555457;False;-1',
     '20^sep:0;0;4;97.243202323812767;190.81026568404903;102.80696055911906;10.139376734253396;102.80696055911906;104.20869385944874;1827.7393361555457;False;9',
     '30^sep:0;0;4;-126.12106202004642;-32.553998659810169;102.80696055911906;-93.567063360236261;102.80696055911906;-42.752068209100081;1827.7393361555457;True;-1',
     '40^sep:0;0;4;-1.3699665851124792;92.197096775123782;102.80696055911906;-93.567063360236261;102.80696055911906;104.20869385944874;1827.7393361555457;True;9']
     actual_output = list(specex.sepso_reduce(key, value_iter))
     self.assertEquals(expected_output, actual_output)
Пример #2
0
 def test_sepso_map(self):
     specex = SpecExPSO(self.opts, [])
     rand = specex.initialization_rand(0)
     particles = list(specex.topology.newparticles(rand))
     key = str(particles[2].id)
     value = repr(particles[2])
     emitted_messages = list(specex.sepso_map(key, value))
     specex.just_evaluate(particles[2])
     message = particles[2].make_message_particle()
     expected_messages = [('2', repr(particles[2])),
             ('0', repr(message)),
             ('1', repr(message)),
             ('2', repr(message)),
             ('3', repr(message)),
             ('4', repr(message))]
     for expected_message in expected_messages:
         self.assert_(expected_message in emitted_messages)
Пример #3
0
 def setUp(self):
     self.opts = default_opts.default_specex_opts()
     self.specex = SpecExPSO(self.opts, [])
     self.pruner = specmethod.OneCompleteIteration()
     self.pruner.setup(self.specex)
     self.rand = self.specex.initialization_rand(0)
     self.smethod = specmethod.ReproducePSO()
     self.smethod.setup(self.specex, self.pruner)
Пример #4
0
    def test_message_ids_with_separticle(self):
        opts = self.opts
        opts.top = 'optprime.topology.Rand'
        opts.top__neighbors = 1
        opts.top__num = 100
        opts.top__noselflink = True
        specex = SpecExPSO(opts, [])
        particles = list(specex.topology.newparticles(self.rand))
        particle = SEParticle(particles[3], specpbest=True, specnbestid=-1)
        rand = specex.neighborhood_rand(particle)
        n = list(specex.topology.iterneighbors(particle, rand))[0]
        ndummy = Dummy(n, particle.iters+1)
        rand = specex.neighborhood_rand(ndummy)
        n2 = list(specex.topology.iterneighbors(ndummy, rand))[0]
        expected_message_ids = [n, n2]
        expected_message_ids.sort()

        self.smethod.setup(specex, self.pruner)
        message_ids = list(self.smethod.message_ids(particle))
        message_ids.sort()
        self.assertEquals(expected_message_ids, message_ids)
Пример #5
0
class TestOneCompleteIteration(unittest.TestCase):

    def setUp(self):
        self.opts = default_opts.default_specex_opts()
        self.specex = SpecExPSO(self.opts, [])
        self.pruner = specmethod.OneCompleteIteration()
        self.pruner.setup(self.specex)
        self.rand = self.specex.initialization_rand(0)

    def test_generate_children(self):
        particles = list(self.specex.topology.newparticles(self.rand))
        particle = particles[4]
        neighbors = [particles[3]]
        children = set(self.pruner.generate_children(particle, neighbors))
        expected_children = set()
        child = SEParticle(particle, specpbest=False, specnbestid=-1)
        self.specex.just_move(child)
        expected_children.add(child)
        child = SEParticle(particle, specpbest=False,
                specnbestid=particles[3].id)
        child.nbestpos = particles[3].pos
        self.specex.just_move(child)
        expected_children.add(child)
        child = SEParticle(particle, specpbest=True, specnbestid=-1)
        child.pbestpos = particle.pos
        self.specex.just_move(child)
        expected_children.add(child)
        child = SEParticle(particle, specpbest=True,
                specnbestid=particles[3].id)
        child.pbestpos = particle.pos
        child.nbestpos = particles[3].pos
        self.specex.just_move(child)
        expected_children.add(child)
        for child in expected_children:
            self.assert_(self.set_contains_item(children, child))
        self.assertEquals(len(children), len(expected_children))

    def set_contains_item(self, set, item):
        for cand in set:
            if repr(cand) == repr(item):
                return True
        else:
            return False
Пример #6
0
class TestPickBestChild(unittest.TestCase):

    def setUp(self):
        self.opts = default_opts.default_specex_opts()
        self.specex = SpecExPSO(self.opts, [])
        self.pruner = specmethod.OneCompleteIteration()
        self.pruner.setup(self.specex)
        self.rand = self.specex.initialization_rand(0)
        self.smethod = specmethod.PickBestChild()
        self.smethod.setup(self.specex, self.pruner)

    def test_pick_child(self):
        opts = self.opts
        opts.func__dims = 1
        specex = SpecExPSO(opts, [])
        self.smethod.setup(specex, self.pruner)
        particle = BranchParticle(4, 1, 2, 5)
        neighbors = [BranchParticle(3, 2, 3, 4), BranchParticle(5, 3, 4, 3)]
        children = [SEParticle(BranchParticle(1, 1, 1, 1), True, -1),
                SEParticle(BranchParticle(2, 2, 2, 2), False, -1),
                SEParticle(BranchParticle(3, 3, 3, 3), False, -1),
                SEParticle(BranchParticle(4, 4, 4, 4), False, -1)]
        for child in children:
            child.nbestval = None
        child = self.smethod.pick_child(particle, neighbors, children)
        self.assertEquals(1, child.id)
        self.assertEquals(1, child.pbestval)
        self.assertEquals(3, child.nbestval)

    def test_pick_child_doesnt_modify_state(self):
        particles = list(self.specex.topology.newparticles(self.rand))
        neighbors = [particles[n]
                for n in self.specex.topology.iterneighbors(particles[3],
                    self.rand)]
        children = list(self.smethod.generate_children(particles[3], neighbors))
        messages = [n.make_message_particle() for n in neighbors]
        old_messages = deepcopy(messages)
        self.smethod.pick_child(particles[3], messages, children)
        self.assertEquals(old_messages, messages)
Пример #7
0
class TestReproducePSO(unittest.TestCase):

    def setUp(self):
        self.opts = default_opts.default_specex_opts()
        self.specex = SpecExPSO(self.opts, [])
        self.pruner = specmethod.OneCompleteIteration()
        self.pruner.setup(self.specex)
        self.rand = self.specex.initialization_rand(0)
        self.smethod = specmethod.ReproducePSO()
        self.smethod.setup(self.specex, self.pruner)

    def test_bad_pruner(self):
        pruner = specmethod._Pruner()
        self.assertRaises(ValueError, self.smethod.setup, self.specex, pruner)

    def test_message_ids_with_particle(self):
        opts = self.opts
        opts.top = 'optprime.topology.Rand'
        opts.top__neighbors = 1
        opts.top__num = 100
        opts.top__noselflink = True
        specex = SpecExPSO(opts, [])
        particles = list(specex.topology.newparticles(self.rand))
        particle = particles[3]
        rand = specex.neighborhood_rand(particle)
        n = list(specex.topology.iterneighbors(particle, rand))[0]
        ndummy = Dummy(n, particle.iters+1)
        rand = specex.neighborhood_rand(ndummy)
        n2 = list(specex.topology.iterneighbors(ndummy, rand))[0]
        n2dummy = Dummy(n2, particle.iters+2)
        rand = specex.neighborhood_rand(n2dummy)
        n3 = list(specex.topology.iterneighbors(n2dummy, rand))[0]
        expected_message_ids = [n, n2, n3]
        expected_message_ids.sort()

        self.smethod.setup(specex, self.pruner)
        message_ids = list(self.smethod.message_ids(particle))
        message_ids.sort()
        self.assertEquals(expected_message_ids, message_ids)

    def test_message_ids_with_separticle(self):
        opts = self.opts
        opts.top = 'optprime.topology.Rand'
        opts.top__neighbors = 1
        opts.top__num = 100
        opts.top__noselflink = True
        specex = SpecExPSO(opts, [])
        particles = list(specex.topology.newparticles(self.rand))
        particle = SEParticle(particles[3], specpbest=True, specnbestid=-1)
        rand = specex.neighborhood_rand(particle)
        n = list(specex.topology.iterneighbors(particle, rand))[0]
        ndummy = Dummy(n, particle.iters+1)
        rand = specex.neighborhood_rand(ndummy)
        n2 = list(specex.topology.iterneighbors(ndummy, rand))[0]
        expected_message_ids = [n, n2]
        expected_message_ids.sort()

        self.smethod.setup(specex, self.pruner)
        message_ids = list(self.smethod.message_ids(particle))
        message_ids.sort()
        self.assertEquals(expected_message_ids, message_ids)

    def test_pick_child(self):
        opts = self.opts
        opts.func__dims = 1
        specex = SpecExPSO(opts, [])
        self.smethod.setup(specex, self.pruner)
        particle = BranchParticle(4, 1, 2, 5)
        neighbors = [BranchParticle(3, 2, 3, 4), BranchParticle(5, 3, 4, 3)]
        children = list(self.smethod.generate_children(particle, neighbors))
        for child in children:
            if child.specpbest and child.specnbestid == 5:
                real_child = child
        child = self.smethod.pick_child(particle, neighbors, children)
        real_child_real_particle = real_child.make_real_particle()
        self.smethod.update_child_bests(particle, neighbors[1],
                real_child_real_particle)
        self.assertEquals(repr(real_child_real_particle), repr(child))

    def test_pick_child_doesnt_modify_state(self):
        particles = list(self.specex.topology.newparticles(self.rand))
        neighbors = [particles[n]
                for n in self.specex.topology.iterneighbors(particles[3],
                    self.rand)]
        children = list(self.smethod.generate_children(particles[3], neighbors))
        messages = [n.make_message_particle() for n in neighbors]
        old_messages = deepcopy(messages)
        self.smethod.pick_child(particles[3], messages, children)
        self.assertEquals(old_messages, messages)

    def test_no_child_in_pick_child(self):
        particles = list(self.specex.topology.newparticles(self.rand))
        neighbors = [particles[n]
                for n in self.specex.topology.iterneighbors(particles[3],
                    self.rand)]
        children = []
        messages = [n.make_message_particle() for n in neighbors]
        self.assertRaises(RuntimeError, self.smethod.pick_child,
                particles[3], messages, children)

    # I'm leaving out tests for pick_neighbor_children and update_neighbor_nbest
    # because it would take a lot of work to get correct tests for them, and
    # they mostly just rely on pick_child anyway.  So they will be indirectly
    # tested in some bigger tests.  If the bigger tests break and none of the
    # smaller ones do, try looking at those two methods that I'm skipping.

    def test_update_child_bests(self):
        particle = BranchParticle(1, 1, 1, 1)
        neighbor = BranchParticle(2, 2, 2, 2)
        child = BranchParticle(3, 3, 3, 3)
        self.smethod.update_child_bests(particle, neighbor, child)
        self.assertEquals(2, child.nbestval)
        self.assertEquals(1, child.pbestval)