c# - 在 C# 中用 GA 进行时间表调度的二进制数组中的基因初始化

标签 c# algorithm initialization genetic

我目前正在做一个司机调度项目,它处于初始阶段。 我决定使用 GA 为司机生成优化的时间表,并且正如大多数 GA 项目所做的那样,人口应以二进制表示。

例如如果分配给司机两个小时的任务并且他的工作持续时间为 9 小时,则该特定日期的可能人口看起来像 110000000、011000000、001100000 等等。

作为 GA 的初始化,我想用两个参数(司机的工作时间和值类时间)动态生成可能的基因,如 000110000。

我设法在 bool 列表中获得了完全随机的二进制代码(见下文),但这不是我想要表示为初始化的内容。

这是在列表中生成随机二进制字符串(技术上是一堆 bool 值)的部分代码。

private Random Rnd = new Random();
        //initial data
    private List<bool[]> CreateInitialData()
    {
        //generate 4 random genes (might be more)
        return Enumerable.Range(0, 1).Select(_ =>
        {
            var array = new bool[GeneLength];
            for(int i = 0; i < GeneLength; i++)
            {
                array[i] = Rnd.Next(0, 2) == 1;
            }
            return array;
        }).ToList();
    }

如何实现初始化函数来生成满足要求的二进制代码(司机的工作时间,预计值类时间)? 如果有比 bool 列表更好的表示方式,也请提出建议。

最佳答案

基于 1 小时的任务,我想到了这个:

private static void Main(string[] args)
{
    var genes = GetGenes(9, 2);
}

private static List<bool[]> GetGenes(int workinghours, int estimateddutyduration)
{
    // get the base representation 
    var hours = GetHours(workinghours, estimateddutyduration);
    var list = new List<bool[]>();
    for (int i = 0; i < (workinghours-estimateddutyduration)+1; i++)
    {
        // add
        list.Add(hours);
        // switch
        hours = SwitchArray(hours);
    }
    return list;
}

private static bool[] SwitchArray(bool[] array)
{
    // copy the array to a list
    var temp = array.ToList();
    // insert the last element at the front
    temp.Insert(0, temp.Last());
    // remove the last
    temp.RemoveAt(temp.Count-1);
    // return as array
    return temp.ToArray();
}

private static bool[] GetHours(int totalhours, int taskduration)
{
    // initialise the list
    var hours = new List<bool>(totalhours);
    // fill the list for the number of working hours
    for (int i = 0; i < totalhours; i++)
    {
        hours.Add(false);
    }
    // iterate for the task duration and set the hours as working
    for (int i = 0; i < taskduration; i++)
    {
        hours[i] = true;
    }
    // return as array
    return hours.ToArray();
}

对于 9, 2 返回

110000000
011000000
001100000
000110000
000011000
000000110
000000011

对于 9, 9 返回

111111111

对于 9,返回 4

111100000
011110000
001111000
000111100
000011110
000001111

这段代码非常冗长,我相信它可以优化。但这是我最想传达的想法。

编辑:如果你想在控制台上显示结果

private static void ShowGenes(List<bool[]> genes)
{
    foreach (var gene in genes)
    {
        foreach (var bit in gene)
        {
            Console.Write(bit ? "1" : "0");
        }
        Console.Write("\n");
    }
}

关于c# - 在 C# 中用 GA 进行时间表调度的二进制数组中的基因初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43207556/

相关文章:

c# - 如何使用 C# 获取第 3 方应用程序按钮的句柄?

c# - 你如何确定哪些库没有被使用

c# - 如何快速定位图片中的空白(例如白色)

ios - 如何在 iOS UITableViewController 中初始化一个属性

java - 在构造函数上使用 init() 方法是一种不好的做法吗?

c - 是否保证全局变量总是用c99初始化为0?

c# - ASP.NET MVC : Register action filter without modifying controller

c++ - 为具有 1 和 2 字节字符的字符集实现退格

algorithm - 根据预先存在的主题自动生成摘要?

algorithm - 排序树与RB树和堆之间的Big-O