当前位置: 首页>>代码示例>>Python>>正文


Python Board.is_valid_action方法代码示例

本文整理汇总了Python中Board.is_valid_action方法的典型用法代码示例。如果您正苦于以下问题:Python Board.is_valid_action方法的具体用法?Python Board.is_valid_action怎么用?Python Board.is_valid_action使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Board的用法示例。


在下文中一共展示了Board.is_valid_action方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: import Board [as 别名]
# 或者: from Board import is_valid_action [as 别名]
def main():
	root = Tk()
	board = Board(master=root)
	board.pack()
	#board.mainloop()
	eps = EPSILON
	#Init Q
	Q = np.random.rand(20,20)
	trace = np.zeros((20,20))
	R = np.zeros((20,20))
	goal = board.goal
	R[goal.i][goal.j] = 1.0
	print "Q initial: "
	print_matrix(Q)

	for iter2 in range(OUTER_ITER): #Episodes
		
		erase_board(board,s=True)
		state = board.get_random_starting_state()
		state.set_fill("red")
		action = random.choice(range(1,5))
		print "Episode: %d" %iter2
		while not board.is_valid_action(state,action):
			action = random.choice(range(1,5))
		for iter in range(INNTER_ITER):
			
			#Take action a
			new_i = state_i = state.i
			new_j = state_j = state.j
			if action == ACTION_DOWN:
				new_i +=1
				txt = "\\/"
				action_name = "DOWN"
			elif action == ACTION_UP:
				new_i -=1
				txt = "/\\"
				action_name = "UP"
			elif action == ACTION_LEFT:
				new_j -=1
				txt = "<"
				action_name = "LEFT"
			elif action == ACTION_RIGHT:
				new_j +=1
				txt = ">"
				action_name = "RIGHT"
			Qq = Q[new_i][new_j]	
		
			#Observe r
			
			#Observe s'
			if board.is_valid_action(state,action):
				new_state = board.board_matrix[new_i][new_j]
				new_font = "Times " + str(int(Qq*30)) + " bold"
				state.addText(txt,new_font) #TAke action
				if iter == 0:
					state.set_fill("red")
				if new_state.color != "red":
					new_state.set_fill("green")
				#print "TO end up in state: %d,%d" %(new_state.i,new_state.j)
				#time.sleep(2)
			else:
				print "ERROR!"
			reward = R[new_i][new_j]
			if reward == 1:
				if iter2 > 200:
					time.sleep(1)
				#print "GOAL!!!!"
				break	
			#new_state = board.board_matrix[new_i][new_j]
			new_action_pair = find_best_action(board,Q,new_state,eps)
			new_action = new_action_pair[0]
			new_action_q = new_action_pair[1]
			#print "New action is " + str(new_action)
			
			if new_action == ACTION_UP:
				qpap_i = new_i - 1
				qpap_j = new_j
			elif new_action == ACTION_DOWN:
				qpap_i = new_i + 1
				qpap_j = new_j
			elif new_action == ACTION_RIGHT:
				qpap_i = new_i
				qpap_j = new_j + 1
			elif new_action == ACTION_LEFT:
				qpap_i = new_i
				qpap_j = new_j - 1
				
			Qp = Q[qpap_i][qpap_j]
			
			
			delta = reward + y*Qp - Qq
			trace[new_i][new_j] += 1.0
			
			
			
			for i in range(20):
				for j in range(20):
					cell = board.board_matrix[i][j]
					Q[i][j] = Q[i][j] + alpha*delta*trace[i][j]
					trace[i][j] = y*lamd*trace[i][j]
#.........这里部分代码省略.........
开发者ID:sdarden2,项目名称:Shortest_Path_Demonstration,代码行数:103,代码来源:Algorithm.py


注:本文中的Board.is_valid_action方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。