|
| 1 | +function SciMLBase.OptimizationFunction(sys::System, args...; kwargs...) |
| 2 | + return OptimizationFunction{true}(sys, args...; kwargs...) |
| 3 | +end |
| 4 | + |
| 5 | +function SciMLBase.OptimizationFunction{iip}(sys::System, |
| 6 | + _d = nothing, u0 = nothing, p = nothing; grad = false, hess = false, |
| 7 | + sparse = false, cons_j = false, cons_h = false, cons_sparse = false, |
| 8 | + linenumbers = true, eval_expression = false, eval_module = @__MODULE__, |
| 9 | + simplify = false, check_compatibility = true, checkbounds = false, cse = true, |
| 10 | + kwargs...) where {iip} |
| 11 | + check_complete(sys, OptimizationFunction) |
| 12 | + check_compatibility && check_compatible_system(OptimizationFunction, sys) |
| 13 | + dvs = unknowns(sys) |
| 14 | + ps = parameters(sys) |
| 15 | + cstr = constraints(sys) |
| 16 | + |
| 17 | + f = generate_cost(sys; expression = Val{false}, eval_expression, |
| 18 | + eval_module, checkbounds, cse, kwargs...) |
| 19 | + |
| 20 | + if grad |
| 21 | + _grad = generate_cost_gradient(sys; expression = Val{false}, eval_expression, |
| 22 | + eval_module, checkbounds, cse, kwargs...) |
| 23 | + else |
| 24 | + _grad = nothing |
| 25 | + end |
| 26 | + if hess |
| 27 | + _hess, hess_prototype = generate_cost_hessian( |
| 28 | + sys; expression = Val{false}, eval_expression, eval_module, |
| 29 | + checkbounds, cse, sparse, simplify, return_sparsity = true, kwargs...) |
| 30 | + else |
| 31 | + _hess = hess_prototype = nothing |
| 32 | + end |
| 33 | + if isempty(cstr) |
| 34 | + cons = lcons = ucons = _cons_j = cons_jac_prototype = _cons_h = nothing |
| 35 | + cons_hess_prototype = cons_expr = nothing |
| 36 | + else |
| 37 | + cons = generate_cons(sys; expression = Val{false}, eval_expression, |
| 38 | + eval_module, checkbounds, cse, kwargs...) |
| 39 | + if cons_j |
| 40 | + _cons_j, cons_jac_prototype = generate_constraint_jacobian( |
| 41 | + sys; expression = Val{false}, eval_expression, eval_module, checkbounds, |
| 42 | + cse, simplify, sparse = cons_sparse, return_sparsity = true, kwargs...) |
| 43 | + else |
| 44 | + _cons_j = cons_jac_prototype = nothing |
| 45 | + end |
| 46 | + if cons_h |
| 47 | + _cons_h, cons_hess_prototype = generate_constraint_hessian( |
| 48 | + sys; expression = Val{false}, eval_expression, eval_module, checkbounds, |
| 49 | + cse, simplify, sparse = cons_sparse, return_sparsity = true, kwargs...) |
| 50 | + else |
| 51 | + _cons_h = cons_hess_prototype = nothing |
| 52 | + end |
| 53 | + cons_expr = toexpr.(subs_constants(cstr)) |
| 54 | + end |
| 55 | + |
| 56 | + obj_expr = subs_constants(cost(sys)) |
| 57 | + |
| 58 | + observedfun = ObservedFunctionCache(sys; eval_expression, eval_module, checkbounds, cse) |
| 59 | + |
| 60 | + return OptimizationFunction{iip}(f, SciMLBase.NoAD(); |
| 61 | + sys = sys, |
| 62 | + grad = _grad, |
| 63 | + hess = _hess, |
| 64 | + hess_prototype = hess_prototype, |
| 65 | + cons = cons, |
| 66 | + cons_j = _cons_j, |
| 67 | + cons_jac_prototype = cons_jac_prototype, |
| 68 | + cons_h = _cons_h, |
| 69 | + cons_hess_prototype = cons_hess_prototype, |
| 70 | + cons_expr = cons_expr, |
| 71 | + expr = obj_expr, |
| 72 | + observed = observedfun) |
| 73 | +end |
| 74 | + |
| 75 | +function SciMLBase.OptimizationProblem(sys::System, args...; kwargs...) |
| 76 | + return OptimizationProblem{true}(sys, args...; kwargs...) |
| 77 | +end |
| 78 | + |
| 79 | +function SciMLBase.OptimizationProblem{iip}( |
| 80 | + sys::System, u0map, parammap = SciMLBase.NullParameters(); lb = nothing, ub = nothing, |
| 81 | + check_compatibility = true, kwargs...) where {iip} |
| 82 | + check_complete(sys, OptimizationProblem) |
| 83 | + check_compatibility && check_compatible_system(OptimizationProblem, sys) |
| 84 | + |
| 85 | + f, u0, p = process_SciMLProblem(OptimizationFunction{iip}, sys, u0map, parammap; |
| 86 | + check_compatibility, tofloat = false, check_length = false, kwargs...) |
| 87 | + |
| 88 | + dvs = unknowns(sys) |
| 89 | + int = symtype.(unwrap.(dvs)) .<: Integer |
| 90 | + if lb === nothing && ub === nothing |
| 91 | + lb = first.(getbounds.(dvs)) |
| 92 | + ub = last.(getbounds.(dvs)) |
| 93 | + isboolean = symtype.(unwrap.(dvs)) .<: Bool |
| 94 | + lb[isboolean] .= 0 |
| 95 | + ub[isboolean] .= 1 |
| 96 | + else |
| 97 | + xor(isnothing(lb), isnothing(ub)) && |
| 98 | + throw(ArgumentError("Expected both `lb` and `ub` to be supplied")) |
| 99 | + !isnothing(lb) && length(lb) != length(dvs) && |
| 100 | + throw(ArgumentError("Expected both `lb` to be of the same length as the vector of optimization variables")) |
| 101 | + !isnothing(ub) && length(ub) != length(dvs) && |
| 102 | + throw(ArgumentError("Expected both `ub` to be of the same length as the vector of optimization variables")) |
| 103 | + end |
| 104 | + |
| 105 | + ps = parameters(sys) |
| 106 | + defs = merge(defaults(sys), to_varmap(parammap, ps), to_varmap(u0map, dvs)) |
| 107 | + lb = varmap_to_vars(dvs .=> lb, dvs; defaults = defs, tofloat = false) |
| 108 | + ub = varmap_to_vars(dvs .=> ub, dvs; defaults = defs, tofloat = false) |
| 109 | + |
| 110 | + if !isnothing(lb) && all(lb .== -Inf) && !isnothing(ub) && all(ub .== Inf) |
| 111 | + lb = nothing |
| 112 | + ub = nothing |
| 113 | + end |
| 114 | + |
| 115 | + cstr = constraints(sys) |
| 116 | + if isempty(cstr) |
| 117 | + lcons = ucons = nothing |
| 118 | + else |
| 119 | + lcons = fill(-Inf, length(cstr)) |
| 120 | + ucons = zeros(length(cstr)) |
| 121 | + lcons[findall(Base.Fix2(isa, Equation), cstr)] .= 0.0 |
| 122 | + end |
| 123 | + |
| 124 | + kwargs = process_kwargs(sys; kwargs...) |
| 125 | + # Call `remake` so it runs initialization if it is trivial |
| 126 | + return remake(OptimizationProblem{iip}(f, u0, p; lb, ub, int, lcons, ucons, kwargs...)) |
| 127 | +end |
| 128 | + |
| 129 | +function check_compatible_system( |
| 130 | + T::Union{Type{OptimizationFunction}, Type{OptimizationProblem}}, sys::System) |
| 131 | + check_time_independent(sys, T) |
| 132 | + check_not_dde(sys) |
| 133 | + check_has_cost(sys, T) |
| 134 | + check_no_jumps(sys, T) |
| 135 | + check_no_noise(sys, T) |
| 136 | + check_no_equations(sys, T) |
| 137 | +end |
0 commit comments