diff --git a/src/model/core/operational_reserves.jl b/src/model/core/operational_reserves.jl index 54d6dab07..d5d0df936 100644 --- a/src/model/core/operational_reserves.jl +++ b/src/model/core/operational_reserves.jl @@ -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 ## diff --git a/src/model/expression_manipulation.jl b/src/model/expression_manipulation.jl index 33b0b8e8e..92b1daac2 100644 --- a/src/model/expression_manipulation.jl +++ b/src/model/expression_manipulation.jl @@ -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 diff --git a/test/expression_manipulation_test.jl b/test/expression_manipulation_test.jl index 71891d80a..8aae64c39 100644 --- a/test/expression_manipulation_test.jl +++ b/test/expression_manipulation_test.jl @@ -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],