Difference between revisions of "Error Messages/41302"

(Created page with "= Example Warning Message = Operating over the dynamic index Time in the call to function Sum from within a dynamic loop. The semantics of this operation may be differ...")
 
 
Line 1: Line 1:
= Example Warning Message =
+
[[Category: Error messages]]
  
Operating over the dynamic index [[Time]] in the call to function [[Sum]] from within a dynamic loop. The
+
== Example warning message ==
semantics of this operation may be different than you expect.
 
  
= Cause =
+
<pre style="background:white; border:white; margin-left: 1em; font-style:italic">
 +
Operating over the dynamic index Time in the call to function Sum from within a dynamic loop.
 +
The semantics of this operation may be different than you expect.
 +
</pre>
 +
 
 +
== Cause ==
  
 
This warning usually highlights a conceptual error in your definition(s).  It occurs when you do something like this:
 
This warning usually highlights a conceptual error in your definition(s).  It occurs when you do something like this:
  
:<code>[[Dynamic]](0, [[Sum]](Revenue,[[Time]]) - newExpenses[ [[Time]]-1 ] )</code>
+
:<code>Dynamic(0, Sum(Revenue, Time) - newExpenses[Time - 1])</code>
  
 
or this:
 
or this:
  
:<code>[[Dynamic]]( [[Cumulate]](Revenue,[[Time]]) )</code>
+
:<code>Dynamic(Cumulate(Revenue, Time))</code>
  
 
The key here being that a function is operating over the [[Time]] index from within a dynamic loop.  The error will result even if the array operation does not appear within the [[Dynamic]] function, but is itself inside a dynamic loop formed by a call to [[Dynamic]] in another variable.  For example:
 
The key here being that a function is operating over the [[Time]] index from within a dynamic loop.  The error will result even if the array operation does not appear within the [[Dynamic]] function, but is itself inside a dynamic loop formed by a call to [[Dynamic]] in another variable.  For example:
  
:Variable Revenue:=
+
:<code>Variable Revenue := Dynamic(... CumRevenue[Time - 1] ... )</code>
::<code>Dynamic(... CumRevenue[Time-1] ... ) </code>
 
  
:Variable CumRevenue :=
+
:<code>Variable CumRevenue := Cumulate(Revenue, Time)</code>
::<code>[[Cumulate]](Revenue,[[Time]])</code>
 
  
 +
In most cases when people try something like this, what they are encoding is quite a bit different from what they think they are encoding, which is why the error arises.  When a [[Dynamic|dynamic loop]] is being evaluated, the definition(s) are being evaluated at a single point in time.  An easy way to conceptualize this is that every value involved in the definition has been implicitly sliced to the current time [[slice]].  So when the <code>Time = 3</code> value is being evaluated, the expression being evaluated for <code>CumRevenue</code> is essentially:
  
In most cases when people try something like this, what they are encoding is quite a bit different from what they think they are encoding, which is why the error arises.  When a [[Dynamic|dynamic loop]] is being evaluated, the definition(s) are being evaluated at a single point in time.  An easy way to conceptualize this is that every value involved in the definition has been implicity sliced to the current time slice.  So when the Time=3 value is being evaluated, the expression being evaluated for CumRevenue is essentially:
+
:<code>Cumulate(Revenue[Time = 3], Time)</code>
::<code>[[Cumulate]](Revenue[Time=3],Time)</code>
 
  
When you wrote the expression, you were probably thinking that the parameter inside [[Cumulate]] would vary along the time index, perhaps containing the values computed so far or something along those lines.  But in fact, what you get is a single element of the array.  So instead of an array containing e.g., [23,24,50,''uncomputed'',''uncomputed'',...] (future times haven't been computed yet), you might get just 56.  When you cumulate this over Time, it is treated as if the array being cumulated is constant for every time period, i.e., [50,50,50,50,50,...].  So the cumulated result would be [50,100,150,200,250,...] -- which is not what you expected, right?
+
When you wrote the expression, you were probably thinking that the parameter inside [[Cumulate]] would vary along the time index, perhaps containing the values computed so far or something along those lines.  But in fact, what you get is a single element of the array.  So instead of an array containing e.g., [23, 24, 50, ''uncomputed'', ''uncomputed'',...] (future times haven't been computed yet), you might get just 56.  When you cumulate this over [[Time]], it is treated as if the array being cumulated is constant for every time period, i.e., [50, 50, 50, 50, 50,...].  So the cumulated result would be [50, 100, 150, 200, 250,...] -- which is not what you expected, right?
  
If you understand that the value being used are pre-sliced, then maybe your expression is doing what you think.  Far more likely, however, you have conceptualized incorrect how you should be expressing your logic inside a dynamic loop.  One thing to keep in mind is that when you operate over a value, the implication is that its full value has already been computed.  So if you compute <code>[[Sum]](A,Time)</code>, this makes sense only if ''A'' has already been fully computed for all time points.  If A requires a result from your current definition (e.g., at an earlier time point), then it cannot possibly be fully computed when your definition is being evaluated.
+
If you understand that the value being used are pre-sliced, then maybe your expression is doing what you think.  Far more likely, however, you have conceptualized incorrect how you should be expressing your logic inside a dynamic loop.  One thing to keep in mind is that when you operate over a value, the implication is that its full value has already been computed.  So if you compute <code>Sum(A, Time)</code>, this makes sense only if <code>A</code> has already been fully computed for all time points.  If <code>A</code> requires a result from your current definition (e.g., at an earlier time point), then it cannot possibly be fully computed when your definition is being evaluated.
  
= Remedy =
+
== Remedy ==
  
In most cases where people attempt to encode logic, such as a cumulation of previous values, along a dynamic index, there are usually more direct ways to encode this using Dynamic.  For example:
+
In most cases where people attempt to encode logic, such as a cumulation of previous values, along a dynamic index, there are usually more direct ways to encode this using [[Dynamic]].  For example:
  
:Variable CumRevenue :=
+
:<code>Variable CumRevenue := Dynamic(Revenue, Revenue + Self[Time - 1])</code>
::[[Dynamic]](Revenue, Revenue + Self[Time-1] )
 
  
:Variable PeakRevenue :=
+
:<code>Variable PeakRevenue := Dynamic(Revenue, Max([Self[Time - 1], Revenue]))</code>
::[[Dynamic]](Revenue, [[Max]]([Self[Time-1],Revenue]) )
 
  
 
In some cases, it makes sense to utilize a local index that spans only previous time periods:
 
In some cases, it makes sense to utilize a local index that spans only previous time periods:
  
:Variable PeakRevenue :=
+
<pre style="background:white; border:white; margin-left: 1em;">
::[[Dynamic]](Revenue,
+
Variable PeakRevenue := Dynamic(Revenue,
:::[[Index]] prevT := [[Subset]]([[IndexValue]](Time) < Time);
+
  Index prevT := Subset(IndexValue(Time) < Time);
:::[[Max]](Revenue[Time=prevT],prevT]
+
      Max(Revenue[Time = prevT], prevT]
::)
+
  )
 
+
</pre>
= See Also =
 
  
 +
== See Also ==
 +
* [[Time]]
 
* [[Dynamic]]
 
* [[Dynamic]]
 +
* [[Cumulate]]
 +
* [[Sum]]

Latest revision as of 01:06, 9 March 2016


Example warning message

 Operating over the dynamic index Time in the call to function Sum from within a dynamic loop. 
 The semantics of this operation may be different than you expect.

Cause

This warning usually highlights a conceptual error in your definition(s). It occurs when you do something like this:

Dynamic(0, Sum(Revenue, Time) - newExpenses[Time - 1])

or this:

Dynamic(Cumulate(Revenue, Time))

The key here being that a function is operating over the Time index from within a dynamic loop. The error will result even if the array operation does not appear within the Dynamic function, but is itself inside a dynamic loop formed by a call to Dynamic in another variable. For example:

Variable Revenue := Dynamic(... CumRevenue[Time - 1] ... )
Variable CumRevenue := Cumulate(Revenue, Time)

In most cases when people try something like this, what they are encoding is quite a bit different from what they think they are encoding, which is why the error arises. When a dynamic loop is being evaluated, the definition(s) are being evaluated at a single point in time. An easy way to conceptualize this is that every value involved in the definition has been implicitly sliced to the current time slice. So when the Time = 3 value is being evaluated, the expression being evaluated for CumRevenue is essentially:

Cumulate(Revenue[Time = 3], Time)

When you wrote the expression, you were probably thinking that the parameter inside Cumulate would vary along the time index, perhaps containing the values computed so far or something along those lines. But in fact, what you get is a single element of the array. So instead of an array containing e.g., [23, 24, 50, uncomputed, uncomputed,...] (future times haven't been computed yet), you might get just 56. When you cumulate this over Time, it is treated as if the array being cumulated is constant for every time period, i.e., [50, 50, 50, 50, 50,...]. So the cumulated result would be [50, 100, 150, 200, 250,...] -- which is not what you expected, right?

If you understand that the value being used are pre-sliced, then maybe your expression is doing what you think. Far more likely, however, you have conceptualized incorrect how you should be expressing your logic inside a dynamic loop. One thing to keep in mind is that when you operate over a value, the implication is that its full value has already been computed. So if you compute Sum(A, Time), this makes sense only if A has already been fully computed for all time points. If A requires a result from your current definition (e.g., at an earlier time point), then it cannot possibly be fully computed when your definition is being evaluated.

Remedy

In most cases where people attempt to encode logic, such as a cumulation of previous values, along a dynamic index, there are usually more direct ways to encode this using Dynamic. For example:

Variable CumRevenue := Dynamic(Revenue, Revenue + Self[Time - 1])
Variable PeakRevenue := Dynamic(Revenue, Max([Self[Time - 1], Revenue]))

In some cases, it makes sense to utilize a local index that spans only previous time periods:

Variable PeakRevenue := Dynamic(Revenue,
   Index prevT := Subset(IndexValue(Time) < Time);
      Max(Revenue[Time = prevT], prevT]
   )

See Also

Comments


You are not allowed to post comments.