Exemple #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
Exemple #2
0
 def test_optimize(self):
     low = np.zeros(2)
     high = np.ones(2)
     func = lambda p: (-np.random.rand(), None)
     pso = ParticleSwarmOptimizer(func, low, high, 10)
     
     maxIter=10
     swarms, gbests = pso.optimize(maxIter)
     assert swarms is not None
     assert gbests is not None
     assert len(swarms) == maxIter
     assert len(gbests) == maxIter
     
     fitness = [part.fitness != 0 for part in pso.swarm]
     assert all(fitness)
     
     assert pso.gbest.fitness != -np.inf 
     
     
Exemple #3
0
 def test_setup(self):
     low = np.zeros(2)
     high = np.ones(2)
     pso = ParticleSwarmOptimizer(None, low, high, 10)
     
     assert pso.swarm is not None
     assert len(pso.swarm) == 10
     
     position = [part.position for part in pso.swarm]
     
     assert (position>=low).all()
     assert (position<=high).all()
     
     velocity = [part.velocity for part in pso.swarm]
     assert (velocity == np.zeros(2)).all()
     
     fitness = [part.fitness == 0 for part in pso.swarm]
     assert all(fitness)
     
     assert pso.gbest.fitness == -np.inf
params = Params(
    ("hubble", [70, 65, 80, 3]), ("ombh2", [0.0226, 0.01, 0.03, 0.001]),
    ("omch2", [0.122, 0.09, 0.2, 0.01]),
    ("scalar_amp", [2.1e-9, 1.8e-9, 2.35e-9, 1e-10]),
    ("scalar_spectral_index", [0.96, 0.8, 1.2, 0.02]),
    ("re_optical_depth", [0.09, 0.01, 0.1, 0.03]), ("sz_amp", [1, 0, 2, 0.4]))

chain = LikelihoodComputationChain(params[:, 1], params[:, 2])
chain.params = params

chain.addLikelihoodModule(PseudoCmbModule())
chain.setup()

# find the best fit value and update our params knowledge
print("find best fit point")
pso = ParticleSwarmOptimizer(chain, params[:, 1], params[:, 2])
psoTrace = np.array([pso.gbest.position.copy() for _ in pso.sample()])
params[:, 0] = pso.gbest.position

storageUtil = InMemoryStorageUtil()
sampler = CosmoHammerSampler(params=params,
                             likelihoodComputationChain=chain,
                             filePrefix="pseudoCmb_pso",
                             walkersRatio=50,
                             burninIterations=0,
                             sampleIterations=100,
                             storageUtil=storageUtil,
                             threadCount=4)

print("start sampling")
sampler.startSampling()
Exemple #5
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
                ("ombh2",                 [0.0226, 0.01, 0.03, 0.001]),
                ("omch2",                 [0.122, 0.09, 0.2, 0.01]),
                ("scalar_amp",            [2.1e-9, 1.8e-9, 2.35e-9, 1e-10]),
                ("scalar_spectral_index", [0.96, 0.8, 1.2, 0.02]),
                ("re_optical_depth",      [0.09, 0.01, 0.1, 0.03]),
                ("sz_amp",                [1,0,2,0.4]))

chain = LikelihoodComputationChain(params[:,1], params[:,2])
chain.params = params

chain.addLikelihoodModule(PseudoCmbModule())
chain.setup()

# find the best fit value and update our params knowledge
print("find best fit point")
pso = ParticleSwarmOptimizer(chain, params[:,1], params[:,2])
psoTrace = np.array([pso.gbest.position.copy() for _ in pso.sample()])
params[:, 0] = pso.gbest.position

storageUtil = InMemoryStorageUtil()
sampler = CosmoHammerSampler(
                params= params, 
                likelihoodComputationChain=chain, 
                filePrefix="pseudoCmb_pso", 
                walkersRatio=50, 
                burninIterations=0, 
                sampleIterations=100,
                storageUtil=storageUtil,
                threadCount=4
                )