Skip to content

Commit 190beb8

Browse files
authored
Add JuliaFormatter (#242)
* Add JuliaFormatter * separate_kwargs_with_semicolon * Updates * Update
1 parent ff1a376 commit 190beb8

32 files changed

+2328
-1084
lines changed

.JuliaFormatter.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Configuration file for JuliaFormatter.jl
2+
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/
3+
4+
always_for_in = true
5+
always_use_return = true
6+
margin = 80
7+
remove_extra_newlines = true
8+
separate_kwargs_with_semicolon = true
9+
short_to_long_function_def = true

.github/workflows/format_check.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: format-check
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- release-*
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: julia-actions/setup-julia@latest
14+
with:
15+
version: '1'
16+
- uses: actions/checkout@v1
17+
- name: Format check
18+
shell: julia --color=yes {0}
19+
run: |
20+
using Pkg
21+
Pkg.add(PackageSpec(name="JuliaFormatter", version="1"))
22+
using JuliaFormatter
23+
format(".", verbose=true)
24+
out = String(read(Cmd(`git diff`)))
25+
if isempty(out)
26+
exit(0)
27+
end
28+
@error "Some files have not been formatted !!!"
29+
write(stdout, out)
30+
exit(1)

docs/make.jl

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,36 @@ function literate_directory(dir)
3535
@testset "$(filename)" begin
3636
_include_sandbox(filename)
3737
end
38-
Literate.markdown(
39-
filename,
40-
dir;
41-
documenter = true,
42-
)
38+
Literate.markdown(filename, dir; documenter = true)
4339
end
4440
return
4541
end
4642

4743
literate_directory(_EXAMPLE_DIR)
4844

4945
makedocs(;
50-
modules=[DiffOpt],
46+
modules = [DiffOpt],
5147
doctest = false,
5248
clean = true,
53-
format=Documenter.HTML(
49+
format = Documenter.HTML(;
5450
prettyurls = get(ENV, "CI", nothing) == "true",
5551
mathengine = Documenter.MathJax2(),
5652
),
57-
pages=[
53+
pages = [
5854
"Home" => "index.md",
5955
"Introduction" => "intro.md",
6056
"Manual" => "manual.md",
6157
"Usage" => "usage.md",
6258
"Reference" => "reference.md",
6359
"Tutorials" => [
64-
joinpath("examples", f) for f in readdir(_EXAMPLE_DIR) if endswith(f, ".md")
60+
joinpath("examples", f) for
61+
f in readdir(_EXAMPLE_DIR) if endswith(f, ".md")
6562
],
6663
],
6764
strict = true, # See https://github.com/JuliaOpt/JuMP.jl/issues/1576
68-
repo="https://github.com/jump-dev/DiffOpt.jl",
69-
sitename="DiffOpt.jl",
70-
authors="JuMP Community",
65+
repo = "https://github.com/jump-dev/DiffOpt.jl",
66+
sitename = "DiffOpt.jl",
67+
authors = "JuMP Community",
7168
)
7269

73-
deploydocs(
74-
repo = "github.com/jump-dev/DiffOpt.jl.git",
75-
push_preview = true,
76-
)
70+
deploydocs(; repo = "github.com/jump-dev/DiffOpt.jl.git", push_preview = true)

docs/src/examples/Thermal_Generation_Dispatch_Example.jl

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ import Plots
3333

3434
# Define the model that will be construct given a set of parameters.
3535

36-
function generate_model(d::Float64; g_sup::Vector{Float64}, c_g::Vector{Float64}, c_ϕ::Float64)
36+
function generate_model(
37+
d::Float64;
38+
g_sup::Vector{Float64},
39+
c_g::Vector{Float64},
40+
c_ϕ::Float64,
41+
)
3742
## Creation of the Model and Parameters
3843
model = Model(() -> DiffOpt.diff_optimizer(HiGHS.Optimizer))
3944
set_silent(model)
4045
I = length(g_sup)
4146

4247
## Variables
43-
@variable(model, g[i in 1:I] >= 0.0)
48+
@variable(model, g[i in 1:I] >= 0.0)
4449
@variable(model, ϕ >= 0.0)
4550

4651
## Constraints
@@ -66,15 +71,20 @@ function diff_forward(model::Model, ϵ::Float64 = 1.0)
6671

6772
## Get the primal solution of the model
6873
vect = MOI.get.(model, MOI.VariablePrimal(), vect_ref)
69-
74+
7075
## Pass the perturbation to the DiffOpt Framework and set the context to Forward
7176
constraint_equation = convert(MOI.ScalarAffineFunction{Float64}, ϵ)
72-
MOI.set(model, DiffOpt.ForwardConstraintFunction(), model[:demand_constraint], constraint_equation)
77+
MOI.set(
78+
model,
79+
DiffOpt.ForwardConstraintFunction(),
80+
model[:demand_constraint],
81+
constraint_equation,
82+
)
7383
DiffOpt.forward_differentiate!(model)
74-
84+
7585
## Get the derivative of the model
7686
dvect = MOI.get.(model, DiffOpt.ForwardVariablePrimal(), vect_ref)
77-
87+
7888
## Return the values as a vector
7989
return [vect; dvect]
8090
end
@@ -88,7 +98,7 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
8898
vect = MOI.get.(model, MOI.VariablePrimal(), vect_ref)
8999

90100
## Set variables needed for the DiffOpt Backward Framework
91-
dvect = Array{Float64, 1}(undef, I + 1)
101+
dvect = Array{Float64,1}(undef, I + 1)
92102
perturbation = zeros(I + 1)
93103

94104
## Loop for each primal variable
@@ -99,12 +109,18 @@ function diff_reverse(model::Model, ϵ::Float64 = 1.0)
99109
DiffOpt.reverse_differentiate!(model)
100110

101111
## Get the value of the derivative of the model
102-
dvect[i] = JuMP.constant(MOI.get(model, DiffOpt.ReverseConstraintFunction(), model[:demand_constraint]))
112+
dvect[i] = JuMP.constant(
113+
MOI.get(
114+
model,
115+
DiffOpt.ReverseConstraintFunction(),
116+
model[:demand_constraint],
117+
),
118+
)
103119
perturbation[i] = 0.0
104120
end
105121

106122
## Return the values as a vector
107-
return [vect;dvect]
123+
return [vect; dvect]
108124
end
109125

110126
# Initialize of Parameters
@@ -114,8 +130,7 @@ I = length(g_sup)
114130
d = 0.0:0.1:80
115131
d_size = length(d)
116132
c_g = [1.0, 3.0, 5.0]
117-
c_ϕ = 10.0
118-
;
133+
c_ϕ = 10.0;
119134

120135
# Generate models for each demand `d`
121136
models = generate_model.(d; g_sup = g_sup, c_g = c_g, c_ϕ = c_ϕ);
@@ -128,37 +143,53 @@ result_reverse = diff_reverse.(models);
128143

129144
# Organization of results to plot
130145
# Initialize data_results that will contain every result
131-
data_results = Array{Float64,3}(undef, 2, d_size, 2*(I+1));
146+
data_results = Array{Float64,3}(undef, 2, d_size, 2 * (I + 1));
132147

133148
# Populate the data_results array
134149
for k in 1:d_size
135-
data_results[1,k,:] = result_forward[k]
136-
data_results[2,k,:] = result_reverse[k]
150+
data_results[1, k, :] = result_forward[k]
151+
data_results[2, k, :] = result_reverse[k]
137152
end
138153

139154
# ## Results with Plot graphs
140155
# ### Results for the forward context
141156
# Result Primal Values:
142-
Plots.plot(d,data_results[1,:,1:I+1],
143-
title="Generation by Demand",label=["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
144-
xlabel="Demand [unit]",ylabel= "Generation [unit]"
157+
Plots.plot(
158+
d,
159+
data_results[1, :, 1:I+1];
160+
title = "Generation by Demand",
161+
label = ["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
162+
xlabel = "Demand [unit]",
163+
ylabel = "Generation [unit]",
145164
)
146165

147166
# Result Sensitivity Analysis:
148-
Plots.plot(d,data_results[1,:,I+2:2*(I+1)],
149-
title="Sensitivity of Generation by Demand",label=["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
150-
xlabel="Demand [unit]",ylabel= "Sensitivity [-]"
167+
Plots.plot(
168+
d,
169+
data_results[1, :, I+2:2*(I+1)];
170+
title = "Sensitivity of Generation by Demand",
171+
label = ["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
172+
xlabel = "Demand [unit]",
173+
ylabel = "Sensitivity [-]",
151174
)
152175

153176
# ### Results for the reverse context
154177
# Result Primal Values:
155-
Plots.plot(d,data_results[2,:,1:I+1],
156-
title="Generation by Demand",label=["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
157-
xlabel="Demand [unit]",ylabel= "Generation [unit]"
178+
Plots.plot(
179+
d,
180+
data_results[2, :, 1:I+1];
181+
title = "Generation by Demand",
182+
label = ["Thermal Generation 1" "Thermal Generation 2" "Thermal Generation 3" "Generation Deficit"],
183+
xlabel = "Demand [unit]",
184+
ylabel = "Generation [unit]",
158185
)
159186

160187
# Result Sensitivity Analysis:
161-
Plots.plot(d,data_results[2,:,I+2:2*(I+1)],
162-
title="Sensitivity of Generation by Demand",label=["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
163-
xlabel="Demand [unit]",ylabel= "Sensitivity [-]"
188+
Plots.plot(
189+
d,
190+
data_results[2, :, I+2:2*(I+1)];
191+
title = "Sensitivity of Generation by Demand",
192+
label = ["T. Gen. 1 Sensitivity" "T. Gen. 2 Sensitivity" "T. Gen. 3 Sensitivity" "Gen. Deficit Sensitivity"],
193+
xlabel = "Demand [unit]",
194+
ylabel = "Sensitivity [-]",
164195
)

0 commit comments

Comments
 (0)