Skip to content

Commit

Permalink
Add add_similar_to_expression! for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
lbonaldo committed Nov 20, 2024
1 parent f1bbba4 commit cc79302
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/model/core/operational_reserves.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ function operational_reserves_core!(EP::Model, inputs::Dict, setup::Dict)
# N-1 contingency requirement is considered only if Unit Commitment is being modeled
if UCommit >= 1 &&
(inputs["pDynamic_Contingency"] >= 1 || inputs["pStatic_Contingency"] > 0)
add_to_expression!(EP[:eRsvReq], EP[:eContingencyReq])
# EP[:eRsvReq] = EP[:eRsvReq] + EP[:eContingencyReq]
add_similar_to_expression!(EP[:eRsvReq], EP[:eContingencyReq])
end

## Objective Function Expressions ##
Expand Down
8 changes: 8 additions & 0 deletions src/model/expression_manipulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ function add_similar_to_expression!(expr1::AbstractArray{GenericAffExpr{C, T}, d
return nothing
end

# If the expressions are vectors of numbers, use the += operator
function add_similar_to_expression!(arr1::AbstractArray{T, dims}, arr2::AbstractArray{T, dims}) where {T <: Number, dims}
for i in eachindex(arr1)
arr1[i] += arr2[i]
end
return nothing
end

###### ###### ###### ###### ###### ######
# Element-wise addition of one term into an expression
# Both arrays must have the same dimensions
Expand Down
6 changes: 6 additions & 0 deletions test/expression_manipulation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ let
GenX.add_similar_to_expression!(EP[:large_expr], EP[:large_const_expr])
@test all(EP[:large_expr][:] .== 18.0)

# Test add_similar_to_expression! with AbstractArray{Number}
@expression(EP, eArr1[i = 1:100, j = 1:50], i*10.0+j*10.0)
@expression(EP, eArr2[i = 1:100, j = 1:50], -(i*10.0+j*10.0))
GenX.add_similar_to_expression!(EP[:eArr1], EP[:eArr2])
@test all(EP[:eArr1][:] .== 0.0)

# Test add_similar_to_expression! returns an error if the dimensions don't match
GenX.create_empty_expression!(EP, :small_expr, (2, 3))
@test_throws ErrorException GenX.add_similar_to_expression!(EP[:large_expr],
Expand Down

0 comments on commit cc79302

Please sign in to comment.