Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] Notes os JuMP issues #1218

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/optimization_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,7 @@ function add_to_objective_invariant_expression!(
) where {T <: JuMP.AbstractJuMPScalar}
T_cf = typeof(container.objective_function.invariant_terms)
if T_cf <: JuMP.GenericAffExpr && T <: JuMP.GenericQuadExpr
# why not add_to...???
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before add didn't work for mixed ExpressionTypes. We can re-test

container.objective_function.invariant_terms += cost_expr
else
JuMP.add_to_expression!(container.objective_function.invariant_terms, cost_expr)
Expand Down
1 change: 1 addition & 0 deletions src/devices_models/devices/AC_branches.jl
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ function _make_flow_expressions!(
sum(
ptdf_col[i] * nodal_balance_expressions[i, t] for
i in 1:length(ptdf_col)
# maybe only sum if nonempy?
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignore

)
)
end
Expand Down
25 changes: 18 additions & 7 deletions src/devices_models/devices/common/add_to_expression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1633,10 +1633,10 @@ function add_to_expression!(
device_base_power,
)
for t in time_steps
fuel_expr = variable[name, t] * prop_term_per_unit * dt
JuMP.add_to_expression!(
expression[name, t],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for more info:
JuMP.add_to_expression!(exp1, float_coef * jump_var) is worse because it creates an extra temporary expression float_coef * jump_var

fuel_expr,
prop_term_per_unit * dt,
variable[name, t],
)
end
elseif value_curve isa PSY.QuadraticCurve
Expand Down Expand Up @@ -1709,22 +1709,33 @@ function add_to_expression!(
for t in time_steps
sos_status = _get_sos_value(container, W, d)
if sos_status == SOSStatusVariable.NO_VARIABLE
bin = 1.0
JuMP.add_to_expression!(
expression[name, t],
P_min * prop_term_per_unit * dt,
)
elseif sos_status == SOSStatusVariable.PARAMETER
param = get_default_on_parameter(d)
bin = get_parameter(container, param, V).parameter_array[name, t]
JuMP.add_to_expression!(
expression[name, t],
P_min * prop_term_per_unit * dt,
bin
)
elseif sos_status == SOSStatusVariable.VARIABLE
var = get_default_on_variable(d)
bin = get_variable(container, var, V)[name, t]
JuMP.add_to_expression!(
expression[name, t],
P_min * prop_term_per_unit * dt,
bin
)
else
@assert false
end
fuel_expr =
variable[name, t] * prop_term_per_unit * dt +
P_min * bin * prop_term_per_unit * dt
JuMP.add_to_expression!(
expression[name, t],
fuel_expr,
prop_term_per_unit * dt,
variable[name, t],
)
end
elseif value_curve isa PSY.QuadraticCurve
Expand Down
6 changes: 4 additions & 2 deletions src/devices_models/devices/common/duration_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ function device_duration_look_ahead!(
lhs_off = JuMP.GenericAffExpr{Float64, JuMP.VariableRef}(0)
for i in UnitRange{Int}(Int(t - duration_data[ix].down + 1), t)
if i in time_steps
JuMP.add_to_expression!(lhs_off, (1 - varon[name, i]))
JuMP.add_to_expression!(lhs_off, 1)
JuMP.add_to_expression!(lhs_off, -1, varon[name, i])
Comment on lines -199 to +200
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the previous allocates more because (1 - varon[name, i]) becomes a new expression

end
end
if t <= duration_data[ix].down
Expand Down Expand Up @@ -322,7 +323,8 @@ function device_duration_parameters!(
for i in UnitRange{Int}(Int(t - duration_data[ix].down + 1), t)
if t <= duration_data[ix].down
if in(i, time_steps)
JuMP.add_to_expression!(lhs_off, (1 - varon[name, i]))
JuMP.add_to_expression!(lhs_off, 1)
JuMP.add_to_expression!(lhs_off, -1, varon[name, i])
end
else
JuMP.add_to_expression!(lhs_off, varstop[name, i])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ function _get_pwl_cost_expression(
name = PSY.get_name(component)
pwl_var_container = get_variable(container, PieceWiseLinearBlockOffer(), T)
gen_cost = JuMP.AffExpr(0.0)
cost_data = PSY.get_y_coords(cost_data)
for (i, cost) in enumerate(cost_data)
_cost_data = PSY.get_y_coords(cost_data)
for (i, cost) in enumerate(_cost_data)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type instability

Copy link
Member

@jd-lara jd-lara Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fixed in main now for the cases that don't user market bid. There is a PR to refactor that code that will incorporate this change

JuMP.add_to_expression!(
gen_cost,
cost * multiplier * pwl_var_container[(name, i, time_period)],
cost * multiplier,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pwl_var_container[(name, i, time_period)],
)
end
return gen_cost
Expand Down Expand Up @@ -196,7 +197,8 @@ function _get_pwl_cost_expression(
for i in 1:length(slopes)
JuMP.add_to_expression!(
ordc_cost,
slopes[i] * multiplier * pwl_var_container[(name, i, time_period)],
slopes[i] * multiplier,
pwl_var_container[(name, i, time_period)],
)
end
return ordc_cost
Expand Down Expand Up @@ -241,7 +243,8 @@ function _get_pwl_cost_expression(
for (i, cost) in enumerate(cost_data)
JuMP.add_to_expression!(
gen_cost,
cost * multiplier * pwl_var_container[(name, i, time_period)],
cost * multiplier,
pwl_var_container[(name, i, time_period)],
)
end
return gen_cost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,12 @@ function _get_pwl_cost_expression(
name = PSY.get_name(component)
pwl_var_container = get_variable(container, PieceWiseLinearCostVariable(), T)
gen_cost = JuMP.AffExpr(0.0)
cost_data = PSY.get_y_coords(cost_data)
for (i, cost) in enumerate(cost_data)
_cost_data = PSY.get_y_coords(cost_data)
for (i, cost) in enumerate(_cost_data)
JuMP.add_to_expression!(
gen_cost,
cost * multiplier * pwl_var_container[(name, i, time_period)],
cost * multiplier,
pwl_var_container[(name, i, time_period)],
)
end
return gen_cost
Expand Down
4 changes: 2 additions & 2 deletions src/devices_models/devices/regulation_device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ function add_constraints!(
R_dn[name, t] ==
(p_factor.dn * area_reserve_dn[agc_name, t]) + R_dn_emergency[name, t]
)
JuMP.add_to_expression!(expr_up[area_name, t], -1 * R_up_emergency[name, t])
JuMP.add_to_expression!(expr_dn[area_name, t], -1 * R_dn_emergency[name, t])
JuMP.add_to_expression!(expr_up[area_name, t], -1, R_up_emergency[name, t])
JuMP.add_to_expression!(expr_dn[area_name, t], -1, R_dn_emergency[name, t])
end
end

Expand Down
3 changes: 2 additions & 1 deletion src/parameters/update_cost_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ function _update_pwl_cost_expression(
for i in 1:length(cost_data)
JuMP.add_to_expression!(
gen_cost,
slopes[i] * upb[i] * dt * pwl_var_container[(component_name, i, time_period)],
slopes[i] * upb[i] * dt,
pwl_var_container[(component_name, i, time_period)],
)
end
return gen_cost
Expand Down
7 changes: 4 additions & 3 deletions src/services_models/agc.jl
Copy link
Member

@jd-lara jd-lara Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AGC functionality is offline for now. We need LHS parameters to revive it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POI is much better nowadays.
Moreover, there is room for improvement, so let me know whenever you want to test stuff and bring it back

Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ function add_constraints!(
const_container = add_constraints_container!(container, T(), PSY.System, time_steps)

for t in time_steps
system_balance = sum(area_balance.data[:, t])
system_balance = JuMP.@expression(container.JuMPmodel, sum(area_balance.data[:, t]))
for agc in agcs
a = PSY.get_name(agc)
area_name = PSY.get_name(PSY.get_area(agc))
JuMP.add_to_expression!(system_balance, R_up[a, t])
JuMP.add_to_expression!(system_balance, -1 * R_dn[a, t])
JuMP.add_to_expression!(system_balance, -1, R_dn[a, t])
JuMP.add_to_expression!(system_balance, R_up_emergency[area_name, t])
JuMP.add_to_expression!(system_balance, -1 * R_dn_emergency[area_name, t])
JuMP.add_to_expression!(system_balance, -1, R_dn_emergency[area_name, t])
end
const_container[t] = JuMP.@constraint(
container.JuMPmodel,
Expand Down Expand Up @@ -303,6 +303,7 @@ function add_proportional_cost!(
) where {T <: PSY.AGC, U <: LiftVariable}
lift_variable = get_variable(container, U(), T)
for index in Iterators.product(axes(lift_variable)...)
# TODO
add_to_objective_invariant_expression!(
container,
SERVICES_SLACK_COST * lift_variable[index...],
Expand Down
6 changes: 4 additions & 2 deletions src/services_models/reserve_group.jl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These improvements should make it to main

Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ function add_constraints!(
for t in time_steps
resource_expression = JuMP.GenericAffExpr{Float64, JuMP.VariableRef}()
for reserve_variable in reserve_variables
JuMP.add_to_expression!(resource_expression, sum(reserve_variable[:, t]))
JuMP.add_to_expression!(resource_expression,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this more effective than pre-allocating the expression in 61?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I got what you mean

JuMP.@expression(container.JuMPmodel, sum(reserve_variable[:, t])))
# consider a for loop to add the reserve variables
end
if use_slacks
resource_expression += slack_vars[t]
JuMP.add_to_expression!(resource_expression, slack_vars[t])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

end
constraint[service_name, t] =
JuMP.@constraint(container.JuMPmodel, resource_expression >= requirement)
Expand Down
19 changes: 13 additions & 6 deletions src/services_models/reserves.jl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These ones are quite important too.

Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,23 @@ function add_constraints!(
param = get_parameter_column_refs(param_container, service_name)
for t in time_steps
if use_slacks
resource_expression = sum(reserve_variable[:, t]) + slack_vars[t]
resource_expression = JuMP.@expression(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be related to #1000

jump_model, sum(reserve_variable[:, t]) + slack_vars[t])
else
resource_expression = sum(reserve_variable[:, t])
resource_expression = JuMP.@expression(
jump_model, sum(reserve_variable[:, t]))
end
constraint[service_name, t] =
JuMP.@constraint(jump_model, resource_expression >= param[t] * requirement)
end
else
for t in time_steps
if use_slacks
resource_expression = sum(reserve_variable[:, t]) + slack_vars[t]
resource_expression = JuMP.@expression(
jump_model, sum(reserve_variable[:, t]) + slack_vars[t])
else
resource_expression = sum(reserve_variable[:, t])
resource_expression = JuMP.@expression(
jump_model, sum(reserve_variable[:, t]))
end
constraint[service_name, t] = JuMP.@constraint(
jump_model,
Expand Down Expand Up @@ -268,9 +272,11 @@ function add_constraints!(
jump_model = get_jump_model(container)
for t in time_steps
resource_expression = JuMP.GenericAffExpr{Float64, JuMP.VariableRef}()
JuMP.add_to_expression!(resource_expression, sum(reserve_variable[:, t]))
JuMP.add_to_expression!(resource_expression,
JuMP.@expression(jump_model, sum(reserve_variable[:, t])))
# consider a for loop
if use_slacks
resource_expression += slack_vars[t]
JuMP.add_to_expression!(resource_expression, slack_vars[t])
end
constraint[service_name, t] =
JuMP.@constraint(jump_model, resource_expression >= requirement)
Expand Down Expand Up @@ -553,6 +559,7 @@ function add_proportional_cost!(
for index in Iterators.product(axes(reserve_variable)...)
add_to_objective_invariant_expression!(
container,
# possibly decouple
DEFAULT_RESERVE_COST / base_p * reserve_variable[index...],
)
end
Expand Down