Skip to content

Commit 58cd680

Browse files
Improve documentation for solve function results
Addresses issue #587 by adding comprehensive documentation about the OptimizationSolution object returned by solve(), including: - Detailed explanation of all solution fields (u/minimizer, objective/minimum, retcode, stats, etc.) - Clear documentation about the aliases (u/minimizer and objective/minimum) - Example code showing how to access and use solution information - Array interface documentation - Cross-references between related documentation pages This helps users understand what properties are available in the solution object and how to use them effectively. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c5648ec commit 58cd680

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

docs/src/API/optimization_solution.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,24 @@
33
```@docs
44
SciMLBase.OptimizationSolution
55
```
6+
7+
## Accessing Solution Information
8+
9+
The `OptimizationSolution` type is returned by the `solve` function and contains all information about the optimization result. Here's a quick reference for the most commonly used fields:
10+
11+
### Solution Values
12+
- `sol.u` or `sol.minimizer`: The optimal parameter values
13+
- `sol.objective` or `sol.minimum`: The objective function value at the optimum
14+
15+
### Convergence Information
16+
- `sol.retcode`: Return code indicating termination reason
17+
- `sol.stats`: Detailed statistics about the optimization process
18+
19+
### Aliases
20+
For user convenience, the following aliases are provided:
21+
- `sol.minimizer` is an alias for `sol.u`
22+
- `sol.minimum` is an alias for `sol.objective`
23+
24+
These aliases allow you to use terminology that feels more natural for your specific problem context.
25+
26+
For detailed examples and usage patterns, see the [Common Solver Options](@ref) section.

docs/src/API/solve.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,65 @@
33
```@docs
44
solve(::OptimizationProblem,::Any)
55
```
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

Comments
 (0)