Skip to content

Commit

Permalink
Merge branch 'main' into optim
Browse files Browse the repository at this point in the history
  • Loading branch information
ocots authored Dec 12, 2024
2 parents 66dd73d + fa8fc5d commit 0f65f29
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 139 deletions.
130 changes: 5 additions & 125 deletions .github/workflows/Breakage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,133 +8,13 @@ on:

jobs:
break:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
pkg: [
"control-toolbox/CTDirect.jl",
"control-toolbox/CTFlows.jl",
"control-toolbox/OptimalControl.jl"
]
pkg: ["control-toolbox/CTDirect.jl", "control-toolbox/CTFlows.jl", "control-toolbox/OptimalControl.jl"]
pkgversion: [latest, stable]
uses: control-toolbox/CTActions/.github/workflows/breakage.yml@main
with:
pkg: ${{ matrix.pkg }}
pkgversion: ${{ matrix.pkgversion }}

steps:
- uses: actions/checkout@v4

# Install Julia
- uses: julia-actions/setup-julia@latest
with:
version: 1
arch: x64
- uses: actions/cache@v4
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1

# Breakage test
- name: 'Breakage of ${{ matrix.pkg }}, ${{ matrix.pkgversion }} version'
env:
URL: ${{ matrix.pkg }}
VERSION: ${{ matrix.pkgversion }}
run: |
set -v
mkdir -p ./pr
echo "${{ github.event.number }}" > ./pr/NR
#
PKG_SRC_PATH=`pwd`
PKG_SRC_NAME=`basename -s ".jl" $PKG_SRC_PATH`
#
git clone https://github.com/$URL
export PKG=$(echo $URL | cut -f2 -d/)
cd $PKG
if [ $VERSION == "stable" ]; then
TAG=$(git tag -l "v*" --sort=-creatordate | head -n1)
if [ -z "$TAG" ]; then
TAG="no_tag"
else
git checkout $TAG
fi
else
TAG=$VERSION
fi
export TAG
#
PKG_SRC_VERSION=$(julia --project=@. -e 'using Pkg, Logging; with_logger(NullLogger()) do; Pkg.instantiate(); Pkg.installed()["'$PKG_SRC_NAME'"] |> println; end')
export PKG_SRC_VERSION
#
julia -e 'using Pkg;
PKG, TAG, VERSION, PKG_SRC_VERSION = ENV["PKG"], ENV["TAG"], ENV["VERSION"], ENV["PKG_SRC_VERSION"]
joburl = joinpath(ENV["GITHUB_SERVER_URL"], ENV["GITHUB_REPOSITORY"], "actions/runs", ENV["GITHUB_RUN_ID"])
open("../pr/$PKG-$VERSION", "w") do io
try
TAG == "no_tag" && error("Not tag for $VERSION")
pkg"activate .";
pkg"instantiate";
pkg"status";
pkg"dev ../";
pkg"build";
pkg"test";
print(io, "[![](https://img.shields.io/badge/$TAG-Pass-green)]($joburl) compat: v", PKG_SRC_VERSION);
catch e
@error e;
print(io, "[![](https://img.shields.io/badge/$TAG-Fail-red)]($joburl) compat: v", PKG_SRC_VERSION);
end;
end'
- uses: actions/upload-artifact@v3
with:
name: pr
path: pr/

upload:
needs: break
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/download-artifact@v3
with:
name: pr
path: pr/

- run: ls
- run: |
cd pr
echo "Breakage test results" > MSG
echo "Date: $(TZ=UTC-2 date '+%Y-%m-%d %H:%M:%S')" >> MSG
echo "| Package name | latest | stable |" >> MSG
echo "|--|--|--|" >> MSG
count=0
for file in *
do
[ "$file" == "NR" ] && continue
[ "$file" == "MSG" ] && continue
if [ $count == "0" ]; then
name=$(echo $file | cut -f1 -d-)
echo -n "| $name | "
else
echo -n "| "
fi
cat $file
if [ $count == "0" ]; then
echo -n " "
count=1
else
echo " |"
count=0
fi
done >> MSG
- uses: actions/upload-artifact@v3
with:
name: pr
path: pr/
11 changes: 1 addition & 10 deletions .github/workflows/Coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,4 @@ on:
tags: '*'
jobs:
test:
name: coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@latest
- uses: julia-actions/julia-runtest@latest
- uses: julia-actions/julia-processcoverage@latest
- uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
uses: control-toolbox/CTActions/.github/workflows/coverage.yml@main
7 changes: 6 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CTBase"
uuid = "54762871-cc72-4466-b8e8-f6c8b58076cd"
authors = ["Olivier Cots <[email protected]>", "Jean-Baptiste Caillau <[email protected]>"]
version = "0.14.0"
version = "0.14.1"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand All @@ -22,9 +22,12 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[weakdeps]
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"

[extensions]
CTBaseLoadSave = ["JLD2", "JSON3"]
CTBasePlots = "Plots"

[compat]
Expand All @@ -33,6 +36,8 @@ DifferentiationInterface = "0.6"
DocStringExtensions = "0.9"
ForwardDiff = "0.10"
Interpolations = "0.15"
JLD2 = "0.5"
JSON3 = "1"
MLStyle = "0.4"
MacroTools = "0.5"
Parameters = "0.12"
Expand Down
64 changes: 64 additions & 0 deletions ext/CTBaseLoadSave.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module CTBaseLoadSave

using CTBase
using DocStringExtensions

using JLD2
using JSON3

"""
$(TYPEDSIGNATURES)
Export OCP solution in JLD / JSON format
"""
function CTBase.export_ocp_solution(sol::OptimalControlSolution; filename_prefix = "solution", format = :JLD)
if format == :JLD
save_object(filename_prefix * ".jld2", sol)
elseif format == :JSON
blob = Dict(
"objective" => sol.objective,
"time_grid" => sol.time_grid,
"state" => state_discretized(sol),
"control" => control_discretized(sol),
"costate" => costate_discretized(sol)[1:(end - 1), :],
"variable" => sol.variable,
)
open(filename_prefix * ".json", "w") do io
JSON3.pretty(io, blob)
end
else
error("Export_ocp_solution: unknow format (should be :JLD or :JSON): ", format)
end
return nothing
end

"""
$(TYPEDSIGNATURES)
Read OCP solution in JLD / JSON format
"""
function CTBase.import_ocp_solution(ocp::OptimalControlModel; filename_prefix = "solution", format = :JLD)

if format == :JLD
return load_object(filename_prefix * ".jld2")
elseif format == :JSON
json_string = read(filename_prefix * ".json", String)
blob = JSON3.read(json_string)

# NB. convert vect{vect} to matrix
return OptimalControlSolution(
ocp,
blob.time_grid,
stack(blob.state, dims = 1),
stack(blob.control, dims = 1),
blob.variable,
stack(blob.costate, dims = 1);
objective = blob.objective,
)
else
error("Export_ocp_solution: unknow format (should be :JLD or :JSON): ", format)
end
end


end
4 changes: 4 additions & 0 deletions src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,8 @@ export @def
export ct_repl, ct_repl_update_model
isdefined(Base, :active_repl) && ct_repl()

# load and save solution
export export_ocp_solution
export import_ocp_solution

end
Loading

0 comments on commit 0f65f29

Please sign in to comment.