gpt4 book ai didi

C# 'Enumerable.Repeat()'如何重载?

转载 作者:太空宇宙 更新时间:2023-11-03 18:54:26 27 4
gpt4 key购买 nike

我为遗传算法编写了这段代码,该算法通过获取基因的随机整数(质量、(样本的)高度和腿部力量)来计算跳跃高度,然后用其他表现最好的随机数复制自身样本存储在名为 geneBoard 的二维数组中。现在我的问题;你看写出 1000 次相同的值是不切实际的,所以我尝试使用这个:

float[,] geneBoard = Enumerable.Repeat(
mass = GetMass(500, 1000),
height = GetHeight(150, 190),
force = GetForce(500, 3500),
fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)), 1000)
.ToArray();

而不是这个:

float[,] geneBoard = new float[4, 4]
{
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) },
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) },
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) },
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) }
};

如果我没有做足够好的解释,这是我的所有代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Genetic_Algorithm
{
class Program
{
static Random random = new Random(); // Getting a seed

static void Main(string[] args)
{


float mass = GetMass(500, 1000);
float height = GetHeight(150, 190);
float force = GetForce(500, 3500);

float fitnessScore = Convert.ToSingle( PhysicalTest(mass, height, force) );
/*
float[,] geneBoard = new float[4, 4]
{
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) },
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) },
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) },
{ mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)) }
};
*/
float[,] geneBoard = Enumerable.Repeat(mass = GetMass(500, 1000), height = GetHeight(150, 190), force = GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)), 1000).ToArray();

float[] sortingAr = new float[] { geneBoard[0, 3], geneBoard[1, 3], geneBoard[2, 3], geneBoard[3, 3] };
Array.Sort(sortingAr, (x, y) => y.CompareTo(x));

Console.WriteLine(sortingAr[0]);
Console.WriteLine(sortingAr[1]);
}

public static float GetMass(int min, int max)
{
int massGr = random.Next(min, max);
float mass = massGr / 10;

return mass;
}

public static float GetHeight(int min, int max)
{
int heightCm = random.Next(min, max);
float height = heightCm / 10;

return height;
}

public static float GetForce(int min, int max)
{
int force = random.Next(min, max);

return force;
}

public static double PhysicalTest(float mass, float height, float force)
{
double d = force * 0.07; // gets distance of applied force
double vi = Math.Sqrt( (force * d) / ( mass / 2 ) ); // calculats v initial
double jumpHeight = (vi * vi) / (2 * 9.8); // calculates jumping height

double fitnessScore = jumpHeight * 100;

return fitnessScore;
}
}

最后,我是初学者,所以任何其他建议都会有所帮助。

最佳答案

我认为 Enumerable.Repeat 不是您真正想要的。正如它的名字所说,它只是一遍又一遍地重复相同元素,而在你的情况下,我认为你想要的是创建 1000 个相似但不同的对象代表你的基因。

在您的情况下,我想对您之前创建的数组进行简单的 for 循环就足够了。

关于C# 'Enumerable.Repeat()'如何重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48744676/

27 4 0