来自杭州电子科技大学自动化专业的大三理工男一枚。
一只整个暑假都在学校学习的学生。
b站搜索:def__PanCake__
steam好友代码:241282993
喜欢的游戏:明日方舟、csgo、彩虹六号……
如果想联系我也可以加QQ:2114496089,加好友都请备注来意。
Personal Blog
来自杭州电子科技大学自动化专业的大三理工男一枚。
一只整个暑假都在学校学习的学生。
b站搜索:def__PanCake__
steam好友代码:241282993
喜欢的游戏:明日方舟、csgo、彩虹六号……
如果想联系我也可以加QQ:2114496089,加好友都请备注来意。
服务器主要功能为个人博客,于2021.7.1开始正式运营,为wordpress模板。
该博客会不定期更新个人学习到的知识进行分享,你也可以通过我的博客了解我。
请不要在此网站泄露任何个人信息,评论姓名等个人信息请匿名填写,个人不能保证此网站不被攻击,如果出问题请自行负责!请爱护服务器,大佬请不要无故攻击谢谢!请不要对服务器运行爬虫或各种压力测试软件,谢谢,服务器设置每秒访问上限,可能你会被封ip。服务器运行速度及网速可能稍慢,望见谅(有钱了买个好的)。
这东西自己看的时候可谓是眼花缭乱,看到一半我就不想看了,因为本人没用过,看b站的时候刷到了,推荐一下大佬的学习网站
https://r2coding.com/ 里面还有很多计算机学习资料 有兴趣的可以看看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import pandas as pd import geatpy as ea import numpy as np import random from multiprocessing import Pool class Data: market, market_value = None, None def __init__(self, df, platform, platform_max): self.platform = platform self.platform_max = platform_max self.x = df['交易量'] self.y = df["交易费用"] def getResult(self, choose, average): choose_dict = [{"index": choose[i], "x": self.x[i], "y": self.y[i]} for i in range(len(choose))] choose_dict = sorted(choose_dict, key=lambda x: x["index"]) result = float("inf") for i in range(self.platform): market_value = 0 market = 0 for j in range(average): if choose_dict[i * average + j]["x"] + market > self.platform_max: result = min(result, market_value) break else: market_value += choose_dict[i * average + j]["y"] market += choose_dict[i * average + j]["x"] if market_value <= self.platform_max: result = min(result, market_value) return result # GA 参数设置 class My_nsga(ea.Problem): def __init__(self, dim, df, platform, platform_max): name = 'GA-NET' M = 1 maxormins = [-1] * M Dim = dim varTypes = [1] * Dim lb = [0] * Dim ub = [Dim - 1] * Dim lbin = [1] * Dim ubin = [1] * Dim self.count = 1 self.data = Data(df, platform, platform_max) self.average = int(len(df) / platform) ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin) # 目标函数即神经网络返回值 def evalVars(self, Vars): ans = np.zeros(len(Vars), dtype=float).reshape(len(Vars), 1) for i in range(len(Vars)): ans[i][0] = self.data.getResult(Vars[i], self.average) # print(ans.max(axis=0)) return ans # 运行 GA def Run_nsga(dim, df, platform, platform_max, loop_id, ndind=30, maxgen=1500): problem = My_nsga(dim, df, platform, platform_max) myAlgorithm = ea.soea_EGA_templet(problem, ea.Population(Encoding='P', NIND=ndind), MAXGEN=maxgen, logTras=0) # 要得到图把 drawing = 1 saveFlag = 1 res = ea.optimize(myAlgorithm, seed=1, verbose=False, drawing=1, outputMsg=1, drawLog=1, saveFlag=saveFlag, dirName='result{}'.format(loop_id)) if saveFlag: np.savetxt("./result{}/result.csv".format(i), res['Vars'][0], delimiter=",") return res['ObjV'][0][0] if __name__ == "__main__": df = pd.read_excel("data.xlsx", sheet_name=None, engine='openpyxl') # 设置每张表分成的 platform 数量 platforms = [2, 4, 10, 20, 10, 50, 50, 100] # 设置每张表 platform 最大值 platform_maxs = [2000, 1000, 1500, 800, 500, 100, 416, 208] # 设置是否训练表 is_train = [0, 0, 1, 1, 0, 0, 0, 0] values = list(df.values())[:4] pool = Pool(sum(is_train)) for i in range(len(values)): if not is_train[i]: continue # 最后两个参数:种群数量,最大迭代次数 pool.apply_async(Run_nsga, args=(len(values[i]), values[i], platforms[i], platform_maxs[i], i, 500, 600)) pool.close() pool.join() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import pandas as pd import geatpy as ea import numpy as np import random from multiprocessing import Pool class Data: market, market_value = None, None def __init__(self, df, platform, platform_max): self.platform = platform self.platform_max = platform_max self.x = df['交易量'] self.y = df["交易费用"] def load(self, choose): self.market = [[] for _ in range(self.platform)] self.market_value = [[] for _ in range(self.platform)] for i in range(len(choose)): if choose[i] != 0: self.market[choose[i] - 1].append(self.x[i]) self.market_value[choose[i] - 1].append(self.y[i]) def getResult(self): if max(list(map(lambda x: sum(x), self.market))) > self.platform_max: return sum(list(map(lambda x: 0 if sum(x) < self.platform_max else self.platform_max - sum(x), self.market))) else: return min(list(map(lambda x: sum(x), self.market_value))) # GA 参数设置 class My_nsga(ea.Problem): def __init__(self, dim, df, platform, platform_max): name = 'GA-NET' M = 1 maxormins = [-1] * M Dim = dim varTypes = [1] * Dim lb = [0] * Dim ub = [platform] * Dim lbin = [1] * Dim ubin = [1] * Dim self.count = 1 self.data = Data(df, platform, platform_max) ea.Problem.__init__(self, name, M, maxormins, Dim, varTypes, lb, ub, lbin, ubin) # 目标函数即神经网络返回值 def evalVars(self, Vars): ans = np.zeros(len(Vars), dtype=float).reshape(len(Vars), 1) for i in range(len(Vars)): self.data.load(Vars[i]) ans[i][0] = self.data.getResult() # print(ans.max(axis=0)) return ans # 得到初始化种群方式1 def getProphet(dim, ndind, df, platform, platform_max): prophet = np.zeros(shape=(ndind, dim), dtype=int) x = df['交易量'] for i in range(ndind): # 使得platform尽可能分配均匀 platforms = [i for i in range(platform + 1)] now = [0 for _ in range(platform)] for j in range(i % dim, dim): if not platforms: platforms = [i for i in range(platform + 1)] choice = random.choice(platforms) platforms.remove(choice) if choice != 0: now[choice - 1] += x[j] if now[choice - 1] < platform_max: prophet[i][j] = choice for j in range(0, i % dim): if not platforms: platforms = [i for i in range(platform + 1)] choice = random.choice(platforms) platforms.remove(choice) if choice != 0: now[choice - 1] += x[j] if now[choice - 1] < platform_max: prophet[i][j] = choice return prophet # 得到初始化种群方式2 def getProphet2(dim, ndind, df, platform, platform_max): prophet = np.zeros(shape=(ndind, dim), dtype=int) x = df['交易量'] for i in range(ndind): # 真随机抽取初始化 now = [0 for _ in range(platform)] for j in range(i % dim, dim): choice = random.choice([i for i in range(platform + 1)]) if choice != 0: now[choice - 1] += x[j] if now[choice - 1] < platform_max: prophet[i][j] = choice for j in range(0, i % dim): choice = random.choice([i for i in range(platform + 1)]) if choice != 0: now[choice - 1] += x[j] if now[choice - 1] < platform_max: prophet[i][j] = choice return prophet # 运行 GA def Run_nsga(dim, df, platform, platform_max, loop_id, ndind=30, maxgen=1500): problem = My_nsga(dim, df, platform, platform_max) myAlgorithm = ea.soea_EGA_templet(problem, ea.Population(Encoding='RI', NIND=ndind), MAXGEN=maxgen, logTras=0) prophet = getProphet(dim, ndind, df, platform, platform_max) # 要得到图把 drawing = 1 saveFlag = 1 res = ea.optimize(myAlgorithm, prophet=prophet, seed=1, verbose=False, drawing=1, outputMsg=1, drawLog=1, saveFlag=saveFlag, dirName='result{}'.format(loop_id)) if saveFlag: np.savetxt("./result{}/result.csv".format(i), res['Vars'][0], delimiter=",") return res['ObjV'][0][0] if __name__ == "__main__": df = pd.read_excel("data.xlsx", sheet_name=None, engine='openpyxl') # 设置每张表分成的 platform 数量 platforms = [2, 4, 10, 20, 10, 50, 50, 100] # 设置每张表 platform 最大值 platform_maxs = [2000, 1000, 1500, 800, 500, 100, 416, 208] # platform_maxs[4] = [100, 200, 300, 400, 500, 500, 600, 700, 800, 900] # 设置是否训练表 # is_train = [1, 1, 1, 1, 1, 1, 1, 1] is_train = [1, 1, 1, 1, 0, 0, 0, 0] result = [] result_value = [] values = list(df.values())[:8] pool = Pool(sum(is_train)) for i in range(len(values)): if not is_train[i]: continue # 最后两个参数:种群数量,最大迭代次数 pool.apply_async(Run_nsga, args=(len(values[i]), values[i], platforms[i], platform_maxs[i], i, 500, 1000)) pool.close() pool.join() # now_value = Run_nsga(len(values[i]), values[i], platforms[i], platform_maxs[i], i, 500, 1000) # result_value.append(now_value) # np.savetxt("result_values.csv", result_value, delimiter=",") |
两个编码方式有差异,我个人认为排列编码会比较好一点,逻辑更加高一些。代码中为EGA模板(即带有精英选择机制的遗传算法),测试下来可能NSGA-ii模板会更优一些,可以在测试的时候选择你想要的算法及结果。data.xlsx
图片均压缩<500kb
python多线程因为GIL锁的原因可以说是假的,但是多进程是可以使用多核 cpu。
测试代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
from threading import Thread def loop1(): a = 0 while 1: a += 1 def loop2(a): while 1: a += 1 for i in range(3): t = Thread(target=loop1()) t.start() for i in range(3): t = Thread(target=loop2(), args=(1,)) t.start() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from multiprocessing import Process, Pool def loop1(): a = 0 while 1: a += 1 def loop2(a): while 1: a += 1 pool = Pool(processes=6) for i in range(3): pool.apply_async(loop1) for i in range(3): pool.apply_async(loop2, args=(1,)) pool.close() pool.join() |
测试结果如下:
结果:我们可以看到 threading 多线程库只可以跑满一核 cpu,但是 multiprocessing 多进程库可以跑满 cpu
作为 backup 使用,为 ubuntu server 22.04,首先 sd 卡配置好 config.txt 以及 network-config。树莓派主要用途为 jupyter code-server frp 映射到公网作为计算服务器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
[all] kernel=vmlinuz cmdline=cmdline.txt initramfs initrd.img followkernel [pi4] max_framebuffers=2 arm_boost=1 [all] # Enable the audio output, I2C and SPI interfaces on the GPIO header. As these # parameters related to the base device-tree they must appear *before* any # other dtoverlay= specification dtparam=audio=on dtparam=i2c_arm=on dtparam=spi=on gpu_mem=256 arm_freq=2100 over_voltage=6 arm_freq_min=1500 # Comment out the following line if the edges of the desktop appear outside # the edges of your display disable_overscan=1 # If you have issues with audio, you may try uncommenting the following line # which forces the HDMI output into HDMI mode instead of DVI (which doesn't # support audio output) #hdmi_drive=2 # Enable the serial pins enable_uart=1 # Autoload overlays for any recognized cameras or displays that are attached # to the CSI/DSI ports. Please note this is for libcamera support, *not* for # the legacy camera stack camera_auto_detect=1 display_auto_detect=1 # Config settings specific to arm64 arm_64bit=1 dtoverlay=dwc2 [cm4] # Enable the USB2 outputs on the IO board (assuming your CM4 is plugged into # such a board) dtoverlay=dwc2,dr_mode=host [all] start_x=1 |
1 2 3 4 5 6 7 8 9 10 |
sudo passwd root su - apt update apt upgrade adduser code adduser code-jupyter apt install python3-pip net-tools pip3 config set global.index-url https://pypi.douban.com/simple pip3 install numpy pandas torch torchvision jupyter bs4 requests openpyxl matplotlib scrapy apt install python3-opencv |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
su code-jupyter mkdir code python3 from notebook.auth import passwd passwd() exit() jupyter notebook --generate-config vim .jupyter/jupyter_notebook_config.py c.NotebookApp.ip = '0.0.0.0' c.NotebookApp.password = ****** c.NotebookApp.notebook_dir = '/home/code-jupyter/code' c.NotebookApp.open_browser = False c.NotebookApp.allow_remote_access = True c.NotebookApp.terminals_enabled = False su - vim /etc/systemd/system/jupyter.service systemctl start jupyter systemctl status jupyter systemctl enable jupyter pip3 config set global.index-url https://pypi.douban.com/simple pip3 install jupyter_contrib_nbextensions pip3 install jupyter_nbextensions_configurator jupyter contrib nbextension install --user systemctl restart jupyter.service |
1 2 3 4 5 6 7 8 9 10 11 12 |
cd /home/code wget https://ghproxy.com/https://github.com/coder/code-server/releases/download/v4.5.1/code-server_4.5.1_arm64.deb dpkg -i code-server_4.5.1_arm64.deb rm -f code-server_4.5.1_arm64.deb su code code-server vim .config/code-server/config.yaml su - vim /etc/systemd/system/code.service systemctl start code systemctl status code systemctl enable code |
1 2 3 4 5 6 7 8 9 10 11 12 |
su ubuntu cd /home/ubuntu wget https://ghproxy.com/https://github.com/fatedier/frp/releases/download/v0.44.0/frp_0.44.0_linux_arm64.tar.gz tar -xvzf frp_0.44.0_linux_arm64.tar.gz rm -f frp_0.44.0_linux_arm64.tar.gz mv frp_0.44.0_linux_arm64/ frp/ cd frp/ vim frpc.ini sudo vim /etc/systemd/system/code.service systemctl start code systemctl status code systemctl enable code |
1 2 3 4 5 6 7 8 9 10 |
[Unit] Description=jupyter After=network.target [Service] User=code-jupyter ExecStart=/usr/local/bin/jupyter-notebook [Install] WantedBy=multi-user.target |
1 2 3 4 5 6 7 8 9 10 |
[Unit] Description=code-server After=network.target [Service] User=code ExecStart=/bin/code-server [Install] WantedBy=multi-user.target |
1 2 3 4 5 6 7 8 9 10 |
[Unit] Description=frp After=network.target [Service] User=ubuntu ExecStart=/home/ubuntu/frp/frpc -c /home/ubuntu/frp/frpc.ini [Install] WantedBy=multi-user.target |
浙ICP备2021019730-1 浙公网安备 33010902002953号
Copyright © 2022 PanCake