Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

imit_model / AnyLogic / UsersManual(AnyLogic)

.pdf
Скачиваний:
125
Добавлен:
06.06.2015
Размер:
4.28 Mб
Скачать

AnyLogic V User’s Manual

Operator Description

for(){ },

Cyclic operators.

while(){ }

 

 

 

if(){ },

Conditional operators.

if(){ }else{ }

 

 

 

traceln()

Print operator.

Table 6. Java language operators

5.4 Algebraic loops

Sometimes formulas form an algebraic loop. In the example shown in Figure 54 the formula in object myObj1 defines dependency of x on y, and the formula in the object myObj2 – dependency of y from x.

Root

myObj1

myObj2

Name:

y

 

 

 

 

 

 

 

 

Name:

y

MyClass1

 

 

 

MyClass2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Name:

x

 

 

 

 

 

 

 

 

Name:

x

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Name:

MyClass1

 

 

Name:

MyClass2

 

 

Equations:

x = - y

 

 

Equations:

y = x - 2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 54. Algebraic loop

Clearly, these two formulas form a set of two algebraic equations with two unknown variables. The user, however, should not care about this. AnyLogic automatically detects

© 1992-2004 XJ Technologies http://www.xjtek.com

117

Chapter 5. Equations

algebraic loops and substitutes formulas with necessary equations. In our example it looks like:

0 = x + y0 = x y 2 .

The solution is: x = 1, y = -1.

An algebraic loop also happens when initial values of two variables refer to each other. For example, when var1 initializes var2, var2 initializes var3, and var3 initializes var1. AnyLogic substitutes these expressions into an algebraic equation system, solves this system numerically, and initializes the variables with the obtained solution.

5.5 Runtime errors caused by equations

During time steps (in between discrete event steps), the model engine numerically solves the global set of equations. This process may cause errors of three types:

A computational error in an expression specified by the user, e.g. division by zero or square root of a negative value.

Incorrectly constructed set of equations, e.g. a variable appears more than once on the left-hand side.

The set of equations cannot be solved with the given initial values.

In case of error of the third type, you should analyze the set of algebraic equations to find out if it has a solution. In case it has, it is recommended to give a good initial approximation to the solution. You should set up the initial approximation just before the set of equations is activated; e.g., in the entry action of the state containing equations.

5.6 Numerical methods

When a model starts, the equations are assembled into the main differential equation system. During the simulation, this DES is solved by one of the numerical methods built in AnyLogic. AnyLogic provides the rich collection of numerical methods for solving ordinal

118

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

differential equations (ODE), algebraic-differential equations (DAE), or algebraic equations (NAE).

By default, the numerical solver is chosen automatically by AnyLogic at runtime. You can set up numerical methods used for solving equations in the Numerical methods section on the Additional page of the experiment’s properties window (see Figure 55).

Figure 55. Additional page of experiment’s properties window. Numerical methods section

Differential equations – preferred method used to solve ordinary differential equations. By default, automatic method is selected, which chooses the fastest possible method for the current equation set.

Algebraic equations – preferred method used to solve algebraic equations. By default, automatic method is selected, which chooses the fastest possible method for the current equation set.

Mixed equations – preferred method used to solve algebraic-differential equations. By default, automatic method is selected, which chooses the fastest possible method for the current equation set.

Absolute accuracy – the desired absolute value accuracy for solving equations. Absolute accuracy is used when it is impossible to use relative accuracy – e.g., when the value is close to zero.

Relative accuracy – the desired relative value accuracy for solving equations with methods that change the integration step (e.g. RK45, RK853, RADAU5). Used by default.

© 1992-2004 XJ Technologies http://www.xjtek.com

119

Chapter 5. Equations

Time accuracy – the desired time accuracy for finding change events (switch points) when solving equations.

Fixed time step – fixed time step for methods using the fixed integration step (e.g. Euler, RK4).

If you do not specify particular solver, AnyLogic chooses the numerical solver automatically at runtime in accordance to the behavior of the system. For instance, when solving differential equations, it starts integration with forth-fifth-order Runge-Kutta method. If the system is stiff, AnyLogic plugs in another solver – RADAU5, designed for stiff differential equation systems. Both these methods change the integration step to achieve the given accuracy. You can set up methods with fixed step (Euler and forth-order Runge-Kutta), which are not as accurate as the first pair of methods, but perform numerical integration more rapidly. However, if you need a precise solution, use the Automatic solver.

120

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

6. Matrices and hyper-arrays

6.1 Matrices

AnyLogic supports variables of vector and matrix type. Since a vector is a special case of a matrix, the term matrix will refer to both matrices and vectors. In AnyLogic you can define equations over matrix variables.

AnyLogic class Matrix provides means for definition, initialization, access, and major operations with matrices, including inverse, transpose, trace, LU decomposition, QR decomposition, Singular value decomposition, Eigenvalue Decomposition, Cholesky Decomposition, Matrix condition (2 norm), One norm, Two norm, Frobenius norm, Infinity norm, and others. This class has been adapted from the JAMA set (A Java Matrix Package, see http://math.nist.gov/javanumerics/jama/). Please consult AnyLogic Class Reference for more details.

6.1.1Defining a matrix

To define a matrix variable

1.Select the variable on a structure diagram.

2.In the Variable type section of the Properties window, select the Matrix option.

3.Specify the number of rows in the Rows edit box.

4.Specify the number of columns in the Columns edit box.

6.1.2Setting an initial value for a matrix

By default all elements of a matrix get zeroes. You can set up initial values for elements of a matrix.

© 1992-2004 XJ Technologies http://www.xjtek.com

121

Chapter 6. Matrices and hyper-arrays

To set initial value for a matrix

1.Select the variable on the structure diagram.

2.In the Equations section of the Properties window, choose No equation from the Form drop-down list.

3.Specify the Initial Value expression.

Provide an initial value expression using such notation: delimit matrix with braces, enter value row-by-row, wrapping each row with braces and separating rows and elements in a row with commas. Be sure all the rows have the same number of elements. For example, a matrix is the following:

5 7.83 -1

0.2-3.4 0

For two-dimensional matrices these values are assigned in row-first order: Into the Initial value property, you type:

{{ 5, 7.83, -1}, { 0.2, -3.4, 0 }}

, or

[5, 7.83, -1 ; 0.2, -3.4, 0]

To increase readability you can split the string into multiple lines:

{{ 5, 7.83, -1 },

{ 0.2, -3.4, 0 }}

You can initialize identity matrices, i.e. matrices with ones on the main diagonal and zeros elsewhere, by typing identity keyword.

122

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

6.1.3Accessing and modifying a matrix from Model Viewer

6.1.3.1 Inspecting a matrix variable

You can inspect the current value of a variable at the model runtime from the variable’s inspect window.

To open the inspect window

1.Right-click the variable in the Model Explorer or in the animated structure diagram and choose Inspect from the popup menu.

The Select Item dialog box is displayed, see Figure 56.

Figure 56. Select Item dialog box

2.Choose whether you want to inspect some specific element of a matrix or all items.

3.Click OK.

The Inspect window is displayed (Figure 57 shows the inspect window of a matrix).

Figure 57. Inspect window of a matrix

© 1992-2004 XJ Technologies http://www.xjtek.com

123

Chapter 6. Matrices and hyper-arrays

6.1.3.2 Modifying a matrix variable

Elements of a matrix can be modified during the model simulation.

To modify an element of a matrix

1.Double-click the variable in the Model Explorer or on the animated structure diagram, or

Right-click the variable in the Model Explorer or on the animated structure diagram and choose Modify from the popup menu.

The Modify dialog box is displayed (see Figure 58).

2.In the Apply to section, specify whether you want to modify some specific item of a matrix, or all items once.

3.Type a new value in the New value edit box.

4.Click OK.

Figure 58. Modifying a matrix variable

6.1.4Accessing and modifying a matrix from code

6.1.4.1 Defining and initializing a matrix from code

To define a variable of matrix type in Java code (commonly in the Startup code section of the active object’s Code window), you use one of the following forms:

124

© 1992-2004 XJ Technologies http://www.xjtek.com

AnyLogic V User’s Manual

Matrix A = new Matrix( <number of rows>, <number of columns> ); to create a two-dimensional matrix the specified number of rows and columns

Matrix A = new Matrix( <integer number> );

to create a vector with the specified number of rows

Matrix A = new Matrix( <real number> );

to create a 1x1 matrix with the single element with the specified value

Matrix A = new Matrix ( <number of rows>, <number of columns>, <real number> )

to create a matrix initialized with a real number

Matrix A = Matrix.random( <number of rows>, <number of columns> ) to create a matrix with elements uniformly distributed in interval [0,1]

Matrix A = Matrix.identity( <number of rows>, <number of columns> ) to create an identity matrix with ones on the main diagonal and zeros elsewhere

Matrix A = new Matrix ( new double[][] { { 5, 7.83, -1 }, { 0.2, -3.4, 0 } } );

to create a 3x4 matrix initialized as shown in the previous section. Use the same notation as used for defining matrix variables in the Initial value property: delimit matrix with braces, enter value row-by-row, wrapping row with braces and separating rows and elements in a row with commas. Be sure all the rows hold the same number of elements.

To initialize matrix in code at once, write the following code:

double a = new double[2][3]; a[0][0] = 5; a[0][1] = 7.83;

// creating a source array of real numbers a[0][2] = -1;

a[1] = new double[]

{ 0.2, -3.4, 0 }

A = new Matrix ( 2,

3, a );

If there is a simple algorithm evaluating the elements, you can initialize it using cycles. See the example below:

1

2

3

4

-5

1

2

3

-5

-5

1

2

Write the following code to initialize this matrix:

int m = 3; // number of rows

© 1992-2004 XJ Technologies http://www.xjtek.com

125

Chapter 6. Matrices and hyper-arrays

int n = 4; // number of columns

A = new Matrix ( m, n ); // create zero matrix for (int i=0; i<m; i++){

for (int j=0; j<n; j++){

if (j >= i) A.set( i, j, 1-i+j ); else A.set( i, j, -5 );

}

}

6.1.4.2 Modifying and accessing elements of a matrix from code

If you want to operate with a matrix itself, you refer to it simply by its name.

If you need a submatrix (e.g., one of the columns) of the matrix A taking part in an equation, use the following function:

A.getMatrix(i0, i1, j0, j1)

where the points (i0,j0) and (i1,j1) specify the upper left and the lower right elements of the submatrix to select from the matrix A.

Please note that indexes in matrices are zero-based – i.e., start with “0”.

You can access the individual elements of matrices in the code by calling:

A.get(i,j) // if A is a matrix

A.get(i)

// if A is a vector

To modify an element of a matrix, use the set(i,j,value) method, e.g.:

A = Matrix.identity(4,4);

A.set(3,0,7);

//

set

left lower element to 7

A.set(0,3,1);

//

set

right upper element to 1

126

© 1992-2004 XJ Technologies http://www.xjtek.com

Соседние файлы в папке AnyLogic