如何在Javascript

时间:2016-12-19 11:14:28

标签: javascript node.js genetic-algorithm

我只是想在N号序列中找到正确的序列。我所说的是假设我们有5台机器,在这些机器上有20个工作要做。我们的概率是20!这是正确的顺序是2,432,902,008,176,640,000。最好的顺序基于完成时间。我们必须找到它。 不幸的是,我有点困惑,如何获得正确和最佳的时间效率序列。 在产生序列的可能性后,我陷入困境。我不知道如何获得正确的序列

我的尝试

var howManyMachines = 2;
var Sequenxe = [
    {
        jobId:1,
        timeToFinish:5
    },
    {
        jobId:2,
        timeToFinish:4
    },
    {
        jobId:3,
        timeToFinish:4
    }


];
var machines = Array(howManyMachines).fill().map((m, i) => {
    var Mindex = i;
    if(i == 0){
        Mindex = 1
    }else{
        Mindex = i+1
    }
    return {
        id: i,
        value: 0,
        jobs: [],
        name:"M"+Mindex

    } });
function permutations(items) {
    if (items.length == 1) return [items];
    var combos = [];
    for (var i = 0; i < items.length; i++) {
        var first = items[i], rest = items.slice(0);
        rest.splice(i, 1);
        permutations(rest).forEach(function(combo){
            combo.unshift(first);
            combos.push(combo);
        });
    }
    return combos;
}



const allSequence = permutations(Sequenxe);


console.log(allSequence.length+" Sequence to test")
console.log(machines.length+" Machines Available");



allSequence.forEach(singleSequence => {
    console.log("===>",singleSequence)
   //I don't Know what to do

});

3 个答案:

答案 0 :(得分:0)

我认为获得完美解决方案的唯一方法是检查所有可能性。 如果你关心性能,这应该但是这应该在大多数情况下给你一个正确的解决方案,同时相当快......

主要步骤区域:

  1. 按timeToFinish排序作业,最长到最短
  2. 将第一份作业添加到最短的帖子
  3. 按总执行时间排序,最短到最长
  4. 转到2然后重复,直到找不到更多工作
  5. var machines = 2;
    var jobs = [{
      jobId: 1,
      timeToFinish: 5
    }, {
      jobId: 2,
      timeToFinish: 4
    }, {
      jobId: 3,
      timeToFinish: 4
    }];
    
    jobs.sort((a, b) => b.timeToFinish - a.timeToFinish);
    
    var threads = new Array(2).fill({
      jobs: [],
      totalTime: 0
    });
    
    while (jobs.length > 0) {
      threads = threads.map(t => {
        j = jobs.shift();
        return j ? {
          jobs: t.jobs.concat(j),
          totalTime: t.totalTime + j.timeToFinish
        } : t;
      });
      threads.sort((a, b) => a.totalTime - b.totalTime);
    }
    
    console.log(JSON.stringify(threads, null, 2))

答案 1 :(得分:0)

根据完成时间 听起来像deadline scheduling

提前规划这些大型工作听起来像knapsack problem。我试试knapsack.js。源代码位于GitHub

答案 2 :(得分:0)

你可以这样做;它将为每个作业生成20个随机时间的作业,然后将它们均匀地分配到5台机器中。

function groupTasks(jobs,machineCount){
  var  sum = jobs.reduce((p,c) => p + c.time, 0),
   initial = [...Array(machineCount)].map(sa => (sa = [], sa.sum = 0, sa));
   console.log("total number of jobs:",jobs.length,"\n");
   console.log("total job time:", sum,"\n");
   console.log("number of machines:", machineCount,"\n");
   console.log("target total job time per machine:", sum/machineCount,"\n");
  return jobs.sort((a,b) => b.time-a.time)
             .reduce((machines,job) => { var machine = machines.reduce((p,c) => p.sum < c.sum ? p : c);
                                         machine.push(job);
                                         machine.sum += job.time;
                                         return machines;
                                       },initial);
}

var jobs = [...Array(20)].map((_,i) => ({id:i, time:~~(Math.random()*10)+1})),
  result = groupTasks(jobs,5);
console.log("jobs: \n", JSON.stringify(jobs));
console.log("jobs per machine:","\n",JSON.stringify(result));