C#_转换变量类型foreach循环

时间:2018-01-30 23:56:55

标签: c#

我遇到了foreach循环的问题。我有一个名为SmartAgent的类和一个名为Population的类。人口类有一个SmartAgent类型的列表,其中包含我的所有代理和功能实时,显示,再现,选择和适应。

代码无法编译,因为在foreach循环中,变量类型是Population,并且它试图遍历其Smart Agent类型的填充列表。我得到的错误是:

  

错误(CS0030):无法将类型'Script_Instance.SmartAgent'转换为'Script_Instance.Population'(第113行)

******问题:有没有办法将“人口”加入SmartAgent类型?或者编程中最正确的方法是什么?**

谢谢!

尼古拉斯

这是该计划的一部分:

Population population = new Population(number, 0.01);


if(run){

  if(geneCounter < lifespan)
  {
     foreach(Population P in population.population){



      P.Live();
      P.display();



    }


 else
  {

      foreach( Population P in population.population){

      P.Fitness();
      P.Selection();
      P.Reproduction();

     }


  }

/// ------------------------- SMART AGENT ----------------- --------- //////////

public class SmartAgent
{
public Point3d location; 
public Vector3d velocity; 
public Vector3d acceleration; 
DNA dna = new DNA();
public double fitness = 0; // each agent will now its own fitness value

public SmartAgent(Point3d Location, Vector3d Velocity, Vector3d Acceleration)
{
  location = Location;
  velocity = Velocity;
  acceleration = Acceleration;
}

public SmartAgent(Point3d Location, DNA Dna)
{
  acceleration = new Vector3d();
  velocity = new Vector3d();
  location = Location;
  dna = Dna;

}

public void ApplyForce(Vector3d force)
{

  force.Unitize();
  force *= maxForce; // constrain force by a scalar value
  acceleration += force; // Update acceleration
}

public void Fitness()
{
  double distanceToTarget = location.DistanceTo(t.location);
  fitness = 1 / distanceToTarget;

  if(distanceToTarget <= 50)
  {
    fitness *= 2;

  }

}

public void Update()
{
  double maxSpeed = 0.6;

  updateGenes();
  Bounds();
  velocity += acceleration;
  location += velocity * maxSpeed;
  acceleration *= 0;
}


public void updateGenes()
{
  //geneCounter++; // Update gene counter by 1
  //dna.Genes();   // Create a random velocity vector through each iteration
  ApplyForce(dna.geneList[geneCounter]); // Apply a force from geneList

}

public Point3d display()
{

  Point3d agentDisplay = new Point3d(location.X, location.Y, location.Z);

  return agentDisplay;
}
}

人口

public class Population
{
//-----PROPERTIES-------//

public int size;
public List<SmartAgent> population;// = new List<SmartAgent>();
public List <SmartAgent> matingPool;// = new List <Vector3d>();
int generations;
double mutationRate;

//-----CONSTRUCTOR-------//
public Population(int Size, double MutationRate)
{
  size = Size;
  mutationRate = MutationRate;
  generations = 0;
  population = new List<SmartAgent>();
  matingPool = new List<SmartAgent>();

  // create a population of Smart Agents
  // with initial random velocities that are inside the
  //DNA() constructor

  for(int i = 0;i < size;i++)
  {
    Point3d location = new Point3d(0, 0, 0);

    population.Add(new SmartAgent(location, new DNA()));
  }

}
//-----METHODS-------//

public void Live()
{
  foreach (SmartAgent agent in population)
  {
    agent.Update();
  }
}

public Point3d display()
{
  Point3d test = new Point3d();

  foreach (SmartAgent agent in population)
  {
    test = agent.display();
  }

  return test;

}

public Vector3d VelocityDisplay()
{
  Vector3d test = new Vector3d();

  foreach (SmartAgent agent in population)
  {

    test = agent.velocity;
  }

  return test;
}


public void Fitness()
{
  foreach (SmartAgent agent in population)
  {
    agent.Fitness();
  }
}

//--------------SELECTION-------------///

public void Selection()
{
  matingPool.Clear();
  double maxFitness = getMaxFitness();

  foreach (SmartAgent agent in population)
  {
    double fitnessNormal = Map(0, maxFitness, 0, 1, agent.getFitness());

    int n = (int) (fitnessNormal * 100);

    for( int j = 0;j < n;j++)
    {
      matingPool.Add(agent);  
    }
  }
}

public void Reproduction()
{


  foreach (SmartAgent agent in population)
  {

    int indexA = ran.Next(0, matingPool.Count);
    int indexB = ran.Next(0, matingPool.Count);

    if(indexA == indexB)
    {
      indexA += 1;
    }

    // pick parents

    SmartAgent parentA = matingPool[indexA];

    SmartAgent parentB = matingPool[indexB];

    // get parent genes

    DNA parentAGenes = parentA.getDNA();
    DNA parentBGenes = parentB.getDNA();

    // produce child

    DNA child = parentBGenes.CrossOver(parentAGenes);

    // gene mutation

    child.Mutation(mutationRate);

    // create new population with next generation

    Point3d location = new Point3d();
    population.Add(new SmartAgent(location, child));

  }

  generations++;
}


public double getMaxFitness()
{
  double record = 0;

  //for(int i = 0;i < population.Count;i++)
  foreach (SmartAgent agent in population)
  {
    if(agent.getFitness() > record)
    {
      record = agent.getFitness();

    }
  }

  return record;
}

}

2 个答案:

答案 0 :(得分:0)

如果population.popuation实际上是SmartAgent个实例的集合,请尝试将其用作foreach

中的预期类型
foreach(SmartAgent a in population.population){
    a.Live();
    a.display();
}

答案 1 :(得分:0)

smartagent应该是儿童类吗?

你为什么要试着活着?如果显示和实时功能来自人口,智能特性上的功能是什么?这些功能意味着什么?

我怀疑解决方案是重组你的类如何工作而不是foreach循环。

例如,显示和实时是按人口运行的,他们可以为代理的indedx或简单地类型为smartagent的对象采用整数来更新显示或实时函数以引用特定代理。即:

foreach(SmartAgent item in population.population) {
    population.live(item);
    population.display(item); }

或者您可以将函数live()和display()放在单个类smartagent上,即:

foreach(SmartAgent item in population.population) {
    item.live();
    item.display(); }

另一个选择是让live和display函数执行循环。