首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

基于深度强化学习的dqn和基准p-learning的边缘计算计算卸载方法总结

后端 4.9KB 30 需要积分: 1
立即下载

资源介绍:

基于深度强化学习的dqn和基准p-learning的边缘计算计算卸载方法总结
import numpy as np import pandas as pd import tensorflow as tf np.random.seed(1) tf.random.set_seed(1) actions=np.array([[0,0],[0,0.1],[0,0.2],[0,0.3],[0,0.4],[0,0.5],[0,0.6],[0,0.7],[0,0.8],[0,0.9],[0,1], [1, 0], [1, 0.1], [1, 0.2], [1, 0.3], [1, 0.4], [1, 0.5], [1, 0.6], [1, 0.7], [1, 0.8], [1, 0.9], [1, 1], [2, 0], [2, 0.1], [2, 0.2], [2, 0.3], [2, 0.4], [2, 0.5], [2, 0.6], [2, 0.7], [2, 0.8], [2, 0.9], [2, 1], [3, 0], [3, 0.1], [3, 0.2], [3, 0.3], [3, 0.4], [3, 0.5], [3, 0.6], [3, 0.7], [3, 0.8], [3, 0.9], [3, 1]]) # Deep Q Network off-policy class DeepQNetwork: def __init__( self, n_actions, n_features, learning_rate=0.05, reward_decay=0.9, e_greedy=0.9, replace_target_iter=300, memory_size=500, batch_size=32, e_greedy_increment=0.001, output_graph=True, ): self.n_actions = n_actions self.n_features = n_features self.lr = learning_rate self.gamma = reward_decay self.epsilon_max = e_greedy self.replace_target_iter = replace_target_iter self.memory_size = memory_size self.batch_size = batch_size self.epsilon_increment = e_greedy_increment # self.epsilon = 0 if e_greedy_increment is not None else self.epsilon_max self.epsilon = 0 # total learning step self.learn_step_counter = 0 # initialize zero memory [s, a, r, s_]#每个状态用n个feature表示 self.memory = np.zeros((self.memory_size, n_features * 2 + 3))#初始化经验池 # consist of [target_net, evaluate_net] self._build_net() t_params = tf.get_collection('target_net_params') e_params = tf.get_collection('eval_net_params') self.replace_target_op = [tf.assign(t, e) for t, e in zip(t_params, e_params)] self.sess = tf.Session() if output_graph: # $ tensorboard --logdir=logs # tf.train.SummaryWriter soon be deprecated, use following tf.summary.FileWriter("logs/", self.sess.graph) self.sess.run(tf.global_variables_initializer()) self.cost_his = [] def _build_net(self):#建立目标网络和评估网络 # ------------------ build evaluate_net ------------------ self.s = tf.placeholder(tf.float32, [None, self.n_features], name='s') # input self.q_target = tf.placeholder(tf.float32, [None, self.n_actions], name='Q_target') # for calculating loss with tf.variable_scope('eval_net'): # c_names(collections_names) are the collections to store variables c_names, n_l1, w_initializer, b_initializer = \ ['eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES], 10, \ tf.random_normal_initializer(0., 0.3), tf.constant_initializer(0.1) # config of layers # first layer. collections is used later when assign to target net with tf.variable_scope('l1'): w1 = tf.get_variable('w1', [self.n_features, n_l1], initializer=w_initializer, collections=c_names) b1 = tf.get_variable('b1', [1, n_l1], initializer=b_initializer, collections=c_names) l1 = tf.nn.relu(tf.matmul(self.s, w1) + b1) # second layer. collections is used later when assign to target net with tf.variable_scope('l2'): w2 = tf.get_variable('w2', [n_l1, self.n_actions], initializer=w_initializer, collections=c_names) b2 = tf.get_variable('b2', [1, self.n_actions], initializer=b_initializer, collections=c_names) self.q_eval = tf.matmul(l1, w2) + b2 with tf.variable_scope('loss'): self.loss = tf.reduce_mean(tf.squared_difference(self.q_target, self.q_eval)) with tf.variable_scope('train'): self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(self.loss) # ------------------ build target_net ------------------ self.s_ = tf.placeholder(tf.float32, [None, self.n_features], name='s_') # input with tf.variable_scope('target_net'): # c_names(collections_names) are the collections to store variables c_names = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES] # first layer. collections is used later when assign to target net with tf.variable_scope('l1'): w1 = tf.get_variable('w1', [self.n_features, n_l1], initializer=w_initializer, collections=c_names) b1 = tf.get_variable('b1', [1, n_l1], initializer=b_initializer, collections=c_names) l1 = tf.nn.relu(tf.matmul(self.s_, w1) + b1) # second layer. collections is used later when assign to target net with tf.variable_scope('l2'): w2 = tf.get_variable('w2', [n_l1, self.n_actions], initializer=w_initializer, collections=c_names) b2 = tf.get_variable('b2', [1, self.n_actions], initializer=b_initializer, collections=c_names) self.q_next = tf.matmul(l1, w2) + b2 def store_transition(self, s, a, r, s_): if not hasattr(self, 'memory_counter'):#函数hasattr查看对象中是否含有属性 self.memory_counter = 0 transition = np.hstack((s, a, r, s_))#将每一行实例按行存储 print("需要存入的记录为:",transition) # replace the old memory with new memory index = self.memory_counter % self.memory_size self.memory[index, :] = transition self.memory_counter += 1 def choose_action(self, observation):#选择动作 # to have batch dimension when feed into tf placeholder observation = observation[np.newaxis, :] print("当前贪心率:",self.epsilon) if np.random.uniform() < self.epsilon: # forward feed the observation and get q value for every actions actions_value = self.sess.run(self.q_eval, feed_dict={self.s: observation}) index = np.argmax(actions_value) action = actions[index] else: index = np.random.randint(0, self.n_actions) action = actions[index] return action def learn(self): # check to replace target parameters if self.learn_step_counter % self.replace_target_iter == 0: self.sess.run(self.replace_target_op) print('\ntarget_params_replaced\n') # sample batch memory from all memory if self.memory_counter > self.memory_size: sample_index = np.random.choice(self.memory_size, size=self.batch_size) else: sample_index = np.random.choice(self.memory_counter, size=self.batch_size) batch_memory = self.memory[sample_index, :] q_next, q_eval = self.sess.run( [self.q_next, self.q_eval], feed_dict={ self.s_: batch_memory[:, -self.n_features:], # fixed params self.s: batch_memory[:, :self.n_features], # newest params }) # change q_target w.r.t q_eval's action q_target = q_eval.copy() batch_index = np.arange(self.batch_size, dtype=np.int32) eval_act_index = batch_memory[:, self.n_features].astype(int) reward = batch_memory[:, self.n_features + 2] q_target[batch_index, eval_act_index] = reward + self.gamma * np.max(q_next, axis=1) """ For example in this batch I have 2 samples and 3 actions: q_eval = [[1, 2, 3], [4, 5, 6]] q_target = q_eval = [[1, 2, 3], [4, 5, 6]] Then change q_target with the real q_target value w.r.t the q_eval's action. For example in: sample 0, I took action 0, and the max q_target value is -1; sample 1, I took action 2

资源文件列表:

边缘计算论文代码下载.zip 大约有2个文件
  1. run_this.py 8.1KB
  2. RL_brain.py 9.02KB
0评论
提交 加载更多评论
其他资源 launch4j安装包,3.50版本,window
launch4j 将jar包转换为exe文件,方便执行 Launch4j 是一个开源的可执行文件打包工具,可以将Java应用程序打包成一个独立的EXE文件。它提供了一个图形化界面和命令行接口,是非常方便易用的
C盘优化清理降低磁盘空间不足工具
本安装包是专为解决C盘空间不足、系统性能下降等问题而设计。通过智能扫描并清理垃圾文件、临时文件、无用程序和注册表冗余项等,有效释放C盘空间,优化系统环境。同时,该工具还具备一键清理功能,让操作更加简便快捷,是提升计算机性能、保持系统流畅运行的必备利器。无论是日常办公还是游戏娱乐,都能享受到更加高效稳定的电脑使用体验。还有一些其它的功能供你选择
Excel 和 Zip 的导出 & 上传
Excel 和 Zip 的导出 & 上传
Tapeout原型参考
Tapeout原型参考
Tapeout原型参考 Tapeout原型参考 Tapeout原型参考
000000c++清理脚本0000000000
适合c++代码缩短大小
武汉理工大学 实训基于verilog的倒计时
武汉理工大学 实训基于verilog的倒计时
Rancher启动hadoop镜像的必要静态文件
Rancher启动的必要静态文件
python安装包-python程序必备
python-3.10.11,python-3.9.7,python-3.8.7 三个版本都可以安装,就是发布的年限不同,使用功能基本一致。装任意一个均可。解压后点击你所需要的版本安装程序,按照步骤进行安装,为了在python程序中正常运行,需要在安装完毕后配置环境变量,不是安装好就能使用