python - Stop iterating over nested loops and continue with most outest loop -
i coding heuristic optimization problem field of production. in heuristic have various conditions, stop criteria etc. in order account these different criteria, worked multiple nested loops, can see in code below:
for tao in periods: print ("iteration:", tao) print ("-----------------------------------------") print (setup_items) z in range(1,periods_count+1-tao): print("z =",z) k in setup_items[tao+z]: print("k =",k) #### exception 1 if production.loc[k][tao] == 0: print("there no setup in period product {}.".format(k)) counter =+ 1 continue #### exception 2 if demand.loc[k][tao+z] > spare_capacity[tao]['spare capacity']: print("capacity in period {} insufficient pre-produce demands product {} period {}.\n".format(tao, k, tao+z)) counter =+ 1 continue if counter == k: print("stop criterion 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)) t in range(tao,periods_count+1): k in products: spare_capacity[t] = capacity[t][1]-sum(production.loc[k][t] k in products) setup_items = [[] t in range(0,periods_count+1)] t in periods: 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')
the idea is, if 1 tao
, there exception true k
, loops terminate early, apart outer one, go next tao
in periods
, starts again.
i tried use counter
variable, did not turn out functioning well.
i have example output (extract):
z = 1 k = 1 capacity in period 1 insufficient pre-produce demands product 1 period 2. k = 2 capacity in period 1 insufficient pre-produce demands product 2 period 2. z = 2 k = 2 capacity in period 1 insufficient pre-produce demands product 2 period 3.
after k=2
in z=1
the iteration should terminate, keeps on checking further z
values.
could give me tip how solve issue? read putting loops functions, 1 can break out of multiple loops, not sure how formulate here, have multiple points of exit..
thanks!
python not have control breaking out of multiple loops @ once.
you can set flag , break out of multiple loops, more info link
Comments
Post a Comment