package GA;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2012</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
public class Modified_GA_ertan {
//程序入口
public static void main(String[] args) {
Modified_GA_ertan key = new Modified_GA_ertan();
key.init();
key.Operation();
}
double Low_v = 24.2;
double Gao_v = 57.9;
double N_Maxzhji = 330; // 装机容量,单位万kW
double N_Minzhji = 98; // 保证出力,单位万kW
double N_MaxQ = 2400; // 发电机组最大应用流量
double A = 8.6; // 出力系数
double N = 0.1; // 精度_得到337个区间,338个点
double a = 1000000; // 调整惩罚的参数
double b = 0.5; // 调整惩罚的参数
int M = 200; // 初始种群规模
int T = 300; // 进化代数
double Ps = 0.8; // 选择概率
double Pc = 0.9; // 交叉概率
double Pm = 0.05; // 变异概率
int Range = 6; // 变异范围
int Longer = 12;
double[] optimal_H = new double[Longer + 1]; // 存放最优水位过程
double[] optimal_N = new double[Longer]; // 存放最优出力过程
double[] optimal_E = new double[T]; // 存放每次进化的最优适应值(解码)
double[] optimal_N_Q = new double[Longer]; // 存放最优发电引用流量过程
double[] loss_Q = new double[Longer]; // 存放最优弃水过程
double[] optimal_chuku_Q = new double[Longer]; // 存放最优出库水量过程
// 入库流量
double[] q_in = new double[] {
1309, 2476, 4299, 3761, 2071, 1127, 713,
529, 466, 465, 555, 670};
// 二滩水库水位——库容关系
double[] ia = new double[] {
1155, 1156, 1158, 1160, 1162, 1164, 1166,
1168, 1170, 1172, 1174, 1176, 1178, 1180, 1182, 1184, 1186, 1188,
1190, 1192, 1194, 1196, 1198, 1200};
double[] iv = new double[] {
24.2, 24.52, 25.56, 26.61, 27.9, 29.2, 30.5,
31.8, 33.1, 34.5, 35.9, 37.3, 38.7, 40.11, 41.78, 43.45, 45.12,
46.8, 48.5, 50.2, 52, 53.9, 55.9, 57.93};
// 二滩水库泄流量——下游水库关系
double[] ib = new double[] {
1009.5, 1010.85, 1012.38, 1014.5, 1016.4,
1018.1, 1019.6, 1020.8, 1021.8, 1022.55, 1023.06, 1023.34, 1023.38};
double[] iq = new double[] {
0, 400, 1000, 2000, 3000, 4000, 5000, 6000,
7000, 8000, 9000, 10000, 11000};
double[] V; // 存库容值
int m; // 库容离散数
double[] H_Cshuiwei; // 对应库容的水位(m+1)个
int[][] individual_Group_Old = new int[M][q_in.length + 1];
int[][] individual_Group_New = new int[M][q_in.length + 1];
public void init() {
// 库容离散(从大到小)
m = (int) ( (Gao_v - Low_v) / N); // 化为m等分,有m+1个点
double[] V = new double[ (m + 1)];
V[0] = Low_v;
for (int i = 0; i < m + 1; i++) {
V[i] = V[0] + i * (Gao_v - Low_v) / m;
}
this.V = V;
// 产生初始种群
// 对应库容的水位(m+1)个
double[] H_Cshuiwei = new ChaZhi().ChaZhiShuiWei(V, ia, iv);
this.H_Cshuiwei = H_Cshuiwei;
Init_Group();
}
public void Operation() {
int t = 1;
while (t <= T) {
Evaluate_Group(t);
Select();
GA_Cross();
GA_Mutation();
GA_Production();
t++;
}
GA_Result();
}
public void Init_Group() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < q_in.length + 1; j++) {
if (j == 0 || j == q_in.length) {
individual_Group_Old[i][j] = 0;
}
else {
int k;
k = new Random().nextInt(V.length);
individual_Group_Old[i][j] = k; // 取得是整数编码的基因——映射库容值,比如存的是45,则对应的库容值是V[45]
}
}
}
}
double[] E_SYZhi = new double[M];
public void Evaluate_Group(int t) {
double[][] xie_chuku = new double[M][q_in.length]; // 出库流量(m3/s)
double[][] xie_q = new double[M][q_in.length]; // 发电引用流量(m3/s)
double[][] q_loss = new double[M][q_in.length]; // 弃水流量(m3/s)
double[][] q_punishment = new double[M][q_in.length]; // 放惩罚水的电量
for (int i = 0; i < M; i++) {
// System.out.println();
for (int j = 0; j < q_in.length; j++) {
xie_chuku[i][j] = ( (V[individual_Group_Old[i][j]] -
V[individual_Group_Old[i][j + 1]]) * Math
.pow(10, 8))
/ (30 * 24 * 3600) + q_in[j];
xie_q[i][j] = xie_chuku[i][j];
if (xie_chuku[i][j] < 0) {
xie_chuku[i][j] = 0;
xie_q[i][j] = 0;
q_punishment[i][j] = a * Math.pow(t, b)
* Math.pow(xie_chuku[i][j], (double) 2);
}
if (xie_chuku[i][j] > N_MaxQ) {
// q_loss[i][j] = xie_chuku[i][j] - N_MaxQ;
xie_q[i][j] = N_MaxQ;
q_punishment[i][j] = a * Math.pow(t, b)
* Math.pow(xie_chuku[i][j] - N_MaxQ, (double) 2);
}
}
}
// 求泄流量对应的库尾水位
double[][] H_damxia = new ChaZhi().KuWeiShuiWei(xie_q, ib, iq, M);
// 求水头差
double[][] H_loss = new double[M][q_in.length];
for (int i = 0; i < M; i++) {
for (int j = 0; j < q_in.length; j++) {
H_loss[i][j] = (H_Cshuiwei[individual_Group_Old[i][j]] +
H_Cshuiwei[individual_Group_Old[i][j + 1]])
/ 2 - H_damxia[i][j];
}
}
// 求适应值
double[][] n_ChuLi = new double[M][q_in.length]; // kW
double[][] E_n_ChuLi = new double[M][q_in.length]; // kW
for (int i = 0; i < M; i++) {
for (int j = 0; j < q_in.length; j++) {
n_ChuLi[i][j] = A * xie_q[i][j] * H_loss[i][j]
/ Math.pow(10, 4); // 万kW
E_n_ChuLi[i][j] = n_ChuLi[i][j] * 30 * 24 * Math.pow(10, 4)
- q_punishment[i][j];
if (n_ChuLi[i][j] > N_Maxzhji) {
xie_q[i][j] = n_ChuLi[i][j] * Math.pow(10, 4)
/ (A * H_loss[i][j]);
// q_loss[i][j]=xie_chuku[i][j]-xie_q[i][j];
n_ChuLi[i][j] = N_Maxzhji;
xie_q[i][j] = n_ChuLi[i][j] * Math.pow(10, 4)
/ (A * H_loss[i][j]);
// zhongjie = n_ChuLi[i][j] * 30 * 24 * Math.pow(10,
// 4);//要不要惩罚
E_n_ChuLi[i][j] = n_ChuLi[i][j] * 30 * 24 * Math.pow(10, 4)
- a * Math.pow(t, b)
* Math.pow(N_Maxzhji - n_ChuLi[i][j], (double) 2);
}
if (n_ChuLi[i][j] < N_Minzhji) {
E_n_ChuLi[i][j] = n_ChuLi[i][j] * 30 * 24 * Math.pow(10, 4)
- a * Math.pow(t, b)
* Math.pow(N_Minzhji - n_ChuLi[i][j], (double) 2);
}
q_loss[i][j] = xie_chuku[i][j] - xie_q[i][j];
}
}
// 存放每代群体中个体的适应值
for (int i = 0; i < M; i++) {
E_SYZhi[i] = 0;
for (int j = 0; j < q_in.length; j++) {
E_SYZhi[i] += E_n_ChuLi[i][j];
}
}
// 找出最佳个体及相应的指标
double max_E = 0;
int Sign_xiabiao = 0;
for (int i = 0; i < M; i++) {
if (E_SYZhi[i] > max_E) {
max_E = E_SYZhi[i];
Sign_xiabiao = i;
}
}
optimal_E[t - 1] = max_E;
if (t == T) {
for (int j = 0; j < q_in.length; j++) { // 出库流量
optimal_chuku_Q[j] = xie_chuku[Sign_xiabiao][j];
loss_Q[j] = q_loss[Sign_xiabiao][j];
optimal_N_Q[j] = xie_q[Sign_xiabiao][j];
optimal_N[j] = n_ChuLi[Sign_xiabiao][j];
}
for (int j = 0; j < q_in.length + 1; j++) { // 出库流量
optimal_H[j] = H_Cshuiwei[individual_Group_Old[Sign_xiabiao][j]];
}
}
}
int[] lable = new int[M];
public void Select() {
HashMap map = new HashMap();
for (int i = 0; i < E_SYZhi.length; i++) {
map.put(new Double(E_SYZhi[i]), new Integer(i)); // 将值和下标存入Map
}
// 降序排列
Arrays.sort(E_SYZhi);
// 查找原始下标
for (int i = 0; i < E_SYZhi.length; i++
评论5