# OptSolution

## OptSolution(opt*, decision*)

Returns the solution to an optimization problem «opt» specified by DefineOptimization.

If you specify as «decision» the name of a global Decision variable used in the Optimization, it returns the solution (optimal value) for that decision. If you omit «decision», it returns the solution to all Decision variables as a vector indexed by a local index **.Vars**, which contains all the decision values flattened so that each solution is a scalar.

Evaluating a variable that uses **OptSolution**() will trigger an attempt to solve the optimization problem, unless it has already been solved by another call to **OptSolution**, or a related function, such as OptStatusText.

**OptSolution** returns a result when it finds an optimal, or likely optimal solution. If it finds no feasible solution it gives a warning if you have Show Result Warnings turned on. It is entirely possible that there is no solution, or that the solver could not find a feasible solution, in which cases the values returned by **OptSolution** are arbitrary. So, you should always check OptStatusText or OptStatusNum to check that it has found a feasible and optimal solution.

If the Optimization used a local variable, say `D1`

, as a Decision, declared as local in the expression using DefineOptimization, for example

`Variable OptimizeIt := VAR d1 := 0; DefineOptimization(Decisions: d1; ....)`

you can get its solution by defining a local with the same identifier, `D1`

, preceding **OptSolution**(), and giving `D1`

as «decision»:

`Variable D1_solution := VAR d1 := 0; OptSolution(OptimizeIt, d1)`

## Example

Find the minimum of the GammaFn(x) for `x > 0`

:

`Var x := 1;`

`Var opt := DefineOptimization(decisions: x, minimize: GammaFn(x), domain: Continuous(lb: 0));`

`OptSolution(opt, x)`

## History

This function was introduced in Analytica 4.3, in earlier versions, use LpSolution.

Enable comment auto-refresher