增强学习--蒙特卡洛方法

蒙特卡洛方法

实例代码

下面代码是constant-α monte carlo,这里有一点介绍

  1 import numpy as np
  2 import random
  3 from collections import defaultdict
  4 from environment import Env
  5 
  6 
  7 # Monte Carlo Agent which learns every episodes from the sample
  8 class MCAgent:
  9     def __init__(self, actions):
 10         self.width = 5
 11         self.height = 5
 12         self.actions = actions
 13         self.learning_rate = 0.01
 14         self.discount_factor = 0.9
 15         self.epsilon = 0.1
 16         self.samples = []
 17         self.value_table = defaultdict(float)#初始化值函数表,0
 18 
 19     # append sample to memory(state, reward, done)
 20     def save_sample(self, state, reward, done):
 21         self.samples.append([state, reward, done])
 22 
 23     # for every episode, agent updates q function of visited states
 24     def update(self):
 25         G_t = 0
 26         visit_state = []
 27         for reward in reversed(self.samples):#此处reverse,状态反转
 28             state = str(reward[0])
 29             if state not in visit_state:#first-visit MC methods
 30                 visit_state.append(state)
 31                 G_t = self.discount_factor * (reward[1] + G_t)#累积回报
 32                 value = self.value_table[state]
 33                 self.value_table[state] = (value +
 34                                            self.learning_rate * (G_t - value))
 35                 #constant-α monte carlo constant-α蒙特卡洛值函数更新
 36 
 37     # get action for the state according to the q function table
 38     # agent pick action of epsilon-greedy policy
 39     def get_action(self, state):
 40         if np.random.rand() < self.epsilon:#以epsilon概率随机选择,Exploration
 41             # take random action
 42             action = np.random.choice(self.actions)
 43         else:
 44             # take action according to the q function table
 45             next_state = self.possible_next_state(state)
 46             action = self.arg_max(next_state)
 47         return int(action)
 48 
 49     # compute arg_max if multiple candidates exit, pick one randomly
 50     @staticmethod
 51     def arg_max(next_state):
 52         max_index_list = []
 53         max_value = next_state[0]
 54         for index, value in enumerate(next_state):
 55             if value > max_value:
 56                 max_index_list.clear()
 57                 max_value = value
 58                 max_index_list.append(index)
 59             elif value == max_value:
 60                 max_index_list.append(index)
 61         return random.choice(max_index_list)
 62 
 63     # get the possible next states
 64     def possible_next_state(self, state):
 65         col, row = state
 66         next_state = [0.0] * 4 #四个方向,Q(s,a)
 67 
 68         if row != 0:
 69             next_state[0] = self.value_table[str([col, row - 1])]
 70         else:
 71             next_state[0] = self.value_table[str(state)]
 72 
 73         if row != self.height - 1:
 74             next_state[1] = self.value_table[str([col, row + 1])]
 75         else:
 76             next_state[1] = self.value_table[str(state)]
 77 
 78         if col != 0:
 79             next_state[2] = self.value_table[str([col - 1, row])]
 80         else:
 81             next_state[2] = self.value_table[str(state)]
 82 
 83         if col != self.width - 1:
 84             next_state[3] = self.value_table[str([col + 1, row])]
 85         else:
 86             next_state[3] = self.value_table[str(state)]
 87 
 88         return next_state
 89 
 90 
 91 # main loop
 92 if __name__ == "__main__":
 93     env = Env()
 94     agent = MCAgent(actions=list(range(env.n_actions)))
 95 
 96     for episode in range(1000):#episode task
 97         import pdb; pdb.set_trace()
 98         state = env.reset()
 99         action = agent.get_action(state)
100 
101         while True:
102             env.render()
103 
104             # forward to next state. reward is number and done is boolean
105             next_state, reward, done = env.step(action)
106             agent.save_sample(next_state, reward, done)
107 
108             # get next action
109             action = agent.get_action(next_state)
110 
111             # at the end of each episode, update the q function table
112             if done:
113                 print("episode : ", episode)
114                 agent.update()
115                 agent.samples.clear()
116                 break

 

转载于:https://www.cnblogs.com/buyizhiyou/p/10250103.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/484357.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

排序算法-查找算法

排序算法 冒泡排序 从左向右扫描数据&#xff0c;选着最大的数据。要点&#xff1a;比较相邻的两个数&#xff0c;如果左边的数大于右边的数就进行交换。 template<class T> void BubbleSort(T *array, const int length) { for (int i 0; i <length-1; i) { fo…

当法律纽带变成“机器红线”,能让自动驾驶汽车更安全吗?

来源&#xff1a; 脑极体美国汽车协会(American Automobile Association, AAA)与哈佛大学的一项研究结果显示&#xff0c;高达77%的人表示担心自己与自动驾驶汽车共享道路的安全性&#xff0c;62%则担心自动驾驶汽车发生意外时的肇责归属。某种程度上来讲&#xff0c;这可以说是…

JAVA面试——计算机网络

网络体系架构&#xff1a;应用层&#xff1a;为特定应用程序提供传输服务&#xff08;HTTP、DNS协议&#xff09;传输层&#xff1a;提供通用数据传输服务&#xff08;TCP、UDP&#xff09;TCP头部20个字节UDP头部8个字节网络层&#xff1a;为主机提供传输服务&#xff0c;将报…

李联宁|量子计算机:技术路线、风险及战略投资

来源&#xff1a;《学术前沿》杂志2021年4月上&#xff08;微信有删节&#xff09;作者&#xff1a;西安交通大学城市学院教授 李联宁原文责编&#xff1a;桂琰新媒体责编&#xff1a;李思琪视觉&#xff1a;王洋图片来源&#xff1a;网络由于现有半导体微电子技术物理极限的原…

vs code 配置java

VS code 入门 Vs code 配置Java 首先到VS code官网下载VS code软件&#xff0c;打开界面如图&#xff1a; 下载安装好&#xff0c;打开界面如图(注下图已汉化)&#xff1a; 为了便于使用&#xff0c;我们先进行汉化&#xff0c;在最左边点击图1-1中的图标&#xff0c;出现如图…

四、HTTP控制器

1、控制器的作用&#xff1a;数据和视图之间的桥梁 2、控制器创建方法&#xff1a; &#xff08;1&#xff09;手动创建 <?php namespace App\Http\Controllers; class UserController extends Controller { public function showProfile($id) { return view(user.profile,…

机器人行业专利构建与维护刻不容缓,专利研发需下苦功!

来源&#xff1a;机器人大讲堂自我国加入世界贸易组织&#xff0c;已经近20年过去。但不可忽视的是&#xff0c;发达国家或地区利用其产业先发优势&#xff0c;也一直在知识产权和专利上&#xff0c;采用知识产权和专利贸易壁垒&#xff0c;限制我国企业参与国际市场竞争。机器…

java-弹簧布局(自适应窗口)

一、布局管理器 弹簧布局管理器以容器和组件的边缘为操作对象&#xff0c;通过组件与容器边缘以及组件与组件边缘建立约束(建立位置关系)&#xff0c;实现对组件的布局管理。主要通过函数putConstraint(Strting s1,Component c1,int len,Strting s2,Component c2);当s2在s1的北…

从1G到5G,从回顾过去到展望未来

来源&#xff1a;《从1G到5G&#xff1a;移动通信如何改变世界》 作者&#xff1a;王建宙始于20世纪80年代的蜂窝式移动通信&#xff0c;只用了30多年的时间&#xff0c;就实现了在全世界的普及。如今&#xff0c;手机是人们随身携带的使用频率最高的工具。手机改变了人类的沟…

ECMA6--目录

ECMA6(ecma2015) ECMA是一个组织规范&#xff0c;是一个标准严格意义上来说并不是JS&#xff0c;当时JavaScript支持是最好的约等于&#xff0c;js。 [理解] ECMA非常重要&#xff0c;JavaScript是一门语言&#xff0c;个人认为语言最重要的几点&#xff1a;词汇量、语法、语言…

学习笔记二:异步FIFO

1 module fifo1 #(parameter DSIZE 8,2 parameter ASIZE 4) //用格雷码的局限性&#xff1a;循环计数深度必须是2的n次幂&#xff0c;否则就失去了每次只变化一位的特性3 (wclk,wrstn,wdata,wfull,winc,rclk,rrstn,rdata,rempty,…

重磅:国拨概算5.34亿!“新一代人工智能”重大项目项目申报指南发布

来源&#xff1a;brainnews各省、自治区、直辖市及计划单列市科技厅&#xff08;委、局&#xff09;&#xff0c;新疆生产建设兵团科技局&#xff0c;国务院各有关部门&#xff0c;各有关单位&#xff1a;为落实国务院印发的《新一代人工智能发展规划》总体部署&#xff0c;现根…

java-多线程知识

几个排序算法比较 采用多线程实现几个排序算法&#xff0c;比较各个排序算法的优劣&#xff1b;java实现&#xff0c;一个主类&#xff0c;多个内部排序算法进程的接口&#xff0c;涉及到进程间的通信&#xff0c;因为每个进程包含自己的储存空间&#xff0c;无法直接访问其他…

Idea中在代码顶部添加自定义作者和时间

一、在工具栏找到File>settings>Editor>File and Code Templates 二、选择右边的File Header,在编辑框中添加自定义信息&#xff0c;例如&#xff1a; 三、然后点击应用&#xff0c;保存就OK啦&#xff01; 转载于:https://www.cnblogs.com/HelloBigTable/p/10261145.…

MFC框架解析

通过上述方法&#xff0c;我们进行代码的编写&#xff0c;创建一个空项目&#xff0c;我们要编写MFC应有程序&#xff0c;因此设置项目的属性&#xff0c; 项目->属性->链接器->系统->子系统为&#xff1a;窗口 (/SUBSYSTEM:WINDOWS)&#xff0c;项目->属性-&g…

162年难题,黎曼猜想被印度数学家迎刃而解?克雷数研所发出质疑

来源 &#xff1a; 新智元黎曼猜想又被证明了&#xff1f;5年前&#xff0c;印度一名数学物理学家Kumar Easwaran声称自己证明了「黎曼猜想」&#xff01;他发表了一篇论文「The Final and Exhaustive Proof of the Riemann Hypothesis from First Principles」解释自己的发现&…

js构造函数内存在的闭包

function Func(x) { this.x x; this.printfunction() { console.info(this.x); (function (){ console.info(x); })(); }}var a new Func(30);console.dir(a);a.age 300;console.dir(a.print());//300,30//存在于构造函数内的闭包 age:30 发生了闭包//this.print 内存在闭包…

MFC的六大关键技术

//main.h头文件 #pragma once #include"a.h" class CShape { public:virtual CRuntimeClassTest* GetRuntimeClassTest() const;BOOL IsKindOf(const CRuntimeClassTest* pClass) const; public:static CRuntimeClassTest classCShape; };static char szCShape[] &q…

20210709未来智能实验室收录资料

整理&#xff1a;未来智能实验室1.加州理工博士&#xff1a;用概率模型解析大脑中的神经活动2.智能化战争的基本形态 3.联合全域指挥控制的人工智能生态系统| 新品推荐4.数字化转型白皮书&#xff1a;数智技术驱动智能制造&#xff0c;42页pdf5.2021综述论文《几何深…