From 7e25653e748faf311e5bf021c5a44be70b28b3c9 Mon Sep 17 00:00:00 2001 From: lbonaldo Date: Wed, 20 Nov 2024 11:22:05 -0500 Subject: [PATCH 1/6] Add add_similar_to_expression! for arrays --- src/model/core/operational_reserves.jl | 3 ++- src/model/expression_manipulation.jl | 8 ++++++++ test/expression_manipulation_test.jl | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/model/core/operational_reserves.jl b/src/model/core/operational_reserves.jl index 54d6dab073..d5d0df9363 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 33b0b8e8e2..92b1daac2c 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 71891d80ac..8aae64c39e 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], From cf11b18ffbb6539698cd151fa429ab3cd7ffc787 Mon Sep 17 00:00:00 2001 From: Luca Bonaldo <39280783+lbonaldo@users.noreply.github.com> Date: Mon, 25 Nov 2024 10:34:05 -0500 Subject: [PATCH 2/6] Update src/model/expression_manipulation.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/model/expression_manipulation.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/model/expression_manipulation.jl b/src/model/expression_manipulation.jl index 92b1daac2c..7d0dd4e566 100644 --- a/src/model/expression_manipulation.jl +++ b/src/model/expression_manipulation.jl @@ -139,7 +139,8 @@ function add_similar_to_expression!(expr1::AbstractArray{GenericAffExpr{C, T}, d 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} +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 From 6cb889517b2e40695fc0ef21476d802a036954f9 Mon Sep 17 00:00:00 2001 From: Luca Bonaldo <39280783+lbonaldo@users.noreply.github.com> Date: Mon, 25 Nov 2024 10:34:12 -0500 Subject: [PATCH 3/6] Update test/expression_manipulation_test.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- test/expression_manipulation_test.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/expression_manipulation_test.jl b/test/expression_manipulation_test.jl index 8aae64c39e..6c403284d0 100644 --- a/test/expression_manipulation_test.jl +++ b/test/expression_manipulation_test.jl @@ -93,8 +93,8 @@ let @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)) + @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) From 923f7ec3a264662501ebedc183963887548a9909 Mon Sep 17 00:00:00 2001 From: lbonaldo Date: Wed, 4 Dec 2024 11:32:47 -0500 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 809a969555..b192f55b74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 number of concurrent Gurobi uses is limited (#783). - Additional long-duration storage constraints to bound state of charge in non-representative periods (#781). +- New version of `add_similar_to_expression!` to support arrays of `Number`s. (#798) ### Changed - The `charge.csv` and `storage.csv` files now include only resources with From de8438257de3e5350c51692265c6c13838bdb0f1 Mon Sep 17 00:00:00 2001 From: lbonaldo Date: Wed, 4 Dec 2024 11:33:23 -0500 Subject: [PATCH 5/6] Update dev version in Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index a6d2b1048d..15ac369587 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "GenX" uuid = "5d317b1e-30ec-4ed6-a8ce-8d2d88d7cfac" authors = ["Bonaldo, Luca", "Chakrabarti, Sambuddha", "Cheng, Fangwei", "Ding, Yifu", "Jenkins, Jesse D.", "Luo, Qian", "Macdonald, Ruaridh", "Mallapragada, Dharik", "Manocha, Aneesha", "Mantegna, Gabe ", "Morris, Jack", "Patankar, Neha", "Pecci, Filippo", "Schwartz, Aaron", "Schwartz, Jacob", "Schivley, Greg", "Sepulveda, Nestor", "Xu, Qingyu", "Zhou, Justin"] -version = "0.4.1-dev.16" +version = "0.4.1-dev.17" [deps] CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b" From 0a53de481d393a4eca2c1b4eb4eb3c96624c989c Mon Sep 17 00:00:00 2001 From: lbonaldo Date: Wed, 4 Dec 2024 11:34:28 -0500 Subject: [PATCH 6/6] Cleanup --- src/model/core/operational_reserves.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/model/core/operational_reserves.jl b/src/model/core/operational_reserves.jl index d5d0df9363..1790db3bb1 100644 --- a/src/model/core/operational_reserves.jl +++ b/src/model/core/operational_reserves.jl @@ -270,7 +270,6 @@ 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) - # EP[:eRsvReq] = EP[:eRsvReq] + EP[:eContingencyReq] add_similar_to_expression!(EP[:eRsvReq], EP[:eContingencyReq]) end