Gurobi does not optimize result (always the same result)
回答済みI run an optimization on when to use different devices based on CO2 signals.
Everything works. All constraints are bein implemented by Gurobi.
My issue now is, that the result are always the same on not optimal.
No matter whether I set GRB.MINIMIZE, GRB.MAXIMIZE. The results stays the same.
Is there a typical step which that I miss?
Here is the code.
import os
from gurobipy import *
import xlrd
'''
author: Kanan Allahyarli
'''
'''SETS'''
'''different types of power plants
plants = ['Biomass', 'Coal', 'Gas', 'Hydro', 'Nuclear', 'Oil', 'Wind', 'Solar',
'Unknown', 'Geothermal', 'Hydro_Discharge']'''
plant = ["Mix"]
'''one average household'''
household = 'household'
'''three devices'''
appliances = ['WM', 'DW', 'TD']
'''optimization horizon: 365 days, 1 hour resolution'''
hour_steps = range(24)
day_steps = range(6)
week_steps = range(2)
'''PARAMETERS'''
'''carbon intensity from consumption and imports'''
carbon_intensity = {}
multiplication_index = 0
day = 0
xl = xlrd.open_workbook('DE.xls')
sh = xl.sheet_by_name('Sheet1')
'''create a mulitdict with carbon intensity values seperated by days'''
for y in range(2):
#lasse jede woche die tag um 7 springen
for j in range(6):
j=(j+1)+(multiplication_index*23)
temp_val_array = []
#day = sh.cell_value(j,0)
for i in range(24):
i=(i+1)+(multiplication_index*24)
temp_val_array.append(sh.cell_value(i,3))
carbon_intensity[day] = temp_val_array
#print(carbon_intensity)
multiplication_index = multiplication_index + 1
day = day + 1
'''add carbon emission factor'''
carbon_emission_factor = {'biomass': 230, 'coal': 820, 'gas': 490, 'geothermal': 38,
'hydro': 24, 'nuclear': 12, 'oil': 650, 'wind': 11,
'solar': 45, 'unknown': 700, 'hydro_discharge': 345}
'''technical characteristics and limits of power plant's CO2 emission in CO2g/kW'''
plants_char = {'Carbon': {'intensity_min': 0 , 'intensity_max': carbon_intensity}
}
'''Each user has two loads to schedule: Wasching machine WM, dish washer DW'''
'''EST means earliest starting time, LET means latest ending time, E is the energy consumed, LOT is the length of operation'''
CED = {'WM': {'EST': 8, 'LET': 24, 'E': 0.89, 'LOT': 3},
'DW': {'EST': 1, 'LET': 24, 'E': 1.19, 'LOT': 3},
'TD': {'EST': 8, 'LET': 24, 'E': 2.45, 'LOT': 2},
}
working = tuplelist([('TD', 0), ('WM',0), ('DW',0), ('WM',1), ('DW',1), ('TD',1), ])
'''MODEL'''
model = Model('Optimal Scheduling')
'''add variables'''
#co2_gen = model.addVars(plants, time_steps, lb = 0, vtype=GRB.CONTINUOUS, name="co2_gen") # generated co2 intensity from plants
co2_gen = model.addVars(plant, day_steps, hour_steps, lb = 0, vtype=GRB.CONTINUOUS, obj=carbon_intensity, name="co2_gen") # generated co2 intensity from plants
tot_d = model.addVars(day_steps, hour_steps, lb = 0, vtype=GRB.CONTINUOUS, name="total_demand") # total demand
status1 = model.addVars(working, hour_steps, vtype=GRB.BINARY, name="Dev_status_user1") # status of devices of user1
st_start1 = model.addVars(working, hour_steps, vtype=GRB.BINARY, name="start_status_user1")
st_end1 = model.addVars(working, hour_steps, vtype=GRB.BINARY, name="end_status_user1")
model.setObjective(quicksum(co2_gen["Mix",d, h] for d in day_steps for h in hour_steps), GRB.MINIMIZE)
'''add constraints'''
'''emission and consumption have to be balanced'''
'''model.addConstrs((quicksum(p_gen[pp,t] for pp in plants) - quicksum(tot_d[u,t] for u in users) + p_grid[t] == 0 for t in time_steps),name='balance')'''
'''generation limit: emissions of power plant are deterministic'''
model.addConstrs((co2_gen['Mix',d,h] == plants_char['Carbon']['intensity_max'][d][h] for d in day_steps for h in hour_steps), name='Carbon_output')
'''DEMAND SIDE-User1'''
'''total demand of users'''
model.addConstrs((tot_d[day,h] == quicksum(CED[a]['E']*status1[a,d,h] for a,d in working) for day in day_steps for h in hour_steps), name='U1totd')
'''device start and end'''
'''the appliance starts each day'''
#model.addConstrs((quicksum(st_start1[a,d,h] for d in day_steps for h in hour_steps if h >= 0 or h <= 23) == 1 for a in appliances), name='U1dailystart')
'''the appliance starts only once between EST and LET-LOT'''
model.addConstrs((quicksum(st_start1[a,d,h] for h in hour_steps if h >= CED[a]['EST'] or h <= CED[a]['LET']-CED[a]['LOT']) == 1 for a,d in working), name='U1singlestart')
'''the appliance is turned off once between EST+LOT and LET'''
model.addConstrs((quicksum(st_end1[a,d,h] for h in hour_steps if h <= CED[a]['LET'] or h >= CED[a]['EST']+CED[a]['LOT']) == 1 for a,d in working), name='U1singleend')
'''appliance cannot turn on or off outside EST and LET'''
model.addConstrs((st_start1[a,d,h] == 0 for a,d in working for h in hour_steps if h < CED[a]['EST'] or h > CED[a]['LET']-CED[a]['LOT']), name='U1startzero')
model.addConstrs((st_end1[a,d,h] == 0 for a,d in working for h in hour_steps if h < CED[a]['EST']+CED[a]['LOT'] or h > CED[a]['LET']), name='U1endzero')
'''appliance is turned off after running LOT time steps'''
model.addConstrs((st_end1[a,d,h] == st_start1[a,d,h-CED[a]['LOT']] for a,d in working for h in hour_steps if h >= CED[a]['LOT']), name='U1startend')
'''TD can only run after WM'''
model.addConstrs((st_start1['TD',d, h] == st_end1['WM', d, h] for a,d in working for h in hour_steps if a=='TD'), name='TDafterWM')
'''device status'''
'''appliance is on exactly LOT time steps'''
model.addConstrs((quicksum(status1[a,d,h] for h in hour_steps if h >= CED[a]['EST'] or h <= CED[a]['LET']) == CED[a]['LOT'] for a,d in working), name='U1duration')
'''appliance status cannot be 1 outside EST and LET'''
model.addConstrs((status1[a,d,h] == 0 for a,d in working for h in hour_steps if h < CED[a]['EST'] or h > CED[a]['LET']), name='U1statuszero')
'''appliance status is 1 before being turned off'''
model.addConstrs((quicksum(status1[a,d,tau] for tau in range(h)) >= CED[a]['LOT']*st_end1[a,d,h] for a,d in working for h in hour_steps), name='U1endafterLOT')
'''appliance status is 1 after being turned on'''
model.addConstrs((quicksum(st_start1[a,d,tau] for tau in range(h+1)) >= status1[a,d,h] for a,d in working for h in hour_steps), name='U1statusafterstart')
'''OBJECTIVE'''
'''obj1 minimizes the total co2 intentisty generated during active device time'''
obj2 = (quicksum(tariff[u][t]*tot_d[u,t]*1/100 for u in users for t in time_steps) - quicksum(price[pp]*p_gen[pp,t]*1/100 for pp in plants for t in time_steps))
model.optimize()
msgdict = {GRB.OPTIMAL : 'Optimal', GRB.INFEASIBLE : 'Infeasible model'}
msg = msgdict[model.status]
if msg == 'Infeasible model':
print('The model is infeasible; computing IIS')
model.computeIIS()
print('\nThe following constraint(s) cannot be satisfied:')
for c in model.getConstrs():
if c.IISConstr:
print('%s' % c.constrName)
for v in model.getVars():
if v.X != 0:
print("%s %f" % (v.Varname, v.X))
print('\n')
-
正式なコメント
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 why not try our AI Gurobot?. -
Hi Kanan!
You could write an LP file of your model to better see whether it actually represents what you are trying to model:
model.write('model.lp')
Your code is, unfortunately, a bit hard to read/run because the indention is lost.
Cheers,
Matthias0 -
Yeah... I did not know the command for the code to be represented in the right way.
0
投稿コメントは受け付けていません。
コメント
3件のコメント