I'm starting to use pulp for lineal optimization in python. I am creating a program for Demand Side Management, which reduces the tariff paid by the users by lowering the power of some appliances (power shiftable loads) and by shifting the energy consumption in time in response of the to tariff (time of use tariff) of other appliances (time shiftable loads). There are also loads that cannot be shifted (non shiftable loads). Therefore each hour, the paid price is = (Total Non shiftable loads consumption + power shiftable loads consumption + time shiftable loads consumption) *price (in that hour). For the power shiftable loads, I have no problem. For the time shiftable loads, first I am moving the start time (ts_af) for each appliance. Then the program verifies at each hour which appliances are ON according to their new start time (tss_aff). When an appliance is ON, the value of Xaft is the value of its power, and when it is off the value es zero. Sum_Xaft is the sum of the value of Xaft of all the appliances at this hour.
The problem is that when finding Xaft, it is always = to their power, even if the appliance is off. Therefore Sum_Xaft is the sum of the power of all the appliances instead of only the power of the appliances that are ON.
For some reason the program always take the first condition as true giving Xaft a value even if the condition (Time= tss_af) is false.
Here is part of the code:
# Import data
nonShiftableLoads = pd.DataFrame.from_csv('csvS2/Non_shiftable_loads.csv', index_col=['Home', 'Time'])
PowerShiftableLoads = pd.DataFrame.from_csv('csvS2/Power_shiftable_loads.csv', index_col=['Home', 'Time', 'PSLoad'])
TimeShiftableLoads = pd.DataFrame.from_csv('csvS2/Time_shiftable_loads2.csv', index_col=['Home', 'TSLoad'])
TOUPrices = pd.DataFrame.from_csv('csvS2/TOUPrice.csv', index_col=['Time'])
TSLindex = pd.DataFrame.from_csv('csvS2/TimeShiftableLoadIndex.csv', index_col=['Time', 'Home', 'Load'])
# define decision variables:
Xajt = pulp.LpVariable.dicts('Xajt', ((Home, Time) for Home, Time, in nonShiftableLoads.index), lowBound=0,
cat='Integer')
dajt = pulp.LpVariable.dicts('dajt', ((Home, Time) for Home, Time, in nonShiftableLoads.index), lowBound=0,
cat='Integer')
Cdajt = pulp.LpVariable.dicts('Cdajt', ((Home, Time) for Home, Time, in nonShiftableLoads.index), lowBound=0,
cat='Integer')
Xaft = pulp.LpVariable.dicts('Xaft', ((Time, Home, Load) for Time, Home, Load, in TSLindex.index),
lowBound=0, cat='Integer')
tss_af = pulp.LpVariable.dicts('tss_af', ((Home, TSLoad) for Home, TSLoad, in TimeShiftableLoads.index),
lowBound=0, cat='Integer')
tes_af = pulp.LpVariable.dicts('tes_af', ((Home, TSLoad) for Home, TSLoad, in TimeShiftableLoads.index),
lowBound=0, cat='Integer')
Sum_Xaft = pulp.LpVariable.dicts('Sum_Xaft', ((Home, Time) for Home, Time in nonShiftableLoads.index), lowBound=0,
cat='Integer')
daft = pulp.LpVariable.dicts('daft', ((Home, TSLoad) for Home, TSLoad, in TimeShiftableLoads.index),
lowBound=0, cat='Integer')
Adaft = pulp.LpVariable.dicts('Adaft', ((Home, TSLoad) for Home, TSLoad in TimeShiftableLoads.index),
lowBound=0, cat='Integer')
CAdaft = pulp.LpVariable.dicts('Adaft', ((Time, Home, Load) for Time, Home, Load in TSLindex.index),
lowBound=0, cat='Integer')
Sum_daft = pulp.LpVariable.dicts('Sum_daft', ((Home, Time) for Home, Time in nonShiftableLoads.index), lowBound=0,
cat='Integer')
# State objective function
model += pulp.lpSum([Xajt[Home, Time] * TOUPrices.loc[(Time), 'Pt'] for Home, Time in nonShiftableLoads.index]
+ [Sum_Xaft[Home, Time] * TOUPrices.loc[(Time), 'Pt'] for Home, Time in nonShiftableLoads.index]
+ [nonShiftableLoads.loc[(Home, Time), 'Sum_Xait'] * TOUPrices.loc[(Time), 'Pt'] for Home, Time in
nonShiftableLoads.index]
# +[0.015* Sum_daft[Home,Time] for Home, Time in nonShiftableLoads.index] (I have not calculated it yet
+ [0.015 * Cdajt[Home, Time] for Home, Time in nonShiftableLoads.index])
This is the part when I set the limits for the start time of every appliance:
for Home, TSLoad in TimeShiftableLoads.index:
maxhaste_ts = TimeShiftableLoads.loc[(Home, TSLoad), 'maxhaste_ts']
maxdelay_ts = TimeShiftableLoads.loc[(Home, TSLoad), 'maxdelay_ts']
n_af = TimeShiftableLoads.loc[(Home, TSLoad), 'n_af']
ts_af = TimeShiftableLoads.loc[(Home, TSLoad), 'ts_af']
demand = TimeShiftableLoads.loc[(Home, TSLoad), 'Demand']
model += tss_af[Home, TSLoad] <= maxdelay_ts
model += tss_af[Home, TSLoad] >= maxhaste_ts
model += tes_af[Home, TSLoad] == tss_af[Home, TSLoad] + n_af
And this is the problematic part:
for Time, Home, Load in TSLindex.index:
demand = TimeShiftableLoads.loc[(Home, Load), 'Demand']
n_af = TimeShiftableLoads.loc[(Home, Load), 'n_af']
if tss_af[Home, Load] == Time:
model += Xaft[Time, Home, Load] == demand
elif tss_af[Home, Load] > Time:
model += Xaft[Time, Home, Load] == 0
elif Time - tss_af[Home, Load] <= n_af- 1:
model += Xaft[Time, Home, Load] == demand
else:
model += Xaft[Time, Home, Load] == 0
Even if Time != tss_af[Home, Load], the program always assign a value for Xaft. When I use ts_af (the initial start time), there is no problem, but with tss_af, I have this problem, I've tried using a condition before the first 'if', to see if tss_af[Home, Load] == None, and the program executes the action.
Can somebody tell me what I am doing wrong? Why does the program skip the rest of the options? After that, I am finding Sum_Xaft, which is done with no problem and telling the program to solve the problem with model.solve()
Aucun commentaire:
Enregistrer un commentaire