我正在为生产领域的优化问题编写启发式编码。在这个启发式中,我有各种条件,停止标准等。为了解释这些不同的标准,我使用了多个嵌套循环,如下面的代码所示:
for tao in PERIODS:
print ("Iteration:", tao)
print ("-----------------------------------------")
print (SETUP_ITEMS)
for z in range(1,periods_count+1-tao):
print("z =",z)
for k in SETUP_ITEMS[tao+z]:
print("k =",k)
#### EXCEPTION 1
if production.loc[k][tao] == 0:
print("There is no setup in this period for product {}.".format(k))
counter =+ 1
continue
#### EXCEPTION 2
if demand.loc[k][tao+z] > spare_capacity[tao]['Spare Capacity']:
print("Capacity in period {} is insufficient to pre-produce demands for product {} from period {}.\n".format(tao, k, tao+z))
counter =+ 1
continue
if counter == k:
print("Stop Criterion is met!")
break
##########################################################################
if SM == 1:
if SilverMeal(k,z) == True:
print("Silver Meal Criterion is", SilverMeal(k,z))
production.loc[k][tao] += demand.loc[k][tao+z]
production.loc[k][tao+z] = 0
else:
print("Else: Silver Meal Criterion is", SilverMeal(k,z))
for t in range(tao,periods_count+1):
for k in PRODUCTS:
spare_capacity[t] = capacity[t][1]-sum(production.loc[k][t] for k in PRODUCTS)
SETUP_ITEMS = [[] for t in range(0,periods_count+1)]
for t in PERIODS:
for k in PRODUCTS:
if production.loc[k][t]==(max(0,demand.loc[k][t]-stock.loc[k][t-1])) > 0:
SETUP_ITEMS[t].append(k)
print(productionplan(production,spare_capacity,CF), '\n\n')
print(productionplan(production,spare_capacity,CF), '\n\n')
这个想法是,如果对于一个tao
,所有k
都有一个例外,所有的循环都会提前终止,除了最外层的循环,所以我们会去tao
中的下一个PERIODS
,一切都重新开始。
我尝试将其与counter
变量一起使用,但这并没有真正发挥作用。
我目前有这个输出(提取):
z = 1
k = 1
Capacity in period 1 is insufficient to pre-produce demands for product 1 from period 2.
k = 2
Capacity in period 1 is insufficient to pre-produce demands for product 2 from period 2.
z = 2
k = 2
Capacity in period 1 is insufficient to pre-produce demands for product 2 from period 3.
在k=2
z=1
之后,迭代应终止,但它会继续检查z
个值。
有人能给我一个提示如何解决这个问题吗?我读到了将循环放入函数中,以便可以打破多个循环,但我不知道如何在这里制定它,因为我会有多个退出点。
谢谢!