示例#1
0
 def mcmc(self):
     result = [0 for i in range(len(self.__space))]
     random_ind = np.random.randint(len(self.__space))
     random_state = self.__space[random_ind]
     for iter in range(self.__m):
         result[random_ind] += 1
         neighbours = self.__get_neighbours(random_ind)
         chosen = neighbours[np.random.randint(len(neighbours))]
         c_E = self.__E(chosen)
         r_E = self.__E(random_state)
         dE = c_E - r_E
         p = self.__get_p_accept_metropolis(
             dE, 1 / len(neighbours),
             1 / len(self.__get_neighbours(chosen)))
         coin = np.random.binomial(1, p)
         random_state = chosen if coin else random_state
         conf = self.__space
         if type(chosen) is not tuple:
             chosen_ind = 0
         else:
             chosen_ind = conf[1:].index(chosen)
         random_ind = chosen_ind if coin else random_ind
     best_configuration = self.__space[np.argmax(np.asarray(result))]
     bd = BD(10, self.__kt, self.__dt, best_configuration, 2,
             self.__protein_dict, self.__protein_vec, self.__df)
     return result, best_configuration, bd.BD_algorithm()
示例#2
0
 def __E(self, c):
     """
     E function that used BD algorithm and calculate the loss of the result
     :param c: the initial state
     :return: the loss score
     """
     bd = BD(10, self.__kt, self.__dt, c, 2, self.__protein_dict,
             self.__protein_vec, self.__df)
     res, bars = bd.BD_algorithm()
     return self.__loss(bars)