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

# -*- coding: utf-8 -*-
"""
Created on Tue Jan  9 10:45:26 2018@author: Administrator
"""
import numpy as np
from scipy import optimize
from scipy.special import lambertw
import scipy.io as sio                     # import scipy.io for .mat file I/
import timedef plot_gain( gain_his):import matplotlib.pyplot as pltimport pandas as pdimport matplotlib as mplgain_array = np.asarray(gain_his)df = pd.DataFrame(gain_his)mpl.style.use('seaborn')fig, ax = plt.subplots(figsize=(15,8))rolling_intv = 20plt.plot(np.arange(len(gain_array))+1, df.rolling(rolling_intv, min_periods=1).mean(), 'b')plt.fill_between(np.arange(len(gain_array))+1, df.rolling(rolling_intv, min_periods=1).min()[0], df.rolling(rolling_intv, min_periods=1).max()[0], color = 'b', alpha = 0.2)plt.ylabel('Gain ratio')plt.xlabel('learning steps')plt.show()def bisection(h, M, weights=[]):# the bisection algorithm proposed by Suzhi BI# average time to find the optimal: 0.012535839796066284 s# parameters and equationso=100p=3u=0.7eta1=((u*p)**(1.0/3))/oki=10**-26   eta2=u*p/10**-10B=2*10**6Vu=1.1epsilon=B/(Vu*np.log(2))x = [] # a =x[0], and tau_j = a[1:]M0=np.where(M==0)[0]M1=np.where(M==1)[0]hi=np.array([h[i] for i in M0])hj=np.array([h[i] for i in M1])if len(weights) == 0:# default weights [1, 1.5, 1, 1.5, 1, 1.5, ...]weights = [1.5 if i%2==1 else 1 for i in range(len(M))]wi=np.array([weights[M0[i]] for i in range(len(M0))])wj=np.array([weights[M1[i]] for i in range(len(M1))])def sum_rate(x):sum1=sum(wi*eta1*(hi/ki)**(1.0/3)*x[0]**(1.0/3))sum2=0for i in range(len(M1)):sum2+=wj[i]*epsilon*x[i+1]*np.log(1+eta2*hj[i]**2*x[0]/x[i+1])return sum1+sum2def phi(v, j):return 1/(-1-1/(lambertw(-1/(np.exp( 1 + v/wj[j]/epsilon))).real))def p1(v):p1 = 0for j in range(len(M1)):p1 += hj[j]**2 * phi(v, j)return 1/(1 + p1 * eta2)def Q(v):sum1 = sum(wi*eta1*(hi/ki)**(1.0/3))*p1(v)**(-2/3)/3sum2 = 0for j in range(len(M1)):sum2 += wj[j]*hj[j]**2/(1 + 1/phi(v,j))return sum1 + sum2*epsilon*eta2 - vdef tau(v, j):return eta2*hj[j]**2*p1(v)*phi(v,j)# bisection starts heredelta = 0.005UB = 999999999LB = 0while UB - LB > delta:v = (float(UB) + LB)/2if Q(v) > 0:LB = velse:UB = vx.append(p1(v))for j in range(len(M1)):x.append(tau(v, j))return sum_rate(x), x[0], x[1:]def cd_method(h):N = len(h)M0 = np.random.randint(2,size = N)gain0,a,Tj= bisection(h,M0)g_list = []M_list = []while True:for j in range(0,N):M = np.copy(M0)M[j] = (M[j]+1)%2gain,a,Tj= bisection(h,M)g_list.append(gain)M_list.append(M)g_max = max(g_list)if g_max > gain0:gain0 = g_maxM0 = M_list[g_list.index(g_max)]else:breakreturn gain0, M0if __name__ == "__main__":h=np.array([6.06020304235508*10**-6,1.10331933767028*10**-5,1.00213540309998*10**-7,1.21610610942759*10**-6,1.96138838395145*10**-6,1.71456339592966*10**-6,5.24563569673585*10**-6,5.89530717142197*10**-7,4.07769429231962*10**-6,2.88333185798682*10**-6])M=np.array([1,0,0,0,1,0,0,0,0,0])
#    h=np.array([1.00213540309998*10**-7,1.10331933767028*10**-5,6.06020304235508*10**-6,1.21610610942759*10**-6,1.96138838395145*10**-6,1.71456339592966*10**-6,5.24563569673585*10**-6,5.89530717142197*10**-7,4.07769429231962*10**-6,2.88333185798682*10**-6])
#    M=np.array([0,0,1,0,1,0,0,0,0,0])#    h = np.array([4.6368924987170947*10**-7,   1.3479411763648968*10**-7,  7.174945246007612*10**-6,   2.5590719803595445*10**-7,  3.3189928740379023*10**-6,  1.2109071327755575*10**-5,  2.394278475886022*10**-6,   2.179121774067472*10**-6,   5.5213902658478367*10**-8,  2.168778154948169*10**-7,   2.053227965874453*10**-6,   7.002952297466865*10**-8,   7.594077851181444*10**-8,   7.904048961975136*10**-7,   8.867218892023474*10**-7,   5.886007653360979*10**-6,   2.3470565740563855*10**-6,  1.387049627074303*10**-7,   3.359475870531776*10**-7,   2.633733784949562*10**-7,   2.189895264149453*10**-6,   1.129177795302099*10**-5,   1.1760290137191366*10**-6,  1.6588656719735275*10**-7,  1.383637788476638*10**-6,   1.4485928387351664*10**-6,  1.4262265958416598*10**-6, 1.1779725004265418*10**-6, 7.738218993031842*10**-7, 4.763534225174186*10**-6])
#    M =np.array( [0,  0,  1,  0, 0,   1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0,  0,  1,])#    time the average speed of bisection algorithm
#    repeat = 1
#    M =np.random.randint(2, size=(repeat,len(h)))
#    start_time=time.time()
#    for i in range(repeat):
#        gain,a,Tj= bisection(h,M[i,:])
#    total_time=time.time()-start_time
#    print('time_cost:%s'%(total_time/repeat))gain,a,Tj= bisection(h,M)print('y:%s'%gain)print('a:%s'%a)print('Tj:%s'%Tj)# test CD method. Given h, generate the max modegain0, M0 = cd_method(h)print('max y:%s'%gain0)print(M0)# test all dataK = [10, 20, 30]                     # number of usersN = 1000                     # number of channelfor k in K:# Load datachannel = sio.loadmat('./data/data_%d' %int(k))['input_h']gain = sio.loadmat('./data/data_%d' %int(k))['output_obj']start_time=time.time()gain_his = []gain_his_ratio = []mode_his = []for i in range(N):if i % (N//10) == 0:print("%0.1f"%(i/N))i_idx = i h = channel[i_idx,:]# the CD methodgain0, M0 = cd_method(h)# memorize the largest rewardgain_his.append(gain0)gain_his_ratio.append(gain_his[-1] / gain[i_idx][0])mode_his.append(M0)total_time=time.time()-start_timeprint('time_cost:%s'%total_time)print('average time per channel:%s'%(total_time/N))plot_gain(gain_his_ratio)print("gain/max ratio: ", sum(gain_his_ratio)/N)

DROO optimization.py相关推荐

  1. DROO demo_alternate_weights.py

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

  2. DROO main.py

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

  3. DROO demo_on_off.py

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

  4. DROO memory.py

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

  5. DROO源码及论文学习

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

  6. BERT:代码解读、实体关系抽取实战

    目录 前言 一.BERT的主要亮点 1. 双向Transformers 2.句子级别的应用 3.能够解决的任务 二.BERT代码解读 1. 数据预处理 1.1 InputExample类 1.2 In ...

  7. 【NLP】NLP实战篇之bert源码阅读(run_classifier)

    本文主要会阅读bert源码 (https://github.com/google-research/bert )中run_classifier.py文件,已完成modeling.py.optimiza ...

  8. Huggingface BERT源码详解:应用模型与训练优化

    ©PaperWeekly 原创 · 作者|李泺秋 学校|浙江大学硕士生 研究方向|自然语言处理.知识图谱 接上篇,记录一下对 HuggingFace 开源的 Transformers 项目代码的理解. ...

  9. 《集体智慧编程》第8章

    1.P175 在计算高斯函数时,代码中的默认标准差为10.0,如果默认标准差为10是得不到正文中的数据的,这里的默认值应该改为1.0 附上高斯函数的公式和图像 公式中的a代表高斯函数的最大值,b代表平 ...

最新文章

  1. 网吧网管在自杀前的遗书
  2. canva旋转图片 js_js和canvas实现旋转图片
  3. linux系统UDP的socket通信编程
  4. 如何用Unity和Cardboard做一款VR游戏
  5. Ubuntu 18.04 LTS环境下 MNN 的编译与使用
  6. 高并发网络架构解决方案分析
  7. 邮件发送类_10 分钟实现 Spring Boot 发生邮件功能
  8. 一季度平板电脑市场:苹果稳居第一,华为第二!
  9. 华为P40系列发布!新机皇临世,售价首次过万!
  10. Linux-shell获取天气
  11. 从前端到后端的跨域攻击与防御
  12. keil git 编译文件_交叉编译链下的Makefile(STM32F4xx)
  13. 使用parseInt转换规范非法输入
  14. I.MX6 Manufacturing Tool V2 (MFGTool2) Emmc mksdcard-android.sh hacking
  15. keras从dataframe中读取数据并进行数据增强进行训练(分类+分割)
  16. 抓取Js动态生成数据且以滚动页面方式分页的网页
  17. 仙岛求药 详解(C++)
  18. 一组图带你回顾UNIT平台的2019
  19. Maven安装配置操作方法
  20. python33个保留字基本含义_Python保留字总结

热门文章

  1. 解析服装行业的现状及数字化转型的步伐
  2. Windows下安装配置wget
  3. linux下ipvsadm命令,ipvsadm命令使用方法
  4. C# 使用 OleDbConnection 连接读取Excel
  5. 6轴并联机器人开发--有限元分析
  6. vue项目中使用iview插件中this.$Modal.confirm的使用方法
  7. talk show: 发泄不满的表达。
  8. html中用来表示主题标签的是什么,HTML5中的article标签是什么?HTML5中的article元素用在什么地方?...
  9. Spark安全威胁及建模方法
  10. 铝电解电容发热问题,查找原因