Skip to content

Commit

Permalink
Merge pull request #62 from JinraeKim/hotfix/maxabsnormalizer
Browse files Browse the repository at this point in the history
Hotfix for NormalisedApproximator
  • Loading branch information
JinraeKim authored May 12, 2023
2 parents 2febf2e + 9252339 commit 4325ff9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ParametrisedConvexApproximators"
uuid = "668502ff-1e8f-42bf-95c7-24f1e819f537"
authors = ["JinraeKim <[email protected]> and contributors"]
version = "0.2.1"
version = "0.2.2"

[deps]
Convex = "f65535da-76fb-5f13-bab9-19810c17039a"
Expand Down
27 changes: 15 additions & 12 deletions src/approximators/normalized_approximators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,37 @@ function MaxAbsNormalisedApproximator(
dataset::DecisionMakingDataset,
)
(; conditions, decisions, costs) = dataset
condition_max_abs = maximum(abs.(hcat(conditions...)), dims=length(size(conditions)))
decision_max_abs = maximum(abs.(hcat(decisions...)), dims=length(size(decisions)))
cost_max_abs = maximum(abs.(hcat(costs...)), dims=length(size(costs)))
c = hcat(conditions...)
d = hcat(decisions...)
J = hcat(costs...)
condition_max_abs = maximum(abs.(c), dims=length(size(c)))
decision_max_abs = maximum(abs.(d), dims=length(size(d)))
cost_max_abs = maximum(abs.(J), dims=length(size(J)))
MaxAbsNormalisedApproximator(network, condition_max_abs, decision_max_abs, cost_max_abs)
end


function (nn::NormalisedApproximator)(x, u)
(; network, condition_max_abs, decision_max_abs, cost_max_abs) = nn
x = normalise(nn, x, :condition)
u = normalise(nn, u, :decision)
f = network(x, u)
f = unnormalise(nn, f, :cost)
return f
x_new = normalise(nn, x, :condition)
u_new = normalise(nn, u, :decision)
f = network(x_new, u_new)
f_new = unnormalise(nn, f, :cost)
return f_new
end


function normalise(nn::MaxAbsNormalisedApproximator, z, which::Symbol)
@assert which in (:condition, :decision, :cost)
factor = getproperty(nn, Symbol(String(which) * "_max_abs"))
z = factor != nothing ? z ./ factor : z
return z
z_new = factor != nothing ? z ./ factor : z
return z_new
end


function unnormalise(nn::MaxAbsNormalisedApproximator, z, which::Symbol)
@assert which in (:condition, :decision, :cost)
factor = getproperty(nn, Symbol(String(which) * "_max_abs"))
z = factor != nothing ? z .* factor : z
return z
z_new = factor != nothing ? z .* factor : z
return z_new
end

2 comments on commit 4325ff9

@JinraeKim
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/83431

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.2 -m "<description of version>" 4325ff96bc66152f13014872a07733dbcf141e6e
git push origin v0.2.2

Please sign in to comment.