From 2728e37f6259904f28db68239381f8bd4b223247 Mon Sep 17 00:00:00 2001 From: ninglang Date: Sat, 11 May 2024 13:15:04 +0800 Subject: [PATCH] Site updated: 2024-05-11 13:15:03 --- search.xml | 904 ++++++++++++++++++++++++++--------------------------- 1 file changed, 452 insertions(+), 452 deletions(-) diff --git a/search.xml b/search.xml index 0570017b7..19aa94371 100644 --- a/search.xml +++ b/search.xml @@ -500,61 +500,6 @@ max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]) coding - - 数字电子基础 - /2022/03/14/Digital%20Electronics%20Fundamentals/ - 一些数电不太能理解的地方

- -

反相器

-

COMS电路

-
    -
  • CMOS的输出特性,CMOS受负载的输出高低电平变化
  • -
  • \(V_{OH}\quad V_{OL}...\)的取法(噪声容限)
  • -
  • CMOS门不允许悬空,和地面的电阻相接不影响
  • -
  • CMOS电路的输入端不允许悬空,因为悬空会使电位不定,破坏正常的逻辑关系。另外,悬空时输入阻抗高,易受外界噪声干扰,使电路产生误动作,而且也极易造成栅极感应静电而击穿。
  • -
-

TTL电路

-
    -
  • 扇出系数的概念,计算

  • -
  • 漏电流

  • -
  • \(T_{cd}和T_{pd}\),为什么\(T_{cd}=0\)的时候,将无法信任电平

    -

    因为\(T_{cd}=0\),表面一旦高电平降低,那么电路进入无效区,这时候就需要重新,\(T_{cd}可以变大点\)

  • -
  • 交流噪声容限(是指噪声信号是高频吗?)

  • -
  • TTL电源不允许大幅调整,不允许超过10%

  • -
  • TTL电路输入端通过电阻接地,电阻值R的大小直接影响电路所处的状态。

  • -
-
- - -
-
    -
  • OC门上拉电阻的选择,不能太大,是担心输出高电平的时候不能输出高电平,不能太小,是因为担心输出低电平的时候电流太大损坏管子(最坏的情况,只有一个管子接地),高电平的时候内阻近视为上拉电阻,而低电平的时候内阻相当于管子的阻抗,这时要求内阻较小
  • -
-
- - -
-
    -
  • OC门和OD的优势:电平偏移,也就是说,输出的电平可以通过上拉电阻来调整
  • -
-]]>
- - 电子 - -
专业英语 /2023/06/05/English_profess/ @@ -641,6 +586,92 @@ means:“我们还与以前依赖于手动定义的样式和内容分解或共 code + + 数字电子基础 + /2022/03/14/Digital%20Electronics%20Fundamentals/ + 一些数电不太能理解的地方

+ +

反相器

+

COMS电路

+
    +
  • CMOS的输出特性,CMOS受负载的输出高低电平变化
  • +
  • \(V_{OH}\quad V_{OL}...\)的取法(噪声容限)
  • +
  • CMOS门不允许悬空,和地面的电阻相接不影响
  • +
  • CMOS电路的输入端不允许悬空,因为悬空会使电位不定,破坏正常的逻辑关系。另外,悬空时输入阻抗高,易受外界噪声干扰,使电路产生误动作,而且也极易造成栅极感应静电而击穿。
  • +
+

TTL电路

+
    +
  • 扇出系数的概念,计算

  • +
  • 漏电流

  • +
  • \(T_{cd}和T_{pd}\),为什么\(T_{cd}=0\)的时候,将无法信任电平

    +

    因为\(T_{cd}=0\),表面一旦高电平降低,那么电路进入无效区,这时候就需要重新,\(T_{cd}可以变大点\)

  • +
  • 交流噪声容限(是指噪声信号是高频吗?)

  • +
  • TTL电源不允许大幅调整,不允许超过10%

  • +
  • TTL电路输入端通过电阻接地,电阻值R的大小直接影响电路所处的状态。

  • +
+
+ + +
+
    +
  • OC门上拉电阻的选择,不能太大,是担心输出高电平的时候不能输出高电平,不能太小,是因为担心输出低电平的时候电流太大损坏管子(最坏的情况,只有一个管子接地),高电平的时候内阻近视为上拉电阻,而低电平的时候内阻相当于管子的阻抗,这时要求内阻较小
  • +
+
+ + +
+
    +
  • OC门和OD的优势:电平偏移,也就是说,输出的电平可以通过上拉电阻来调整
  • +
+]]>
+ + 电子 + +
+ + 毛概实践课 + /2022/06/10/Introduction%20to%20Mao%20Zedong%20Thought/ + 毛概实践课🤗

+ +

经过我们小组接近三个星期的努力,没错!它来了,毛概实践课的成果😁,它可能不是那么优秀,但的确是我们努力的成果

+ + + +

看看我们的大合照😁:

+

+

​ +哈哈哈,还希望大家不要吐槽我的眯眯眼😛,当时光线太强,实在睁不开眼,不是故意的哈😜。我的ps技术也不是很好,只能做成这样,还希望大家多多担待

+

​ +这次实践课给我留下印象还是很十分的深刻的,疫情期间,我这边(一个人在学校)要是没有室友们的帮助,估计是完成不了的,视频剪辑的时候,我和邵明禹哥哥把pr工程文件传来传去(每次都是2~3个G),邵明禹哥哥吐槽了n多次网络问题,我这边修改视频的时候,也不知道怎么回事,pr崩了5、6次,还把我的素材直接全部删除了😭,找也找不回,最后只能重新剪辑。

+
+ + +
+

​ +这次大家没能全员出镜,可以算得上团队火力没有全开吧,但是我个人感觉还是不错的,因为在刚开始选题的时候,就想要做一些有思想深度的视频,疫情期间,多个人不能见面的情况下,从个体的角度入手,个体和个体之间用一些手段连接,去拔高整个实践课的深度,我认为是可行的。

+

​ +总之,十分感谢那些和我一起走过这段道路的小伙伴,谢谢你们的付出,也谢谢那些帮助我们完成实践课的各位同学,就如视频所说,你们是冬天里的暖阳😉😉😉😉。

+]]>
+ + 思想觉悟 + +
GANs /2023/06/03/GANs/ @@ -743,131 +774,6 @@ ChatGPT(一):十分钟读懂 Transformer - 知乎

code
- - 毛概实践课 - /2022/06/10/Introduction%20to%20Mao%20Zedong%20Thought/ - 毛概实践课🤗

- -

经过我们小组接近三个星期的努力,没错!它来了,毛概实践课的成果😁,它可能不是那么优秀,但的确是我们努力的成果

- - - -

看看我们的大合照😁:

-

-

​ -哈哈哈,还希望大家不要吐槽我的眯眯眼😛,当时光线太强,实在睁不开眼,不是故意的哈😜。我的ps技术也不是很好,只能做成这样,还希望大家多多担待

-

​ -这次实践课给我留下印象还是很十分的深刻的,疫情期间,我这边(一个人在学校)要是没有室友们的帮助,估计是完成不了的,视频剪辑的时候,我和邵明禹哥哥把pr工程文件传来传去(每次都是2~3个G),邵明禹哥哥吐槽了n多次网络问题,我这边修改视频的时候,也不知道怎么回事,pr崩了5、6次,还把我的素材直接全部删除了😭,找也找不回,最后只能重新剪辑。

-
- - -
-

​ -这次大家没能全员出镜,可以算得上团队火力没有全开吧,但是我个人感觉还是不错的,因为在刚开始选题的时候,就想要做一些有思想深度的视频,疫情期间,多个人不能见面的情况下,从个体的角度入手,个体和个体之间用一些手段连接,去拔高整个实践课的深度,我认为是可行的。

-

​ -总之,十分感谢那些和我一起走过这段道路的小伙伴,谢谢你们的付出,也谢谢那些帮助我们完成实践课的各位同学,就如视频所说,你们是冬天里的暖阳😉😉😉😉。

-]]>
- - 思想觉悟 - -
- - LinkWe Technology - /2021/10/08/LinkWe/ - LinkWe Technology product

- - -]]>
- - Product design - -
- - Pointer - /2021/09/03/Point/ - Review the knowledge of pointers

- -

Pointer Category

-
    -
  • Pointer point the constant:

    -
    const char *name = "chen"    //statement a pointer point a constant
    -

    because using const,so the Pointer can't change variable -in the address which it point ,so the statement as follows is incorrect -:

    -
    name[3]='a'					//incorrect,pointer "name" can't change constant 
    -

    but name is a normal pointer ,so it could change the items it -point,statement as follows are correct:

    -
    name = 'zhang'				//change the address the pointer point ,correct
    -

    Also,Even you have changed your string you point ,you still can't -change the string, Please somebody tell me why ,Thank you !

    -
    name[3]='y'					//incorrect,but I don't know why!
  • -
  • Constant Pointer

    -

    A pointer can't change the address it point ,but it still can change -the content it point,example:

    -
    char *const name ="chen";     //define a constant pointer
    name[3] = 'a'; //correct ,it can change the constent of pointer
    name = "zhang"; //incorrect ,it can't change the address it points
  • -
  • Constant Pointer points to constant

    -

    A constant pointer points a constant ,the address pointer point is -unchangeable and the content of address is unchangeable,example :

    -
    const char *const name="chen";	//define a constant pointer point the constant
    name[3] = 'q'; //incorrect ,the constant is unchangeable
    name ='ninglang'; //incorrect ,the address pointed is unchangeable
  • -
  • Const

    -

    Using a const to define a integer variable ,the keyword -omitted is acceptable the definition as following is same:

    -
    const int LIMITS = 100;
    const LIMITS = 100;
    -

    formal parameters also can be describe by const,for -example:

    -
    int MAX(const int*ptr)
    -

    the method promise the array can't be changed ,only be read.

  • -
-

Pointer array

-
#include<stdio.h>

int main(void){
char *arr[3]={'abc','def','ghi'};
char *pArr = arr[0];
printf("The string array arr's element :");
for(int index=0;index<3;index++){
printf("%s",arr[index]);
}
printf("\n");
printf("The string arr's first element's every element:");
for(int index = 0;index<3;index++){
printf("%c",*(pArr+index));
}
printf("\n");
return 0;
}
-

in fact , you see the definition of Pointer array ,It is like as -follows:

-
char *arr[3]={'abc','def','ghi'};
-
char *pChar1 = 'abc',*pChar2 = 'def',*pChar3='ghi'
char *arr[3] = {pChar1,pChar2,pChar3};
-

At the same time :

-
arr[0] = pChar;   //the arr first element is the pointer pChar
-

and the pChar is pointing the 'abc''s first element 'a', -so we can use the code to print 'a'

-
printf("%c",pChar[0]);   //print 'a'
// print'a','b','c'
for(int i = 0;i<3;i++){
printf("%c ",pChar[i]);
}
-

Pointer Function

-
int fun(int x,int y);			//normal function return integers
-

This function declaration is normal ,but There are some difference in -next function declaration

-
int *fun(int x,int y)
-

This function declaration is pointer function ,the return is a -pointer to int ,This is an address

-

Pointer To Function

-

To state a pointer to function ,which is a Pointer pointing function -.declaration form:

-
int (*fun)(int x,int y)
-

There are two ways to assign values to pointer variables

-
fun = &function;
fun = function;
-

There are also two ways to call pointer to function

-
x=(*fun)();
x=fun();
-

Example:

-
#include<stdio.h>
#include<iostream>
using namespace std;
int add(int x,int y){
return x+y;
}

int sub(int x,int y){
return x-y;
}

int (*fun)(int x,int y);
int main(){
//first kind
fun = add;
cout<<"(*fun)(1,2)="<<(*fun)(1,2)<<endl;
//second kind
fun = &sub;
cout<<"(*fun)(5,3)="<<(*fun)(5,3)<<" "<<fun(5,3);


return 0;

}
-

New and Delete

-

operation new can get a space from heap and return the -pointer to point the first address of the memory,and delete -can free the space

-
int *p;
p = new int ; //new dynamically allocate an integer memory space,assign the first address to pointer variable


delete p; //free the space allocated by new
-

new assign space for multidimensional array:

-
int i = 3;
int *pi = new int[3][4];
-

new assign space with initial value:

-
#include<iostream>
using namespace std;
int main(){
int *p;
p = new int(99); //initial value
cout<<*p;
delete p;
return 0;
}
-

Malloc && Free get space

-]]>
- - coding - -
Neural network(1)---Logistic Regression /2021/08/24/Neural-network(1)---Logistic-Regression/ @@ -1032,20 +938,165 @@ href="http://www.isee.zju.edu.cn/2023/0517/c21109a2758376/page.htm">浙江大学

+

2023年南京大学电子科学与工程学院优秀大学生夏令营 +- 南京大学 - 保研论坛-保研经验分享 - Powered by Discuz!

+

2023年南京大学电子科学与工程学院优秀大学生夏令营报名通知

+

2023年南京大学电子科学与工程学院夏令营报名

+

+]]> + + 生活 + +
+ + Pointer + /2021/09/03/Point/ + Review the knowledge of pointers

+ +

Pointer Category

+
    +
  • Pointer point the constant:

    +
    const char *name = "chen"    //statement a pointer point a constant
    +

    because using const,so the Pointer can't change variable +in the address which it point ,so the statement as follows is incorrect +:

    +
    name[3]='a'					//incorrect,pointer "name" can't change constant 
    +

    but name is a normal pointer ,so it could change the items it +point,statement as follows are correct:

    +
    name = 'zhang'				//change the address the pointer point ,correct
    +

    Also,Even you have changed your string you point ,you still can't +change the string, Please somebody tell me why ,Thank you !

    +
    name[3]='y'					//incorrect,but I don't know why!
  • +
  • Constant Pointer

    +

    A pointer can't change the address it point ,but it still can change +the content it point,example:

    +
    char *const name ="chen";     //define a constant pointer
    name[3] = 'a'; //correct ,it can change the constent of pointer
    name = "zhang"; //incorrect ,it can't change the address it points
  • +
  • Constant Pointer points to constant

    +

    A constant pointer points a constant ,the address pointer point is +unchangeable and the content of address is unchangeable,example :

    +
    const char *const name="chen";	//define a constant pointer point the constant
    name[3] = 'q'; //incorrect ,the constant is unchangeable
    name ='ninglang'; //incorrect ,the address pointed is unchangeable
  • +
  • Const

    +

    Using a const to define a integer variable ,the keyword +omitted is acceptable the definition as following is same:

    +
    const int LIMITS = 100;
    const LIMITS = 100;
    +

    formal parameters also can be describe by const,for +example:

    +
    int MAX(const int*ptr)
    +

    the method promise the array can't be changed ,only be read.

  • +
+

Pointer array

+
#include<stdio.h>

int main(void){
char *arr[3]={'abc','def','ghi'};
char *pArr = arr[0];
printf("The string array arr's element :");
for(int index=0;index<3;index++){
printf("%s",arr[index]);
}
printf("\n");
printf("The string arr's first element's every element:");
for(int index = 0;index<3;index++){
printf("%c",*(pArr+index));
}
printf("\n");
return 0;
}
+

in fact , you see the definition of Pointer array ,It is like as +follows:

+
char *arr[3]={'abc','def','ghi'};
+
char *pChar1 = 'abc',*pChar2 = 'def',*pChar3='ghi'
char *arr[3] = {pChar1,pChar2,pChar3};
+

At the same time :

+
arr[0] = pChar;   //the arr first element is the pointer pChar
+

and the pChar is pointing the 'abc''s first element 'a', +so we can use the code to print 'a'

+
printf("%c",pChar[0]);   //print 'a'
// print'a','b','c'
for(int i = 0;i<3;i++){
printf("%c ",pChar[i]);
}
+

Pointer Function

+
int fun(int x,int y);			//normal function return integers
+

This function declaration is normal ,but There are some difference in +next function declaration

+
int *fun(int x,int y)
+

This function declaration is pointer function ,the return is a +pointer to int ,This is an address

+

Pointer To Function

+

To state a pointer to function ,which is a Pointer pointing function +.declaration form:

+
int (*fun)(int x,int y)
+

There are two ways to assign values to pointer variables

+
fun = &function;
fun = function;
+

There are also two ways to call pointer to function

+
x=(*fun)();
x=fun();
+

Example:

+
#include<stdio.h>
#include<iostream>
using namespace std;
int add(int x,int y){
return x+y;
}

int sub(int x,int y){
return x-y;
}

int (*fun)(int x,int y);
int main(){
//first kind
fun = add;
cout<<"(*fun)(1,2)="<<(*fun)(1,2)<<endl;
//second kind
fun = &sub;
cout<<"(*fun)(5,3)="<<(*fun)(5,3)<<" "<<fun(5,3);


return 0;

}
+

New and Delete

+

operation new can get a space from heap and return the +pointer to point the first address of the memory,and delete +can free the space

+
int *p;
p = new int ; //new dynamically allocate an integer memory space,assign the first address to pointer variable


delete p; //free the space allocated by new
+

new assign space for multidimensional array:

+
int i = 3;
int *pi = new int[3][4];
+

new assign space with initial value:

+
#include<iostream>
using namespace std;
int main(){
int *p;
p = new int(99); //initial value
cout<<*p;
delete p;
return 0;
}
+

Malloc && Free get space

+]]>
+ + coding + +
+ + LinkWe Technology + /2021/10/08/LinkWe/ + LinkWe Technology product

+ + +]]>
+ + Product design + +
+ + Python 机器学习 + /2022/01/19/Python%E5%9B%BE%E5%83%8F%E8%AF%86%E5%88%AB/ + 下面是本人总结的Python机器学习-图像处理的步骤

+ +

环境配置(垃圾分类项目)

+

项目使用树莓派4B,具体流程如下

+
    +
  • 下载win32_disk_imager,烧录镜像,在BOOT分区下建立文件名为SSH的文件,无后缀
  • +
  • 下载PUTTY(记得将后缀it删除)和VNC
  • +
  • 或者使用xshellxftp操作
  • +
  • 使用SSH连接树莓派,树莓派自带Python3和Python2,使用pip配置环境
  • +
+
    +
  1. 修改deb镜像源
  2. +
+
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
+

修改树莓派pip源。请记住,/etc/pip.conf,里面默认是

+
[global]
extra-index-url=https://www.piwheels.org/simple
+

请不要修改,这个网址专为树莓定制

+

pip永久换源(win和linux通用)

+
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
+

linux里面的永久源的地址为/home/pi/.config/pip/pip.conf,需要修改可以直接编辑

+

也可以这样换源

+
mkdir ~/.pip
vim ~/.pip/pip.conf
+
[global]
timeout=100
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url= http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=
pypi.tuna.tsinghua.edu.cn
mirrors.aliyun.com
+

这里的trusted-host只有当链接不是https时需要,即将阿里云的网站改为

+
extra-index-url= https://mirrors.aliyun.com/pypi/simple
+

ubuntu配置opencv

+
    +
  • 网上所有的教程都无法直接成功,所以,个人的踩坑经历
  • +
  • 首先去opencv-python下载对应Python版本的opencv
  • +
  • 然后移入树莓派中,使用pip3 install ...whl即可成功
  • +
  • 很多的包都有这个特点,所以,要注意这个点,当自己的树莓派无法安装时,可以考虑这个专门为树莓派做的网站
-

2023年南京大学电子科学与工程学院优秀大学生夏令营 -- 南京大学 - 保研论坛-保研经验分享 - Powered by Discuz!

-

2023年南京大学电子科学与工程学院优秀大学生夏令营报名通知

-

2023年南京大学电子科学与工程学院夏令营报名

-

+

安装多线程工具axel

+
apt-get install axel
+

下载方式

+
axel 参数 文件下载地址
常用可选参数:
-s 设置最大下载速度,如果限制到512KB/s,则填写512000
-n 指定连接数
-o 指定另存为目录,或者指定的目录+文件名
-H 指定header
-U 指定useragent
-q 静默模式
-a 更改默认进度条样式

eg:
axel -n 30 http://archive.cloudera.com/cm5/cm/5/cloudera-manager-centos7-cm5.15.2_x86_64.tar.gz
]]>
- 生活 + coding
@@ -1166,57 +1217,6 @@ href="https://pypi.org/project/torch/1.8.0/#modal-close">torch-wheel下载 Code - - Python 机器学习 - /2022/01/19/Python%E5%9B%BE%E5%83%8F%E8%AF%86%E5%88%AB/ - 下面是本人总结的Python机器学习-图像处理的步骤

- -

环境配置(垃圾分类项目)

-

项目使用树莓派4B,具体流程如下

-
    -
  • 下载win32_disk_imager,烧录镜像,在BOOT分区下建立文件名为SSH的文件,无后缀
  • -
  • 下载PUTTY(记得将后缀it删除)和VNC
  • -
  • 或者使用xshellxftp操作
  • -
  • 使用SSH连接树莓派,树莓派自带Python3和Python2,使用pip配置环境
  • -
-
    -
  1. 修改deb镜像源
  2. -
-
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi
-

修改树莓派pip源。请记住,/etc/pip.conf,里面默认是

-
[global]
extra-index-url=https://www.piwheels.org/simple
-

请不要修改,这个网址专为树莓定制

-

pip永久换源(win和linux通用)

-
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
-

linux里面的永久源的地址为/home/pi/.config/pip/pip.conf,需要修改可以直接编辑

-

也可以这样换源

-
mkdir ~/.pip
vim ~/.pip/pip.conf
-
[global]
timeout=100
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url= http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=
pypi.tuna.tsinghua.edu.cn
mirrors.aliyun.com
-

这里的trusted-host只有当链接不是https时需要,即将阿里云的网站改为

-
extra-index-url= https://mirrors.aliyun.com/pypi/simple
-

ubuntu配置opencv

-
    -
  • 网上所有的教程都无法直接成功,所以,个人的踩坑经历
  • -
  • 首先去opencv-python下载对应Python版本的opencv
  • -
  • 然后移入树莓派中,使用pip3 install ...whl即可成功
  • -
  • 很多的包都有这个特点,所以,要注意这个点,当自己的树莓派无法安装时,可以考虑这个专门为树莓派做的网站
  • -
-

安装多线程工具axel

-
apt-get install axel
-

下载方式

-
axel 参数 文件下载地址
常用可选参数:
-s 设置最大下载速度,如果限制到512KB/s,则填写512000
-n 指定连接数
-o 指定另存为目录,或者指定的目录+文件名
-H 指定header
-U 指定useragent
-q 静默模式
-a 更改默认进度条样式

eg:
axel -n 30 http://archive.cloudera.com/cm5/cm/5/cloudera-manager-centos7-cm5.15.2_x86_64.tar.gz
-]]>
- - coding - -
Qt /2021/09/20/Qt1/ @@ -1274,6 +1274,16 @@ href="https://www.piwheels.org/project/opencv-python/">opencv-python下载 coding + + 学业生涯规划指导经验交流贴 + /2023/10/31/Study%20exchange%20meeting/ + 欢迎交流

+ +]]>
+ + 学习 + +
DSP /2022/03/14/Signals%20and%20Systems/ @@ -1389,16 +1399,6 @@ src="https://picturnl.oss-cn-shanghai.aliyuncs.com/20210202145703.png" />

coding
- - 学业生涯规划指导经验交流贴 - /2023/10/31/Study%20exchange%20meeting/ - 欢迎交流

- -]]>
- - 学习 - -
出租车载客模型 /2021/08/15/Taxi%20passenger%20model/ @@ -1627,88 +1627,6 @@ W_{\text {long }}=\frac{2 W_{L}+W_{\text {city }}(T)}{2 L / v_{\text

计算即可

代码

-]]> - - Math model - -
- - 评价类数学模型总结 - /2022/01/22/common_model/ - 小小总结,希望以后对自己有帮助

- -

权重系数确定的方法

-

题设:n个评价对象,m个评价指标观测值为 \[ -a_{ij}\quad(i=1,2,...n;j=1,2,..,m) -\]

-

\[ -\begin{bmatrix}a_{11} & a_{12}&...&a_{1,m}\\\\a_{21} & -a_{22}&...&a_{2m}\\ \vdots&...&...&\\a_{n1} & -a_{n2}&...&a_{nm}\end{bmatrix} -\]

-

均方差法

-

\[ -\mu_j=\frac{1}{n}\sum_{i=1}^{n}a_{ij}\qquad -s_j=\sqrt{\frac{1}{n}(a_{ij}-\mu_j)^2}\\ -\]

-

\[ -\begin{bmatrix}a_{11}\\a_{21}\\\vdots\\a_{n1}\end{bmatrix}\cdots\begin{bmatrix}a_{1m}\\a_{2m}\\\vdots\\a_{nm}\end{bmatrix}\longrightarrow\begin{bmatrix}\mu_1&\cdots\mu_m\end{bmatrix}\longrightarrow -\begin{bmatrix}s_1&\cdots&s_m\end{bmatrix}\\ -\]

-

\[ -w_j=\frac{s_j}{\sum_{k=1}^{m}s_k}(j=1,2,3,4...m)\\ -\]

-

\[ -\begin{bmatrix}w_1& w_2&\cdots w_m\end{bmatrix} -\]

-

极差法

-

\[ -r_{j}=\max _{1 \leq i<k \leq n}\left\{\left|a_{i j}-a_{k -j}\right|\right\}(j=1,2, \cdots, m)\\ -\]

-

\[ -\max_{every -element}\begin{bmatrix}rand(a_{i1}-a_{k1})&\cdots&rand(a_{ij}-a_{kj})\end{bmatrix}\longrightarrow\begin{bmatrix}r_1&r_2&\cdots&r_m\end{bmatrix} -\]

-

所以第\(j\)项指标的权重系数为 \[ -w_{j}=\frac{s_{j}}{\sum_{l=1}^{m} r_{k}}(j=1,2, \cdots, m) -\]

-

熵值法

-

特征比重:

-

\[\mu_j=\sum_{i=1}^{n} a_{i -j}>0\],第\(j\)项指标的特征比重为 \[ -p_{i j}=\frac{a_{i j}}{\sum_{i=1}^{n} a_{i j}}(i=1,2, \cdots, n ; j=1,2, -\cdots, m) -\]

-

\[ -\begin{bmatrix}a_{11}\\a_{21}\\\vdots\\a_{n1}\end{bmatrix}\cdots\begin{bmatrix}a_{1m}\\a_{2m}\\\vdots\\a_{nm}\end{bmatrix}\longrightarrow\begin{bmatrix}\mu_1&\cdots\mu_m\end{bmatrix}\longrightarrow\begin{bmatrix}p_{11}&\cdots&p_{1m}\\p_{21}&\cdots&p_{2m}\\\vdots&\cdots&\\p_{n1}&\cdots&p_{nm}\end{bmatrix} -\]

-

\(j\)项的熵值为: \[ -e_{j}=-\frac{1}{\ln n} \sum_{i=1}^{n} p_{i j} \ln p_{i j}(j=1,2, \cdots, -m) -\]

-

\[ -\begin{bmatrix}p_{11}&\cdots&p_{1m}\\p_{21}&\cdots&p_{2m}\\\vdots&\cdots&\\p_{n1}&\cdots&p_{nm}\end{bmatrix}\longrightarrow\begin{bmatrix}e_1&\cdots&e_m\end{bmatrix} -\]

-

不难看出,如果第\(j\)项指标的观测值差异越大,熵值越小;反之,熵值越大。

-

计算第\(j\)项指标的差异系数为 \[ -g_{j}=1-e_{j}(j=1,2, \cdots, m) -\] 如果第项指标的观测值差异越大,则差异系数\(g\)就越大,第\(j\)项指标也就越重要。

-

\(j\)项的权重系数为 \[ -w_{j}=\frac{g_{j}}{\sum_{k=1}^{m} g_{k}}(j=1,2, \cdots, m) -\] 参考文章:

-

熵的定义信息熵

]]>
Math model @@ -2131,38 +2049,85 @@ class="math inline">\(O(1)\)
- Docker-ubuntu安装ssh - /2024/01/22/docker-ubuntu-ssh%E5%AE%89%E8%A3%85/ - 这是一篇有关Docker-ubuntu-ssh的安装教程

+ 评价类数学模型总结 + /2022/01/22/common_model/ + 小小总结,希望以后对自己有帮助

-

STEP1:安装openssh-server

-

1.更新源

-
apt update
-

2.安装openssh-server

-
apt install openssh-server
-

对于Alpine-linux,则用下面命令安装

-
apk add --update openssh-server
-

还需安装openrc

-
apk add openrc
-

STEP2:设置允许root登录

-

设置/etc/ssh/sshd_config

-

追加如下指令

-

-- PermitRootLogin yes

-

NGINX-Docker无法使用apt问题

-

:star:先换源

-
cd /etc/apt
-

生成source文件

-
touch  sources.list
-

写入源

-
echo "deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free" >>sources.list
echo "deb http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free" >>sources.list
-

删除缓存

-
rm -fR /var/lib/apt/lists/*
-

在更新

-
apt-get update
-
while true; do
ping -c 1 -v6 -w 1 www.baidu.com > /dev/null
sleep 15
done &
+

权重系数确定的方法

+

题设:n个评价对象,m个评价指标观测值为 \[ +a_{ij}\quad(i=1,2,...n;j=1,2,..,m) +\]

+

\[ +\begin{bmatrix}a_{11} & a_{12}&...&a_{1,m}\\\\a_{21} & +a_{22}&...&a_{2m}\\ \vdots&...&...&\\a_{n1} & +a_{n2}&...&a_{nm}\end{bmatrix} +\]

+

均方差法

+

\[ +\mu_j=\frac{1}{n}\sum_{i=1}^{n}a_{ij}\qquad +s_j=\sqrt{\frac{1}{n}(a_{ij}-\mu_j)^2}\\ +\]

+

\[ +\begin{bmatrix}a_{11}\\a_{21}\\\vdots\\a_{n1}\end{bmatrix}\cdots\begin{bmatrix}a_{1m}\\a_{2m}\\\vdots\\a_{nm}\end{bmatrix}\longrightarrow\begin{bmatrix}\mu_1&\cdots\mu_m\end{bmatrix}\longrightarrow +\begin{bmatrix}s_1&\cdots&s_m\end{bmatrix}\\ +\]

+

\[ +w_j=\frac{s_j}{\sum_{k=1}^{m}s_k}(j=1,2,3,4...m)\\ +\]

+

\[ +\begin{bmatrix}w_1& w_2&\cdots w_m\end{bmatrix} +\]

+

极差法

+

\[ +r_{j}=\max _{1 \leq i<k \leq n}\left\{\left|a_{i j}-a_{k +j}\right|\right\}(j=1,2, \cdots, m)\\ +\]

+

\[ +\max_{every +element}\begin{bmatrix}rand(a_{i1}-a_{k1})&\cdots&rand(a_{ij}-a_{kj})\end{bmatrix}\longrightarrow\begin{bmatrix}r_1&r_2&\cdots&r_m\end{bmatrix} +\]

+

所以第\(j\)项指标的权重系数为 \[ +w_{j}=\frac{s_{j}}{\sum_{l=1}^{m} r_{k}}(j=1,2, \cdots, m) +\]

+

熵值法

+

特征比重:

+

\[\mu_j=\sum_{i=1}^{n} a_{i +j}>0\],第\(j\)项指标的特征比重为 \[ +p_{i j}=\frac{a_{i j}}{\sum_{i=1}^{n} a_{i j}}(i=1,2, \cdots, n ; j=1,2, +\cdots, m) +\]

+

\[ +\begin{bmatrix}a_{11}\\a_{21}\\\vdots\\a_{n1}\end{bmatrix}\cdots\begin{bmatrix}a_{1m}\\a_{2m}\\\vdots\\a_{nm}\end{bmatrix}\longrightarrow\begin{bmatrix}\mu_1&\cdots\mu_m\end{bmatrix}\longrightarrow\begin{bmatrix}p_{11}&\cdots&p_{1m}\\p_{21}&\cdots&p_{2m}\\\vdots&\cdots&\\p_{n1}&\cdots&p_{nm}\end{bmatrix} +\]

+

\(j\)项的熵值为: \[ +e_{j}=-\frac{1}{\ln n} \sum_{i=1}^{n} p_{i j} \ln p_{i j}(j=1,2, \cdots, +m) +\]

+

\[ +\begin{bmatrix}p_{11}&\cdots&p_{1m}\\p_{21}&\cdots&p_{2m}\\\vdots&\cdots&\\p_{n1}&\cdots&p_{nm}\end{bmatrix}\longrightarrow\begin{bmatrix}e_1&\cdots&e_m\end{bmatrix} +\]

+

不难看出,如果第\(j\)项指标的观测值差异越大,熵值越小;反之,熵值越大。

+

计算第\(j\)项指标的差异系数为 \[ +g_{j}=1-e_{j}(j=1,2, \cdots, m) +\] 如果第项指标的观测值差异越大,则差异系数\(g\)就越大,第\(j\)项指标也就越重要。

+

\(j\)项的权重系数为 \[ +w_{j}=\frac{g_{j}}{\sum_{k=1}^{m} g_{k}}(j=1,2, \cdots, m) +\] 参考文章:

+

熵的定义信息熵

]]>
- Code + Math model
@@ -2196,6 +2161,41 @@ class="math inline">\(O(1)\)
if [ -f /root/script/ping.sh ]; then
/root/script/ping.sh
fi

更新一下

rc-update add local
+]]> + + Code + +
+ + Docker-ubuntu安装ssh + /2024/01/22/docker-ubuntu-ssh%E5%AE%89%E8%A3%85/ + 这是一篇有关Docker-ubuntu-ssh的安装教程

+ +

STEP1:安装openssh-server

+

1.更新源

+
apt update
+

2.安装openssh-server

+
apt install openssh-server
+

对于Alpine-linux,则用下面命令安装

+
apk add --update openssh-server
+

还需安装openrc

+
apk add openrc
+

STEP2:设置允许root登录

+

设置/etc/ssh/sshd_config

+

追加如下指令

+

-- PermitRootLogin yes

+

NGINX-Docker无法使用apt问题

+

:star:先换源

+
cd /etc/apt
+

生成source文件

+
touch  sources.list
+

写入源

+
echo "deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free" >>sources.list
echo "deb http://mirrors.ustc.edu.cn/debian stable-updates main contrib non-free" >>sources.list
+

删除缓存

+
rm -fR /var/lib/apt/lists/*
+

在更新

+
apt-get update
+
while true; do
ping -c 1 -v6 -w 1 www.baidu.com > /dev/null
sleep 15
done &
]]>
Code @@ -2349,6 +2349,68 @@ verbose详细显示,-f file文件属性)

3.进入onlyoffice修改etc/onlyoffice/documentserver/default.json,修改如下

"rejectUnauthorized": false



"allowPrivateIPAddress": true,
"allowMetaIPAddress": true

重启docker

+]]> + + Code + +
+ + ikuai搭建seafile-docker + /2024/01/20/seafile6.3.4%E7%88%B1%E5%BF%AB%E7%B3%BB%E7%BB%9F%E6%90%AD%E5%BB%BA%E6%95%99%E7%A8%8B/ + 这是一篇有关使用ikuai搭建6.3.4的博客,并且实现了ipv6的访问

+ +

step1: 安装seafile-docker

+

下载seafileltd/seafile:latest镜像

+
+ + +
+

在容器设置中挂载目录

+
    +
  • /shared
  • +
+

如下环境变量设置账号和密码

+
    +
  • SEAFILE_ADMIN_EMAIL
  • +
  • SEAFILE_ADMIN_PASSWORD
  • +
+

step2:安装 iputils-ping

+

因为docker中的ipv6如果不往外界发出信号,路由是无法知道该容器的ipv6地址,所以需要安装ping工具每隔一段时间不断发包,表示心跳

+

1.更新apt包

+
apt update
+

2.安装iputils-ping

+
apt-get install -y iputils-ping
+

3.测试ping百度

+
ping -6 -c 1 www.baidu.com
+

3.编写/root/script/ping.sh

+
while true; do
ping -c 1 -6 www.baidu.com > /dev/null
sleep 15
done &
+

4.修改ping.sh为可执行文件

+
chmod +x /root/script/ping.sh
+

5.测试脚本执行情况:如下执行成功

+

6.修改自启动脚本

+

/root/.bashrc中追加

+
if [ -f /root/script/ping.sh ]; then
/root/script/ping.sh
fi
+

一般而言,由于seafile不自动开bash,因此,建议将上述加入.bashrc加入到seafile-server-latest/seafile.sh中,添加到echo "Seafile server started"的前面.

+

step3:设置nginx反向代理

+

1.创建/etc/nginx/conf.d/seafile.conf,内容如下

+
server {
listen [::]:80;
server_name seafile.ninglang.fun;

proxy_set_header X-Forwarded-For $remote_addr;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_read_timeout 1200s;

# used for view/edit office file via Office Online Server
client_max_body_size 0;

access_log /var/log/nginx/seahub.access.log;
error_log /var/log/nginx/seahub.error.log;
}


location /seafhttp {
rewrite ^/seafhttp(.*)$ $1 break;
proxy_pass http://127.0.0.1:8082;
client_max_body_size 0;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 36000s;
proxy_read_timeout 36000s;
proxy_send_timeout 36000s;

send_timeout 36000s;
}

location /seafdav {
fastcgi_pass 127.0.0.1:8080;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
client_max_body_size 0;
proxy_connect_timeout 36000s;
proxy_read_timeout 36000s;
proxy_send_timeout 36000s;
send_timeout 36000s;

# This option is only available for Nginx >= 1.8.0. See more details below.
proxy_request_buffering off;
access_log /var/log/nginx/seafdav.access.log;
error_log /var/log/nginx/seafdav.error.log;
}

}

+

2.测试文件格式正常

+
nginx -t

#nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
#nginx: configuration file /etc/nginx/nginx.conf test is successful
+

重启docker容器

+

step4:设置seafile

+

修改SERVICE_URLFILE_SERVER_ROOT

+

SERVICE_URL:http://seafile.ninglang.fun

+

FILE_SERVER_ROOT:http://seafile.ninglang.fun/seafhttp

+

step5:设置seafdav

+

设置/conf/seafdav.conf中为

+
enabled = true
port = 8080
fastcgi = true
share_name = /seafdav
+

在手机端设置

+

网络地址:seafile.ninglang.fun/seafdav

+

账号密码如实填写

]]>
Code @@ -2484,95 +2546,6 @@ href="https://blog.csdn.net/m0_51961114/article/details/120699238">(34条消息) code
- - ikuai搭建seafile-docker - /2024/01/20/seafile6.3.4%E7%88%B1%E5%BF%AB%E7%B3%BB%E7%BB%9F%E6%90%AD%E5%BB%BA%E6%95%99%E7%A8%8B/ - 这是一篇有关使用ikuai搭建6.3.4的博客,并且实现了ipv6的访问

- -

step1: 安装seafile-docker

-

下载seafileltd/seafile:latest镜像

-
- - -
-

在容器设置中挂载目录

-
    -
  • /shared
  • -
-

如下环境变量设置账号和密码

-
    -
  • SEAFILE_ADMIN_EMAIL
  • -
  • SEAFILE_ADMIN_PASSWORD
  • -
-

step2:安装 iputils-ping

-

因为docker中的ipv6如果不往外界发出信号,路由是无法知道该容器的ipv6地址,所以需要安装ping工具每隔一段时间不断发包,表示心跳

-

1.更新apt包

-
apt update
-

2.安装iputils-ping

-
apt-get install -y iputils-ping
-

3.测试ping百度

-
ping -6 -c 1 www.baidu.com
-

3.编写/root/script/ping.sh

-
while true; do
ping -c 1 -6 www.baidu.com > /dev/null
sleep 15
done &
-

4.修改ping.sh为可执行文件

-
chmod +x /root/script/ping.sh
-

5.测试脚本执行情况:如下执行成功

-

6.修改自启动脚本

-

/root/.bashrc中追加

-
if [ -f /root/script/ping.sh ]; then
/root/script/ping.sh
fi
-

一般而言,由于seafile不自动开bash,因此,建议将上述加入.bashrc加入到seafile-server-latest/seafile.sh中,添加到echo "Seafile server started"的前面.

-

step3:设置nginx反向代理

-

1.创建/etc/nginx/conf.d/seafile.conf,内容如下

-
server {
listen [::]:80;
server_name seafile.ninglang.fun;

proxy_set_header X-Forwarded-For $remote_addr;

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_read_timeout 1200s;

# used for view/edit office file via Office Online Server
client_max_body_size 0;

access_log /var/log/nginx/seahub.access.log;
error_log /var/log/nginx/seahub.error.log;
}


location /seafhttp {
rewrite ^/seafhttp(.*)$ $1 break;
proxy_pass http://127.0.0.1:8082;
client_max_body_size 0;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 36000s;
proxy_read_timeout 36000s;
proxy_send_timeout 36000s;

send_timeout 36000s;
}

location /seafdav {
fastcgi_pass 127.0.0.1:8080;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
client_max_body_size 0;
proxy_connect_timeout 36000s;
proxy_read_timeout 36000s;
proxy_send_timeout 36000s;
send_timeout 36000s;

# This option is only available for Nginx >= 1.8.0. See more details below.
proxy_request_buffering off;
access_log /var/log/nginx/seafdav.access.log;
error_log /var/log/nginx/seafdav.error.log;
}

}

-

2.测试文件格式正常

-
nginx -t

#nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
#nginx: configuration file /etc/nginx/nginx.conf test is successful
-

重启docker容器

-

step4:设置seafile

-

修改SERVICE_URLFILE_SERVER_ROOT

-

SERVICE_URL:http://seafile.ninglang.fun

-

FILE_SERVER_ROOT:http://seafile.ninglang.fun/seafhttp

-

step5:设置seafdav

-

设置/conf/seafdav.conf中为

-
enabled = true
port = 8080
fastcgi = true
share_name = /seafdav
-

在手机端设置

-

网络地址:seafile.ninglang.fun/seafdav

-

账号密码如实填写

-]]>
- - Code - -
- - 遗传算法 - /2022/02/20/yichuan/ - 遗传算法的初步了解

- -

TSP问题为例,遗传算法的流程图

-

img

-

首先创建最初种群

-

配置环境

-
import numpy as np
import config as conf
from ga import Ga
import matplotlib.pyplot as plt
config = conf.get_config()
-

计算适应度

-
def build_dist_mat(input_list):
n = config.city_num
print("input_list",input_list)
dist_mat = np.zeros([n, n])
for i in range(n):
for j in range(i + 1, n):
d = input_list[i, :] - input_list[j, :]
print("d",d)
# 计算点积
dist_mat[i, j] = np.dot(d, d)
dist_mat[j, i] = dist_mat[i, j]
return dist_mat
-

创建最初种群

-
# 城市坐标
city_pos_list = np.random.rand(config.city_num, config.pos_dimension)
# 城市距离矩阵
city_dist_mat = build_dist_mat(city_pos_list)
-

基因配置

-
gene_len = config.city_num  #基因长度,就是城市的数量
individual_num = config.individual_num #每次个体的数量
gen_num = config.gen_num #遗传代数
mutate_prob = config.mutate_prob #突变率
-

数组深拷贝

-
def copy_list(old_arr: [int]):
new_arr = []
for element in old_arr:
new_arr.append(element)
return new_arr
-

种群中的一个个体,一个个体就是TSP的一个路线

-
class Individual:
def __init__(self, genes=None):
# 随机生成序列
if genes is None:
genes = [i for i in range(gene_len)]#如果是初代,那么路线则为默认12345
random.shuffle(genes)#然后打乱路线
self.genes = genes
self.fitness = self.evaluate_fitness()
def evaluate_fitness(self): #计算路线的长度
# 计算个体适应度
fitness = 0.0
for i in range(gene_len - 1):
# 起始城市和目标城市
from_idx = self.genes[i]
to_idx = self.genes[i + 1]
fitness += city_dist_mat[from_idx, to_idx]
# 连接首尾
fitness += city_dist_mat[self.genes[-1], self.genes[0]]
return fitness

-

GA算法

-
class Ga:
def __init__(self, input_):
global city_dist_mat
city_dist_mat = input_
self.best = None # 每一代的最佳个体
self.individual_list = [] # 每一代的个体列表
self.result_list = [] # 每一代对应的解
self.fitness_list = [] # 每一代对应的适应度

def cross(self): #交叉
new_gen = [] #新一代
random.shuffle(self.individual_list)
for i in range(0, individual_num - 1, 2):
# 父代基因
genes1 = copy_list(self.individual_list[i].genes)
genes2 = copy_list(self.individual_list[i + 1].genes)
index1 = random.randint(0, gene_len - 2)
index2 = random.randint(index1, gene_len - 1)
pos1_recorder = {value: idx for idx, value in enumerate(genes1)}
pos2_recorder = {value: idx for idx, value in enumerate(genes2)}
#键是原来的数,值是该数在数组的位置

# 交叉
for j in range(index1, index2):
value1, value2 = genes1[j], genes2[j] #取出j位置的该值
pos1, pos2 = pos1_recorder[value2], pos2_recorder[value1]#取出相反该值的位置
#两个基因交换
genes1[j], genes1[pos1] = genes1[pos1], genes1[j]
genes2[j], genes2[pos2] = genes2[pos2], genes2[j]
pos1_recorder[value1], pos1_recorder[value2] = pos1, j
pos2_recorder[value1], pos2_recorder[value2] = j, pos2
new_gen.append(Individual(genes1))
new_gen.append(Individual(genes2))
return new_gen

def mutate(self, new_gen): #变异
for individual in new_gen:
if random.random() < mutate_prob:
# 翻转切片
old_genes = copy_list(individual.genes)
index1 = random.randint(0, gene_len - 2)
index2 = random.randint(index1, gene_len - 1)
genes_mutate = old_genes[index1:index2]
genes_mutate.reverse()
individual.genes = old_genes[:index1] + genes_mutate + old_genes[index2:]
# 两代合并
self.individual_list += new_gen

def select(self): #选择
# 锦标赛
group_num = 10 # 小组数
group_size = 10 # 每小组人数
group_winner = individual_num // group_num # 每小组获胜人数
winners = [] # 锦标赛结果
for i in range(group_num):
group = []
for j in range(group_size):
# 随机组成小组
player = random.choice(self.individual_list)
player = Individual(player.genes)
group.append(player)
group = Ga.rank(group)
# 取出获胜者
winners += group[:group_winner]
self.individual_list = winners

@staticmethod
def rank(group):
# 冒泡排序
for i in range(1, len(group)):
for j in range(0, len(group) - i):
if group[j].fitness > group[j + 1].fitness:
group[j], group[j + 1] = group[j + 1], group[j]
return group

def next_gen(self):
# 交叉
new_gen = self.cross()
# 变异
self.mutate(new_gen)
# 选择
self.select()
# 获得这一代的结果
for individual in self.individual_list:
if individual.fitness < self.best.fitness:
self.best = individual

def train(self):
# 初代种群
self.individual_list = [Individual() for _ in range(individual_num)] #初始化初代种群
#individual里面genes只有数字的数组


self.best = self.individual_list[0]
# 迭代
for i in range(gen_num):
self.next_gen()
# 连接首尾
result = copy_list(self.best.genes)
result.append(result[0])
self.result_list.append(result)
self.fitness_list.append(self.best.fitness)
return self.result_list, self.fitness_list
-]]>
- - coding - -
时间预测模型 /2022/02/20/%E6%97%B6%E9%97%B4%E9%A2%84%E6%B5%8B%E6%A8%A1%E5%9E%8B/ @@ -2647,6 +2620,33 @@ class="math display">\[ Math Model + + 遗传算法 + /2022/02/20/yichuan/ + 遗传算法的初步了解

+ +

TSP问题为例,遗传算法的流程图

+

img

+

首先创建最初种群

+

配置环境

+
import numpy as np
import config as conf
from ga import Ga
import matplotlib.pyplot as plt
config = conf.get_config()
+

计算适应度

+
def build_dist_mat(input_list):
n = config.city_num
print("input_list",input_list)
dist_mat = np.zeros([n, n])
for i in range(n):
for j in range(i + 1, n):
d = input_list[i, :] - input_list[j, :]
print("d",d)
# 计算点积
dist_mat[i, j] = np.dot(d, d)
dist_mat[j, i] = dist_mat[i, j]
return dist_mat
+

创建最初种群

+
# 城市坐标
city_pos_list = np.random.rand(config.city_num, config.pos_dimension)
# 城市距离矩阵
city_dist_mat = build_dist_mat(city_pos_list)
+

基因配置

+
gene_len = config.city_num  #基因长度,就是城市的数量
individual_num = config.individual_num #每次个体的数量
gen_num = config.gen_num #遗传代数
mutate_prob = config.mutate_prob #突变率
+

数组深拷贝

+
def copy_list(old_arr: [int]):
new_arr = []
for element in old_arr:
new_arr.append(element)
return new_arr
+

种群中的一个个体,一个个体就是TSP的一个路线

+
class Individual:
def __init__(self, genes=None):
# 随机生成序列
if genes is None:
genes = [i for i in range(gene_len)]#如果是初代,那么路线则为默认12345
random.shuffle(genes)#然后打乱路线
self.genes = genes
self.fitness = self.evaluate_fitness()
def evaluate_fitness(self): #计算路线的长度
# 计算个体适应度
fitness = 0.0
for i in range(gene_len - 1):
# 起始城市和目标城市
from_idx = self.genes[i]
to_idx = self.genes[i + 1]
fitness += city_dist_mat[from_idx, to_idx]
# 连接首尾
fitness += city_dist_mat[self.genes[-1], self.genes[0]]
return fitness

+

GA算法

+
class Ga:
def __init__(self, input_):
global city_dist_mat
city_dist_mat = input_
self.best = None # 每一代的最佳个体
self.individual_list = [] # 每一代的个体列表
self.result_list = [] # 每一代对应的解
self.fitness_list = [] # 每一代对应的适应度

def cross(self): #交叉
new_gen = [] #新一代
random.shuffle(self.individual_list)
for i in range(0, individual_num - 1, 2):
# 父代基因
genes1 = copy_list(self.individual_list[i].genes)
genes2 = copy_list(self.individual_list[i + 1].genes)
index1 = random.randint(0, gene_len - 2)
index2 = random.randint(index1, gene_len - 1)
pos1_recorder = {value: idx for idx, value in enumerate(genes1)}
pos2_recorder = {value: idx for idx, value in enumerate(genes2)}
#键是原来的数,值是该数在数组的位置

# 交叉
for j in range(index1, index2):
value1, value2 = genes1[j], genes2[j] #取出j位置的该值
pos1, pos2 = pos1_recorder[value2], pos2_recorder[value1]#取出相反该值的位置
#两个基因交换
genes1[j], genes1[pos1] = genes1[pos1], genes1[j]
genes2[j], genes2[pos2] = genes2[pos2], genes2[j]
pos1_recorder[value1], pos1_recorder[value2] = pos1, j
pos2_recorder[value1], pos2_recorder[value2] = j, pos2
new_gen.append(Individual(genes1))
new_gen.append(Individual(genes2))
return new_gen

def mutate(self, new_gen): #变异
for individual in new_gen:
if random.random() < mutate_prob:
# 翻转切片
old_genes = copy_list(individual.genes)
index1 = random.randint(0, gene_len - 2)
index2 = random.randint(index1, gene_len - 1)
genes_mutate = old_genes[index1:index2]
genes_mutate.reverse()
individual.genes = old_genes[:index1] + genes_mutate + old_genes[index2:]
# 两代合并
self.individual_list += new_gen

def select(self): #选择
# 锦标赛
group_num = 10 # 小组数
group_size = 10 # 每小组人数
group_winner = individual_num // group_num # 每小组获胜人数
winners = [] # 锦标赛结果
for i in range(group_num):
group = []
for j in range(group_size):
# 随机组成小组
player = random.choice(self.individual_list)
player = Individual(player.genes)
group.append(player)
group = Ga.rank(group)
# 取出获胜者
winners += group[:group_winner]
self.individual_list = winners

@staticmethod
def rank(group):
# 冒泡排序
for i in range(1, len(group)):
for j in range(0, len(group) - i):
if group[j].fitness > group[j + 1].fitness:
group[j], group[j + 1] = group[j + 1], group[j]
return group

def next_gen(self):
# 交叉
new_gen = self.cross()
# 变异
self.mutate(new_gen)
# 选择
self.select()
# 获得这一代的结果
for individual in self.individual_list:
if individual.fitness < self.best.fitness:
self.best = individual

def train(self):
# 初代种群
self.individual_list = [Individual() for _ in range(individual_num)] #初始化初代种群
#individual里面genes只有数字的数组


self.best = self.individual_list[0]
# 迭代
for i in range(gen_num):
self.next_gen()
# 连接首尾
result = copy_list(self.best.genes)
result.append(result[0])
self.result_list.append(result)
self.fitness_list.append(self.best.fitness)
return self.result_list, self.fitness_list
+]]>
+ + coding + +
卷积神经网络 /2023/06/25/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/