/// <summary> /// 开始优化 /// </summary> /// <param name="ConfigFile">算法参数配置文件路径</param> /// <param name="X_Init">优化变量初值</param> /// <param name="X_MaxStep">优化变量最大步长</param> /// <param name="X_Lb">优化变量下限</param> /// <param name="X_Ub">优化变量上限</param> /// <returns>使适应度函数最小的变量值</returns> /// <returns>最优目标函数</returns> public double[] StartOpt(string ConfigFile, double[] X_Init, double[] X_MaxStep, double[] X_Lb, double[] X_Ub, out double Fun, out double Delta) { if (X_Init.Length != X_MaxStep.Length || X_Init.Length != X_Lb.Length || X_Init.Length != X_Ub.Length) { throw new Exception("Variable number are not set correctly!"); } this.G = new General(); List <string[]> Config = new List <string[]>(); try { Config = G.ReadCSV(ConfigFile); this.Max_FEs = int.Parse(Config[0][1]); // 最大评价次数 this.Min_TolFun = double.Parse(Config[1][1]); // 结束条件 } catch (System.Exception ex) { throw new Exception("ConfigFile are not existed!"); } var M = Matrix <double> .Build; // 生成矩阵 //var V = Vector<double>.Build; // 生成向量 this.X_Dim = X_Init.Length; this.X = new double[X_Dim]; this.X_Lb = new double[X_Dim]; this.X_Ub = new double[X_Dim]; double[] X_Diff = new double[X_Dim]; for (int i = 0; i < X_Dim; i++) { this.X_Lb[i] = Math.Max(X_Init[i] - X_MaxStep[i], X_Lb[i]); this.X_Ub[i] = Math.Min(X_Init[i] + X_MaxStep[i], X_Ub[i]); X_Diff[i] = this.X_Ub[i] - this.X_Lb[i]; } this.X_Opt = new double[X_Dim]; this.xmeanw = M.Dense(X_Dim, 1); this.xold = M.Dense(X_Dim, 1); this.zmeanw = M.Dense(X_Dim, 1); this.flginiphase = true; this.sigma = 0.25; // Initial step size this.Min_sigma = 1.0e-15; // Minimal step size this.Max_sigma = X_Diff.Max() / Math.Sqrt((double)X_Dim); for (int i = 0; i < X_Dim; i++) { this.xmeanw[i, 0] = X_Init[i]; } this.lambda = 4 + (int)Math.Floor(3.0 * Math.Log((double)X_Dim)); this.mu = (int)Math.Floor((double)lambda / 2.0); this.arweights = M.Dense(mu, 1); for (int i = 0; i < mu; i++) { this.arweights[i, 0] = Math.Log((lambda + 1.0) / 2.0) - Math.Log(i + 1.0); } this.cc = 4.0 / (X_Dim + 4.0); this.ccov = 2.0 / Math.Pow((X_Dim + Math.Pow(2.0, 0.5)), 2.0); this.cs = 4.0 / (X_Dim + 4.0); this.damp = (1.0 - Math.Min(0.7, X_Dim * lambda / Max_FEs)) / cs + 1.0; this.B = M.DenseIdentity(X_Dim); this.D = M.DenseIdentity(X_Dim); this.BD = M.Dense(X_Dim, X_Dim); this.C = M.Dense(X_Dim, X_Dim); this.CC = M.Dense(X_Dim, X_Dim); this.BD = B * D; this.C = BD * BD.Transpose(); this.pc = M.Dense(X_Dim, 1); this.ps = M.Dense(X_Dim, 1); this.cw = arweights.RowSums().Sum() / arweights.L2Norm(); this.chiN = Math.Pow(X_Dim, 0.5) * (1.0 - 1.0 / (4.0 * X_Dim) + 1.0 / (21.0 * Math.Pow(X_Dim, 2.0))); this.arz = M.Dense(X_Dim, lambda); this.arx = M.Dense(X_Dim, lambda); this.arz_mu = M.Dense(X_Dim, mu); this.arx_mu = M.Dense(X_Dim, mu); this.Fun = new double[lambda]; // 各个个体的适应度 this.Delta = new double[lambda]; // 各个个体的约束违反度 this.Index = new int[lambda]; // 各个个体的序号 this.rd = new Random(); Opt(); // 优化 Fun = Best_Fun; Delta = Best_Delta; return(X_Opt); }