无法静态引用非静态字段“ Population.gen”

时间:2018-10-13 09:07:18

标签: processing

我正在弄一个教程,并试图绘制变量gen

神经网络

Population test;
PVector goal  = new PVector(400, 10);

void setup() {
  size(800, 800); //size of the window
  frameRate(100);//increase this to make the dots go faster
  test = new Population(1000);//create a new population with 1000 members
}


void draw() { 
  background(255);

  //draw goal
  fill(255, 0, 0);
  ellipse(goal.x, goal.y, 10, 10);

  //draw obstacle(s)
  fill(0, 0, 255);

  rect(0, 300, 600, 10);

  text(Population.gen,10,10);

  if (test.allDotsDead()) {
    //genetic algorithm
    test.calculateFitness();
    test.naturalSelection();
    test.mutateDemBabies();
  } else {
    //if any of the dots are still alive then update and then show them

    test.update();
    test.show();
  }
}

人口

class Population {
  Dot[] dots;

  float fitnessSum;
  int gen = 1;

  int bestDot = 0;//the index of the best dot in the dots[]

  int minStep = 1000;

  Population(int size) {
    dots = new Dot[size];
    for (int i = 0; i< size; i++) {
      dots[i] = new Dot();
    }
  }


  //------------------------------------------------------------------------ 
 ------------------------------------------------------
  //show all dots
  void show() {
    for (int i = 1; i< dots.length; i++) {
      dots[i].show();
    }
    dots[0].show();
  }

  //------------------------------------------------------------------------ 
-------------------------------------------------------
  //update all dots 
  void update() {
    for (int i = 0; i< dots.length; i++) {
      if (dots[i].brain.step > minStep) {//if the dot has already taken more 
steps than the best dot has taken to reach the goal
        dots[i].dead = true;//then it dead
      } else {
        dots[i].update();
      }
    }
  }

  //------------------------------------------------------------------------ 
-----------------------------------------------------------
  //calculate all the fitnesses
  void calculateFitness() {
    for (int i = 0; i< dots.length; i++) {
      dots[i].calculateFitness();
    }
  }


  //------------------------------------------------------------------------ 
------------------------------------------------------------
  //returns whether all the dots are either dead or have reached the goal
  boolean allDotsDead() {
    for (int i = 0; i< dots.length; i++) {
      if (!dots[i].dead && !dots[i].reachedGoal) { 
        return false;
      }
    }

    return true;
  }



  //------------------------------------------------------------------------ 
-------------------------------------------------------------

  //gets the next generation of dots
  void naturalSelection() {
    Dot[] newDots = new Dot[dots.length];//next gen
    setBestDot();
    calculateFitnessSum();

    //the champion lives on 
    newDots[0] = dots[bestDot].gimmeBaby();
    newDots[0].isBest = true;
    for (int i = 1; i< newDots.length; i++) {
      //select parent based on fitness
      Dot parent = selectParent();

      //get baby from them
      newDots[i] = parent.gimmeBaby();
    }

    dots = newDots.clone();
    gen ++;
  }


  //------------------------------------------------------------------------ 
 --------------------------------------------------------------
  //you get it
  void calculateFitnessSum() {
    fitnessSum = 0;
    for (int i = 0; i< dots.length; i++) {
      fitnessSum += dots[i].fitness;
    }
  }

  //------------------------------------------------------------------------ 
-------------------------------------------------------------

  //chooses dot from the population to return randomly(considering fitness)

  //this function works by randomly choosing a value between 0 and the sum 
of all the fitnesses
  //then go through all the dots and add their fitness to a running sum and 
if that sum is greater than the random value generated that dot is chosen
  //since dots with a higher fitness function add more to the running sum 
then they have a higher chance of being chosen
  Dot selectParent() {
    float rand = random(fitnessSum);


    float runningSum = 0;

    for (int i = 0; i< dots.length; i++) {
      runningSum+= dots[i].fitness;
      if (runningSum > rand) {
        return dots[i];
      }
    }

    //should never get to this point

    return null;
  }

  //------------------------------------------------------------------------ 
------------------------------------------------------------------
  //mutates all the brains of the babies
  void mutateDemBabies() {
    for (int i = 1; i< dots.length; i++) {
      dots[i].brain.mutate();
    }
  }

  //------------------------------------------------------------------------ 
---------------------------------------------------------------------
  //finds the dot with the highest fitness and sets it as the best dot
  void setBestDot() {
    float max = 0;
    int maxIndex = 0;
    for (int i = 0; i< dots.length; i++) {
      if (dots[i].fitness > max) {
        max = dots[i].fitness;
        maxIndex = i;
      }
    }

    bestDot = maxIndex;

    //if this dot reached the goal then reset the minimum number of steps it 
takes to get to the goal
    if (dots[bestDot].reachedGoal) {
      minStep = dots[bestDot].brain.step;
      println("step:", minStep);
    }
  }
}

错误:无法静态引用非静态字段“ Population.gen”

我认为这与“人口”下的变量有关,因此我需要对其进行转换?

谢谢,请用最简单的术语进行解释

1 个答案:

答案 0 :(得分:1)

gen是一个实例变量,表示它不属于 Population ,而是属于该类的 instances 。结果,对于您创建的每个人口gen的值将有所不同。

使用test = new Population(1000);,创建 Population 类的新实例。因此,对象test具有一个gen变量,而类 Population 仍然没有。

您遇到问题是因为您试图访问属于 Population (静态引用)的gen变量,但是{{ 1}}仅存在于( Population )的实例中(作为非静态字段)(即,诸如gen之类的 Population 对象您创建的)。


您有两种选择可以解决您的问题:

  • 请参阅属于test对象的gen变量: test

  • 将修饰符text(test.gen,10,10);添加到staticgenstatic int gen = 1;变量将属于 Population class ,您可以在尝试使用gen来引用它。但是,如果您创建更多的人口,他们将全部共享这一价值,所以这可能不是您想要的。