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

Add add_similar_to_expression! for arrays #798

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/model/core/operational_reserves.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ 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])
add_similar_to_expression!(EP[:eRsvReq], EP[:eContingencyReq])
end

## Objective Function Expressions ##
Expand Down
9 changes: 9 additions & 0 deletions src/model/expression_manipulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ 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
Loading