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



我已经为遗传算法编写了此代码,该算法通过获取基因的随机int来计算跳跃高度(质量,高度(spemiment(和腿力(,然后再现本身具有其他表现出色的随机样品,该样本存储在称为geneBoard的2D数组中。现在解决我的问题;您会发现写出相同值的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不是您真正想要的。顾名代表您的基因。

在您的情况下,我想一个简单的 for循环在您之前创建的数组上绝对足够。

您患有的错误(没有过载方法'重复'为5个参数。(是正常的。

枚举。repeat进行2个参数:

  • 重复的元素
  • 重复多少次

您正在给它5个参数,其中4个描述了一个元素。

解决方法是创建一个类以保持您的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();

您可能想查看您的逻辑。因为这将为您提供相同的元素1000次,我假设您想要具有相同值的1000个不同的objet。

相关内容

  • 没有找到相关文章

最新更新