VAR (assignments)

<< Click to Display Table of Contents >>

Navigation:  Gekko User Manual > Gekko statements >

VAR (assignments)

Previous pageReturn to chapter overviewNext page

Gekko statements of the type ... = ... ; assign the right-hand side expression to the left-hand side variable, which is also called an assignment. Assignments can be done for different types of variables, and you can read more about the specific variable types and their syntax on these pages: SERIES, VAL, DATE, STRING, LIST, MAP and MATRIX.

 

The var keyword can always just be omitted, so it is in a sense superfluous in assignment statements. For example, these three statements are equivalent:

 

= 100;
var y = 100;
series y = 100;

 

The first statement works because Gekko notices that the left-hand side variable has no type symbol % or #, so the variable must therefore be a timeseries (with constant value 100). The next statement just adds the superfluous var keyword. The third statement guarantees that y ends up being a timeseries, but we knew this already. Therefore,

 

If you use a type indicator, it will guarantee that the left-hand side of the expressions ends up being of the indicated type. For instance:

 

date %= 2020;

 

In this case, the right-hand side is actually a numeric value (it would have been a date, if it had been stated as for instance 2020a, or 2020q3), and the date keyword indicates that Gekko should try to convert the right-hand side into a date, which is possible in this case. On the contrary,

 

var %= 2020;

 

does not indicate any type on %d, so %d will end up being a value type. The same goes for the following:

 

%= 2020;

 

In this case, %d also ends up being a value type, too. As a last note, the following statements are equivalent:

 

date %= 2020;
%= date(2020);

 

To conclude: in most cases, the type identifier (or the var keyword) can just be omitted in assignment statements (statements of the form ... = ... ;, unless the user wants to be absolutely sure of the type of a given left-hand side variable. The following are examples of the compact way of assigning, without using type indicators or VAR:

 

= 100;                         //will become a series
<2020 2030> = 100;             //will become a series
%= 'abc';                      //will become a string
%= 2.5;                        //will become a value
%= 2020;                       //will become a value: works fine as annual date, too
#= (1, 2*3-4);                 //will become a list of values
#= 1, 2;                       //allowed for simple numbers (no math expressions).
#= ('a', 'b');                 //will become a list of strings, "#x = a, b;" can be used too
#= a, b;                       //allowed for simple strings
#= (%x1 = 'abc', %x2 = 2.5);   //will become a map
#= [1, 2];                     //will become a 1x2 matrix (row vector)

 

In this appendix, variable assignment rules, including variable types, is explained in more detail.

 

 


 

Related statements

 

SERIES, VAL, DATE, STRING, LIST, MAP, MATRIX