Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
hydraulicModel
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
刘乐
hydraulicModel
Commits
4805137f
Commit
4805137f
authored
Oct 23, 2020
by
刘乐
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1,算法修改
parent
561c5284
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
232 additions
and
23 deletions
+232
-23
main.cpp
funcDemo/main.cpp
+11
-0
GenAlg.cpp
pandaAlgorithm/GenAlg.cpp
+169
-13
GenAlg.h
pandaAlgorithm/GenAlg.h
+45
-7
Genome.h
pandaAlgorithm/Genome.h
+7
-3
No files found.
funcDemo/main.cpp
View file @
4805137f
...
@@ -9,15 +9,26 @@
...
@@ -9,15 +9,26 @@
#include "CivHydrTest.h"
#include "CivHydrTest.h"
#include <vector>
#include <vector>
#include <string>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <windows.h>
#include <consoleapi2.h>
#include <consoleapi2.h>
using
namespace
std
;
using
namespace
std
;
float
random
()
{
return
rand
()
/
(
RAND_MAX
+
1.0
);
}
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
{
srand
((
unsigned
)
time
(
NULL
));
/*这一句dao很重要,zhi如果没有的话,每dao次产生内的数据都是不变的。*/
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
printf
(
"%d"
,
rand
()
%
2
);
/*产生10个0到容1的随机数*/
}
while
(
true
)
while
(
true
)
{
{
const
char
*
uri
=
"host=192.168.19.100 port=5432 dbname=JinXian user=postgres password=admin"
;
const
char
*
uri
=
"host=192.168.19.100 port=5432 dbname=JinXian user=postgres password=admin"
;
...
...
pandaAlgorithm/GenAlg.cpp
View file @
4805137f
...
@@ -2,24 +2,30 @@
...
@@ -2,24 +2,30 @@
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
#include <stdio.h>
#include <stdio.h>
#include <random>
#include "OptScheduling.h"
GenAlg
::
GenAlg
(
OptScheduling
*
optScheduling
)
:
mOptScheduling
(
std
::
unique_ptr
<
OptScheduling
>
(
optScheduling
))
GenAlg
::
GenAlg
(
OptScheduling
*
optScheduling
,
GenType
genType
)
:
mOptScheduling
(
std
::
unique_ptr
<
OptScheduling
>
(
optScheduling
)),
mGenType
(
genType
)
{
{
}
}
int
GenAlg
::
random
(
)
double
GenAlg
::
random
(
int
min
,
int
max
)
{
{
srand
((
unsigned
)
time
(
NULL
));
double
m1
=
(
double
)(
rand
()
%
101
)
/
101
;
// 计算 0,1之间的随机小数,得到的值域近似为(0,1)
return
rand
();
min
++
;
//将 区间变为(min+1,max),
double
m2
=
(
double
)((
rand
()
%
(
max
-
min
+
1
))
+
min
);
//计算 min+1,max 之间的随机整数,得到的值域为[min+1,max]
m2
=
m2
-
1
;
//令值域为[min,max-1]
return
m1
+
m2
;
//返回值域为(min,max),为所求随机浮点
}
}
void
GenAlg
::
init
(
int
popsize
,
double
MutRate
,
double
CrossRate
,
int
GenLeng
ht
,
double
LeftPoint
,
double
RightPoint
)
void
GenAlg
::
init
(
int
popsize
,
double
MutRate
,
double
CrossRate
,
int
GenLeng
th
,
double
LeftPoint
,
double
RightPoint
)
{
{
mPopSize
=
popsize
;
mPopSize
=
popsize
;
mMutationRate
=
MutRate
;
mMutationRate
=
MutRate
;
mCrossoverRate
=
CrossRate
;
mCrossoverRate
=
CrossRate
;
mChromoLength
=
GenLeng
ht
;
mChromoLength
=
GenLeng
th
;
mTotalFitness
=
0
;
mTotalFitness
=
0
;
mGeneration
=
0
;
mGeneration
=
0
;
...
@@ -41,7 +47,18 @@ void GenAlg::init(int popsize, double MutRate, double CrossRate, int GenLenght,
...
@@ -41,7 +47,18 @@ void GenAlg::init(int popsize, double MutRate, double CrossRate, int GenLenght,
// 把所有的基因编码初始化为函数区间内的随机数。
// 把所有的基因编码初始化为函数区间内的随机数。
for
(
int
j
=
0
;
j
<
mChromoLength
;
j
++
)
for
(
int
j
=
0
;
j
<
mChromoLength
;
j
++
)
{
{
vecPop
[
i
].
vecGenome
.
push_back
(
random
()
*
(
mRightPoint
-
mLeftPoint
)
+
mLeftPoint
);
switch
(
mGenType
)
{
case
RealValueCoding
:
vecPop
[
i
].
vecGenome
.
push_back
(
random
(
0
,
MAX_RANDOM
)
*
(
mRightPoint
-
mLeftPoint
)
+
mLeftPoint
);
break
;
case
BinaryCoding
:
vecPop
[
i
].
mBinaryGenVec
.
push_back
((
int
)
random
()
%
2
);
break
;
default
:
break
;
}
}
}
}
}
}
}
...
@@ -52,7 +69,7 @@ void GenAlg::mutate(vector<double>& chromo)
...
@@ -52,7 +69,7 @@ void GenAlg::mutate(vector<double>& chromo)
for
(
int
i
=
0
;
i
<
chromo
.
size
();
++
i
)
for
(
int
i
=
0
;
i
<
chromo
.
size
();
++
i
)
{
{
// 如果发生突变的话
// 如果发生突变的话
if
(
random
()
<
mMutationRate
)
if
(
random
(
0
,
1
)
<
mMutationRate
)
{
{
// 使该权值增加或者减少一个很小的随机数值
// 使该权值增加或者减少一个很小的随机数值
chromo
[
i
]
+=
((
random
()
-
0.5
)
*
mMaxPerturbation
);
chromo
[
i
]
+=
((
random
()
-
0.5
)
*
mMaxPerturbation
);
...
@@ -70,10 +87,90 @@ void GenAlg::mutate(vector<double>& chromo)
...
@@ -70,10 +87,90 @@ void GenAlg::mutate(vector<double>& chromo)
}
}
}
}
void
GenAlg
::
mutate
(
vector
<
char
>&
chromo
)
{
// 遵循预定的突变概率,对基因进行突变
for
(
int
i
=
0
;
i
<
chromo
.
size
();
++
i
)
{
// 如果发生突变的话
if
(
random
(
0
,
1
)
<
mMutationRate
)
{
chromo
[
i
]
=
(
chromo
[
i
]
&
0x01
)
^
0x01
;
}
}
}
void
GenAlg
::
select
()
{
}
int
GenAlg
::
nextRandom
(
int
total
,
vector
<
int
>
visited
)
{
int
index
=
random
(
0
,
total
);
std
::
vector
<
int
>::
iterator
iter
=
std
::
find
(
visited
.
begin
(),
visited
.
end
(),
index
);
if
(
iter
!=
visited
.
end
())
return
nextRandom
(
total
,
visited
);
visited
.
push_back
(
index
);
return
index
;
}
void
GenAlg
::
corssver
()
{
// 染色体个数
int
popSize
=
vecPop
.
size
();
// 种群数组的索引
std
::
vector
<
int
>
randIndexVec
;
for
(
int
i
=
0
;
i
<
popSize
;
i
++
)
randIndexVec
.
push_back
(
i
);
// 随机排序,为了随机配对染色体
std
::
random_shuffle
(
randIndexVec
.
begin
(),
randIndexVec
.
end
());
int
left
=
0
;
int
right
=
popSize
-
1
;
while
(
left
<
right
)
{
int
indexLeft
=
randIndexVec
[
left
];
int
indexRight
=
randIndexVec
[
right
];
// 两个染色体互换
crossover
(
vecPop
[
indexLeft
].
mBinaryGenVec
,
vecPop
[
indexRight
].
mBinaryGenVec
);
left
++
;
right
--
;
}
}
void
GenAlg
::
crossover
(
vector
<
char
>&
chromo1
,
vector
<
char
>&
chromo2
)
{
if
(
chromo1
.
size
()
!=
chromo2
.
size
())
return
;
int
total
=
chromo1
.
size
();
// 基因交叉重组
for
(
int
i
=
0
;
i
<
total
;
i
++
)
{
// 满足交叉概率
if
(
random
(
0
,
1
)
<
mCrossoverRate
)
{
int
index
=
random
(
0
,
total
);
for
(
int
i
=
index
;
i
<
total
;
i
++
)
{
swap
(
chromo1
[
i
],
chromo2
[
i
]);
}
}
}
}
Genome
GenAlg
::
chromoRoulette
()
Genome
GenAlg
::
chromoRoulette
()
{
{
// 产生一个 0 到种群总适应性评分总和之间的随机数. mTotalFitness记录了整个种群的适应性分数总和
// 产生一个 0 到种群总适应性评分总和之间的随机数. mTotalFitness记录了整个种群的适应性分数总和
double
slice
=
(
random
())
*
mTotalFitness
;
double
slice
=
(
random
(
0
,
1
))
*
mTotalFitness
;
// 这个基因将承载转盘所选出来的那个个体.
// 这个基因将承载转盘所选出来的那个个体.
Genome
theChosenOne
;
Genome
theChosenOne
;
...
@@ -87,7 +184,7 @@ Genome GenAlg::chromoRoulette()
...
@@ -87,7 +184,7 @@ Genome GenAlg::chromoRoulette()
//累计适应性分数.
//累计适应性分数.
fitnessSoFar
+=
vecPop
[
i
].
fitness
;
fitnessSoFar
+=
vecPop
[
i
].
fitness
;
//如果累计分数大于随机数,就选择此时的基因.
//
如果累计分数大于随机数,就选择此时的基因.
if
(
fitnessSoFar
>=
slice
)
if
(
fitnessSoFar
>=
slice
)
{
{
theChosenOne
=
vecPop
[
i
];
theChosenOne
=
vecPop
[
i
];
...
@@ -100,12 +197,46 @@ Genome GenAlg::chromoRoulette()
...
@@ -100,12 +197,46 @@ Genome GenAlg::chromoRoulette()
void
GenAlg
::
calculateBestWorstAvTot
()
void
GenAlg
::
calculateBestWorstAvTot
()
{
{
int
total
=
vecPop
.
size
();
for
(
int
i
=
0
;
i
<
total
;
i
++
)
{
double
fitNessTemp
=
vecPop
[
i
].
fitness
;
// 计算总适应度值
mTotalFitness
+=
fitNessTemp
;
// 查找最好适应度值
if
(
mBestFitness
<
fitNessTemp
)
mBestFitness
=
fitNessTemp
;
// 查找最差适应度值
if
(
mWorstFitness
>
fitNessTemp
)
mWorstFitness
=
fitNessTemp
;
}
// 计算平均适应度
mAverageFitness
=
mTotalFitness
/
(
double
)
total
;
}
}
void
GenAlg
::
reset
()
void
GenAlg
::
reset
()
{
{
mPopSize
=
0
;
mMutationRate
=
0
;
mCrossoverRate
=
0
;
mChromoLength
=
0
;
mTotalFitness
=
0
;
mGeneration
=
0
;
mBestFitness
=
0.0
;
mWorstFitness
=
99999999
;
mAverageFitness
=
0
;
mMaxPerturbation
=
0.001
;
mLeftPoint
=
0
;
mRightPoint
=
0
;
// 清空种群容器,以初始化
vecPop
.
clear
();
}
}
void
GenAlg
::
epoch
(
vector
<
Genome
>&
vecNewPop
)
void
GenAlg
::
epoch
(
vector
<
Genome
>&
vecNewPop
)
...
@@ -115,12 +246,36 @@ void GenAlg::epoch(vector<Genome>& vecNewPop)
...
@@ -115,12 +246,36 @@ void GenAlg::epoch(vector<Genome>& vecNewPop)
Genome
GenAlg
::
bestFitness
()
Genome
GenAlg
::
bestFitness
()
{
{
Genome
genome
;
int
maxIndex
=
-
1
;
return
genome
;
for
(
int
i
=
0
;
i
<
vecPop
.
size
();
i
++
)
{
if
(
mBestFitness
==
vecPop
[
i
].
fitness
)
maxIndex
=
i
;
}
// 没找到构造一个空的基因型对象
if
(
maxIndex
==
-
1
)
return
Genome
();
return
vecPop
[
maxIndex
];
}
}
double
GenAlg
::
averageFitness
()
double
GenAlg
::
averageFitness
()
{
{
return
0
;
return
mAverageFitness
;
}
void
GenAlg
::
fitnessfunction
()
{
}
void
GenAlg
::
encoding
()
{
}
}
void
GenAlg
::
decoding
()
{
}
\ No newline at end of file
pandaAlgorithm/GenAlg.h
View file @
4805137f
...
@@ -2,20 +2,31 @@
...
@@ -2,20 +2,31 @@
#include "pandaAlgorithm.h"
#include "pandaAlgorithm.h"
#include "genome.h"
#include "genome.h"
#include <vector>
#include <memory>
#include <memory>
#include <vector>
#include <time.h>
#include<stdlib.h>
using
namespace
std
;
using
namespace
std
;
class
OptScheduling
;
#define MAX_RANDOM 2147483647.0
class
OptScheduling
;
/**
/**
遗传算法引擎
遗传算法引擎
*/
*/
class
GenAlg
class
PANDAALGORITHM_API
GenAlg
{
{
public
:
public
:
// 基因编码类型
enum
GenType
{
RealValueCoding
,
BinaryCoding
};
//构造函数
//构造函数
GenAlg
(
OptScheduling
*
optScheduling
);
GenAlg
(
OptScheduling
*
optScheduling
,
GenType
genType
);
//初始化变量
//初始化变量
void
reset
();
void
reset
();
...
@@ -29,9 +40,13 @@ public:
...
@@ -29,9 +40,13 @@ public:
// 轮盘赌选择函数
// 轮盘赌选择函数
Genome
chromoRoulette
();
Genome
chromoRoulette
();
// 基因变异函数
// 基因变异函数(实值编码)
void
mutate
(
vector
<
double
>&
chromo
);
void
mutate
(
vector
<
double
>&
chromo
);
// 基因变异函数(二进制编码)
void
mutate
(
vector
<
char
>&
chromo
);
//这函数产生新一代基因
//这函数产生新一代基因
void
epoch
(
vector
<
Genome
>&
vecNewPop
);
void
epoch
(
vector
<
Genome
>&
vecNewPop
);
...
@@ -41,13 +56,36 @@ public:
...
@@ -41,13 +56,36 @@ public:
// 获取平均适应度
// 获取平均适应度
double
averageFitness
();
double
averageFitness
();
// 产生随机数
// 所有基因重组
int
random
();
void
corssver
();
// 选择运算,前群体中适应度较高的个体按某种规则或模型遗传到下一代群体中
void
select
();
// 计算个体自适应度
void
fitnessfunction
();
// 编码,
void
encoding
();
// 解码
void
decoding
();
private
:
// 产生[a,b)之间的浮点数
double
random
(
int
a
=
0
,
int
b
=
RAND_MAX
);
int
nextRandom
(
int
total
,
vector
<
int
>
visited
);
// 两个染色体交叉运算
void
crossover
(
vector
<
char
>&
chromo1
,
vector
<
char
>&
chromo2
);
private
:
private
:
// 这个容器将储存每一个个体的染色体
// 这个容器将储存每一个个体的染色体
vector
<
Genome
>
vecPop
;
vector
<
Genome
>
vecPop
;
// 基因编码类型
GenType
mGenType
;
// 种群数量
// 种群数量
int
mPopSize
;
int
mPopSize
;
...
...
pandaAlgorithm/Genome.h
View file @
4805137f
...
@@ -2,18 +2,22 @@
...
@@ -2,18 +2,22 @@
#include <vector>
#include <vector>
/**
/**
染色体
染色体,实值编码
*/
*/
class
Genome
class
Genome
{
{
public
:
public
:
friend
class
GenAlg
;
friend
class
GenAlg
;
friend
class
GenEngine
;
friend
class
GenEngine
;
Genome
()
:
fitness
(
0
)
{}
Genome
()
:
fitness
(
0
)
{}
Genome
(
std
::
vector
<
double
>
vec
,
double
f
)
:
vecGenome
(
vec
),
fitness
(
f
)
{}
//类的带参数初始化参数。
Genome
(
std
::
vector
<
double
>
vec
,
double
f
)
:
vecGenome
(
vec
),
fitness
(
f
)
{}
//类的带参数初始化参数。
Genome
(
std
::
vector
<
char
>
vec
,
double
f
)
:
mBinaryGenVec
(
vec
),
fitness
(
f
)
{}
private
:
private
:
std
::
vector
<
double
>
vecGenome
;
// 装载基因的容器
std
::
vector
<
double
>
vecGenome
;
// 装载基因的容器
std
::
vector
<
char
>
mBinaryGenVec
;
// 二进制编码
double
fitness
;
// 适应度
double
fitness
;
// 适应度
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment