Example #1
0
    def pso(self, n_particles, n_iterations, lower_start=None, upper_start=None, threadCount=1, init_pos=None,
            mpi=False, print_key='PSO'):
        """
        returns the best fit for the lense model on catalogue basis with particle swarm optimizer
        """
        if lower_start is None or upper_start is None:
            lower_start, upper_start = np.array(self.lower_limit), np.array(self.upper_limit)
            print("PSO initialises its particles with default values")
        else:
            lower_start = np.maximum(lower_start, self.lower_limit)
            upper_start = np.minimum(upper_start, self.upper_limit)
        if mpi is True:
            pso = MpiParticleSwarmOptimizer(self.chain, lower_start, upper_start, n_particles, threads=1)
            if pso.isMaster():
                print('MPI option chosen')
        else:
            pso = ParticleSwarmOptimizer(self.chain, lower_start, upper_start, n_particles, threads=threadCount)
        if init_pos is None:
            init_pos = (upper_start - lower_start) / 2 + lower_start
        if not init_pos is None:
            pso.gbest.position = init_pos
            pso.gbest.velocity = [0]*len(init_pos)
            pso.gbest.fitness, _ = self.chain.likelihood(init_pos)
        X2_list = []
        vel_list = []
        pos_list = []
        time_start = time.time()
        if pso.isMaster():
            print('Computing the %s ...' % print_key)
        num_iter = 0
        for swarm in pso.sample(n_iterations):
            X2_list.append(pso.gbest.fitness*2)
            vel_list.append(pso.gbest.velocity)
            pos_list.append(pso.gbest.position)
            num_iter += 1
            if pso.isMaster():
                if num_iter % 10 == 0:
                    print(num_iter)
        if not mpi:
            result = pso.gbest.position
        else:
            result = MpiUtil.mpiBCast(pso.gbest.position)

        #if (pso.isMaster() and mpi is True) or self.chain.sampling_option == 'X2_catalogue':
        if mpi is True and not pso.isMaster():
            pass
        else:
            lens_dict, source_dict, lens_light_dict, ps_dict, kwargs_cosmo = self.chain.param.args2kwargs(result)
            print(pso.gbest.fitness * 2 / (self.chain.effectiv_numData_points()), 'reduced X^2 of best position')
            print(pso.gbest.fitness, 'logL')
            print(self.chain.effectiv_numData_points(), 'effective number of data points')
            print(lens_dict, 'lens result')
            print(source_dict, 'source result')
            print(lens_light_dict, 'lens light result')
            print(ps_dict, 'point source result')
            print(kwargs_cosmo, 'cosmo result')
            time_end = time.time()
            print(time_end - time_start, 'time used for PSO', print_key)
            print('===================')
        return result, [X2_list, pos_list, vel_list, []]
Example #2
0
 def pso(self, n_particles, n_iterations, lowerLimit, upperLimit, threadCount=1, init_pos=None, mpi_monch=False):
     """
     returns the best fit for the lense model on catalogue basis with particle swarm optimizer
     """
     if mpi_monch is True:
         pso = MpiParticleSwarmOptimizer(self.chain, lowerLimit, upperLimit, n_particles, threads=1)
     else:
         pso = ParticleSwarmOptimizer(self.chain, lowerLimit, upperLimit, n_particles, threads=threadCount)
     if not init_pos is None:
         pso.gbest.position = init_pos
         pso.gbest.velocity = [0]*len(init_pos)
         pso.gbest.fitness, _ = self.chain.likelihood(init_pos)
     X2_list = []
     vel_list = []
     pos_list = []
     time_start = time.time()
     if pso.isMaster():
         pass
     for swarm in pso.sample(n_iterations):
         X2_list.append(pso.gbest.fitness*2)
         vel_list.append(pso.gbest.velocity)
         pos_list.append(pso.gbest.position)
     if mpi_monch is True:
         result = MpiUtil.mpiBCast(pso.gbest.position)
     else:
         result = pso.gbest.position
     if pso.isMaster() and mpi_monch is True:
         print(pso.gbest.fitness*2/(self.chain.easyLens.get_pixels_unmasked()), 'reduced X^2 of best position')
         print(result, 'result')
         time_end = time.time()
         print(time_end - time_start, 'time used for PSO')
     return result, [X2_list, pos_list, vel_list]
Example #3
0
 def test_splitlist_1(self, mpi_mock):
     sequence = self._get_sequence(7)
     n = 1
     sList = MpiUtil.splitList(sequence, n)
     assert len(sList) == n
     for i in range(len(sequence)):
         assert np.all(sList[0][i] == sequence[i])
Example #4
0
 def pso(self,
         n_particles=10,
         n_iterations=10,
         lowerLimit=-0.2,
         upperLimit=0.2,
         threadCount=1,
         mpi=False,
         print_key='default'):
     """
     returns the best fit for the lense model on catalogue basis with particle swarm optimizer
     """
     init_pos = self.chain.get_args(self.chain.kwargs_data_init)
     num_param = self.chain.num_param
     lowerLimit = [lowerLimit] * num_param
     upperLimit = [upperLimit] * num_param
     if mpi is True:
         pso = MpiParticleSwarmOptimizer(self.chain,
                                         lowerLimit,
                                         upperLimit,
                                         n_particles,
                                         threads=1)
     else:
         pso = ParticleSwarmOptimizer(self.chain,
                                      lowerLimit,
                                      upperLimit,
                                      n_particles,
                                      threads=threadCount)
     if not init_pos is None:
         pso.gbest.position = init_pos
         pso.gbest.velocity = [0] * len(init_pos)
         pso.gbest.fitness, _ = self.chain.likelihood(init_pos)
     X2_list = []
     vel_list = []
     pos_list = []
     time_start = time.time()
     if pso.isMaster():
         print('Computing the %s ...' % print_key)
     num_iter = 0
     for swarm in pso.sample(n_iterations):
         X2_list.append(pso.gbest.fitness * 2)
         vel_list.append(pso.gbest.velocity)
         pos_list.append(pso.gbest.position)
         num_iter += 1
         if pso.isMaster():
             if num_iter % 10 == 0:
                 print(num_iter)
     if not mpi:
         result = pso.gbest.position
     else:
         result = MpiUtil.mpiBCast(pso.gbest.position)
     kwargs_data = self.chain.update_data(result)
     if mpi is True and not pso.isMaster():
         pass
     else:
         time_end = time.time()
         print("Shifts found: ", result)
         print(time_end - time_start, 'time used for PSO', print_key)
     return kwargs_data, [X2_list, pos_list, vel_list, []]
Example #5
0
    def test_splitlist_80(self, mpi_mock):
        sequence = self._get_sequence(160)
        
        n = 80
        sList = MpiUtil.splitList(sequence, n)
        assert len(sList) == n

        l0 = 2
        for k in range(n):
            assert len(sList[k]) == l0
            for i in range(l0):
                assert np.all(sList[k][i] == sequence[(k*l0)+i])
Example #6
0
    def test_splitlist_2(self, mpi_mock):
        sequence = self._get_sequence(7)
        n = 2
        sList = MpiUtil.splitList(sequence, n)
        assert len(sList) == n

        assert len(sList[0]) == 4
        for i in range(4):
            assert np.all(sList[0][i] == sequence[0+i])
            
        assert len(sList[1]) == 3
        for i in range(3):
            assert np.all(sList[1][i] == sequence[4+i])
Example #7
0
    def test_splitlist_3(self, mpi_mock):
        sequence = self._get_sequence(7)
        n = 3
        sList = MpiUtil.splitList(sequence, n)
        assert len(sList) == n

        l0 = 2
        assert len(sList[0]) == l0
        for i in range(l0):
            assert np.all(sList[0][i] == sequence[0+i])
        
        l1 = 3    
        assert len(sList[1]) == l1
        for i in range(l1):
            assert np.all(sList[1][i] == sequence[2+i])
            
        l2 = 2
        assert len(sList[2]) == l2
        for i in range(l2):
            assert np.all(sList[2][i] == sequence[5+i])