I'd just like to start of with saying thanks for all the help I've been getting so far. It's been imperative for my work!
I'm runing into a problem where my model is significantly slowed down due to dense constraints. In my bigger model I'm iterating over 864000 values and for every instance I need to note the value of the previous instance plus the instance of the current instance. (This is a battery state of charge problem).
Below you can find a simpel model that runs.
However I would like to reduce the value of "bat_SOC1" in every instance so that the expression dosen't become so large as in my real model the constraints become extremly dense due to size of the linear expression.
Something like this but for my expression would be what I need I imagine.
import gurobipy as gp
from gurobipy import GRB
#Declaring decision variables here. Change here to alter model.
up_soc_limit = 0.9
lower_soc_limit = 0.1
intake = 0.1 # kW, power capacity
output = 0.1 # kW, power capacity
in_eff = 0.9
out_eff = 0.9
bat_size = 0.3 # kWh
c_bat_up = bat_size * up_soc_limit #Used in constrains
c_bat_down = bat_size * lower_soc_limit
time = range(11)
e_s = [1,1,1,1,10,10,10,10,10,10,1] # EUR/MWh, sell
e_b = [10,10,10,1,100,100,0,0,0,100,8] # EUR/MWh, buy
m = gp.Model()
time1 = pd.DataFrame() #Create empty dataframe (matrix)
time1["Price_Sell"] = e_s #Add a collumn
time1["Price_Buy"] = e_b
time1["Time"] = time
bat_SOC1 = bat_size / 2
bat_SOC_test = bat_SOC1
tot_rev = 
for i, linerow in time1.iterrows():
bat_out = m.addVar(0,output, name = "sell" ) #Creates a output energy variable with 0 as the lower bound and out as the max bound for the batteries output
bat_in = m.addVar(0,intake, name = "buy") #Creates a charging energy variable with 0 as the lower bound and intake as the max bound for the batteries input
bat_SOC = m.addVar(c_bat_down,c_bat_up,name="bat_SOC")
revenue = bat_out * e_s[i] * out_eff - bat_in * e_b[i]
tot_rev.append(revenue) #add the variables input to the final calculation
bat_delta = bat_in * 0.9 - bat_out # Sums the total in - out that was used
bat_SOC1 += bat_delta #Adjusts the battery SoC to the energy that was used.
m.addConstr(bat_SOC1, GRB.EQUAL, bat_SOC)
bob = gp.quicksum(tot_rev)
m.setObjective(bob,GRB.MAXIMIZE) #Maximizes our revenue
print(bat_SOC1.size()) #Shows the size of our expression
How do I reduce the size of bat_SOC1 so that it only gives me the value at that instance? So at the end it would give me the final bat_SOC1 value?
My problem is that I need the variable bat_SOC to "know" it's value for every timestep and I haven't been able to solve this problem in any other way than this.
I hope I could get some help on this as it's making my larger model unusable due to the size of the expressions that are being generated.
Please sign in to leave a comment.