# OptScalarToDecision

## Contents

## Function OptScalarToDecision(opt, decision, scalarValues*, decisionVector*)

When you define a *Structured Optimization* problem using DefineOptimization(), any given decision in the problem may have intrinsic indexes, making it an array-valued decision. Internally, each decision in the structured model must be flattened into a vector, and the vector for each decision must be concatenated to obtain the scalar representation of the problem, which can the be passed to a solver engine. Analytica take care of this mapping from structured decisions to internal vector and back again for you. However, should a reason arise where you need to map from the scalar decisions vector back to an individual structured decision, **OptScalarToDecision** performs that mapping. It extracts the portion of the «scalarValues» corresponding to the indicated «decision», then unflattens that subvector to obtain a result with the intrinsic indexes of «decision».

To use, «opt» must be the result of a call to DefineOptimization, «constraint» must identify a constraint in the optimization, «scalarValues» is an array indexed by the index «decisionVector», which must have exactly the same number of elements as there are scalar decision variables in the problem. The «decisionVector» index parameter is optional when «scalarValues» is 1-D.

## Examples

The function call OptSolution(opt), with a single parameter, returns the solution values for all scalar decision variables. The solution for one particular decision, `Price`

, can be obtained as:

`var opt_v:=OptSolution(opt) do OptScalarToDecision(opt, Price, opt_v ,opt_v.DecisionVector)`

This result is identical to what you get from a call to `OptSolution(opt, Price)`

with the second parameter specified.

## Uses

One example where it has been used has been as followed. A large LP model was formulated in Analytica using Structured Optimization. The scalar representation of the problem was exported using OptWrite, and an external optimization package (Gusek) was used to compute the solution (rather than one of the built-in Engines). The external solves wrote the solution to a file, which was then loaded in and parsed, yielding a vector of the scalar decision values. These were then mapped back into the dimensionality of the original decision variables and used within the model.

Another use is to help you interpret the contents of an LP file written by OptWrite. When debugging a problem formulation, it is sometimes useful to write the problem out to an LP file using OptWrite, and then view your problem formulation from there to see if it is doing what you intended. However, the format of that file drops the structure of your model and represents everything as scalar variables `x0, x1, ...`

. How do you know which of these variables corresponds to your *Price* decision variable? You can find out as follows:

`Index decisionVector := OptInfo(opt, "decisionVector");`

`OptScalarToDecision(opt, Price,"x" & (@decisionVector-1))`

The result has the dimensionality of `Price`

, with each cell containing the name (e.g., "x234") of the scalar decision that corresponds to that cell.

## History

This function was introduced in Analytica 4.3.

## See Also

- OptScalarToConstraint
- These functions automatically perform this mapping when the «constraint» parameter is specified:

Enable comment auto-refresher