注册 登录
编程论坛 C# 论坛

代码错误主要是在if else 之中,当第一次迭代的时候, previous_population , previous_fitness 等明明已经计算出来的

米粒大小3 发布于 2017-07-25 07:39, 1991 次点击
代码错误主要是在if else 之中,当第一次迭代的时候,  previous_population , previous_fitness 等明明已经计算出来的,但在进行第二次迭代的时候,这些都为空了,不知道为什么,以前没有碰到过类似的问题,恳请路过的留下指导意见,感激不尽了
while (Iteration < Max_iteration)
            {
                int Flame_no = Convert.ToInt16(System.Math.Round((double)Max_iteration / (Iteration + Max_iteration / N)));
                double[,] Moth_fitness = new double[1, population];
                Moth_fitness = fitness(Moth_pos, ub, lb);
                double[,] fitness_sorted = new double[1, population];
                int[] I = new int[population];
                double[,] sorted_population = new double[population, dim];
                double[,] best_flames = new double[population, dim];
                double[,] best_flame_fitness = new double[1, population];
                double[,] previous_population = new double[population, dim];
                double[,] previous_fitness = new double[1, population];
                double[,] double_population = new double[2 * population, dim];
                double[,] double_fitness = new double[1, 2 * population];
                double[,] double_fitness_sorted=new double[1,2*population];
                double[,] double_sorted_population = new double[2 * population, dim];
                int[] II=new int[2*population];
               /* previous_population = Moth_pos;
                previous_fitness = Moth_fitness;*/
                if (Iteration == 1)
                {
                    Sort(Moth_fitness, out fitness_sorted, out I);
                    for (int i = 0; i < population; i++)
                    {
                        for (int j = 0; j < dim; j++)
                        {
                            sorted_population[i, j] = Moth_pos[I[i], j];
                        }
                    }
                   /* best_flames = sorted_population;
                    best_flame_fitness = fitness_sorted;*/
                }               
                else
                {
                    JuZhenHangHeBing(previous_population, best_flames,out double_population);
                    JuZhenLieHebing(previous_fitness, best_flame_fitness, out double_fitness);

                    Sort(double_fitness, out double_fitness_sorted, out II);
                    for (int i = 0; i < 2*population; i++)
                    {
                        for (int j = 0; j < dim; j++)
                        {
                            double_sorted_population[i, j] = double_population[II[i], j];
                        }
                    }
                    for (int i = 0; i < N; i++)
                    {
                        for (int j = 0; j < dim; j++)
                        {
                            sorted_population[i, j] = double_sorted_population[i, j];
                        }
                    }
                    for (int i = 0; i < N; i++)
                    {
                        fitness_sorted[0, i] = double_fitness_sorted[0, i];
                    }

                    best_flames = sorted_population;
                    best_flame_fitness = fitness_sorted;

                }
                best_flames = sorted_population;
                best_flame_fitness = fitness_sorted;
                //Update the position best flame obtained so far
                Best_flame_score = fitness_sorted[0, 0];
                for (int i = 0; i < dim; i++)
                {
                    Best_flame_pos[0,i] = sorted_population[0, i];
                }
                previous_population = Moth_pos;
                previous_fitness = Moth_fitness;
1 回复
#2
yhlvht2017-07-25 19:51
  double[,] previous_population = new double[population, dim];
  double[,] previous_fitness = new double[1, population];
不是每一次都new了新的么
1