Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C_uu转换每个循环的变量类型_C# - Fatal编程技术网

C# C_uu转换每个循环的变量类型

C# C_uu转换每个循环的变量类型,c#,C#,我遇到了foreach循环的问题。我有一个名为SmartAgent的类和一个名为Population的类。population类有一个SmartAgent类型列表,其中包含所有my Agent和函数Live、Display、Reproduction、Selection和Fitness 代码无法编译,因为在foreach循环中,变量类型为Population,并且它正在尝试遍历智能代理类型的填充列表。我得到的错误是: 错误CS0030:无法将类型“Script\u Instance.SmartAg

我遇到了foreach循环的问题。我有一个名为SmartAgent的类和一个名为Population的类。population类有一个SmartAgent类型列表,其中包含所有my Agent和函数Live、Display、Reproduction、Selection和Fitness

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

错误CS0030:无法将类型“Script\u Instance.SmartAgent”转换为“Script\u 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();

     }


  }
///-------------智能代理-------------//////////

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;
}

}
如果population.population实际上是SmartAgent实例的集合,请尝试将其用作foreach中的预期类型


smartagent应该是人口的子类吗

为什么要尝试在smartagent上运行“实时”功能如果显示和实时功能来自于人口,这些功能的目的是什么

我怀疑解决方案是重构类的工作方式,而不是foreach循环

例如,如果display和live是按人口运行的,则它们可以为代理的indedx取一个整数,或者只取smartagent类型的对象,以更新引用特定代理的display或live函数。即:

foreach(SmartAgent item in population.population) {
    population.live(item);
    population.display(item); }
或者,您也可以将函数实时显示在单个类smartagent上,即:

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

另一种选择是让live和display功能自行执行循环。

什么是人口。人口?假设这是您的SmartAgent列表,您的foreach循环应该是foreach SmartAgent s in population.population Hi nlwalker,感谢您的回复,population.population包含我的SmartAgent列表,但无论如何,foor循环不能是SmartAgent类型,因为类不包含display、live、fitness、,选择和复制方法。如果是这种情况,你需要将调用P.Live、P.display等更改为population.Live、population.display等,因为这就是你调用它们的对象。向我们显示population和SmartAgent类。刚刚将这两个类添加到帖子中:嘿,斯科特,谢谢你的回复。如果我进行类型为SmartAgent的循环,我仍然不会编译,因为我的类SmartAgent不包含live、display..的方法。。等
foreach(SmartAgent item in population.population) {
    item.live();
    item.display(); }