Create network by code

Let’s go through this simple example that will allow an agent to move through a network, avoiding the use of deprecated constructors in AnyLogic 8.4.

Create the network. This network will be necessary for an agent to follow the proper paths from point A to point B. All these functions can be run on startup of Main.

Network myNetwork = new Network( this, “myNetwork”);

Next step, generating the initial point nodes in Main. In this example I use 3 points. The agent will go from point p1 to p3 passing through p2.

PointNode p1=new PointNode(this, 100, 100, 0.0 );

PointNode p2=new PointNode(this, 200, 100, 0.0);

PointNode p3=new PointNode(this, 200,200,0.0);

Then you need to construct the markup segments.

MarkupSegmentLine ms1,ms2;

ms1 = new MarkupSegmentLine(p1.getX(),p1.getY(),0.0, p2.getX(), p2.getY(), 0.0 );

ms2 = new MarkupSegmentLine(p2.getX(),p2.getY(),0.0, p3.getX(), p3.getY(), 0.0 );

Then, construct the paths that will use ms1 and ms2. If you want them to be bidirectional, this is the place to set that up.

Path path1=new Path(this);

Path path2=new Path(this);

path1.addSegment(ms1);

path2.addSegment(ms2);

path1.setBidirectional(true);

path2.setBidirectional(true);

Then you need to generate the connections for each point node. Each of them must be connected to one or more paths. In this case, p1 is the initial point and p3 is the end point, but since the paths are bidirectional, it doesn’t really matter if you set the pathEndType as BEGIN or END.

p1.addConnection(path1, PathEndType.BEGIN);

p2.addConnection(path1,  PathEndType.END);

p2.addConnection(path2,  PathEndType.BEGIN);

p3.addConnection(path2, PathEndType.END);

Everything is set up, now you can safely initialize everything.

p1.initialize();

p2.initialize();

p3.initialize();

path1.initialize();

path2.initialize();

After initialization, add everything to your network and initialize it.

myNetwork.addAll(path1,path2,p1,p2,p3);

myNetwork.initialize();

Everything is ready, make the paths and nodes visible if you want:

presentation.add(p1);

presentation.add(p2);

presentation.add(p3);

presentation.add(path1);

presentation.add(path2);

Now you can create an agent and move it from p1 to p3, or vice-versa.

MyAgent agent=add_myAgent();

agent.jumpTo(p1);

agent.moveTo(p3);

Download the Model Here

Your Comment: