def do_simulate(self, morn_state_a, morn_state_b, next_state_a, next_state_b): #先考虑a, 变化全部来自于还的减去借的 p_a = 0 r_a = 0 diff_a = next_state_a - morn_state_a #diff = return - rent a_max_rent = morn_state_a #最多借早上有的车辆数 for rent_a in range(a_max_rent + 1): return_a = diff_a + rent_a if return_a < 0: continue rent_a_prob = get_prob(self.ps_a_rent, rent_a) return_a_prob = get_prob(self.ps_a_return, return_a) tmp_prob = rent_a_prob * return_a_prob p_a += tmp_prob r_a += tmp_prob * rent_a * self.rent_return p_b = 0 r_b = 0 diff_b = next_state_b - morn_state_b #diff = return - rent b_max_rent = morn_state_b for rent_b in range(b_max_rent + 1): return_b = diff_b + rent_b if return_b < 0: continue rent_b_prob = get_prob(self.ps_b_rent, rent_b) return_b_prob = get_prob(self.ps_b_return, return_b) tmp_prob = rent_b_prob * return_b_prob p_b += tmp_prob r_b += tmp_prob * rent_b * self.rent_return return p_a * p_b, r_a + r_b
def plot_prob(): N = 10_000 T = 1_000 tau = 0.01 ws = [0.96, 0.98, 1., 1.01, 1.03] v0 = np.array([1, 0]) t = np.linspace(0, T, N) fig, ax = plt.subplots(figsize=(10, 4)) for i, w, in enumerate(ws): p = get_prob(v0, N, T, tau, w) ax.plot( t, p, color=cm.viridis(i/len(ws)), label="$\\omega = {}$".format(w) ) ax.plot( t, sin(t*tau/2)**2, "-.k", label="$\sin^2(t \\tau /2)$" ) ax.set_xlabel("$t/[2mL^2/\\hbar]$") ax.set_ylabel("$|a_1|^2$") ax.legend() plt.tight_layout() plt.savefig(FIG_PATH + "rabi_osc.pdf")
def compute_q(MDP, V, s, a): '''根据给定的MDP,价值函数V,计算状态行为对s,a的价值qsa ''' S, A, R, P, gamma = MDP q_sa = 0 print('对当前的行为' + str(a) + '计算它的行为价值计算,对行为,计算其对应所有状态的价值') for s_prime in S: print('状态' + str(s) + '经过a行为,' + str(a) + '获取转移概率' + str(get_prob(P, s, a, s_prime)) + '以及分数' + str(get_value(V, s_prime))) q_sa += get_prob(P, s, a, s_prime) * get_value(V, s_prime) print('状态' + str(s) + '经过a行为,' + str(a) + str(q_sa)) q_sa = get_reward(R, s, a) + gamma * q_sa print('算出这个行为价值为' + str(q_sa)) return q_sa
def compute_q(MDP, V, s, a): S, A, R, P, gamma = MDP #s采取动作a之后可能的状态 q_sa = 0.0 for s_prime in S: q_sa += get_prob(P, s, a, s_prime) * get_value(V, s_prime) q_sa = get_reward(R, s, a) + gamma * q_sa return q_sa
def compute_q(MDP, V, s, a): ''' According to the given MDP, the value function V, calculate the value qsa of the state behavior to s, a formula $$q_{\pi}(s,a) = R^a_b + \gamma \sum_{s' \in S}P^a_{ss'} \nu \pi(s') $$ #markdown ''' S, A, R, P, gamma = MDP q_sa = 0 for s_prime in S: q_sa += get_prob(P, s, a, s_prime) * get_value(V, s_prime) q_sa = get_reward(R, s, a) + gamma * q_sa return q_sa
def compute_q(MDP, V, s, a): '''根据给定的MDP, 价值函数V, 计算(状态行为对)s, a的价值qsa 公式 2.16 ''' S, A, R, P, gamma = MDP q_sa = 0 for s_prime in S: q_sa += get_prob(P, s, a, s_prime) * get_value(V, s_prime) q_sa = get_reward(R, s, a) + gamma * q_sa return q_sa
def __init__(self, corpus): self.grammar = {} self.lexicon = {} self.get_pcfg(corpus) self.freq_tokens = {} for tag in self.lexicon.keys(): for word in self.lexicon[tag].keys(): if word in self.freq_tokens.keys(): self.freq_tokens[word] += self.lexicon[tag][word] else: self.freq_tokens[word] = self.lexicon[tag][word] sum = np.sum(list(self.freq_tokens.values())) for word in self.freq_tokens: self.freq_tokens[word] /= sum self.set_artificial_tags = set() self.chomskyfy() self.freq_terminal_tags = {tag: np.sum(list(counts.values())) for (tag, counts) in self.lexicon.items()} sum = np.sum(list(self.freq_terminal_tags.values())) for tag in self.freq_terminal_tags: self.freq_terminal_tags[tag] /= sum self.grammar = get_prob(self.grammar) self.lexicon = get_prob(self.lexicon) list_all_tags = get_tag_list(self.grammar) self.list_artificial_symbols = list(self.set_artificial_tags) self.list_tags = list(set(list_all_tags).difference(self.set_artificial_tags)) self.list_all_tags = self.list_tags + self.list_artificial_symbols self.nb_tags = len(self.list_tags) self.nb_all_tags = len(self.list_all_tags)
model = model.to(device) model.eval() test_fn = 'images/test_img_1.jpg' img = cv.imread(test_fn) img = cv.resize(img, (im_size, im_size)) img = get_image(img) imgs = torch.zeros([1, 3, im_size, im_size], dtype=torch.float) imgs[0] = img with torch.no_grad(): output = model(imgs) feature = output[0].cpu().numpy() x = feature / np.linalg.norm(feature) cosine = np.dot(features, x) cosine = np.clip(cosine, -1, 1) print('cosine.shape: ' + str(cosine.shape)) max_index = int(np.argmax(cosine)) max_value = cosine[max_index] print('max_index: ' + str(max_index)) print('max_value: ' + str(max_value)) print('name: ' + name_list[max_index]) print('fps: ' + str(fps_list[max_index])) print('idx: ' + str(idx_list[max_index])) theta = math.acos(max_value) theta = theta * 180 / math.pi print('theta: ' + str(theta)) prob = get_prob(theta) print('prob: ' + str(prob))