Objective function call to external function
AnsweredHello,
I am trying to optimize a battery module in which I have to take into account battery degradation. The battery degradation depends on the number of cycles for each depth of discharge. So to calculate this depth of discharge, we need to access the solutions of the problem i.e., how much energy is committed by the battery. But as far as I understood it is not possible to access the variable value (var.x) before the optimization step. So is there a way for me to include the degradation term in my objective function?
# set the objective
# m.setObjective(sum(pb_i[d] * qbr[d] - ps_i[d] * qsr[d] for d in D), GRB.MAXIMIZE)
m.update()
m.setObjective(gp.quicksum(pb_i[d][j] *qbr[d][j] fordinDforjinrange(len(pb_i[d]))) \
-gp.quicksum(ps_i[d][j] *qsr[d][j] fordinDforjinrange(len(ps_i[d]))) \
-get_battery_degradation(battery, qsr, qbr),
GRB.MAXIMIZE)
where get_battery_degradation takes the decision variables qsr and qbr and returns a scalar value for degradation
def get_battery_degradation(battery, qsr, qbr) -> float:
"""
Returns the battery degradation cost
"""
print("QSR", qsr)
print("QBR", qbr)
initial_soh=battery.get_soh()
battery.save_parameters_pseudo()
for d in range(24):
selling_power=0
buying_power=0
power_contracted=0
selling_power=gp.quicksum(qsr[d][i] foriinrange(len(qsr[d])))
buying_power=gp.quicksum(qbr[d][i] foriinrange(len(qbr[d])))
print(f"The selling power at hour {d} is {selling_power}")
print(f"The buying power at hour {d} is {buying_power}")
# calculate the degradation
# power_contracted = selling_power if selling_power > 0.0 else -1.0 * buying_power
power_contracted=selling_power-buying_power
battery.step(power_contracted, 1)
end for
final_soh=battery.get_soh()
battery.load_parameters_pseudo()
return (initial_soh-final_soh)
It gives me this error for now:
Thank you very much in advance!
-
Official comment
This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi,
I assume the battery degradation function is a black-box function that does not have a known analytical form. If this assumption is correct, the best you can do is to approximate it as a piecewise linear objective. Please check out the piecewise linear objective section in the documentation for more details.
Best regards,
Maliheh
0 -
In addition, you can also refer to Is it possible to feed black-box functions to Gurobi?.
0
Post is closed for comments.
Comments
3 comments