def __init__(self, lattice, model, chain_list = None, first_structure = None, temperature = 1.0): self.lattice = lattice self.model = model if first_structure is None: self.last_structure = random_avoid(self.model.natoms, self.lattice, model = model, chain_list = chain_list) else: self.last_structure = first_structure self.model.calculate_energy(self.last_structure) self.best_structure = self.last_structure self.best_energy = self.best_structure.energy self.mover = Reptate() self.step = 0 if temperature > 0: self.rt = float(temperature) else: raise ValueError("temperature must be > 0")
class MonteCarlo(Search): '''Perform a Metropolis Monte Carlo search. Parameters ---------- Required lattice: A Lattice frombcga.lattice model: A Model from bcga.model Optional termini: A list containing the indices of the ends of protein chains (only needed for multicain systems) first_structure: A bcga.lattice_structure object to start the search (A random one will be generated if none is supplied) temperature: The temperature used in the Metropolis test (units are dependent on the model being used) ''' def __init__(self, lattice, model, chain_list = None, first_structure = None, temperature = 1.0): self.lattice = lattice self.model = model if first_structure is None: self.last_structure = random_avoid(self.model.natoms, self.lattice, model = model, chain_list = chain_list) else: self.last_structure = first_structure self.model.calculate_energy(self.last_structure) self.best_structure = self.last_structure self.best_energy = self.best_structure.energy self.mover = Reptate() self.step = 0 if temperature > 0: self.rt = float(temperature) else: raise ValueError("temperature must be > 0") def run(self, steps): for self.step in range (self.step +1, self.step + steps + 1): structure = self.mover.move(self.last_structure) energy = self.model.calculate_energy(structure) #print self.step, structure.energy, self.last_structure.energy, self.best_structure.energy if structure.energy < self.best_structure.energy: self.best_structure = structure self.best_energy = energy if random.random() < metropolis_probability(self.last_structure.energy, structure.energy, self.rt): self.last_structure = structure return self.best_structure