完整代码在文章作者的仓库:https://github.com/revenol/DROO。为方便自己看代码所以放上来,看完就删。
这个是论文《Deep Reinforcement Learning for Online Offloading in Wireless Powered Mobile-Edge Computing Networks》的tf1.x版本代码。

#  #################################################################
#  Deep Reinforcement Learning for Online Offloading in Wireless Powered Mobile-Edge Computing Networks
#
#  This file contains a demo evaluating the performance of DROO by randomly turning on/off some WDs. It loads the training samples from ./data/data_#.mat, where # denotes the number of active WDs in the MEC network. Note that, the maximum computation rate need be recomputed by solving (P2) once a WD is turned off/on.
#
#  References:
#  [1] 1. Liang Huang, Suzhi Bi, and Ying-jun Angela Zhang, “Deep Reinforcement Learning for Online Offloading in Wireless Powered Mobile-Edge Computing Networks”, submitted to IEEE Journal on Selected Areas in Communications.
#
# version 1.0 -- April 2019. Written by Liang Huang (lianghuang AT zjut.edu.cn)
#  #################################################################import scipy.io as sio                     # import scipy.io for .mat file I/
import numpy as np                         # import numpyfrom memory import MemoryDNN
from optimization import bisection
from main import plot_rate, save_to_txtimport timedef WD_off(channel, N_active, N):# turn off one WDif N_active > 5: # current we support half of WDs are offN_active = N_active - 1# set the (N-active-1)th channel to close to 0# since all channels in each time frame are randomly generated, we turn of the WD with greatest indexchannel[:,N_active] = channel[:, N_active] / 1000000 # a programming trick,such that we can recover its channel gain once the WD is turned on again.print("    The %dth WD is turned on."%(N_active +1))# update the expected maximum computation raterate = sio.loadmat('./data/data_%d' %N_active)['output_obj']return channel, rate, N_activedef WD_on(channel, N_active, N):# turn on one WDif N_active < N:N_active = N_active + 1# recover (N_active-1)th channel channel[:,N_active-1] = channel[:, N_active-1] * 1000000 print("    The %dth WD is turned on."%(N_active))# update the expected maximum computation  raterate = sio.loadmat('./data/data_%d' %N_active)['output_obj']        return channel, rate, N_activeif __name__ == "__main__":''' This demo evaluate DROO for MEC networks where WDs can be occasionally turned off/on. After DROO converges, we randomly turn off on one WD at each time frame 6,000, 6,500, 7,000, and 7,500, and then turn them on at time frames 8,000, 8,500, and 9,000. At time frame 9,500 , we randomly turn off two WDs, resulting an MEC network with 8 acitve WDs.'''N = 10                     # number of usersN_active = N               # number of effective usersN_off = 0                  # number of off-usersn = 10000                     # number of time frames, <= 10,000K = N                   # initialize K = Ndecoder_mode = 'OP'    # the quantization mode could be 'OP' (Order-preserving) or 'KNN'Memory = 1024          # capacity of memory structureDelta = 32             # Update interval for adaptive Kprint('#user = %d, #channel=%d, K=%d, decoder = %s, Memory = %d, Delta = %d'%(N,n,K,decoder_mode, Memory, Delta))# Load datachannel = sio.loadmat('./data/data_%d' %N)['input_h']rate = sio.loadmat('./data/data_%d' %N)['output_obj']# increase h to close to 1 for better training; it is a trick widely adopted in deep learningchannel = channel * 1000000channel_bak = channel.copy()# generate the train and test data sample index# data are splitted as 80:20# training data are randomly sampled with duplication if n > total data sizesplit_idx = int(.8* len(channel))num_test = min(len(channel) - split_idx, n - int(.8* n)) # training data sizemem = MemoryDNN(net = [N, 120, 80, N],learning_rate = 0.01,training_interval=10, batch_size=128, memory_size=Memory)start_time=time.time()rate_his = []rate_his_ratio = []mode_his = []k_idx_his = []K_his = []h = channel[0,:]for i in range(n):# for dynamic number of WDsif i ==0.6*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_off(channel, N_active, N)if i ==0.65*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_off(channel, N_active, N)if i ==0.7*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_off(channel, N_active, N)if i ==0.75*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_off(channel, N_active, N)if i ==0.8*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_on(channel, N_active, N)if i ==0.85*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_on(channel, N_active, N)if i ==0.9*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_on(channel, N_active, N)channel, rate, N_active = WD_on(channel, N_active, N)if i == 0.95*n:print("At time frame %d:"%(i))channel, rate, N_active = WD_off(channel, N_active, N)channel, rate, N_active = WD_off(channel, N_active, N)if i % (n//10) == 0:print("%0.1f"%(i/n))if i> 0 and i % Delta == 0:# index counts from 0if Delta > 1:max_k = max(k_idx_his[-Delta:-1]) +1; else:max_k = k_idx_his[-1] +1; K = min(max_k +1, N)i_idx = ih = channel[i_idx,:]# the action selection must be either 'OP' or 'KNN'm_list = mem.decode(h, K, decoder_mode)r_list = []for m in m_list:# only acitve users are used to compute the rater_list.append(bisection(h[0:N_active]/1000000, m[0:N_active])[0])# memorize the largest rewardrate_his.append(np.max(r_list))rate_his_ratio.append(rate_his[-1] / rate[i_idx][0])# record the index of largest rewardk_idx_his.append(np.argmax(r_list))# record K in case of adaptive KK_his.append(K)# save the mode with largest rewardmode_his.append(m_list[np.argmax(r_list)])
#        if i <0.6*n:# encode the mode with largest rewardmem.encode(h, m_list[np.argmax(r_list)])total_time=time.time()-start_timemem.plot_cost()plot_rate(rate_his_ratio)print("Averaged normalized computation rate:", sum(rate_his_ratio[-num_test: -1])/num_test)print('Total time consumed:%s'%total_time)print('Average time per channel:%s'%(total_time/n))# save data into txtsave_to_txt(k_idx_his, "k_idx_his.txt")save_to_txt(K_his, "K_his.txt")save_to_txt(mem.cost_his, "cost_his.txt")save_to_txt(rate_his_ratio, "rate_his_ratio.txt")save_to_txt(mode_his, "mode_his.txt")

DROO demo_on_off.py相关推荐

  1. DROO demo_alternate_weights.py

    为方便自己看代码所以放上来,看完就删.完整代码在文章作者的仓库:https://github.com/revenol/DROO. 这个是论文<Deep Reinforcement Learnin ...

  2. DROO memory.py

    这个是论文<Deep Reinforcement Learning for Online Offloading in Wireless Powered Mobile-Edge Computing ...

  3. DROO main.py

    是论文<Deep Reinforcement Learning for Online Offloading in Wireless Powered Mobile-Edge Computing Ne ...

  4. DROO optimization.py

    为方便自己看代码所以放上来,看完就删.完整代码在文章作者的仓库:https://github.com/revenol/DROO. 这个是论文<Deep Reinforcement Learnin ...

  5. 汉字的首拼音字母生成

    生成助记码(取汉字的第一个字母) SET NOCOUNT ON GO IF EXISTS(SELECT name    FROM   sysobjects    WHERE  name = N'hzp ...

  6. python零基础对main.py的学习(DROO)

    在Python中处理Matlab数据时,需要用到sicpy.io**;** sicpy.io提供两个函数: 1.loadmat: loadmat(file_name[, mdict, appendma ...

  7. DROO源码及论文学习

    目录 源码描述之optimization.py 公式描述 local计算: 卸载到边: CD法 交替方向乘子法ADMM 算法流程 DROO的网络模型 略谈边缘计算卸载模型 卸载模式 信道模型 计算模型 ...

  8. 解决 win10 pycurl安装出错 Command python setup.py egg_info failed with error code 10 编译安装包 安装万金油...

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yexiaohhjk/article/d ...

  9. SyntaxError: Non-ASCII character ‘\xe5‘ in file(xxlrt_1.py) on line 7, but no encoding declared;

    具体报错如下: SyntaxError: Non-ASCII character '\xe5' in file /home/hly/work/python/work/xlrt_1.py on line ...

最新文章

  1. 练习PYTHON之EPOLL
  2. Java-POI操作excel清除单元格分行字符
  3. HarmonyOS之常用组件ProgressBar的功能和使用
  4. 苹果平板怎么卸载软件_怎么很好的卸载流氓软件!
  5. gdb 调试kvm虚拟机_GDB+QEMU调试内核模块(实践篇)
  6. mysql57包解压安装_mysql5.7解压包安装教程
  7. Win7 下安装 Sketsa.SVG.Editor v7.0.1
  8. 重装系统大师计算机硬件不兼容,360重装系统教你如何处理提示不兼容的软件
  9. fiddler模拟弱网测试
  10. 阿里云Maven仓库pom文件配置
  11. 一个偶然发现还挺有意思的逻辑题
  12. C语言 字符‘0‘和‘\0‘ 以及整数0的区别
  13. 数据科学和人工智能技术笔记 十八、Keras
  14. /usr/bin/ld: /tmp/ccIHWHTn.o: in function `Cdisk::Cdisk()‘:编译报错解决方案
  15. Camera Surface 从应用到cameraserver的流转
  16. 大数据就业前景,分析的太到位了
  17. go down to用法
  18. 矩阵的Kronecker积的相关结论
  19. Coursera--DataStructure-加州理工大学圣地亚哥分校课程
  20. JavaSE----2

热门文章

  1. 《Nature》发布药物筛选新突破:一种有效的方法来发现新的抗膜蛋白的配体和抑制剂
  2. 全球汽贸网汽车资讯:芯片相关的减产速度有所放缓
  3. OpenCV-Python学习(14)—— OpenCV 绘制箭头线(cv.arrowedLine)
  4. maya动画制作(3)——龙的各种姿态动画制作(续)(修改更新)
  5. 诚风老师-直销十三步走
  6. 铁道部掌上12306手机客户端预计11月底推出-铁道部-掌上12306-手机客户端
  7. JS大写转小写小写转大写,JS字符串大小写互换
  8. oracle 外键更改为on delete cascade属性,SQL脚本更改所有外键以添加ON DELETE CASCADE
  9. 文件夹隐藏属性去不掉的原因及解决方法
  10. android aes文件加密,如何在Android中使用AES从SD卡加密文件?