我正在使用优先级矩阵优化作业订单。 例如;在作业6完成之前不能执行作业8。如果是这种情况,我在位置[8,6]上的优先矩阵显示一个。如果没有优先关系,例如对于位置[1,2],则它显示零。
在我当前的代码中,如果优先级矩阵显示为1,我使用'next'命令进入下一次迭代。但是,现在它的编程方式只是转到下一个j,而我想要它转到下一个'我'(下一个要考虑的工作)。但是,i是if循环之前的一个循环,其中使用了'next'。有没有办法让它转到for(i in n:jobs)循环而不是下一个for(j in n:jobs)?
在我当前的代码下面
# Initialisatie
nJobs = 10
jobDone = rep(FALSE, nJobs)
currentJob = 1;
jobDone[currentJob] = TRUE;
totalFine = 0;
finishingTime = durations[currentProject]
jobOrder = c(currentJob);
jobFines = c(totalFine);
jobIndFines = c(totalFine)
jobTimes = c(finishingTime)
jobIndTimes = c(durations[currentJob])
for(iter in 1:(nJobs-1))
{
bestFine = -1;
bestOrder = -1;
for(i in 1:nJobs)
{
for(j in 1:nJobs){
if(precedences[i,j] == 1){ next } #hoe kom ik nu naar de volgende i ipv de volgende j?
else{
if( ! jobDone[i] )
{
# If this is the first option or the cheapest option, we update the best option
if(bestOrder == -1 || ((finishingTime+durations[i])-deadlines[i])*fines[i] > bestFine)
{
bestFine = ((finishingTime+durations[i])-deadlines[i])*fines[i];
bestOrder = i;
}
}
}
}
}
# Add the best job to the order of jobs
jobDone[bestOrder] = TRUE;
jobOrder[iter+1] = bestOrder;
# Update the current order of projects
fine = ((finishingTime+durations[bestOrder])-deadlines[bestOrder])*fines[bestOrder]
jobIndFines[iter+1] = fine
if (fine>0){
totalFine = totalFine + fine;
}
jobFines[iter+1] = totalFine;
#jobIndFines[iter+1] = totalFine - jobFines[iter];
finishingTime = finishingTime + durations[bestOrder];
jobTimes[iter+1] = finishingTime
jobIndTimes[iter+1] = durations[bestOrder]
}
totalFine
finishingTime
jobOrder
jobFines
jobTimes
jobIndTimes
jobDone
jobIndFines
非常感谢!!
答案 0 :(得分:0)
尝试break
。
这些代码段说明了next
和break
之间的区别。
next
在 next 索引值处重新启动当前循环:
for (i in 1:2) {
for (j in 1:2) {
print(paste("j =", j))
next
print("Python is better than R at everything.") # Never prints, obviously.
}
print(paste("i =", i))
}
#> [1] "j = 1"
#> [1] "j = 2"
#> [1] "i = 1"
#> [1] "j = 1"
#> [1] "j = 2"
#> [1] "i = 2"
break
完全让你退出当前循环并将控制传递给循环外的下一个语句:
for (i in 1:2) {
for (j in 1:2) {
print(paste("j =", j))
break
print("Python is better than R at everything.") # Never prints, obviously.
}
print(paste("i =", i))
}
#> [1] "j = 1"
#> [1] "i = 1"
#> [1] "j = 1"
#> [1] "i = 2"