Choice
Choice(i, n)
Choice displays a drop-down menu to allow an end user to select from a list of options. The index «i» is the list of options -- an index containing the values appearing on the popup. When I: Self
, the values listed in the variable's domain are used. «N» is the position of the currently selected option in that list, or 0 for All. This should always be an explicit integer, not an expression.
When the user selects a different option, it changes the value of «n». You can use Choice to provide a menu as the definition of a variable or in a cell of an Edit table (or ProbTable or DetermTable). Choice must be the top level of the definition or the cell, and may not be nested within an expression. You must create an Input node for the variable before it will display choices as menus.
When a user changes the selection using the dropdown, the second parameter in the definition is replaced with the new choice.
The full declaration of Choice() is:
- Choice(I: Index; n: atomic nonNegative; inclAll: optional boolean = True; Eval: optional boolean = True; Result: optional Array[I], separator: optional text)
Optional parameters
InclAll
«inclAll» (default True
) specifies whether the "All" option is displayed.
By default, the menu will include All as the first option. If you choose All, the result will be computed for each value of index «i». This makes it easy to do a parametric analysis to see how the choices affect the results. When you choose All, it sets parameter «n» to 0.
If you select All for multiple Choice menus, results will be computed for all combinations of values from those menus. Each selection of All may add an index dimension to the results. So if there are more than two or three, the number of dimensions may make the result hard to understand, and long to compute. In a few cases, the model may not be able to compute correctly with All. In such cases, you can turn off All as an option by setting an optional third parameter, «inclAll» to False
(0). It usually advisable to set «inclAll» to False
whenever you use Choice in an Edit table.
Eval
The «Eval» parameter specifies whether handle or expression terms are evaluated for the return value.
The «Eval» parameter has no effect when the Result parameter is specified, or when «I» contains only constants, such as numbers and text strings. Setting «Eval» to false is recommended in most cases when «I» contains handles (variable identifiers). The «Eval» parameter has no effect when «I» is Self (it is always treated as false).
Result
The Choice function has an optional parameter, «Result», that can be used to specify the value returned when Choice is evaluated in a completely general manner: «Result» specifies a return value for each choice different from the value in «I». Using the «Result» parameter, the result of evaluating Choice can be totally separate from the value appearing on the pull-down itself.
Choice normally returns the selected index label. The following syntax returns the index position (rather than label) of the selected value:
Choice(I, 1, Result: @I)
Suppose you have a table A
indexed by I
, and you want your user to select a row of I
, and then select out only the selected row. A standard formulation of this is:
Variable A := Table(I)(...)
Decision selectedRow := Choice(I, 1)
Variable selectedA := A[I = selectedRow]
Using the «Result» parameter, a variable can be eliminated:
Variable A := Table(I)(...)
Decision selectedA := Choice(I, 1, Result: A)
When the «Result» parameter is specified, the «Eval» parameter does nothing.
Separator
You can specify that certain items in I
are to display as separators. Separators are horizontal lines on the popup that are nonselectable. To include separators, use the «separator» parameter to specify what value in «I» should display as a separator. You can include that value in I
multiple times for multiple separators:
Choice(I, 1, separator:'----')
Index Country_S := ['Canada', 'Mexico', 'USA', '---',
'England', 'France', 'Germany', 'Russia', '---',
'China', 'India', 'Japan','Philippines', '---',
'Australia', 'New Zealand', '---',
'Argentina', 'Brazil', 'Chile']
Variable Select_country := Choice(Country_S, 1, separator:'---')
Examples
Suppose multiple variables use Choice(I, n) with the same index I
, and you choose All for each of them, e.g.
Index I := ['Low', 'Mid', 'High']
Variable A := Choice(I, 0)
Variable B := Choice(I, 0)
Variable Result := IF A = 'Low' AND B = 'Low' THEN 0 ELSE 100 →
Array(I, [0, 100, 100])
Result
is indexed by I only once, and A and B are treated as if they have the same values. If you wanted to vary them independently, they should use different indexes, e.g.
Index I := ['Low', 'Mid', 'High']
Variable A := Choice(I, 0)
Index J := CopyIndex(I)
Variable B := Choice(J, 0)
Variable Result := IF A = 'Low' AND B = 'Low' THEN 0 ELSE 100 →
Array(I, J, [0, 100, 100], [100, 100, 100], [100, 100, 100])
The function CopyIndex(I) makes returns a new index with the same values as I
.
Details and more examples
Self-Indexed Choices
When a variable is defined with the first parameter set to Self, as in the definition:
Choice(Self, 0)
then the set of values appearing in the choice are located in the variable's own domain attribute. In this case, the domain must be either a list, list-of-labels or index type.
The easiest way to create a Self-Choice is to select "Choice..." from the definition type popup. Analytica will then fill in a default one-element list-of-labels selection.
When a domain is a list, the elements are usually numbers, but may also be variable identifiers or expressions. (See "Identifier and Expression items" below).
If you use an Index domain, then the values for domain of Self come from another index variable. In this case, Choice(Self,1)
when domain = I
is very similar to Choice(I,1)
with no domain set. There are, however, several differences.
First, if you use an index domain, then your variable will have both an index value and mid/prob values, while if you use Choice(I, 1)
your variable will not have an index semantics.
Second, if "All" is selected in the pulldown, the result of <ode>Choice(I, 0) is indexed by I
, while Choice(Self, 0)
is indexed by Self.
Third, in an index domain using Index I, the values in the domain are the evaluated values of I
. If I
contains only numbers and text strings, then these are the same as the values of I
, but if I
contains identifiers or expressions, then there is a difference. Choice(I, 1)
would show the identifiers or expressions, while Choice(Self, 1)
with domain = I
would show the values of those variables in the pulldown.
Input Nodes
Selecting "Make Input" from the Object menu creates a pull-down control that you can position on your diagram as part of a user-interface form.
Pull-downs in edit tables
You can define an edit table cell using the Choice function. This is a useful way to restrict the possible values your users can enter into the cell.
To create a choice pull-down, simply type the expression, such as Choice(option, 1, 0)
. In a table, you will usually want the third option, «inclAll», to be 0 so that the All option does not appear.
Choice cells can also appear in DetermTables, ProbTables, IntraTables and SubTables. Checkboxes can also appear in table cells in the same fashion.
In Analytica 4.5 or later
As soon as you enter a valid Choice expression, the cell displays as a pull-down control. You can immediately use the control to select the desired option for the cell. An expression/choice selector also appears on the right edge near the top of the window:
This selector is useful if you need to modify or view the textual expression. If you want to remove the control, the best option is to view the expression and replace it with a single number.
In browse mode, cells with controls always appear as controls. The will be in a disabled state (in which their value cannot be changed) in browse mode unless you have created an input node for the table.
In Analytica 4.0 thru Analytica 4.4
When you are in edit mode, the expression is shown, allowing you to edit the definition.
When the edit table is viewed from browse mode, a pulldown control appears in the cell. If your table does not have an input node associated with it, then the table itself is read-only and the value cannot be changed. Thus, when you are using Choice cells, you will usually create an input node for your edit table. When an edit table with an associated input node is viewed, choice cells display active, changeable pulldown controls.
Using Choice with DetermTable
A useful arrangement is to use Choice variables for key parameters, especially parameters where you may want to perform a parametric analysis (by selecting "All"), and then organizing tabular data in DetermTables (rather than standard Edit Tables). This arrangement looks like the following:
Decision Scenario := Choice(Self, 0)
Domain of Scenario : [1, 2, 3, 4]
Variable A := DetermTable(Scenario)(...)
In this arrangement, the definition of A
appears as an edit table, indexed by all domain values of Scenario
. However, when evaluated, only the selected row is returned.
With this arrangement, you can compute downstream results for a single Scenario
(or other dimension), or for every scenario when "All" is selected. When available memory limits your ability to perform a full parameter analysis across many dimensions (since the number of combinations increases multiplicatively), this setup makes it easy to limit the parametric analysis to only selected dimensions.
A second variation on this is:
Index AllScenarios := [1, 2, 3, 4]
Decision Scenario := Choice(Self, 0)
Domain of Scenario : Index AllScenarios
Variable A := DetermTable(Scenario)(...)
Identifier and Expression Items
When an option in «I» is a handle to a variable, then the pulldown will display either the title of the object or its identifier, depending on whether "Show By Identifier" is selected. The "Show By Identifier" setting is found on the Object menu and can also be toggled by pressing Ctrl+Y .
When you want to set up an explicit list of identifiers to select between, the best way to do this is to create an index node and turn on its MetaOnly attribute. Then set the definition to be a list and enter the identifiers in each cell. You can also compute a list of identifiers.
When the selected item is a handle to a variable, the result of evaluating the Choice function will be either the handle or the result of evaluating the handle, as follows. If the «eval» parameter is specified and set to false, or if the «I» option is Self, then the result is the handle. If the «eval» parameter is specified and true, and the «I» parameter is an index (i.e., not Self), then the result is the result of evaluating the selected variable. When «eval» is not specified, then a handle is returned if «I» is index node with the MetaOnly attribute set or if «I» species the domain of another variable, or otherwise the result of evaluating the variable is returned.
If you are dealing with Choices involving variable identifiers, using Eval: False
is generally preferred. Operations such as comparing the selected value to the original index, or use of a DetermTable based on the choice variable, function more consistently and less ambiguously. As a comparison, if you return values (e.g., Eval: True
), and two variables in the choice happen to have the same value, then an ambiguity over which variable was selected exists, but with Eval: False
no such ambiguity exists.
History
Releases prior to Analytica 4.4.2 do not support extremely long option lists on the Choice popup. In those releases, the choice pulldown displays only the first 254 items, plus the selected item. This limitation is fixed in patch release 4.4.2, where no such limitation exists. If you have many options and for some reason cannot upgrade to 4.4.2 or later, consider creating a 2-stage hierarchical menu, where the first pulldown offers subgroups, and the second shows a the items in that subgroup.
Pulldowns in Analytica 4.5 and later are described above.
Enable comment auto-refresher