Proactive Evaluation

Revision as of 19:17, 19 January 2013 by Max (talk | contribs)


Unlike conventional computer languages and spreadsheets, Analytica only evaluates each variable when it needs to -- e.g. when you press a "Calc" button to view its result, or the value of a variable that depends on it. It does't (usually) evaluate variables you don't yet need. After evaluating a variable, it keeps (caches) the value until something that it depends on is changed, when it is reset to "uncomputed". We refer to this scheme as lazy evaluation. The advantages of lazy evaluation are:

  • you don't have to wait while it computes results you don't need (e.g. when loading the model), and
  • you don't have to wait while it recomputes results that it computed previously.

Proactive evaluation is the opposite of lazy evaluation: Analytica evaluates a proactively evaluated variable while loading the model. It re-evaluates it immediately after you change its definition or any variable it depends on. In some cases, a variable with standard lazy evaluation may be evaluated on loading anyway because it is used by another variable that needs immediate evaluation. One example is where a Choice pulldown input control appears on a diagram. In order to display the diagram, Analytica must compute the index values for the Choice, and hence, the index object is evaluated. Another example is an index used in an edit table. The length of the index value is needed to parse the edit table (which happens when loading the model), and so must be evaluated immediately.

For these reasons, Analytica (by default) evaluates index variables proactively -- when loading the model or immediately after a change in its definition or a predecessor. Usually indexes are simple sequences or lists of text values that take negligible time to evaluate anyway. But, occasionally they involve long calculations. One example is where the model queries an external database. If you use an index object that calls DbQuery(..), it executes the query to read the data when first loading the model, causing a excessive load time if the query is complex.

In such cases, you can change the Index from Proactive to Lazy evaluation, so that they don't slow down loading. There are three ways to do that:

  • Simply change the Class of the Index variable to a standard Variable. Standard variables use lazy evaluation. A standard variable with the definition of an index will always work fine as an index -- e.g. as an index of an array. You can change the class from the Class pulldown menu at the top of the variable's Object window, or by displaying Class in the Attribute panel.
  • In the Preferences dialog (from Edit menu), uncheck Proactively evaluate indexes. This will change all Indexes to use lazy evaluation.
  • Change the ProactivelyEvaluate Attribute of the Index to lazy evaluation (e.g. to zero). See next section.

Proactive evaluation is also useful if you want output nodes to show its computed values immediately when displaying the diagram containing those outputs, without requiring the user to click the Calc buttons.

Attribute ProactivelyEvaluate

You can explicitly control whether each variable or index object is proactively or lazily evaluated by setting the attribute ProactivelyEvaluate.

The attribute setting can be used to control the default behavior for any particular Analytica class (you would normally change it from the typescript window). For example, in the typescript window you can see how the Index class is configured to proactively evaluate by default:

Untitled> Profile Index
Object Index
NodeColor: 39321,39325,65535
ProactivelyEvaluate: 1

By changing the attribute to 0, or deleting the attribute value entirely, you can alter Analytica 4.3's behavior so that index objects will be evaluated lazily by default:

Untitled> ProactivelyEvaluate Index : 0

Likewise, you could configure Constants to be proactively evaluated:

Untitled> ProactivelyEvaluate Constant : 1

For object classes like Decision, Variable, Index, Objective, Constraint, and Constant, the attribute controls the global default behavior for all objects of the indicated class. You can override this default for individual variable objects by setting the attribute explicitly for the indicated variable. For example:

Untitled> ProactivelyEvaluate Va1 : 1
Untitled> ProactivelyEvaluate In1 : 0

The above lines change the default so that index In1 is lazily evaluated and variable Va1 is proactively evaluated, regardless of the class default.

The value assigned to the ProactivelyEvaluate attribute is actually a bit-field, which can be the sum of any of the following values:

1 = Index Value
2 = Domain value
4 = Mid value
8 = Prob value

Setting to 1, as the previous examples do, cause Analytica to compute the index value. In many cases, computing the index value (or discovering that there is no self-index value) will also require evaluate of the Mid value. Alternatively, setting it to 4 would force the proactive computation of the mid-value.

When Proactive Evaluation occurs

When a variable X is configured to be proactively evaluated, this proactive evaluation occurs: (a) when the model is first loaded, and (b) when the definition of X is changed (e.g., by editing it in the object window).

Suppose the definition of a variable that X depends on changes. This does not cause the immediate proactive evaluation of X. Thus, if you have an output control for X, it will switch to a [Calc] button. If the definition of X is edited, then it will proactively evaluate. (Enhancement being considered: A variable set to proactively evaluate Mid=4 or Prob=8 would automatically evaluate when the output control is rendered on the diagram. The [Calc] button would appear only when the evaluation is unsuccessful because of an error or user abort).

Comments


You are not allowed to post comments.