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

imit_model / AnyLogic / UsersManual(AnyLogic)

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

AnyLogic V User’s Manual

Sometimes you may need to modify a submatrix. For instance, you have a block matrix X used in some equations. Blocks A, B, C, and D should be calculated in different ways (by different formulas).

 

0

3

4

5

 

0

 

 

 

 

 

A

 

 

B

 

 

 

 

 

2

 

 

 

 

 

3

 

 

 

 

 

C

 

 

D

 

 

 

 

 

5

 

 

 

 

 

 

 

 

 

 

 

The idea is to calculate the blocks separately and then combine them into one matrix using transforming matrices. To deal the problem with A and B blocks:

1.Define a matrix variable A with dimensions [3,4] and set a necessary formula to it.

2.Define a matrix variable Aleft with dimensions [6,3].

3.Type in the Initial Value property of Aleft the key word identity.

4.Define a matrix variable Aright with dimensions [4,6].

5.Type in the Initial Value property of Aright the keyword identity.

6.Set the formula for X: Aright*A*Aleft.

7.Now block A of X matrix gets the value from variable A and other blocks hold zeros. Block B does not lie on the main diagonal, so the right transforming matrix is not an identity one. We will initialize it in startup code.

8.Define a matrix variable B with dimensions [3,2] and set a necessary formula to it.

9.Define a matrix variable Bleft with dimensions [6,3].

10.Type in the Initial Value property of Bleft the keyword identity.

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

127

Chapter 6. Matrices and hyper-arrays

11.Define a matrix variable Bright with dimensions [2,6]. Leave Initial Value property empty.

12.In the startup code type:

Bright[0,4] = 1;

Bright[1,5] = 1;

13. Change the formula for X: Aright*A*Aleft + Bleft*B*Bright.

Now blocks A and B are calculated properly. To complete the task, perform analogous steps for blocks C and D.

Working with the example, you noticed, perhaps, that matrix Aleft equals Bleft, Aright equals Cright and so on. Actually, you need only four matrices instead of eight as it may have appeared.

6.1.5Using matrices in equations

AnyLogic supports matrices as well as primitive types in equations. You can define a set of differential equations, algebraic equations, and formulas to describe continuous changes of variables over time. Thus you can define continuous time object behavior. See Chapter 5, “Equations” to know how to define equations.

If you use matrices in equations, in the right-hand side you can use the operations listed in Table 7. Variables A and B are assumed to be matrices, x is a real variable.

Operation

Description

 

 

A+B, A-B, -A

Addition, subtraction, unary minus.

 

 

x*A, A*x

Scalar multiplication.

 

 

A*B

Matrix multiplication.

 

 

1/B

Inverse matrix.

 

 

A/B

Multiply with inversed matrix, equals to A*(1/B).

 

 

128

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

 

 

AnyLogic V User’s Manual

 

 

 

 

 

A.transpose()

Transpose matrix.

 

 

 

 

 

 

A[i,j]

Access to a matrix element.

 

 

 

 

 

 

A[i]

Access to a matrix row (element, in case of vector).

 

 

 

 

 

 

f( …, A, … )

A call to a method accepting Matrix type arguments.

 

 

 

 

 

Table 7. Available operations for matrices

While generating code for a right-hand side, AnyLogic follows these rules:

If all variables are scalars, the simple code like x*y+z is generated, and the result is treated as scalar.

Otherwise AnyLogic assumes matrices and generates code like

Matrix.add(Matrix.mult(x,y),z).

To change the order of operations, use the round brackets. For instance, there is a matrix differential equation:

dX

= −D (5A + B1 ) CT e0 ,

dt

 

where A, B, X are square matrices n*n; C, D, E – vectors n*1.

The appropriate right-hand side expression of the integral equation is:

- D * ( 5*A + 1/B ) * C.transpose() * E[0]

You can explicitly notify AnyLogic that the equation is of scalar type by placing the ‘#’ symbol at the beginning of expression, e.g. a=#x*y+z. This may result in simulation performance improvement.

The right-hand side of a scalar variable may refer to matrices, if the result of the expression is scalar. In this example, real variable x could be calculated through the formula:

# - D * ( 5*A + 1/B ) * C.transpose()

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

129

Chapter 6. Matrices and hyper-arrays

6.1.6Working with matrices

The Matrix class supports many of linear algebra operations you can use in equations, active object class code and in discrete event handlers (timer’s Expiry action, or statechart transition’s Action code). Table 8 gives a quick reference to matrix operations.

Operation

Description

 

 

A.plus(B)

Matrix addition: A+B.

 

 

A.minus(B)

Matrix subtraction: A-B.

 

 

A.uminus()

Unary minus: (-A).

 

 

A.times(x)

Scalar multiplication: x*A.

 

 

A.times(B)

Matrix multiplication: A*B.

 

 

A.inverse()

Inverse matrix: A-1.

 

 

A.transpose()

Transpose matrix: AT.

 

 

A.norm1()

Maximum column sum.

 

 

A.norm2()

Maximum singular value.

 

 

A.normInf()

Maximum row sum.

 

 

A.normF()

Frobenius norm; square root of sum of squares of all elements.

 

 

A.solve(B)

Matrix X, which satisfies A*X = B, if A is not square, X is the

least squares solution.

 

 

 

A.solveTranspose(B)

Matrix X, which satisfies both A*X = B, A'*X' = B', if A is not

square, X is the least squares solution.

 

 

A.inverse()

Inverse or pseudo-inverse matrix.

 

 

A.det()

Matrix determinant.

 

 

130

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

 

 

AnyLogic V User’s Manual

 

 

 

 

 

A.rank()

Effective numerical rank obtained from singular value

 

 

decomposition.

 

 

 

 

 

 

 

 

 

A.cond ()

Matrix condition; ratio of largest to smallest singular value.

 

 

 

 

 

 

 

Table 8. Matrix operations

There is an example. You should get a matrix that is the result of an expression such as:

Z = −D (5A + B1 ) CT e0

where A, B, X are square matrices n*n; C, D, E – vectors n*1.

Type the following code to create such a matrix:

Matrix Z = D.uminus().times(((A.times(5)).plus(B.inverse()))).times(

C.transpose()).times(E.getMatrix(0,0,0,E.getRowDimension()));

Note that if you write code like A=B, you only let A refer to the same matrix object that B does. If you change matrix B, A changes too, and otherwise. To avoid this dependency you must assign matrix A a copy of matrix B. To make a copy of a matrix, type

A=B.copy();

If you need checking the matrix content at some moment, you can print it out to the global log. Just type in the appropriate place of the event handler the following:

A.print(Engine.log, <number of characters in column>, <number of digits after the decimal>);

To solve a linear equation, write:

Matrix x = A.solve(B)

To get the eigenvalues of a matrix, write:

EigenvalueDecomposition e = A.eig();

double[] eigRe = e.getRealEigenvalues(); // vector of real parts double[] eigIm = e.getImagEigenvalues(); // vector of image parts Matrix V = e.getV(); // matrix of eigenvectors

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

131

Chapter 6. Matrices and hyper-arrays

6.2 Hyper-arrays

Hyper-array is storage of real numbers that has N dimensions. Each dimension has finite number of indexes - subscripts. There are many operations and functions defined for hyperarrays. You can define equations for hyper-arrays in the same way you do for scalar variables.

Hyper-arrays are used when:

It is necessary to store a large set of coefficients and access them.

There are multiple model layers.

The second case is useful when you have defined a model for some subsystem and there are other subsystems, which have the same structure, as the first one, but other numerical parameters. It is a model with multiple layers (Figure 59). The layers can be independent or have some dependencies between their variables.

Figure 59. A scalar model and a model with multiple layers

One can implement such multi-dimensional models making copies of the default diagram and changing the parameters. Such approach has one great disadvantage: if you want to change the model, you need do it so much times, as many layers you have; the diagram grows and becomes incomprehensive.

Hyper-array allows you to create a single diagram for all the layers. Therefore, the model remains compact, and changes you make will affect the whole model, but not a single layer.

Before studying hyper-arrays, you should know what an enumeration is.

132

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

AnyLogic V User’s Manual

6.2.1Enumerations

Sometimes you need a parameter representing an attribute which can take a finite set of values. For example, you work with colors. You can create an integer parameter representing a color and, e.g., assign number 1 to red color, 2 to green color, 3 to blue one. However, you see only integer numbers in code, but not the names of colors. To increase the readability of code, you should use enumerations. Each enumeration has named elements, each of them having an integer counterpart that is hidden from you. For example, you can define Color enumeration with Red, Green, and Blue elements and use these self-descriptive names instead of integer numbers in code.

Color myColor = Red;

...

myColor = Green;

...

if (myColor == Green) traceln(“I’m green”);

...

6.2.1.1 Defining an enumeration

To define an enumeration

1.Click the New Enumeration toolbar button, or Choose Insert|New Enumeration… from the main menu. The New Enumeration dialog box is displayed.

Specify the name of the new enumeration, choose the package, which will contain the enumeration, and click OK.

2.Alternatively, in the Project window, right-click the package, which will contain the enumeration, and choose New Enumeration… from the popup menu.

The New Enumeration dialog box is displayed.

Specify the name of the new enumeration and click OK.

An enumeration has the following set of properties:

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

133

Chapter 6. Matrices and hyper-arrays

Properties

Name – name of the enumeration.

Elements – the set of enumeration elements.

Exclude from build – if set, the enumeration is excluded from the model.

The enumeration elements are specified in the Elements table on the enumeration’s properties page. Each element is specified in an individual row.

To add an enumeration element

1.In the Project window, click the enumeration.

2.In the Properties window, go to the last row of the Elements table.

3.Type the enumeration element name.

The order of enumeration elements in the table is very significant.

To move an element up/down in the table

1.In the Properties window, select the element in the Elements table.

2.Press Ctrl+Up/Ctrl+Down.

When the enumeration is defined, you can:

Define model parameters of enumeration type.

Use the enumeration as a dimension of a hyper-array.

6.2.2Defining variables of hyper-array type

AnyLogic supports both variables and parameters of hyper-array type.

To define a hyper-array parameter

1.In the Parameter dialog box, specify HyperArray as a parameter type.

134

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

AnyLogic V User’s Manual

2.Initialize the hyper-array in the Default value combo box.

For example, you can initialize the hyper-array with another one, defined and initialized in the Additional class code code section of the active object class.

To define a hyper-array variable

1.Select the variable on a structure diagram.

2.In the Properties window, choose the variable type Array.

3.Specify as much dimensions as you need in the table on the right of the Array option. Each row of the table defines one dimension.

To define a new dimension, go to the last row of the table and click the Dimension field. In the combo box, choose an enumeration to be a hyper-array’s subscript.

A hyper-array cannot have a single enumeration as two or more dimensions.

The order of dimensions is also important. You can reorder the dimensions pressing Ctrl+Up and Ctrl+Down.

6.2.3Setting an initial value for a hyper-array

To set the initial value for a hyper-array

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.

There are two different notations for hyper-array constants. We illustrate both notations by hyper-array constants used as initial values of some variables. The subscripts are listed before each constant in the order of dimension declarations in these variables.

Suppose you create a model of a nation’s health, describing some social or health processes in respect to different groups of population. Separate people by three characteristics: gender,

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

135

Chapter 6. Matrices and hyper-arrays

age group, and social group. The characteristics fit well in the enumeration concept. There are such enumerations: Gender(male, female), Age(child, teenager, adult, aged), and SocialGroup(wealthy, middleclass, deprived).

Matrix-style. This notation can be used for 1D and 2D arrays.

Age

[ 1, 0.8, 0.62, 0.2 ]

Age, Gender

[

1

,

1.2

;

 

0.8

,

0.75

;

 

0.62

,

0.5

;

 

0.2

,

0.1

]

Java-style. This notation can be used for hyper-arrays with arbitrary dimension number.

Gender

{1 /*male*/, 1.2 /*female*/ }

Age, Gender

{ {

1

,

1.2

},

// Age: child

{

0.8

, 0.75

},

// Age: teenager

{ 0.62

,

0.5

},

// Age: adult

{

0.2

,

0.1

}

// Age: aged

}

 

 

 

 

 

136

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

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