コード例 #1
0
    def generate(self, chain):
        """
        generates the positions by running the PSO and using the chain's min and max and then calling 
        the paraboloid fitter in order to estimate the covariance matrix. The position will then
        be generated by drawing position from a multivariant gaussian distribution defined by
        the best fit and the estimated covariance matrix.
        The progress of the PSO is successively stored to a the disk.
        """

        if (self.particleCount is None):
            self.particleCount = MIN_PARTICLE_COUNT

        if (self.mpi):
            #only import when needed in order to avoid an error in case mpi4py is not installed
            from cosmoHammer.pso.MpiParticleSwarmOptimizer import MpiParticleSwarmOptimizer

            pso = MpiParticleSwarmOptimizer(chain,
                                            self.low,
                                            self.high,
                                            self.particleCount,
                                            threads=self.threads)
        else:
            pso = ParticleSwarmOptimizer(chain,
                                         self.low,
                                         self.high,
                                         self.particleCount,
                                         threads=self.threads)

        if (pso.isMaster()):
            print("I am the master")
        swarm = []
        with open(self.filePrefix + self.BEST_FILE_NAME, "w") as f:
            for i, cswarm in enumerate(
                    pso.sample(maxIter=self.maxIter,
                               p=self.p,
                               m=self.m,
                               n=self.n)):
                self._save(f, i, pso)
                if (i >= 0):
                    swarm.append(cswarm)

            self._save(f, i + 1, pso)

        if (pso.isMaster()):
            print("Best fit found after %s iteration: %f %s" %
                  (i + 1, pso.gbest.fitness, pso.gbest.position))

            if self.maxIter > 5:
                fswarm = []
                for i in range(1, 5):
                    fswarm += swarm[-i]
                self._storeSwarm(fswarm)
            print("Start process of fitting.It uses all gbest found")
            fitter = CurvatureFitter(swarm, pso.gbest)
            mean, _cov = fitter.fit()

            self._storeFit(pso.gbest, _cov)
        pass
コード例 #2
0
    def generate(self,chain):
        """
        generates the positions by running the PSO and using the chain's min and max and then calling 
        the paraboloid fitter in order to estimate the covariance matrix. The position will then
        be generated by drawing position from a multivariant gaussian distribution defined by
        the best fit and the estimated covariance matrix.
        The progress of the PSO is successively stored to a the disk.
        """
        
        if(self.particleCount is None):
            self.particleCount = MIN_PARTICLE_COUNT

        if(self.mpi):
            #only import when needed in order to avoid an error in case mpi4py is not installed
            from cosmoHammer.pso.MpiParticleSwarmOptimizer import MpiParticleSwarmOptimizer
            
            pso = MpiParticleSwarmOptimizer(chain, self.low, self.high, self.particleCount, threads=self.threads)
        else:
            pso = ParticleSwarmOptimizer(chain, self.low, self.high, self.particleCount, threads=self.threads)
        
        if(pso.isMaster()):
            print("I am the master")
        swarm = []
        with open(self.filePrefix+self.BEST_FILE_NAME, "w") as f:
            for i, cswarm in enumerate(pso.sample(maxIter=self.maxIter,p=self.p,m=self.m,n=self.n)):
                self._save(f, i, pso)
                if(i>=0):
                    swarm.append(cswarm)

            self._save(f, i+1, pso)
            
        if(pso.isMaster()):
            print("Best fit found after %s iteration: %f %s"%(i+1, pso.gbest.fitness, pso.gbest.position))
            
            if self.maxIter > 5 :
                fswarm = []
                for i in range(1,5):
                    fswarm += swarm[-i]
                self._storeSwarm(fswarm)
            print("Start process of fitting.It uses all gbest found")
            fitter = CurvatureFitter(swarm, pso.gbest)
            mean, _cov = fitter.fit()
            
            self._storeFit(pso.gbest, _cov)
        pass
コード例 #3
0
    def generate(self):
        """
        generates the positions by running the PSO and using the chain's min and max and then calling 
        the paraboloid fitter in order to estimate the covariance matrix. The position will then
        be generated by drawing position from a multivariant gaussian distribution defined by
        the best fit and the estimated covariance matrix.
        The progress of the PSO is successively stored to a the disk.
        """
        
        chain = self.sampler.likelihoodComputationChain
        
        if(self.particleCount is None):
            self.particleCount = self.get_particle_count()

        if(self.mpi):
            #only import when needed in order to avoid an error in case mpi4py is not installed
            from cosmoHammer.sampler.util.pso.MpiParticleSwarmOptimizer import MpiParticleSwarmOptimizer
            
            pso = MpiParticleSwarmOptimizer(chain, chain.min, chain.max, self.particleCount, threads=self.threads)
        else:
            pso = ParticleSwarmOptimizer(chain, chain.min, chain.max, self.particleCount, threads=self.threads)
        
        swarm = []
        with open(self.sampler.filePrefix+self.BEST_FILE_NAME, "w") as f:
            for i, cswarm in enumerate(pso.sample(self.maxIter)):
                self._save(f, i, pso)
                if(i>=0):
                    swarm.append(cswarm)

            self._save(f, i+1, pso)
        self.sampler.log("Best fit found after %s iteration: %f %s"%(i+1, pso.gbest.fitness, pso.gbest.position))
        
        
        fswarm = []
        for i in range(1,5):
            fswarm += swarm[-i]

        self._storeSwarm(fswarm)
        
        fitter = CurvatureFitter(fswarm, pso.gbest)
        mean, _cov = fitter.fit()
        
        self._storeFit(pso.gbest, _cov)

#         dim = len(mean)-1
#         sigma = 0.4
#         factor = _cov[dim,dim] / numpy.sqrt(sigma)
#         _cov[:-1,dim] = _cov[:-1,dim]/factor
#         _cov[dim,:-1] = _cov[dim,:-1]/factor
#         _cov[dim,dim] = sigma
#         print ""
#         fitter = ParaboloidFitter(fswarm, pso.gbest, True)
#         mean, _cov = fitter.fit()
        sigma = numpy.sqrt(numpy.diag(_cov))
        print("=> found sigma:", sigma)
        
#        fitter = ParaboloidFitter(pso.swarm, pso.gbest)
#        mean, _cov = fitter.fit()
#        sigma = numpy.sqrt(numpy.diag(_cov))
#        print "=> found sigma:", sigma
        
        samples = numpy.random.multivariate_normal(mean, _cov, self.sampler.nwalkers)
#         print numpy.std(samples, axis=0)
        return samples
コード例 #4
0
    def generate(self):
        """
        generates the positions by running the PSO and using the chain's min and max and then calling 
        the paraboloid fitter in order to estimate the covariance matrix. The position will then
        be generated by drawing position from a multivariant gaussian distribution defined by
        the best fit and the estimated covariance matrix.
        The progress of the PSO is successively stored to a the disk.
        """

        chain = self.sampler.likelihoodComputationChain

        if (self.particleCount is None):
            self.particleCount = self.get_particle_count()

        if (self.mpi):
            #only import when needed in order to avoid an error in case mpi4py is not installed
            from cosmoHammer.sampler.util.pso.MpiParticleSwarmOptimizer import MpiParticleSwarmOptimizer

            pso = MpiParticleSwarmOptimizer(chain,
                                            chain.min,
                                            chain.max,
                                            self.particleCount,
                                            threads=self.threads)
        else:
            pso = ParticleSwarmOptimizer(chain,
                                         chain.min,
                                         chain.max,
                                         self.particleCount,
                                         threads=self.threads)

        swarm = []
        with open(self.sampler.filePrefix + self.BEST_FILE_NAME, "w") as f:
            for i, cswarm in enumerate(pso.sample(self.maxIter)):
                self._save(f, i, pso)
                if (i >= 0):
                    swarm.append(cswarm)

            self._save(f, i + 1, pso)
        self.sampler.log("Best fit found after %s iteration: %f %s" %
                         (i + 1, pso.gbest.fitness, pso.gbest.position))

        fswarm = []
        for i in range(1, 5):
            fswarm += swarm[-i]

        self._storeSwarm(fswarm)

        fitter = CurvatureFitter(fswarm, pso.gbest)
        mean, _cov = fitter.fit()

        self._storeFit(pso.gbest, _cov)

        #         dim = len(mean)-1
        #         sigma = 0.4
        #         factor = _cov[dim,dim] / numpy.sqrt(sigma)
        #         _cov[:-1,dim] = _cov[:-1,dim]/factor
        #         _cov[dim,:-1] = _cov[dim,:-1]/factor
        #         _cov[dim,dim] = sigma
        #         print ""
        #         fitter = ParaboloidFitter(fswarm, pso.gbest, True)
        #         mean, _cov = fitter.fit()
        sigma = numpy.sqrt(numpy.diag(_cov))
        print("=> found sigma:", sigma)

        #        fitter = ParaboloidFitter(pso.swarm, pso.gbest)
        #        mean, _cov = fitter.fit()
        #        sigma = numpy.sqrt(numpy.diag(_cov))
        #        print "=> found sigma:", sigma

        samples = numpy.random.multivariate_normal(mean, _cov,
                                                   self.sampler.nwalkers)
        #         print numpy.std(samples, axis=0)
        return samples