Reputation: 25
I am very new to CPlex optimization and Python and I am trying to solve an optimization problem.
Now I have a DataFrame called df_current that looks like this: Current dataframe Have defined the model and the variables as under:
m = Model(name="Hedge optimization")
seven_24 = m.continuous_var(name = '7x24 block')
seven_16 = m.continuous_var(name = '7x16 block')
five_16 = m.continuous_var(name = '5x16 block')
hour = [0,1,2,3,4,5,6,23]
sum = df_current['volume'].sum(axis=1)
dow = ['Monday','Tuesday','Wednesday','Thursday','Friday']
Now I want the constraints for the seven_16 and five_16 to be as under:
seven_24_constraint: seven_24 > 200 and <=450
seven_16_constraint: seven_16 = 0 if Hour column in the dataframe matches any value in the hour variable
five_16_constraint: five_16 = 0 if Hour and Day column in the dataframe matches any value in the hour and dow variables respectively
I am trying to define them as under:
seven_24constraint = m.add_constraint(seven_24 > 200 & seven_24 <=450)
seven_16constraint = m.add_constraint(seven_16 = 0 if df_current['Hour'] in hour)
five_16constraint = m.add_constraint(five_16 = 0 if (df_current['Hour'] in hour && df_current['Day'] in dow)
But it is giving me an error.
If I can debug this error, my goal is to write a minimization function as under
goal = m.minimize(sum - seven_24*364*24 - seven_16*364*16 - five_16*260*16)
To put more perspective, the variables, seven_24, seven_16 and five_16 can be visualized as under:
7x24 Mon to Sun (24 Hours)
7x16 Mon to Sun (7 am to 11 pm)
5x16 Mon to Fri (7 am to 11 pm)
How can I debug the constraints?
Upvotes: 0
Views: 248
Reputation: 91
First, in your constraints you cannot use python assignment (=) but you should use the equality operator (==) which is overloaded for python variables.
You say 'hour' is a variable but it is declared as a constant array, it is confusing. If you actually want to add a conditional constraints please consider using 'add_if_then' from http://ibmdecisionoptimization.github.io/docplex-doc/mp/docplex.mp.model.html
Upvotes: 0