Warning: linear constraint 0 and linear constraint 1 have the same name "Resourcebalane [1,0]"
Answeredimport numpy as np
import pandas as pd
import xlrd
import csv
from collections import OrderedDict
import math
import gurobipy as gp
from gurobipy import GRB
group2heats = {'1': [1, 2, 3, 4], '2': [5, 6, 7, 8], '3': [9, 10, 11, 12],
'4': [13, 14, 15, 16, 17], '5': [18, 19, 20], '6': [21, 22, 23, 24]}
equip2mw = {'EAF': 85, 'AOD': 2, 'LF': 2, 'CC1': 7, 'CC2': 7}
energy_price = [33.28, 30.00, 29.15, 28.49, 34.66, 50.01, #14 sept nord pool day ahead SE1
71.52, 77.94, 81.97, 87.90, 92.76, 94.98,
92.31, 90.03, 90.09, 87.44, 85.37, 79.97,
79.92, 77.83, 76.28, 65.06, 53.07, 34.16]
equip2process_time = {
'EAF1':{'1':80.0,'2':80.0,'3':80.0,'4':80.0,'5':85.0,'6':85.0,'7':85.0,'8':85.0,'9':90.0,'10':90.0,'11':90.0,'12':90.0,'13':85.0,'14':85.0,'15':85.0,'16':85.0,'17':80.0,'18':80.0,'19':80.0,'20':80.0,'21':80.0,'22':80.0,'23':80.0,'24':80.0},
'EAF2':{'1':80.0,'2':80.0,'3':80.0,'4':80.0,'5':85.0,'6':85.0,'7':85.0,'8':85.0,'9':90.0,'10':90.0,'11':90.0,'12':90.0,'13':85.0,'14':85.0,'15':85.0,'16':85.0,'17':80.0,'18':80.0,'19':80.0,'20':80.0,'21':80.0,'22':80.0,'23':80.0,'24':80.0},
'AOD1':{'1':75.0,'2':75.0,'3':75.0,'4':75.0,'5':80.0,'6':80.0,'7':80.0,'8':80.0,'9':95.0,'10':95.0,'11':95.0,'12':95.0,'13':85.0,'14':85.0,'15':85.0,'16':85.0,'17':85.0,'18':95.0,'19':95.0,'20':95.0,'21':80.0,'22':80.0,'23':80.0,'24':80.0},
'AOD2':{'1':75.0,'2':75.0,'3':75.0,'4':75.0,'5':80.0,'6':80.0,'7':80.0,'8':80.0,'9':95.0,'10':95.0,'11':95.0,'12':95.0,'13':85.0,'14':85.0,'15':85.0,'16':85.0,'17':85.0,'18':95.0,'19':95.0,'20':95.0,'21':80.0,'22':80.0,'23':80.0,'24':80.0},
'LF1':{'1':35.0,'2':35.0,'3':35.0,'4':35.0,'5':45.0,'6':45.0,'7':20.0,'8':20.0,'9':45.0,'10':45.0,'11':45.0,'12':45.0,'13':25.0,'14':25.0,'15':25.0,'16':25.0,'17':25.0,'18':45.0,'19':45.0,'20':30.0,'21':30.0,'22':30.0,'23':30.0,'24':30.0},
'LF2':{'1':35.0,'2':35.0,'3':35.0,'4':35.0,'5':45.0,'6':45.0,'7':20.0,'8':20.0,'9':45.0,'10':45.0,'11':45.0,'12':45.0,'13':25.0,'14':25.0,'15':25.0,'16':25.0,'17':25.0,'18':45.0,'19':45.0,'20':30.0,'21':30.0,'22':30.0,'23':30.0,'24':30.0},
'CC1':{'1':50.0,'2':50.0,'3':50.0,'4':50.0,'5':60.0,'6':60.0,'7':55.0,'8':55.0,'9':60.0,'10':60.0,'11':60.0,'12':60.0,'13':70.0,'14':70.0,'15':75.0,'16':75.0,'17':75.0,'18':60.0,'19':70.0,'20':70.0,'21':50.0,'22':50.0,'23':50.0,'24':50.0},
'CC2':{'1':50.0,'2':50.0,'3':50.0,'4':50.0,'5':60.0,'6':60.0,'7':55.0,'8':55.0,'9':60.0,'10':60.0,'11':60.0,'12':60.0,'13':70.0,'14':70.0,'15':75.0,'16':75.0,'17':75.0,'18':60.0,'19':70.0,'20':70.0,'21':50.0,'22':50.0,'23':60.0,'24':60.0}
}
stage2units = {'1': {'EAF': 2}, '2': {'AOD': 2}, '3': {'LF': 2}, '4': {'CC1': 1, 'CC2': 1}}
trans_time = {'TR_S1': 10, 'TR_S2': 4, 'TR_S3': 10}
trans_time_max = {'TR_S1': 240, 'TR_S2': 240, 'TR_S3': 120}
setup_time = {'CC1': 70, 'CC2': 50}
equip2process_time['EAF'] = equip2process_time.pop('EAF1')
equip2process_time.pop('EAF2')
equip2process_time['AOD'] = equip2process_time.pop('AOD1')
equip2process_time.pop('AOD2')
equip2process_time['LF'] = equip2process_time.pop('LF1')
equip2process_time.pop('LF2')
model = 'rtn1'
rtn_t0 = 15
price_energy = []
for price in energy_price:
price_energy = price_energy + [int(price)] * int(60 / rtn_t0) #prices for 96 time slots
num_t = 24 * 60 / rtn_t0 #total no of time slots i.e 96
num_stage = len((stage2units).keys()) #total no of stages i.e 4
num_groups = len((group2heats).keys()) #total no of groups i.e 6
num_heats = 0
for group, heats in group2heats.items():
num_heats += len(heats) #total no of heats i.e 24
equip2num = {} #calculating each eqipment has how many units
for unit2num in stage2units.values():
for unit, num in unit2num.items():
equip2num[unit] = num
res_cat2idx = OrderedDict()
r_idx = 1
# equipment resources #creating a ordered dict where at 1st position there is resource EAF, 2nd position there is resource AOD , 3rd position there is resource LF, 4th position there is resource CC1, 5th position there is resource CC2
for stage in range(1, num_stage + 1):
for unit in (stage2units)[str(stage)].keys():
res_cat2idx[unit] = [r_idx]
r_idx = r_idx + 1
# intermediate products and final products, A - after, B - before
for stage in range(1, num_stage+1):
res_cat2idx['H_A_S%s' % stage] = range(r_idx, r_idx + num_heats) #'H_A_S1'=EAd1 (after stage1) 'H_B_S4'=EAs4(before stage4) #'H_A_S4'=intermediate product after stage 4 i.e final products heats
r_idx = r_idx + num_heats
if int(stage) == 1:
continue
res_cat2idx['H_B_S%s' % stage] = range(r_idx, r_idx + num_heats)
r_idx =r_idx + num_heats
# energy resource
res_cat2idx['EN'] = [r_idx]
res_num = r_idx
[res_cat2idx, res_num]
tasks = OrderedDict()
i_idx = 1
for stage in range(1,num_stage + 1): #calculates in order how many processing and transfer tasks are there. task_num is 156 for only 3 stages.(24 heats * 6 types of tasks for 3 stages(3 processsng and 3 transfer))
for unit in (stage2units)[str(stage)].keys(): #CC1 and CC2 tasks are not counted in task_num
if int(stage) == 4:
tasks[unit] = range(i_idx, i_idx + num_groups)
i_idx = i_idx + num_groups
else:
tasks[unit] = range(i_idx, i_idx + num_heats)
i_idx = i_idx + num_heats
tasks['TR_S%s' % stage] = range(i_idx, i_idx + num_heats)
i_idx =i_idx + num_heats
task_num = i_idx - 1
[tasks, task_num]
task_duration = dict()
task_cleanup_duration = dict()
for heat in range(1, num_heats + 1): #for heat 1, EAF,AOD,LF,TR_S1,TR_S2,TR_S3 , similar for heat 2...so on...
for task_type in ['EAF', 'AOD', 'LF']:
task_duration[tasks[task_type][heat - 1]]=int(math.ceil(float((equip2process_time)[task_type][str(heat)])/rtn_t0))
for task_type in ['TR_S1', 'TR_S2', 'TR_S3']:
task_duration[tasks[task_type][heat - 1]]=int(math.ceil(float((trans_time)[task_type])/rtn_t0))
for group in range(1, num_groups + 1):
for task_type in ['CC1', 'CC2']:
cast_time = [(equip2process_time)[task_type][str(heat)] for heat in group2heats[str(group)]]
total_time = float(sum(cast_time))
task_duration[tasks[task_type][group - 1]] = int(math.ceil(total_time/rtn_t0))
total_time = float(sum(cast_time) + setup_time[task_type])
task_cleanup_duration[tasks[task_type][group - 1]] = int(math.ceil(total_time/rtn_t0))
task_duration,task_cleanup_duration
rtn_profile = dict()
for res_category, res_idxes in res_cat2idx.items(): #resources=res_cat2idx,created 174 blank dictionaries for 174 res_num
for res_idx in res_idxes:
rtn_profile[res_idx] = dict()
# equipment usage
for stage in range(1, num_stage + 1):
for unit in (stage2units)[str(stage)].keys(): #[RESOURCE][TASK]'[1][1-25]'(EAF) , '[2][49-73]'(AOD) ,'[3][97-121]'(LF),''[4][145-151]'(CC1) ,'[5][151-157](CC2)'
res = res_cat2idx[unit][0]
for task in tasks[unit]:
rtn_profile[res][task] = [-1] + [0] * (task_duration[task] - 1) + [1]
if stage == 4:
rtn_profile[res][task] = [-1] + [0] * (task_cleanup_duration[task] - 1) + [1]
# heat consumption and generation for the first three stages
for heat_idx in range(0, num_heats):
# process task generate intermediate heat H_A_S (after stage)
for task_cat, res_cat in [('EAF', 'H_A_S1'), ('AOD', 'H_A_S2'), ('LF', 'H_A_S3')]:
task = tasks[task_cat][heat_idx]
resource = res_cat2idx[res_cat][heat_idx]
rtn_profile[resource][task] = [0] * task_duration[task] + [1]
# process task consume intermediate heat H_B_S (before stage)
for task_cat, res_cat in [('AOD', 'H_B_S2'), ('LF', 'H_B_S3')]:
task = tasks[task_cat][heat_idx]
resource = res_cat2idx[res_cat][heat_idx]
rtn_profile[resource][task] = [-1] + [0] * task_duration[task]
# transfer task transports heat
for task_cat, res_cat in [('TR_S1', ['H_A_S1', 'H_B_S2']),('TR_S2', ['H_A_S2', 'H_B_S3']),('TR_S3', ['H_A_S3', 'H_B_S4'])]:
task = tasks[task_cat][heat_idx]
resource1 = res_cat2idx[res_cat[0]][heat_idx]
rtn_profile[resource1][task] = [-1] + [0] * task_duration[task]
resource2 =res_cat2idx[res_cat[1]][heat_idx]
rtn_profile[resource2][task] = [0] * task_duration[task] + [1]
# group-heat consumption and generation
heat_consume_time_in_group = dict()
heat_generate_time_in_group = dict()
for unit in (stage2units)['4']:
heat_consume_time_in_group[unit] = dict()
heat_generate_time_in_group[unit] = dict()
for group, heats in group2heats.items():
task = tasks[unit][int(group)-1]
duration = 0
for heat in heats:
consume_time = int(math.floor(duration/rtn_t0))
resource = res_cat2idx['H_B_S4'][heat - 1]
rtn_profile[resource][task] = [0] * (task_duration[task] + 1)
rtn_profile[resource][task][consume_time] = -1
duration += (equip2process_time)[unit][str(heat)]
generate_time = int(math.ceil(duration/rtn_t0))
resource = res_cat2idx['H_A_S4'][heat - 1]
rtn_profile[resource][task] = [0] * (task_duration[task] + 1)
rtn_profile[resource][task][generate_time] = 1
heat_consume_time_in_group[unit][heat] = consume_time
heat_generate_time_in_group[unit][heat] = generate_time
# energy usage
for stage, units in stage2units.items():
stage = int(stage)
for unit in units.keys():
norm_mw = float(equip2mw[unit])
for task in tasks[unit]:
total_energy = 0
if stage < 4:
heat = task - tasks[unit][0] + 1
total_energy = norm_mw * equip2process_time[unit][str(heat)] / 60
elif stage == 4:
group = task - tasks[unit][0] + 1
for heat in group2heats[str(group)]:
total_energy += norm_mw * equip2process_time[unit][str(heat)] / 60
profile = [norm_mw * rtn_t0 / 60] * (task_duration[task] - 1)
profile = profile + [total_energy - sum(profile)] + [0]
rtn_profile[res_cat2idx['EN'][0]][task] = profile
rtn_profile, heat_consume_time_in_group, heat_generate_time_in_group
time = list()
for x in range(0,int(num_t)+1):
time.append(x)
res_list = [res for cat, cat_res in res_cat2idx.items() for res in cat_res]
task_list=[task for cat, cat_task in tasks.items() for task in cat_task]
steel=gp.Model('steel RTN')
Resources=steel.addVars(res_list,time,vtype=GRB.INTEGER,name="Resources")
Tasks=steel.addVars(task_list,time,vtype= GRB.BINARY,name="Tasks")
res_list1 = [res for cat, cat_res in res_cat2idx.items() if cat != 'EN' for res in cat_res]
Resourcebalance = {}
for res in res_list1:
for t in time:
sumexpression = gp.LinExpr(0)
for task in task_list:
if task not in rtn_profile[res]:
continue
duration = len(rtn_profile[res][task]) - 1
for theta in range(0, min(duration, t) + 1):
sumexpression.add(rtn_profile[res][task][theta]* Tasks[task,t-theta] )
Resourcebalance[res,t] = steel.addConstr(Resources[res,t] ==
Resources[res,time[time.index(t)-1]] + sumexpression,
name="Resourcebalane[%s,%d]"%(res,t))
res = res_cat2idx['EN'][0]
Energyusage = {}
for ti in range(int(num_t)):
sumexpression = gp.LinExpr(0)
for task_cat, task_list1 in tasks.items():
if 'TR' in task_cat: # transportation has no energy consumption
continue
for task in task_list1:
duration = task_duration[task]
for theta in range(0, min(duration, ti) + 1):
sumexpression.add(rtn_profile[res][task][theta]* Tasks[task,ti-theta] )
Energyusage[res,ti] = steel.addConstr(Resources[res,ti] ==sumexpression,name="Energyusage[%s,%d]"%(res,ti))
heat2tasks = [task for task_cat, task_list2 in tasks.items()
if 'CC' not in task_cat for task in task_list2]
Taskexecution=steel.addConstrs((gp.quicksum(Tasks[task,t] for t in time)==1 for task in heat2tasks),name="Taskexecution")
group2tasks = dict()
for group in range(1, num_groups + 1):
group2tasks[group] = [tasks[caster][group - 1] for caster in stage2units['4'].keys()]
Taskexecution1=steel.addConstrs((gp.quicksum(Tasks[task,t] for t in time for task in group2tasks) ==1 for group in range(1, num_groups + 1)),name="Taskexecution1")
for res_cat, resources in res_cat2idx.items():
if 'H_A_' not in res_cat:
continue
print(resources)
resourcesafterprocessingtask= list(range(6,30)) + list(range(30,54)) + list(range(78,102)) + list(range(126,150))
Transfertime=steel.addConstrs((((Resources[res,t] for res in resourcesafterprocessingtask) == 0) for t in time),name ="Transfertime")
x={}
for res_cat, resources in res_cat2idx.items():
if 'H_B_' not in res_cat:
continue
max_wait_time = trans_time_max['TR_S%d' % (int(res_cat[-1]) - 1)]
min_tran_time = trans_time['TR_S%d' % (int(res_cat[-1]) - 1)]
for res in resources: # each heat a constraint
x[res] = (math.ceil((max_wait_time-min_tran_time)/rtn_t0))
Transfertime1= steel.addConstrs((gp.quicksum(Resources[res,t] for t in time )==x[res] for res in x.keys()),name="Transfertime1")
res_list3 = [res for cat, cat_res in res_cat2idx.items() if cat == 'H_B_S4' for res in cat_res]
x=[]
x.append(time[95])
Endresources=steel.addConstrs((gp.quicksum(Resources[res,t] for t in x)==1 for res in res_list3),name="Endresources")
reslist2=[]
reslist2.append(res_cat2idx['EN'][0])
obj=gp.quicksum(price_energy[t-1] for t in time)*gp.quicksum(Resources[res,t] for res in reslist2 for t in time)
steel.setObjective(obj,GRB.MINIMIZE)
steel.optimize()
My Model is showing this error. I tried many times using lp.write or IIS , but I am stuck like what this is stating, I have decoded 5 times but still I am here.
Please can anyone throw some light ,like I have tried to check what is the issue but I am not able to resolve it.

0
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
You get the Warning because multiple constraints have the same name. The solution is described in your other post Nested Summation constraint for MILP problem.
0
Post is closed for comments.
Comments
2 comments