|
3 | 3 | ```@docs |
4 | 4 | solve(::OptimizationProblem,::Any) |
5 | 5 | ``` |
| 6 | + |
| 7 | +## Understanding the Solution Object |
| 8 | + |
| 9 | +The `solve` function returns an `OptimizationSolution` object that contains the results of the optimization. This object has several important fields that provide information about the solution and the optimization process. |
| 10 | + |
| 11 | +### Main Solution Fields |
| 12 | + |
| 13 | +- **`sol.u`** or **`sol.minimizer`**: The optimal values found by the optimizer. These are aliases - both refer to the same solution vector. Use whichever feels more natural for your problem context. |
| 14 | + |
| 15 | +- **`sol.objective`** or **`sol.minimum`**: The objective function value at the solution point. These are also aliases for the same value. |
| 16 | + |
| 17 | +- **`sol.retcode`**: The return code indicating how the optimization terminated. Common values include: |
| 18 | + - `ReturnCode.Success`: Optimization converged successfully |
| 19 | + - `ReturnCode.MaxIters`: Maximum iterations reached |
| 20 | + - `ReturnCode.MaxTime`: Maximum time limit reached |
| 21 | + - `ReturnCode.Failure`: Optimization failed (check solver output for details) |
| 22 | + |
| 23 | +- **`sol.stats`**: Statistics about the optimization process, including: |
| 24 | + - `iterations`: Number of iterations performed |
| 25 | + - `time`: Total computation time |
| 26 | + - `fevals`: Number of objective function evaluations |
| 27 | + - `gevals`: Number of gradient evaluations (if applicable) |
| 28 | + |
| 29 | +- **`sol.original`**: The original output from the underlying solver package (solver-specific) |
| 30 | + |
| 31 | +- **`sol.cache`**: The optimization cache used during solving (advanced use) |
| 32 | + |
| 33 | +### Example Usage |
| 34 | + |
| 35 | +```julia |
| 36 | +using Optimization, OptimizationOptimJL |
| 37 | + |
| 38 | +# Define and solve an optimization problem |
| 39 | +rosenbrock(u, p) = (p[1] - u[1])^2 + p[2] * (u[2] - u[1]^2)^2 |
| 40 | +optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff()) |
| 41 | +prob = OptimizationProblem(optf, zeros(2), [1.0, 100.0]) |
| 42 | +sol = solve(prob, Optim.BFGS()) |
| 43 | + |
| 44 | +# Access solution information |
| 45 | +println("Solution: ", sol.u) # or sol.minimizer |
| 46 | +println("Objective value: ", sol.objective) # or sol.minimum |
| 47 | +println("Return code: ", sol.retcode) |
| 48 | +println("Iterations: ", sol.stats.iterations) |
| 49 | +println("Function evaluations: ", sol.stats.fevals) |
| 50 | +println("Total time: ", sol.stats.time, " seconds") |
| 51 | + |
| 52 | +# Check if optimization was successful |
| 53 | +if sol.retcode == ReturnCode.Success |
| 54 | + println("Optimization converged successfully!") |
| 55 | +end |
| 56 | +``` |
| 57 | + |
| 58 | +### Array Interface |
| 59 | + |
| 60 | +The solution object also supports an array interface for convenience: |
| 61 | + |
| 62 | +```julia |
| 63 | +# Access solution components like an array |
| 64 | +sol[1] # First component of the solution vector |
| 65 | +sol[:] # All components (same as sol.u) |
| 66 | +length(sol) # Number of optimization variables |
| 67 | +``` |
0 commit comments