我已经为遗传算法编写了这段代码,该算法通过获取基因的随机int(质量,高度(指标)和腿力)来计算跳跃高度,然后用其他最佳表现随机再现自身存储在称为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;
}
}
}
最后,我是初学者,所以任何其他建议都会有所帮助。
答案 0 :(得分:6)
我认为Enumerable.Repeat
不是你真正想要的。正如其名称所示,它只是一遍又一遍地重复相同的元素,而在您的情况下,我认为您想要的是创建1000个类似但不同的对象,代表你的基因。
在你的情况下,我想你之前创建的一个简单的for
循环是绝对足够的。
答案 1 :(得分:0)
你遇到的错误(没有重载方法'重复'需要5个参数。)是正常的,因为你正在以错误的方式使用Enumerable.Repeat。
Enumerable.Repeat需要2个参数:
你给它5个参数,其中4个描述了AN元素。
解决方法是创建一个类来保存您的4个值:
public class Element
{
public float Mass { get; set; }
public float Height { get; set; }
public float Force { get; set; }
public double FitnessScore { get; set; }
// Add constructor to take the 4 parameters
}
然后像这样使用它:
Element[] elements = Enumerable.Repeat(new Element(GetMass(500, 1000), GetHeight(150, 190), GetForce(500, 3500), fitnessScore = Convert.ToSingle(PhysicalTest(mass, height, force)), 1000).ToArray();
你可能想查看你的逻辑。因为这会给你100次SAME元素,我假设你想要1000个不同的objets具有相同的值。