Skip to content

Commit 51978e7

Browse files
authored
Merge pull request #137 from control-toolbox/103-dev-range-linear-constraint-dimension-check
codim_f
2 parents d73e55d + 1f3f616 commit 51978e7

5 files changed

Lines changed: 27 additions & 17 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CTModels"
22
uuid = "34c4fa32-2049-4079-8329-de33c2a22e2d"
33
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
4-
version = "0.3.9"
4+
version = "0.4.0"
55

66
[deps]
77
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"

src/constraints.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ function __constraint!(
5454
n::Dimension,
5555
m::Dimension,
5656
q::Dimension;
57-
rg::Union{OrdinalRange{<:Int},Nothing}=nothing,
57+
rg::Union{Int,OrdinalRange{Int},Nothing}=nothing,
5858
f::Union{Function,Nothing}=nothing,
59-
lb::Union{ctVector,Nothing}=nothing,
60-
ub::Union{ctVector,Nothing}=nothing,
59+
lb::Union{ctNumber,ctVector,Nothing}=nothing,
60+
ub::Union{ctNumber,ctVector,Nothing}=nothing,
6161
label::Symbol=__constraint_label(),
62+
codim_f::Union{Dimension,Nothing}=nothing,
6263
)
6364

6465
# checkings: the constraint must not be set before
@@ -153,6 +154,14 @@ function __constraint!(
153154
end
154155

155156
(::Nothing, ::Function, ::ctVector, ::ctVector) => begin
157+
# ensure that codim_f has same length as lb if codim_f is not nothing
158+
if codim_f !== nothing
159+
@ensure(
160+
length(lb) == codim_f,
161+
CTBase.IncorrectArgument("The length of `lb` and `ub` must match codim_f = $codim_f.")
162+
)
163+
end
164+
156165
# set the constraint
157166
if type [:boundary, :path]
158167
ocp_constraints[label] = (type, f, lb, ub)
@@ -203,6 +212,7 @@ function constraint!(
203212
lb::Union{ctNumber,ctVector,Nothing}=nothing,
204213
ub::Union{ctNumber,ctVector,Nothing}=nothing,
205214
label::Symbol=__constraint_label(),
215+
codim_f::Union{Dimension,Nothing}=nothing,
206216
)
207217

208218
# checkings: times, state and control must be set before adding constraints
@@ -238,6 +248,7 @@ function constraint!(
238248
lb=as_vector(lb),
239249
ub=as_vector(ub),
240250
label=label,
251+
codim_f=codim_f,
241252
)
242253
end
243254

test/Project.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
[deps]
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"
4-
CTDirect = "790bbbee-bee9-49ee-8912-a9de031322d5"
5-
CTParser = "32681960-a1b1-40db-9bff-a1ca817385d1"
64
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
75
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
8-
NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71"
96
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
107
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
118

129
[compat]
1310
Aqua = "0.8"
1411
CTBase = "0.16"
15-
CTDirect = "0.14"
16-
CTParser = "0.3"
1712
JLD2 = "0.5"
1813
JSON3 = "1"
19-
NLPModelsIpopt = "0.10"
2014
Plots = "1.40"
2115
Test = "1"
2216
julia = "1.10"

test/runtests.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
using Test
22
using Aqua
33
using CTBase
4-
using CTDirect
54
using CTModels
6-
using NLPModelsIpopt
75

8-
import CTParser: CTParser, @def
9-
CTParser.prefix!(:CTModels); # code generated by @def is prefixed by CTModels (not by OptimalControl - the default)
6+
#using CTDirect
7+
#using NLPModelsIpopt
8+
#import CTParser: CTParser, @def
9+
#CTParser.prefix!(:CTModels); # code generated by @def is prefixed by CTModels (not by OptimalControl - the default)
1010

1111
#
1212
include("solution_example.jl")
13-
include("solution_example_path_constraints.jl")
13+
#include("solution_example_path_constraints.jl")
1414

1515
#
1616
@testset verbose = true showtiming = true "CTModels tests" begin
@@ -26,11 +26,11 @@ include("solution_example_path_constraints.jl")
2626
:constraints,
2727
:model,
2828
:ocp,
29-
# :plot,
3029
:init,
31-
# :export_import,
3230
:utils,
3331
:solution,
32+
# :plot,
33+
# :export_import,
3434
)
3535
@testset "$(name)" begin
3636
test_name = Symbol(:test_, name)

test/test_constraints.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ function test_constraints()
5050
ocp_set, :state, lb=[0, 1], ub=[0, 1, 2]
5151
)
5252

53+
# x(1) == [0, 0, 1] must raise an error if x is of dimension 2
54+
@test_throws CTBase.IncorrectArgument CTModels.constraint!(
55+
ocp_set, :boundary, lb=[0, 0, 1], ub=[0, 1, 2], codim_f=2
56+
)
57+
5358
# if no range nor function is provided, lb and ub must have the right length:
5459
# depending on state, control, or variable
5560
@test_throws CTBase.IncorrectArgument CTModels.constraint!(

0 commit comments

Comments
 (0)