Dimension Mismatch in variables for optimization
回答済みI have a formula that looks as below
T = 24
t = np.linspace(1, T, num=T) #linspace equal intervals in the 24 hrs
N = 286 #has 286 light bulbs
Ppvt = list(dataset['summer profile'])
print(Ppvt)
[0.0, 0.0, 0.0, 0.0, 0.119215418, 0.47466921799999995, 2.544781328, 3.5552832160000003, 3.873314614, 4.450941625, 4.9872334369999995, 5.207509807, 5.082005476, 4.564966206, 3.6020937280000003, 2.859118978, 3.07072322, 1.670160364, 0.47174145100000003, 0.004510168, 0.0, 0.0, 0.0, 0.0]
#ASSIGN VARIABLES
pgridabs = model.addVars(T,1,name ='power absorbed from grid')
Psl = model.addVars(N,T, name ='supplemental lighting power')
model.addConstrs(pgridabs[t] + Ppvt[t] == (gp.quicksum(Pslt[n,t])for n in range (N) for t in range[t]))
The error reads ====as below, the variable that I have created 'pgridabs' expressed as
pgridabs = model.addVars(T,1,name ='power absorbed from grid') or
pgridabs = model.addVars(T,name ='power absorbed from grid')
both are seeming to create the problem.. is there a way to solve this?
TypeError Traceback (most recent call last) <ipython-input-22-dd84db46d292> in <module> 56 57 #ENERGY BALANCE ---> 58 model.addConstrs(pgridabs[t] + Ppvt[t] == (gp.quicksum(Pslt[n,t])for n in range (N) for t in range[t])) 59 #dimension is mot 60 TypeError: unhashable type: 'numpy.ndarray'
-
正式なコメント
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,
There is a couple of things happening here.
You are using terms \(\texttt{Pslt}\) which are not defined in your post. I assume, you wanted to use \(\texttt{Psl}\). The way you define list \(\texttt{t}\) it is given as
t = [1.0, 2.0, ..., 24.0]
Then you define the optimization variables \(\texttt{pgriabs}\) over \(\texttt{T, 1}\). Defining the variables over \(\texttt{T}\) results in the variables having indices ranging from 0 - 23 and not 1 - 24. Moreover, the additional 1 dimension only makes accessing the variables more complex, because now you have to access the \(\texttt{pgridabs}\) variables via
print(pgridabs[0,0])
#instead of
print(pgridabs[0])When constructing the constraint, you are trying to go over \(\texttt{range[t]}\). However \(\texttt{t}\) is already a list of floating values. Moreover, when using the quicksum function, you have to provide data generated by, e.g., a list instead of just one variable entry. The constraint construction should most likely read
model.addConstrs(pgridabs[t1-1,0] + Ppvt[int(t1-1)] == gp.quicksum(Psl[n,t1-1] for n in range(N)) for t1 in t)
Note that you have to convert the value \(\texttt{t1-1}\) to an integer, because indices cannot be \(\texttt{numpy.float64}\) for python lists.
Best regards,
Jaromił0 -
Dear Jaromil,
Thank you very much for the reply i am following ur advice and would like to ask if you can review this adjustment.
So, after this I assigned our time into a list of integers ranging from 0-23 instead of floats from 1-24
#ASSIGN TIME FRAME
T = 23
t = np.linspace(0, T, 24) #linspace equal intervals in the 24 hrs
int_t= t.astype(int) #make into integers not numpy.float64
t1 = int_t.tolist() #make into listN = 286 #has 286 light bulbs
Which also makes us adjust the variable assignment
#ASSIGN VARIABLES
pgridabs = model.addVars(t1,1,name ='power absorbed from grid')
Psl = model.addVars(N,t1, name ='supplemental lighting power')Then, adjusted the energy balance as below (the suggestion was to have t1-1, but since I already assigned t1 as a list of int from 0-23 , I assumed this would be appropriate?
#ENERGY BALANCE
model.addConstrs(pgridabs[t1,0] + Ppvt[int(t1)] == quicksum(Psl[n,t1] for n in range(N)) for t in range (t1))There is still a dimension error. I am seeing that I lack a bit of foundation on python but I am really trying to get this. Is there something that I am confusing here again..?
0 -
Hi,
The object \(\texttt{t1}\) is a list. Thus, you cannot generate the range of it, i.e., \(\texttt{range(t1)}\) does not work.
Additionally, \(\texttt{t}\) is a list object and simultaneously used in the \(\texttt{for}\)-loop iterating over the elements of \(\texttt{t1}\). The following code tackles these issues:
model.addConstrs(pgridabs[ti,0] + Ppvt[ti] == gp.quicksum(Psl[n,ti] for n in range(N)) for ti in t1)
Best regards,
Jaromił0 -
Dear Jaromil,
Thank you for your quick reply!
my last constraint with quicksum is basically multiplying all n (lightbulb) over t1 (time frame from 0-23) with a column from a csv file with binary variables to indicate which times the lights are turned on (1) and off (0) which is a dataframe.
1. Would the multiplication of a dataframe and the work in this case..?
2. (follow up q from ur above reply, [n.ti] what is ti indicating..?
#supplemental lighting input data
hoursof_sl = pd.read_csv ('hoursofsl.csv',
#encoding= 'unicode_escape',
index_col= 'Hour',
sep = ',')
print(hoursof_sl)Supplemental Lighting Hour 0 0 1 0 2 0 3 0 4 0 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 0 22 0 23 0
#Supplemental Lighting
p_sl_min = 0.054
#per lamp each hour
#p_sl_minn == 15.4 #all 286 lamps hour kWh
p_sl_bet = 17
p_sl_max = 0.066
#p_sl_maxx == 18.8 #each hour all 286 cannot exceed this power use
p_sl_ttl = 292.864 #all 286 light bulbs max power use per day
#each light bulb cannot exceed or fall behind the min and max
model.addConstrs(Psl[n,ti] <= p_sl_max for ti in t for n in range (N))
model.addConstrs(Psl[n,ti] >= p_sl_min for ti in t for n in range (N))
#supplemental lighting constraint multiplying with time so that it operates during 0500-2300
model.addConstrs(gp.quicksum(Psl[n,ti]* hoursof_sl for t in ti) <= p_sl_ttl for n in range(N))0 -
Hi Olivia,
1. Would the multiplication of a dataframe and the work in this case..?
You have to access the values of the dataframe. Please refer to Python tutorials on dataframes for more informations.
2. (follow up q from ur above reply, [n.ti] what is ti indicating..?
\(\texttt{ti}\) is just a loop variable which should always have a different name to all variables used in the rest of the program. Since \(\texttt{t}\) and \(\texttt{t1}\) are already used, I used \(\texttt{ti}\) as loop variable to loop over the list \(\texttt{t1}\).
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
6件のコメント