@@ -53,10 +53,8 @@ sol = solve(prob, Optim.KrylovTrustRegion())
53
53
54
54
# Now derivative-based optimizers with various constraints
55
55
56
- cons = (res,x,p) -> res .= [x[1]^2 + x[2]^2]
57
- optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff();cons= cons)
58
- #prob = OptimizationProblem(optf, x0, _p)
59
- #sol = solve(prob, IPNewton()) # No lcons or rcons, so constraints not satisfied
56
+ cons = (res,x,p) -> res .= [x[1]^2 + x[2]^2]
57
+ optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff();cons= cons)
60
58
61
59
prob = OptimizationProblem(optf, x0, _p, lcons = [-Inf], ucons = [Inf])
62
60
sol = solve(prob, IPNewton()) # Note that -Inf < x[1]^2 + x[2]^2 < Inf is always true
@@ -73,6 +71,14 @@ prob = OptimizationProblem(optf, x0, _p, lcons = [0.5], ucons = [0.5],
73
71
sol = solve(prob, IPNewton()) # Notice now that x[1]^2 + x[2]^2 ≈ 0.5:
74
72
# cons(sol.minimizer, _p) = 0.49999999999999994
75
73
74
+ function con_c(res,x,p)
75
+ res .= [x[1]^2 + x[2]^2]
76
+ end
77
+
78
+ optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff();cons= con_c)
79
+ prob = OptimizationProblem(optf, x0, _p, lcons = [-Inf], ucons = [0.25^2])
80
+ sol = solve(prob, IPNewton()) # -Inf < cons_circ(sol.minimizer, _p) = 0.25^2
81
+
76
82
function con2_c(res,x,p)
77
83
res .= [x[1]^2 + x[2]^2, x[2]*sin(x[1])-x[1]]
78
84
end
@@ -81,10 +87,7 @@ optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff();cons= con
81
87
prob = OptimizationProblem(optf, x0, _p, lcons = [-Inf,-Inf], ucons = [Inf,Inf])
82
88
sol = solve(prob, IPNewton())
83
89
84
- cons_circ = (x,p) -> res .= [x[1]^2 + x[2]^2]
85
- optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff();cons= cons_circ)
86
- prob = OptimizationProblem(optf, x0, _p, lcons = [-Inf], ucons = [0.25^2])
87
- sol = solve(prob, IPNewton()) # -Inf < cons_circ(sol.minimizer, _p) = 0.25^2
90
+
88
91
89
92
# Now let's switch over to OptimizationOptimisers with reverse-mode AD
90
93
@@ -107,7 +110,7 @@ prob = OptimizationProblem(optf, x0, _p)
107
110
sol = solve(prob, Opt(:LN_BOBYQA, 2))
108
111
sol = solve(prob, Opt(:LD_LBFGS, 2))
109
112
110
- ## Add some box constarints and solve with a few NLopt.jl methods
113
+ ## Add some box constraints and solve with a few NLopt.jl methods
111
114
112
115
prob = OptimizationProblem(optf, x0, _p, lb=[-1.0, -1.0], ub=[0.8, 0.8])
113
116
sol = solve(prob, Opt(:LD_LBFGS, 2))
0 commit comments