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

imit_model / AnyLogic / UsersManual(AnyLogic)

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

AnyLogic V User’s Manual

To remove an additional stop condition

1.Select the condition in the Additional stop conditions list.

2.Click the Remove button.

13.4 Controlling model replications

AnyLogic enables you to control model replications either using AnyLogic experiment’s properties, or programmatically.

13.4.1Writing code to be executed between model replications

AnyLogic enables you to specify arbitrary actions to be performed between model replications. You can write any Java code to be executed before and after each model replication on the Code page of the project’s properties window.

To write code to be executed between model replications

1.In the Project window, click the project item (the top-most item in the workspace tree).

2.On the Code page of the Properties window, type code to be executed before each model replication in the Before replication section.

3.Type code to be executed after each model replication in the After replication section.

13.4.2API to control replications

You can control simulations and replications programmatically by overriding the method executionControl() of the root object of the model. The mechanism of controlling replications is based on the following feature of the root object:

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

337

Chapter 13. Simulation settings

The tree of active objects is constructed before each replication and deleted afterwards. The root object is responsible for creation and destruction of the tree. The root object itself survives between subsequent replications.

This means that the member variables of the root object remain untouched. You can use this property of the root object, for example, to collect datasets across multiple replications or perform parameter iteration.

Please do not confuse the terms simulation and replication. Replication is one execution of a model. Simulation is an execution of one or more replications. The result of a simulation is the value of the observable to be optimized by AnyLogic optimization subsystem. Several simulations take place only if you invoke optimization. Otherwise, only one simulation takes place. By overriding the method executionControl(), you control how much replications are executed in one simulation. The root object does not survive between simulations.

By default, a simulation performs one replication and stops. To tell AnyLogic to execute multiple replications, you override the method executionControl() of the root object. In this method, you call Engine.execute() to perform a replication. You can program any algorithm (e.g. nested loops, an optimization strategy) around replications. To override the method executionControl(), you use the Additional class code code section of the root object class.

The default implementation of executionControl() calls Engine.execute() once.

Example

In the following example, one replication calculates the result result depending on the parameter param. The optimization algorithm defined by the method executionControl() finds the parameter value (with the given accuracy eps), with which the result equals 0.

public void executionControl() { double a = 0;

double b = 10; double eps = 0.01;

param = a; Engine.execute();

338

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

AnyLogic V User’s Manual

double fa = result;

param = b; Engine.execute(); double fb = result;

assert(

fa * fb < 0,

“The function sign must be different on the interval ends”

);

while ( b – a > eps ) { param = ( a + b ) / 2; Engine.execute();

if ( result == 0 ) break;

if ( result * fa > 0 ) { fa = result;

a = param;

}

else {

fb = result; b = param;

}

}

}

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

339

Chapter 14. Debugging a model

14. Debugging a model

AnyLogic supports model debugging. This chapter provides information on AnyLogic debugging tools.

AnyLogic supports on-the-fly checking of types, parameters, and diagram syntax. The errors found during code generation and compilation are displayed in AnyLogic Output window and graphically highlighted in error location windows.

Using AnyLogic Events window, you can view the event queue of AnyLogic simulation engine to view what is happening at the simulation engine at the lowest level details and to make some changes to the event processing.

You can debug your model by setting a breakpoint on a model element to stop the model execution when this element becomes active, examine the model state, and perform some actions in response.

AnyLogic supports runtime error ability. You can throw runtime error and terminate model execution as a reaction to different undesirable occurrences.

You can trace model execution by writing custom information to AnyLogic log windows on different occurrences.

AnyLogic detects errors in Java code written by the user and logical errors of model execution (simulation errors). If such an error occurs, AnyLogic stops the model and notifies you with error message.

You can debug Java code using a third-party debugger by running the model within the debugger using command line execution feature or by attaching the debugger to the currently running model.

14.1Checking model syntax

AnyLogic supports on-the-fly checking of types, parameters, and diagram syntax. The errors found during code generation and compilation are displayed in AnyLogic Output window (see Figure 156). For each error, the Output window displays description and location.

340

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

AnyLogic V User’s Manual

Figure 156. Output window

To show/hide the Output window

1.Click the Output toolbar button, or Choose View|Output from the main menu, or Press Alt+2.

You can open an error. Depending on the error, opening it may result in displaying different windows If, for example, it is a graphical error, the corresponding diagram is opened with invalid shapes highlighted.

To open an error

1.Double-click the error in the Output window.

It is not always possible to give an exact error location in AnyLogic windows. For example, if you are trying to use an identifier Java cannot resolve, it could be an undeclared variable, or a parameter, or anything else. In such cases, AnyLogic displays a .java file and positions the cursor at the error location. This file is opened read-only and it is up to you to track down the real error location in AnyLogic.

To copy error messages on the Clipboard

1.Select the error messages you want to copy.

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

341

Chapter 14. Debugging a model

2.Click the Copy toolbar button, or Choose Edit|Copy from the main menu, or

Right-click the error message and choose Copy from the popup menu, or Press Ctrl+Ins.

14.2Viewing and modifying AnyLogic events

AnyLogic simulates the model as a sequence of time steps and event steps. The information presented in section 14.2.1, “Event processing at the simulation engine”, might be useful for better understanding of how the AnyLogic engine simulates discrete events and continuous behavior. Section 14.2.2, ”Events window”, describes how to view the event queue of AnyLogic simulation engine to view what is happening at the simulation engine at the lowest level details and to make some changes to the event processing.

14.2.1Event processing at the simulation engine

AnyLogic simulates the model as a sequence of time steps and event steps. During a time step:

The model clock is advanced.

The “discrete” state of the model (the statechart, port, event, thread, etc. states) remains unchanged.

Active equations, if any, are being solved numerically and the variables are changed correspondingly.

Awaited change events are tested for occurrence.

During an event step:

No model time elapses.

The actions of states, transitions, timers, ports, etc. corresponding to this event are executed.

The state of the model may change.

342

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

AnyLogic V User’s Manual

Some scheduled events may be deleted, and the new events may be scheduled in the AnyLogic Engine event queue.

14.2.1.1 Engine events

AnyLogic engine events are events that occur at runtime. Please do not confuse them with static/dynamic events that are a part of AnyLogic modeling language. There are several types of engine events:

current – events that can be executed at the time now

chosen – one of the enabled events that is chosen to be executed next

enabled – other current events (those that potentially could be executed next)

scheduled – events scheduled at some particular known time in the future

pending – events that may occur in the future, but the time is not known

Engine events reside in the engine event queue. Any event present in the engine event queue may be associated with:

An active timer

A transition triggered on a timeout expiry

A thread executing a delay() statement

In addition, current events may be associated with something that has just happened as a result of other event execution:

A transition being triggered by a port, immediately, or by a static, signal or change event

A thread successfully exiting its waitEvent() or waitForMessage() statement

14.2.1.2 Time step

If there are no current events, AnyLogic makes a time step to the nearest event (or events) in the queue, i.e., advances its clock. During a time step a change event may occur. The discrete part of AnyLogic engine does not know when a change event associated with a transition occurs: it depends on the equation set being solved numerically by a continuous part of the

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

343

Chapter 14. Debugging a model

engine. Once this happens, the clock is advanced to the time reported by the continuoustime equation solver, and the event step is executed.

14.2.1.3 Event step

Several events may be scheduled to occur at the same moment of time. If there are several current events, AnyLogic chooses one and executes it. This is repeated until there are no current events. Thus, several event steps may be made in succession, whereas a time step is always followed by an event step. Simultaneous events may depend on each other or be truly concurrent. The serialization of concurrent events is called interleaving a model.

Depending on your task, you can tell AnyLogic simulation engine to do random or deterministic serialization of events. Random instead of deterministic serialization ensures that a bigger part of the system state space is covered by a simulation, so it is more likely that an undesirable behavior will be detected. Note that random event serialization slightly slows your model simulation.

To set up deterministic/random event serialization

1.In the Project window, click the project item (the top-most item in the workspace tree).

2.On the General page of the Properties window, choose Deterministically/Randomly from the Event scheduling algorithm drop-down list.

The execution of a timer event is actually the execution of the timer’s action code. The execution of a transition event is the execution of a set of actions associated with the transition. As a result of the event execution, the discrete state of the model may change: statecharts may change their states, other equations may be activated, other transitions may begin waiting, and other timers may be activated. Thus, some events may be deleted from the event queue and other events may be added to it.

The example of AnyLogic event queue processing is shown in Figure 157.

344

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

 

 

 

 

AnyLogic V User’s Manual

 

 

Events scheduled

H

 

B

D

to occur at the

G

 

same time

 

A

C

E

F

I

Past Now Future

 

 

 

time

Time step: the clock is advanced to A and B - the head of the event queue. Active algebraic-differential equations are being solved. The event queue remains unchanged.

 

Chosen

 

H

 

B

event

 

G

 

D

 

 

Current events

C

E

F

I

A

Past Now

Future

 

 

time

Event step: B is chosen and occurs. No time elapses. Model state changes. A, E and G are deleted from the event queue. J and K are scheduled.

D

 

H

K

C

J

F

I

Past Now Future

 

 

time

Time step: the clock is advanced to C and D. Active algebraic-differential equations are being solved. Suddenly change event Q is detected.

 

D

 

H

K

Q

C

J

F

I

Past Now

Future

 

 

time

Event step: Q is chosen and occurs. No time elapses. Model state changes. C and K are deleted from the event queue. L is scheduled.

 

 

L

 

 

 

H

 

D

J

F

I

Past Now Future

 

 

time

Time step: the clock is advanced to D …

Figure 157. AnyLogic event queue (pending events not shown)

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

345

Chapter 14. Debugging a model

14.2.2Events window

The events window, see Figure 158, displays the event queue of AnyLogic simulation engine either for the whole model or for a particular active object. You can use the events window for debugging purposes to view what is happening at the simulation engine at the lowest level details and to make some changes to the event processing. The user often works with the events window using the Primitive Step and Detailed Play commands, see section 11.1.2, “Controlling the model execution”.

Figure 158. Events window

To open the global events window

1.Choose View|Model Events from the main menu.

Each event in the events window is displayed in the following form:

Flag for the chosen event, for enabled events, no flag for other events.

Location – model path to the object that is associated with this event.

Type – “Dynamic” for dynamic events and dynamic timers, “Static” for static timers, “Change” for change events, and “Timeout” for timed transitions.

346

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

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