【发布时间】:2023-03-25 11:53:01
【问题描述】:
您好,我是编程新手,需要指导。
我们在种植园。我有许多包含不同数量树木的字段。在每个领域都必须完成一系列任务。任务相同,但时间不同,因为字段大小不同。我想生成一个与当天分配的工作时间最匹配的任务列表。
我相信这是一个作业车间调度问题(NP-hard),但据我所知,由于数据集很小,它可以通过蛮力搜索来解决。如何在指定时间内生成所有组合并返回最佳组合?我试图查看一些伪代码,但坦率地说,我很迷茫,而且我的尝试很差:
//暴力搜索
// 1. first(P): generate a first candidate solution for P.
// 2. next(P,c): generate the next candidate for P after the current one c.
// 3. valid(P,c): check whether candidate c is a solution for P-
// 4. output(P,c): use the solution c of P as appropriate to the application.
public static ArrayList<Task> generatedList2(int totalTime) {
ArrayList<Task> bestFit = new ArrayList<>();
ArrayList<Task> tmpFit = new ArrayList<>();
int tmpTime = 0;
int bestFitTime = -1;
Task testTask = new Task("TestTask", 0);
bestFit.add(testTask); //1
for(Field f : fields) { //2
for(Task t : f.getUndoneTasks()) {
if(f.getTaskTime(t) < totalTime) {
tmpFit.add(t);
tmpTime += f.getTaskTime(t);
totalTime -= f.getTaskTime(t);
}
}
if(tmpTime < bestFitTime) { //3
bestFit = new ArrayList<Task>(tmpFit);
bestFitTime = tmpTime;
tmpFit.clear();
}
else {
tmpFit.clear();
}
}
return bestFit; //4
}
更新解决方案:
public static ArrayList<Task> RecursivelyGetAnswer(ArrayList<Task> listSoFar,
ArrayList<Task> masterList, ArrayList<Task> bestList, int limit, int index) {
for (int i = index; i < masterList.size(); i++) {
Task task = masterList.get(i);
double listSoFarTotal = getTotal(listSoFar) + task.getTaskLength();
if (listSoFarTotal <= limit) {
int bestListTotal = getTotal(bestList);
listSoFar.add(task);
if (listSoFarTotal > bestListTotal) {
bestList = new ArrayList<Task>(listSoFar);
}
else if(100 - ((float) (limit - bestListTotal)/bestListTotal * 100) > 95) {
break;
}
bestList = RecursivelyGetAnswer(listSoFar, masterList, bestList, limit, i+1);
listSoFar.remove(task);
}
}
return bestList;
}
【问题讨论】:
-
您是否被限制在一天内工作一个领域?
-
不,我不是,但是我的代码可能会暗示这一点。我不知道如何构造它,以便您可以在所有领域工作。