首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法对非静态字段Population.gen进行静态引用

无法对非静态字段Population.gen进行静态引用
EN

Stack Overflow用户
提问于 2018-10-13 17:07:18
回答 1查看 39关注 0票数 0

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

神经网络

代码语言:javascript
复制
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();
  }
}

人口

代码语言:javascript
复制
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进行静态引用

我认为这与变量在Population下有关,所以我需要转换它或其他什么?

谢谢,请用最简单的语言解释。

EN

回答 1

Stack Overflow用户

发布于 2018-10-13 18:59:45

gen是一个实例变量,这意味着它不属于群体,而是属于该类的实例。因此,对于您创建的每个群体,gen将具有不同的值。

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

您遇到了一个问题,因为您试图访问属于 Population (静态引用)的gen变量,但gen仅存在于Population的实例(即,.填充像您创建的test这样的对象)。

您有两个解决问题的方法:

修改器引用属于修改器对象的gen变量:text(test.gen,10,10);

  • Add
  • static to genstatic int gen = 1;gen变量将属于Population ,您可以使用Population.gen引用它。但是,如果您创建更多的人口,它们将共享这一个值,因此这可能不是您想要的。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52791338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档